github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/felixge/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  	"github.com/hellobchain/newcryptosm/http"
    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  			http.ResponseWriter
    74  		}{rw}
    75  	// combination 2/16
    76  	case !i0 && !i1 && !i2 && i3:
    77  		return struct {
    78  			http.ResponseWriter
    79  			io.ReaderFrom
    80  		}{rw, rw}
    81  	// combination 3/16
    82  	case !i0 && !i1 && i2 && !i3:
    83  		return struct {
    84  			http.ResponseWriter
    85  			http.Hijacker
    86  		}{rw, rw}
    87  	// combination 4/16
    88  	case !i0 && !i1 && i2 && i3:
    89  		return struct {
    90  			http.ResponseWriter
    91  			http.Hijacker
    92  			io.ReaderFrom
    93  		}{rw, rw, rw}
    94  	// combination 5/16
    95  	case !i0 && i1 && !i2 && !i3:
    96  		return struct {
    97  			http.ResponseWriter
    98  			http.CloseNotifier
    99  		}{rw, rw}
   100  	// combination 6/16
   101  	case !i0 && i1 && !i2 && i3:
   102  		return struct {
   103  			http.ResponseWriter
   104  			http.CloseNotifier
   105  			io.ReaderFrom
   106  		}{rw, rw, rw}
   107  	// combination 7/16
   108  	case !i0 && i1 && i2 && !i3:
   109  		return struct {
   110  			http.ResponseWriter
   111  			http.CloseNotifier
   112  			http.Hijacker
   113  		}{rw, rw, rw}
   114  	// combination 8/16
   115  	case !i0 && i1 && i2 && i3:
   116  		return struct {
   117  			http.ResponseWriter
   118  			http.CloseNotifier
   119  			http.Hijacker
   120  			io.ReaderFrom
   121  		}{rw, rw, rw, rw}
   122  	// combination 9/16
   123  	case i0 && !i1 && !i2 && !i3:
   124  		return struct {
   125  			http.ResponseWriter
   126  			http.Flusher
   127  		}{rw, rw}
   128  	// combination 10/16
   129  	case i0 && !i1 && !i2 && i3:
   130  		return struct {
   131  			http.ResponseWriter
   132  			http.Flusher
   133  			io.ReaderFrom
   134  		}{rw, rw, rw}
   135  	// combination 11/16
   136  	case i0 && !i1 && i2 && !i3:
   137  		return struct {
   138  			http.ResponseWriter
   139  			http.Flusher
   140  			http.Hijacker
   141  		}{rw, rw, rw}
   142  	// combination 12/16
   143  	case i0 && !i1 && i2 && i3:
   144  		return struct {
   145  			http.ResponseWriter
   146  			http.Flusher
   147  			http.Hijacker
   148  			io.ReaderFrom
   149  		}{rw, rw, rw, rw}
   150  	// combination 13/16
   151  	case i0 && i1 && !i2 && !i3:
   152  		return struct {
   153  			http.ResponseWriter
   154  			http.Flusher
   155  			http.CloseNotifier
   156  		}{rw, rw, rw}
   157  	// combination 14/16
   158  	case i0 && i1 && !i2 && i3:
   159  		return struct {
   160  			http.ResponseWriter
   161  			http.Flusher
   162  			http.CloseNotifier
   163  			io.ReaderFrom
   164  		}{rw, rw, rw, rw}
   165  	// combination 15/16
   166  	case i0 && i1 && i2 && !i3:
   167  		return struct {
   168  			http.ResponseWriter
   169  			http.Flusher
   170  			http.CloseNotifier
   171  			http.Hijacker
   172  		}{rw, rw, rw, rw}
   173  	// combination 16/16
   174  	case i0 && i1 && i2 && i3:
   175  		return struct {
   176  			http.ResponseWriter
   177  			http.Flusher
   178  			http.CloseNotifier
   179  			http.Hijacker
   180  			io.ReaderFrom
   181  		}{rw, rw, rw, rw, rw}
   182  	}
   183  	panic("unreachable")
   184  }
   185  
   186  type rw struct {
   187  	w http.ResponseWriter
   188  	h Hooks
   189  }
   190  
   191  func (w *rw) Header() http.Header {
   192  	f := w.w.(http.ResponseWriter).Header
   193  	if w.h.Header != nil {
   194  		f = w.h.Header(f)
   195  	}
   196  	return f()
   197  }
   198  
   199  func (w *rw) WriteHeader(code int) {
   200  	f := w.w.(http.ResponseWriter).WriteHeader
   201  	if w.h.WriteHeader != nil {
   202  		f = w.h.WriteHeader(f)
   203  	}
   204  	f(code)
   205  }
   206  
   207  func (w *rw) Write(b []byte) (int, error) {
   208  	f := w.w.(http.ResponseWriter).Write
   209  	if w.h.Write != nil {
   210  		f = w.h.Write(f)
   211  	}
   212  	return f(b)
   213  }
   214  
   215  func (w *rw) Flush() {
   216  	f := w.w.(http.Flusher).Flush
   217  	if w.h.Flush != nil {
   218  		f = w.h.Flush(f)
   219  	}
   220  	f()
   221  }
   222  
   223  func (w *rw) CloseNotify() <-chan bool {
   224  	f := w.w.(http.CloseNotifier).CloseNotify
   225  	if w.h.CloseNotify != nil {
   226  		f = w.h.CloseNotify(f)
   227  	}
   228  	return f()
   229  }
   230  
   231  func (w *rw) Hijack() (net.Conn, *bufio.ReadWriter, error) {
   232  	f := w.w.(http.Hijacker).Hijack
   233  	if w.h.Hijack != nil {
   234  		f = w.h.Hijack(f)
   235  	}
   236  	return f()
   237  }
   238  
   239  func (w *rw) ReadFrom(src io.Reader) (int64, error) {
   240  	f := w.w.(io.ReaderFrom).ReadFrom
   241  	if w.h.ReadFrom != nil {
   242  		f = w.h.ReadFrom(f)
   243  	}
   244  	return f(src)
   245  }