gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/httpsnoop/wrap_generated_lt_1.8.go (about)

     1  //go:build !go1.8
     2  // +build !go1.8
     3  
     4  // Code generated by "httpsnoop/codegen"; DO NOT EDIT
     5  
     6  package httpsnoop
     7  
     8  import (
     9  	"bufio"
    10  	http "gitee.com/ks-custle/core-gm/gmhttp"
    11  	"io"
    12  	"net"
    13  )
    14  
    15  // HeaderFunc is part of the http.ResponseWriter interface.
    16  type HeaderFunc func() http.Header
    17  
    18  // WriteHeaderFunc is part of the http.ResponseWriter interface.
    19  type WriteHeaderFunc func(code int)
    20  
    21  // WriteFunc is part of the http.ResponseWriter interface.
    22  type WriteFunc func(b []byte) (int, error)
    23  
    24  // FlushFunc is part of the http.Flusher interface.
    25  type FlushFunc func()
    26  
    27  // CloseNotifyFunc is part of the http.CloseNotifier interface.
    28  type CloseNotifyFunc func() <-chan bool
    29  
    30  // HijackFunc is part of the http.Hijacker interface.
    31  type HijackFunc func() (net.Conn, *bufio.ReadWriter, error)
    32  
    33  // ReadFromFunc is part of the io.ReaderFrom interface.
    34  type ReadFromFunc func(src io.Reader) (int64, error)
    35  
    36  // Hooks defines a set of method interceptors for methods included in
    37  // http.ResponseWriter as well as some others. You can think of them as
    38  // middleware for the function calls they target. See Wrap for more details.
    39  type Hooks struct {
    40  	Header      func(HeaderFunc) HeaderFunc
    41  	WriteHeader func(WriteHeaderFunc) WriteHeaderFunc
    42  	Write       func(WriteFunc) WriteFunc
    43  	Flush       func(FlushFunc) FlushFunc
    44  	CloseNotify func(CloseNotifyFunc) CloseNotifyFunc
    45  	Hijack      func(HijackFunc) HijackFunc
    46  	ReadFrom    func(ReadFromFunc) ReadFromFunc
    47  }
    48  
    49  // Wrap returns a wrapped version of w that provides the exact same interface
    50  // as w. Specifically if w implements any combination of:
    51  //
    52  // - http.Flusher
    53  // - http.CloseNotifier
    54  // - http.Hijacker
    55  // - io.ReaderFrom
    56  //
    57  // The wrapped version will implement the exact same combination. If no hooks
    58  // are set, the wrapped version also behaves exactly as w. Hooks targeting
    59  // methods not supported by w are ignored. Any other hooks will intercept the
    60  // method they target and may modify the call's arguments and/or return values.
    61  // The CaptureMetrics implementation serves as a working example for how the
    62  // hooks can be used.
    63  func Wrap(w http.ResponseWriter, hooks Hooks) http.ResponseWriter {
    64  	rw := &rw{w: w, h: hooks}
    65  	_, i0 := w.(http.Flusher)
    66  	_, i1 := w.(http.CloseNotifier)
    67  	_, i2 := w.(http.Hijacker)
    68  	_, i3 := w.(io.ReaderFrom)
    69  	switch {
    70  	// combination 1/16
    71  	case !i0 && !i1 && !i2 && !i3:
    72  		return struct {
    73  			Unwrapper
    74  			http.ResponseWriter
    75  		}{rw, rw}
    76  	// combination 2/16
    77  	case !i0 && !i1 && !i2 && i3:
    78  		return struct {
    79  			Unwrapper
    80  			http.ResponseWriter
    81  			io.ReaderFrom
    82  		}{rw, rw, rw}
    83  	// combination 3/16
    84  	case !i0 && !i1 && i2 && !i3:
    85  		return struct {
    86  			Unwrapper
    87  			http.ResponseWriter
    88  			http.Hijacker
    89  		}{rw, rw, rw}
    90  	// combination 4/16
    91  	case !i0 && !i1 && i2 && i3:
    92  		return struct {
    93  			Unwrapper
    94  			http.ResponseWriter
    95  			http.Hijacker
    96  			io.ReaderFrom
    97  		}{rw, rw, rw, rw}
    98  	// combination 5/16
    99  	case !i0 && i1 && !i2 && !i3:
   100  		return struct {
   101  			Unwrapper
   102  			http.ResponseWriter
   103  			http.CloseNotifier
   104  		}{rw, rw, rw}
   105  	// combination 6/16
   106  	case !i0 && i1 && !i2 && i3:
   107  		return struct {
   108  			Unwrapper
   109  			http.ResponseWriter
   110  			http.CloseNotifier
   111  			io.ReaderFrom
   112  		}{rw, rw, rw, rw}
   113  	// combination 7/16
   114  	case !i0 && i1 && i2 && !i3:
   115  		return struct {
   116  			Unwrapper
   117  			http.ResponseWriter
   118  			http.CloseNotifier
   119  			http.Hijacker
   120  		}{rw, rw, rw, rw}
   121  	// combination 8/16
   122  	case !i0 && i1 && i2 && i3:
   123  		return struct {
   124  			Unwrapper
   125  			http.ResponseWriter
   126  			http.CloseNotifier
   127  			http.Hijacker
   128  			io.ReaderFrom
   129  		}{rw, rw, rw, rw, rw}
   130  	// combination 9/16
   131  	case i0 && !i1 && !i2 && !i3:
   132  		return struct {
   133  			Unwrapper
   134  			http.ResponseWriter
   135  			http.Flusher
   136  		}{rw, rw, rw}
   137  	// combination 10/16
   138  	case i0 && !i1 && !i2 && i3:
   139  		return struct {
   140  			Unwrapper
   141  			http.ResponseWriter
   142  			http.Flusher
   143  			io.ReaderFrom
   144  		}{rw, rw, rw, rw}
   145  	// combination 11/16
   146  	case i0 && !i1 && i2 && !i3:
   147  		return struct {
   148  			Unwrapper
   149  			http.ResponseWriter
   150  			http.Flusher
   151  			http.Hijacker
   152  		}{rw, rw, rw, rw}
   153  	// combination 12/16
   154  	case i0 && !i1 && i2 && i3:
   155  		return struct {
   156  			Unwrapper
   157  			http.ResponseWriter
   158  			http.Flusher
   159  			http.Hijacker
   160  			io.ReaderFrom
   161  		}{rw, rw, rw, rw, rw}
   162  	// combination 13/16
   163  	case i0 && i1 && !i2 && !i3:
   164  		return struct {
   165  			Unwrapper
   166  			http.ResponseWriter
   167  			http.Flusher
   168  			http.CloseNotifier
   169  		}{rw, rw, rw, rw}
   170  	// combination 14/16
   171  	case i0 && i1 && !i2 && i3:
   172  		return struct {
   173  			Unwrapper
   174  			http.ResponseWriter
   175  			http.Flusher
   176  			http.CloseNotifier
   177  			io.ReaderFrom
   178  		}{rw, rw, rw, rw, rw}
   179  	// combination 15/16
   180  	case i0 && i1 && i2 && !i3:
   181  		return struct {
   182  			Unwrapper
   183  			http.ResponseWriter
   184  			http.Flusher
   185  			http.CloseNotifier
   186  			http.Hijacker
   187  		}{rw, rw, rw, rw, rw}
   188  	// combination 16/16
   189  	case i0 && i1 && i2 && i3:
   190  		return struct {
   191  			Unwrapper
   192  			http.ResponseWriter
   193  			http.Flusher
   194  			http.CloseNotifier
   195  			http.Hijacker
   196  			io.ReaderFrom
   197  		}{rw, rw, rw, rw, rw, rw}
   198  	}
   199  	panic("unreachable")
   200  }
   201  
   202  type rw struct {
   203  	w http.ResponseWriter
   204  	h Hooks
   205  }
   206  
   207  func (w *rw) Unwrap() http.ResponseWriter {
   208  	return w.w
   209  }
   210  
   211  func (w *rw) Header() http.Header {
   212  	f := w.w.(http.ResponseWriter).Header
   213  	if w.h.Header != nil {
   214  		f = w.h.Header(f)
   215  	}
   216  	return f()
   217  }
   218  
   219  func (w *rw) WriteHeader(code int) {
   220  	f := w.w.(http.ResponseWriter).WriteHeader
   221  	if w.h.WriteHeader != nil {
   222  		f = w.h.WriteHeader(f)
   223  	}
   224  	f(code)
   225  }
   226  
   227  func (w *rw) Write(b []byte) (int, error) {
   228  	f := w.w.(http.ResponseWriter).Write
   229  	if w.h.Write != nil {
   230  		f = w.h.Write(f)
   231  	}
   232  	return f(b)
   233  }
   234  
   235  func (w *rw) Flush() {
   236  	f := w.w.(http.Flusher).Flush
   237  	if w.h.Flush != nil {
   238  		f = w.h.Flush(f)
   239  	}
   240  	f()
   241  }
   242  
   243  func (w *rw) CloseNotify() <-chan bool {
   244  	f := w.w.(http.CloseNotifier).CloseNotify
   245  	if w.h.CloseNotify != nil {
   246  		f = w.h.CloseNotify(f)
   247  	}
   248  	return f()
   249  }
   250  
   251  func (w *rw) Hijack() (net.Conn, *bufio.ReadWriter, error) {
   252  	f := w.w.(http.Hijacker).Hijack
   253  	if w.h.Hijack != nil {
   254  		f = w.h.Hijack(f)
   255  	}
   256  	return f()
   257  }
   258  
   259  func (w *rw) ReadFrom(src io.Reader) (int64, error) {
   260  	f := w.w.(io.ReaderFrom).ReadFrom
   261  	if w.h.ReadFrom != nil {
   262  		f = w.h.ReadFrom(f)
   263  	}
   264  	return f(src)
   265  }
   266  
   267  type Unwrapper interface {
   268  	Unwrap() http.ResponseWriter
   269  }
   270  
   271  // Unwrap returns the underlying http.ResponseWriter from within zero or more
   272  // layers of httpsnoop wrappers.
   273  func Unwrap(w http.ResponseWriter) http.ResponseWriter {
   274  	if rw, ok := w.(Unwrapper); ok {
   275  		// recurse until rw.Unwrap() returns a non-Unwrapper
   276  		return Unwrap(rw.Unwrap())
   277  	} else {
   278  		return w
   279  	}
   280  }