Wrap returns a wrapped version of w that provides the exact same interface as w. Specifically if w implements any combination of: - http.Flusher - httpFlushError - http.CloseNotifier - http.Hijacker - io.ReaderFrom - deadliner - fullDuplexEnabler - http.Pusher - io.StringWriter The wrapped version
(w http.ResponseWriter, hooks Hooks)
| 105 | // The CaptureMetrics implementation serves as a working example for how the |
| 106 | // hooks can be used. |
| 107 | func Wrap(w http.ResponseWriter, hooks Hooks) http.ResponseWriter { |
| 108 | state := &rwState{w: w} |
| 109 | var combo uint16 |
| 110 | if hooks.Header != nil { |
| 111 | state.header = hooks.Header(w.Header) |
| 112 | } |
| 113 | if hooks.WriteHeader != nil { |
| 114 | state.writeHeader = hooks.WriteHeader(w.WriteHeader) |
| 115 | } |
| 116 | if hooks.Write != nil { |
| 117 | state.write = hooks.Write(w.Write) |
| 118 | } |
| 119 | if t0, i0 := w.(http.Flusher); i0 { |
| 120 | combo |= 1 << 8 |
| 121 | if hooks.Flush != nil { |
| 122 | state.flush = hooks.Flush(t0.Flush) |
| 123 | } |
| 124 | } |
| 125 | if t1, i1 := w.(httpFlushError); i1 { |
| 126 | combo |= 1 << 7 |
| 127 | if hooks.FlushError != nil { |
| 128 | state.flushError = hooks.FlushError(t1.FlushError) |
| 129 | } else if state.flush != nil { |
| 130 | state.flushError = func() (err error) { hooks.Flush(func() { err = t1.FlushError() })(); return err } |
| 131 | } |
| 132 | } |
| 133 | if t2, i2 := w.(http.CloseNotifier); i2 { |
| 134 | combo |= 1 << 6 |
| 135 | if hooks.CloseNotify != nil { |
| 136 | state.closeNotify = hooks.CloseNotify(t2.CloseNotify) |
| 137 | } |
| 138 | } |
| 139 | if t3, i3 := w.(http.Hijacker); i3 { |
| 140 | combo |= 1 << 5 |
| 141 | if hooks.Hijack != nil { |
| 142 | state.hijack = hooks.Hijack(t3.Hijack) |
| 143 | } |
| 144 | } |
| 145 | if t4, i4 := w.(io.ReaderFrom); i4 { |
| 146 | combo |= 1 << 4 |
| 147 | if hooks.ReadFrom != nil { |
| 148 | state.readFrom = hooks.ReadFrom(t4.ReadFrom) |
| 149 | } |
| 150 | } |
| 151 | if t5, i5 := w.(deadliner); i5 { |
| 152 | combo |= 1 << 3 |
| 153 | if hooks.SetReadDeadline != nil { |
| 154 | state.setReadDeadline = hooks.SetReadDeadline(t5.SetReadDeadline) |
| 155 | } |
| 156 | if hooks.SetWriteDeadline != nil { |
| 157 | state.setWriteDeadline = hooks.SetWriteDeadline(t5.SetWriteDeadline) |
| 158 | } |
| 159 | } |
| 160 | if t6, i6 := w.(fullDuplexEnabler); i6 { |
| 161 | combo |= 1 << 2 |
| 162 | if hooks.EnableFullDuplex != nil { |
| 163 | state.enableFullDuplex = hooks.EnableFullDuplex(t6.EnableFullDuplex) |
| 164 | } |
searching dependent graphs…