wrap_generated_gteq_1.8.go 9.6 KB

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