|
| 1 | +// +build go1.7 !go1.8 |
| 2 | + |
| 3 | +package nethttp |
| 4 | + |
| 5 | +import ( |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +type statusCodeTracker struct { |
| 11 | + http.ResponseWriter |
| 12 | + status int |
| 13 | +} |
| 14 | + |
| 15 | +func (w *statusCodeTracker) WriteHeader(status int) { |
| 16 | + w.status = status |
| 17 | + w.ResponseWriter.WriteHeader(status) |
| 18 | +} |
| 19 | + |
| 20 | +// wrappedResponseWriter returns a wrapped version of the original |
| 21 | +// ResponseWriter and only implements the same combination of additional |
| 22 | +// interfaces as the original. This implementation is based on |
| 23 | +// https://github.com/felixge/httpsnoop. |
| 24 | +func (w *statusCodeTracker) wrappedResponseWriter() http.ResponseWriter { |
| 25 | + var ( |
| 26 | + hj, i0 = w.ResponseWriter.(http.Hijacker) |
| 27 | + cn, i1 = w.ResponseWriter.(http.CloseNotifier) |
| 28 | + fl, i3 = w.ResponseWriter.(http.Flusher) |
| 29 | + rf, i4 = w.ResponseWriter.(io.ReaderFrom) |
| 30 | + ) |
| 31 | + i2 := false |
| 32 | + |
| 33 | + switch { |
| 34 | + case !i0 && !i1 && !i2 && !i3 && !i4: |
| 35 | + return struct { |
| 36 | + http.ResponseWriter |
| 37 | + }{w} |
| 38 | + case !i0 && !i1 && !i2 && !i3 && i4: |
| 39 | + return struct { |
| 40 | + http.ResponseWriter |
| 41 | + io.ReaderFrom |
| 42 | + }{w, rf} |
| 43 | + case !i0 && !i1 && !i2 && i3 && !i4: |
| 44 | + return struct { |
| 45 | + http.ResponseWriter |
| 46 | + http.Flusher |
| 47 | + }{w, fl} |
| 48 | + case !i0 && !i1 && !i2 && i3 && i4: |
| 49 | + return struct { |
| 50 | + http.ResponseWriter |
| 51 | + http.Flusher |
| 52 | + io.ReaderFrom |
| 53 | + }{w, fl, rf} |
| 54 | + case !i0 && i1 && !i2 && !i3 && !i4: |
| 55 | + return struct { |
| 56 | + http.ResponseWriter |
| 57 | + http.CloseNotifier |
| 58 | + }{w, cn} |
| 59 | + case !i0 && i1 && !i2 && !i3 && i4: |
| 60 | + return struct { |
| 61 | + http.ResponseWriter |
| 62 | + http.CloseNotifier |
| 63 | + io.ReaderFrom |
| 64 | + }{w, cn, rf} |
| 65 | + case !i0 && i1 && !i2 && i3 && !i4: |
| 66 | + return struct { |
| 67 | + http.ResponseWriter |
| 68 | + http.CloseNotifier |
| 69 | + http.Flusher |
| 70 | + }{w, cn, fl} |
| 71 | + case !i0 && i1 && !i2 && i3 && i4: |
| 72 | + return struct { |
| 73 | + http.ResponseWriter |
| 74 | + http.CloseNotifier |
| 75 | + http.Flusher |
| 76 | + io.ReaderFrom |
| 77 | + }{w, cn, fl, rf} |
| 78 | + case i0 && !i1 && !i2 && !i3 && !i4: |
| 79 | + return struct { |
| 80 | + http.ResponseWriter |
| 81 | + http.Hijacker |
| 82 | + }{w, hj} |
| 83 | + case i0 && !i1 && !i2 && !i3 && i4: |
| 84 | + return struct { |
| 85 | + http.ResponseWriter |
| 86 | + http.Hijacker |
| 87 | + io.ReaderFrom |
| 88 | + }{w, hj, rf} |
| 89 | + case i0 && !i1 && !i2 && i3 && !i4: |
| 90 | + return struct { |
| 91 | + http.ResponseWriter |
| 92 | + http.Hijacker |
| 93 | + http.Flusher |
| 94 | + }{w, hj, fl} |
| 95 | + case i0 && !i1 && !i2 && i3 && i4: |
| 96 | + return struct { |
| 97 | + http.ResponseWriter |
| 98 | + http.Hijacker |
| 99 | + http.Flusher |
| 100 | + io.ReaderFrom |
| 101 | + }{w, hj, fl, rf} |
| 102 | + case i0 && i1 && !i2 && !i3 && !i4: |
| 103 | + return struct { |
| 104 | + http.ResponseWriter |
| 105 | + http.Hijacker |
| 106 | + http.CloseNotifier |
| 107 | + }{w, hj, cn} |
| 108 | + case i0 && i1 && !i2 && !i3 && i4: |
| 109 | + return struct { |
| 110 | + http.ResponseWriter |
| 111 | + http.Hijacker |
| 112 | + http.CloseNotifier |
| 113 | + io.ReaderFrom |
| 114 | + }{w, hj, cn, rf} |
| 115 | + case i0 && i1 && !i2 && i3 && !i4: |
| 116 | + return struct { |
| 117 | + http.ResponseWriter |
| 118 | + http.Hijacker |
| 119 | + http.CloseNotifier |
| 120 | + http.Flusher |
| 121 | + }{w, hj, cn, fl} |
| 122 | + case i0 && i1 && !i2 && i3 && i4: |
| 123 | + return struct { |
| 124 | + http.ResponseWriter |
| 125 | + http.Hijacker |
| 126 | + http.CloseNotifier |
| 127 | + http.Flusher |
| 128 | + io.ReaderFrom |
| 129 | + }{w, hj, cn, fl, rf} |
| 130 | + default: |
| 131 | + return struct { |
| 132 | + http.ResponseWriter |
| 133 | + }{w} |
| 134 | + } |
| 135 | +} |
0 commit comments