gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/httpsnoop/wrap_generated_gteq_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  // PushFunc is part of the http.Pusher interface.
    37  type PushFunc func(target string, opts *http.PushOptions) error
    38  
    39  // Hooks defines a set of method interceptors for methods included in
    40  // http.ResponseWriter as well as some others. You can think of them as
    41  // middleware for the function calls they target. See Wrap for more details.
    42  type Hooks struct {
    43  	Header      func(HeaderFunc) HeaderFunc
    44  	WriteHeader func(WriteHeaderFunc) WriteHeaderFunc
    45  	Write       func(WriteFunc) WriteFunc
    46  	Flush       func(FlushFunc) FlushFunc
    47  	CloseNotify func(CloseNotifyFunc) CloseNotifyFunc
    48  	Hijack      func(HijackFunc) HijackFunc
    49  	ReadFrom    func(ReadFromFunc) ReadFromFunc
    50  	Push        func(PushFunc) PushFunc
    51  }
    52  
    53  // Wrap returns a wrapped version of w that provides the exact same interface
    54  // as w. Specifically if w implements any combination of:
    55  //
    56  // - http.Flusher
    57  // - http.CloseNotifier
    58  // - http.Hijacker
    59  // - io.ReaderFrom
    60  // - http.Pusher
    61  //
    62  // The wrapped version will implement the exact same combination. If no hooks
    63  // are set, the wrapped version also behaves exactly as w. Hooks targeting
    64  // methods not supported by w are ignored. Any other hooks will intercept the
    65  // method they target and may modify the call's arguments and/or return values.
    66  // The CaptureMetrics implementation serves as a working example for how the
    67  // hooks can be used.
    68  func Wrap(w http.ResponseWriter, hooks Hooks) http.ResponseWriter {
    69  	rw := &rw{w: w, h: hooks}
    70  	_, i0 := w.(http.Flusher)
    71  	_, i1 := w.(http.CloseNotifier)
    72  	_, i2 := w.(http.Hijacker)
    73  	_, i3 := w.(io.ReaderFrom)
    74  	_, i4 := w.(http.Pusher)
    75  	switch {
    76  	// combination 1/32
    77  	case !i0 && !i1 && !i2 && !i3 && !i4:
    78  		return struct {
    79  			Unwrapper
    80  			http.ResponseWriter
    81  		}{rw, rw}
    82  	// combination 2/32
    83  	case !i0 && !i1 && !i2 && !i3 && i4:
    84  		return struct {
    85  			Unwrapper
    86  			http.ResponseWriter
    87  			http.Pusher
    88  		}{rw, rw, rw}
    89  	// combination 3/32
    90  	case !i0 && !i1 && !i2 && i3 && !i4:
    91  		return struct {
    92  			Unwrapper
    93  			http.ResponseWriter
    94  			io.ReaderFrom
    95  		}{rw, rw, rw}
    96  	// combination 4/32
    97  	case !i0 && !i1 && !i2 && i3 && i4:
    98  		return struct {
    99  			Unwrapper
   100  			http.ResponseWriter
   101  			io.ReaderFrom
   102  			http.Pusher
   103  		}{rw, rw, rw, rw}
   104  	// combination 5/32
   105  	case !i0 && !i1 && i2 && !i3 && !i4:
   106  		return struct {
   107  			Unwrapper
   108  			http.ResponseWriter
   109  			http.Hijacker
   110  		}{rw, rw, rw}
   111  	// combination 6/32
   112  	case !i0 && !i1 && i2 && !i3 && i4:
   113  		return struct {
   114  			Unwrapper
   115  			http.ResponseWriter
   116  			http.Hijacker
   117  			http.Pusher
   118  		}{rw, rw, rw, rw}
   119  	// combination 7/32
   120  	case !i0 && !i1 && i2 && i3 && !i4:
   121  		return struct {
   122  			Unwrapper
   123  			http.ResponseWriter
   124  			http.Hijacker
   125  			io.ReaderFrom
   126  		}{rw, rw, rw, rw}
   127  	// combination 8/32
   128  	case !i0 && !i1 && i2 && i3 && i4:
   129  		return struct {
   130  			Unwrapper
   131  			http.ResponseWriter
   132  			http.Hijacker
   133  			io.ReaderFrom
   134  			http.Pusher
   135  		}{rw, rw, rw, rw, rw}
   136  	// combination 9/32
   137  	case !i0 && i1 && !i2 && !i3 && !i4:
   138  		return struct {
   139  			Unwrapper
   140  			http.ResponseWriter
   141  			http.CloseNotifier
   142  		}{rw, rw, rw}
   143  	// combination 10/32
   144  	case !i0 && i1 && !i2 && !i3 && i4:
   145  		return struct {
   146  			Unwrapper
   147  			http.ResponseWriter
   148  			http.CloseNotifier
   149  			http.Pusher
   150  		}{rw, rw, rw, rw}
   151  	// combination 11/32
   152  	case !i0 && i1 && !i2 && i3 && !i4:
   153  		return struct {
   154  			Unwrapper
   155  			http.ResponseWriter
   156  			http.CloseNotifier
   157  			io.ReaderFrom
   158  		}{rw, rw, rw, rw}
   159  	// combination 12/32
   160  	case !i0 && i1 && !i2 && i3 && i4:
   161  		return struct {
   162  			Unwrapper
   163  			http.ResponseWriter
   164  			http.CloseNotifier
   165  			io.ReaderFrom
   166  			http.Pusher
   167  		}{rw, rw, rw, rw, rw}
   168  	// combination 13/32
   169  	case !i0 && i1 && i2 && !i3 && !i4:
   170  		return struct {
   171  			Unwrapper
   172  			http.ResponseWriter
   173  			http.CloseNotifier
   174  			http.Hijacker
   175  		}{rw, rw, rw, rw}
   176  	// combination 14/32
   177  	case !i0 && i1 && i2 && !i3 && i4:
   178  		return struct {
   179  			Unwrapper
   180  			http.ResponseWriter
   181  			http.CloseNotifier
   182  			http.Hijacker
   183  			http.Pusher
   184  		}{rw, rw, rw, rw, rw}
   185  	// combination 15/32
   186  	case !i0 && i1 && i2 && i3 && !i4:
   187  		return struct {
   188  			Unwrapper
   189  			http.ResponseWriter
   190  			http.CloseNotifier
   191  			http.Hijacker
   192  			io.ReaderFrom
   193  		}{rw, rw, rw, rw, rw}
   194  	// combination 16/32
   195  	case !i0 && i1 && i2 && i3 && i4:
   196  		return struct {
   197  			Unwrapper
   198  			http.ResponseWriter
   199  			http.CloseNotifier
   200  			http.Hijacker
   201  			io.ReaderFrom
   202  			http.Pusher
   203  		}{rw, rw, rw, rw, rw, rw}
   204  	// combination 17/32
   205  	case i0 && !i1 && !i2 && !i3 && !i4:
   206  		return struct {
   207  			Unwrapper
   208  			http.ResponseWriter
   209  			http.Flusher
   210  		}{rw, rw, rw}
   211  	// combination 18/32
   212  	case i0 && !i1 && !i2 && !i3 && i4:
   213  		return struct {
   214  			Unwrapper
   215  			http.ResponseWriter
   216  			http.Flusher
   217  			http.Pusher
   218  		}{rw, rw, rw, rw}
   219  	// combination 19/32
   220  	case i0 && !i1 && !i2 && i3 && !i4:
   221  		return struct {
   222  			Unwrapper
   223  			http.ResponseWriter
   224  			http.Flusher
   225  			io.ReaderFrom
   226  		}{rw, rw, rw, rw}
   227  	// combination 20/32
   228  	case i0 && !i1 && !i2 && i3 && i4:
   229  		return struct {
   230  			Unwrapper
   231  			http.ResponseWriter
   232  			http.Flusher
   233  			io.ReaderFrom
   234  			http.Pusher
   235  		}{rw, rw, rw, rw, rw}
   236  	// combination 21/32
   237  	case i0 && !i1 && i2 && !i3 && !i4:
   238  		return struct {
   239  			Unwrapper
   240  			http.ResponseWriter
   241  			http.Flusher
   242  			http.Hijacker
   243  		}{rw, rw, rw, rw}
   244  	// combination 22/32
   245  	case i0 && !i1 && i2 && !i3 && i4:
   246  		return struct {
   247  			Unwrapper
   248  			http.ResponseWriter
   249  			http.Flusher
   250  			http.Hijacker
   251  			http.Pusher
   252  		}{rw, rw, rw, rw, rw}
   253  	// combination 23/32
   254  	case i0 && !i1 && i2 && i3 && !i4:
   255  		return struct {
   256  			Unwrapper
   257  			http.ResponseWriter
   258  			http.Flusher
   259  			http.Hijacker
   260  			io.ReaderFrom
   261  		}{rw, rw, rw, rw, rw}
   262  	// combination 24/32
   263  	case i0 && !i1 && i2 && i3 && i4:
   264  		return struct {
   265  			Unwrapper
   266  			http.ResponseWriter
   267  			http.Flusher
   268  			http.Hijacker
   269  			io.ReaderFrom
   270  			http.Pusher
   271  		}{rw, rw, rw, rw, rw, rw}
   272  	// combination 25/32
   273  	case i0 && i1 && !i2 && !i3 && !i4:
   274  		return struct {
   275  			Unwrapper
   276  			http.ResponseWriter
   277  			http.Flusher
   278  			http.CloseNotifier
   279  		}{rw, rw, rw, rw}
   280  	// combination 26/32
   281  	case i0 && i1 && !i2 && !i3 && i4:
   282  		return struct {
   283  			Unwrapper
   284  			http.ResponseWriter
   285  			http.Flusher
   286  			http.CloseNotifier
   287  			http.Pusher
   288  		}{rw, rw, rw, rw, rw}
   289  	// combination 27/32
   290  	case i0 && i1 && !i2 && i3 && !i4:
   291  		return struct {
   292  			Unwrapper
   293  			http.ResponseWriter
   294  			http.Flusher
   295  			http.CloseNotifier
   296  			io.ReaderFrom
   297  		}{rw, rw, rw, rw, rw}
   298  	// combination 28/32
   299  	case i0 && i1 && !i2 && i3 && i4:
   300  		return struct {
   301  			Unwrapper
   302  			http.ResponseWriter
   303  			http.Flusher
   304  			http.CloseNotifier
   305  			io.ReaderFrom
   306  			http.Pusher
   307  		}{rw, rw, rw, rw, rw, rw}
   308  	// combination 29/32
   309  	case i0 && i1 && i2 && !i3 && !i4:
   310  		return struct {
   311  			Unwrapper
   312  			http.ResponseWriter
   313  			http.Flusher
   314  			http.CloseNotifier
   315  			http.Hijacker
   316  		}{rw, rw, rw, rw, rw}
   317  	// combination 30/32
   318  	case i0 && i1 && i2 && !i3 && i4:
   319  		return struct {
   320  			Unwrapper
   321  			http.ResponseWriter
   322  			http.Flusher
   323  			http.CloseNotifier
   324  			http.Hijacker
   325  			http.Pusher
   326  		}{rw, rw, rw, rw, rw, rw}
   327  	// combination 31/32
   328  	case i0 && i1 && i2 && i3 && !i4:
   329  		return struct {
   330  			Unwrapper
   331  			http.ResponseWriter
   332  			http.Flusher
   333  			http.CloseNotifier
   334  			http.Hijacker
   335  			io.ReaderFrom
   336  		}{rw, rw, rw, rw, rw, rw}
   337  	// combination 32/32
   338  	case i0 && i1 && i2 && i3 && i4:
   339  		return struct {
   340  			Unwrapper
   341  			http.ResponseWriter
   342  			http.Flusher
   343  			http.CloseNotifier
   344  			http.Hijacker
   345  			io.ReaderFrom
   346  			http.Pusher
   347  		}{rw, rw, rw, rw, rw, rw, rw}
   348  	}
   349  	panic("unreachable")
   350  }
   351  
   352  type rw struct {
   353  	w http.ResponseWriter
   354  	h Hooks
   355  }
   356  
   357  func (w *rw) Unwrap() http.ResponseWriter {
   358  	return w.w
   359  }
   360  
   361  func (w *rw) Header() http.Header {
   362  	f := w.w.(http.ResponseWriter).Header
   363  	if w.h.Header != nil {
   364  		f = w.h.Header(f)
   365  	}
   366  	return f()
   367  }
   368  
   369  func (w *rw) WriteHeader(code int) {
   370  	f := w.w.(http.ResponseWriter).WriteHeader
   371  	if w.h.WriteHeader != nil {
   372  		f = w.h.WriteHeader(f)
   373  	}
   374  	f(code)
   375  }
   376  
   377  func (w *rw) Write(b []byte) (int, error) {
   378  	f := w.w.(http.ResponseWriter).Write
   379  	if w.h.Write != nil {
   380  		f = w.h.Write(f)
   381  	}
   382  	return f(b)
   383  }
   384  
   385  func (w *rw) Flush() {
   386  	f := w.w.(http.Flusher).Flush
   387  	if w.h.Flush != nil {
   388  		f = w.h.Flush(f)
   389  	}
   390  	f()
   391  }
   392  
   393  func (w *rw) CloseNotify() <-chan bool {
   394  	f := w.w.(http.CloseNotifier).CloseNotify
   395  	if w.h.CloseNotify != nil {
   396  		f = w.h.CloseNotify(f)
   397  	}
   398  	return f()
   399  }
   400  
   401  func (w *rw) Hijack() (net.Conn, *bufio.ReadWriter, error) {
   402  	f := w.w.(http.Hijacker).Hijack
   403  	if w.h.Hijack != nil {
   404  		f = w.h.Hijack(f)
   405  	}
   406  	return f()
   407  }
   408  
   409  func (w *rw) ReadFrom(src io.Reader) (int64, error) {
   410  	f := w.w.(io.ReaderFrom).ReadFrom
   411  	if w.h.ReadFrom != nil {
   412  		f = w.h.ReadFrom(f)
   413  	}
   414  	return f(src)
   415  }
   416  
   417  func (w *rw) Push(target string, opts *http.PushOptions) error {
   418  	f := w.w.(http.Pusher).Push
   419  	if w.h.Push != nil {
   420  		f = w.h.Push(f)
   421  	}
   422  	return f(target, opts)
   423  }
   424  
   425  type Unwrapper interface {
   426  	Unwrap() http.ResponseWriter
   427  }
   428  
   429  // Unwrap returns the underlying http.ResponseWriter from within zero or more
   430  // layers of httpsnoop wrappers.
   431  func Unwrap(w http.ResponseWriter) http.ResponseWriter {
   432  	if rw, ok := w.(Unwrapper); ok {
   433  		// recurse until rw.Unwrap() returns a non-Unwrapper
   434  		return Unwrap(rw.Unwrap())
   435  	} else {
   436  		return w
   437  	}
   438  }