|
1 | 1 | package middleware
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bufio" |
5 | 4 | "compress/flate"
|
6 | 5 | "compress/gzip"
|
7 | 6 | "io"
|
8 |
| - "net" |
9 | 7 | "net/http"
|
10 | 8 | "strings"
|
11 | 9 | )
|
@@ -62,7 +60,7 @@ func Compress(level int, types ...string) func(next http.Handler) http.Handler {
|
62 | 60 | }
|
63 | 61 | defer mcw.Close()
|
64 | 62 |
|
65 |
| - next.ServeHTTP(mkGenericWrapper(mcw), r) |
| 63 | + next.ServeHTTP(mcw, r) |
66 | 64 | }
|
67 | 65 |
|
68 | 66 | return http.HandlerFunc(fn)
|
@@ -175,82 +173,15 @@ func (w *maybeCompressResponseWriter) Write(p []byte) (int, error) {
|
175 | 173 | return w.w.Write(p)
|
176 | 174 | }
|
177 | 175 |
|
178 |
| -func (w *maybeCompressResponseWriter) Unwrap() http.ResponseWriter { |
179 |
| - return w.ResponseWriter |
| 176 | +func (w *maybeCompressResponseWriter) Flush() { |
| 177 | + if f, ok := w.w.(http.Flusher); ok { |
| 178 | + f.Flush() |
| 179 | + } |
180 | 180 | }
|
181 | 181 |
|
182 | 182 | func (w *maybeCompressResponseWriter) Close() error {
|
183 |
| - if c, ok := w.w.(io.Closer); ok { |
| 183 | + if c, ok := w.w.(io.WriteCloser); ok { |
184 | 184 | return c.Close()
|
185 | 185 | }
|
186 | 186 | return nil
|
187 | 187 | }
|
188 |
| - |
189 |
| -type responseWriterWrapper interface { |
190 |
| - http.ResponseWriter |
191 |
| - Unwrap() http.ResponseWriter |
192 |
| -} |
193 |
| - |
194 |
| -type genericWrapper struct { |
195 |
| - inner responseWriterWrapper |
196 |
| -} |
197 |
| - |
198 |
| -func (g *genericWrapper) Unwrap() http.ResponseWriter { |
199 |
| - return g.inner |
200 |
| -} |
201 |
| - |
202 |
| -func (g *genericWrapper) Header() http.Header { |
203 |
| - return g.inner.Header() |
204 |
| -} |
205 |
| - |
206 |
| -func (g *genericWrapper) Write(data []byte) (int, error) { |
207 |
| - return g.inner.Write(data) |
208 |
| -} |
209 |
| - |
210 |
| -func (g *genericWrapper) WriteHeader(status int) { |
211 |
| - g.inner.WriteHeader(status) |
212 |
| -} |
213 |
| - |
214 |
| -var _ http.ResponseWriter = &genericWrapper{} |
215 |
| - |
216 |
| -type httpGenericWrapper struct { |
217 |
| - genericWrapper |
218 |
| -} |
219 |
| - |
220 |
| -func (h *httpGenericWrapper) CloseNotify() <-chan bool { |
221 |
| - if cn, ok := h.genericWrapper.inner.(http.CloseNotifier); ok { |
222 |
| - return cn.CloseNotify() |
223 |
| - } |
224 |
| - |
225 |
| - return h.genericWrapper.inner.Unwrap().(http.CloseNotifier).CloseNotify() |
226 |
| -} |
227 |
| -func (h *httpGenericWrapper) Flush() { |
228 |
| - if f, ok := h.genericWrapper.inner.(http.Flusher); ok { |
229 |
| - f.Flush() |
230 |
| - return |
231 |
| - } |
232 |
| - |
233 |
| - h.genericWrapper.inner.Unwrap().(http.Flusher).Flush() |
234 |
| -} |
235 |
| - |
236 |
| -func (h *httpGenericWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
237 |
| - if hj, ok := h.genericWrapper.inner.(http.Hijacker); ok { |
238 |
| - return hj.Hijack() |
239 |
| - } |
240 |
| - |
241 |
| - return h.genericWrapper.inner.Unwrap().(http.Hijacker).Hijack() |
242 |
| -} |
243 |
| - |
244 |
| -func (h *httpGenericWrapper) ReadFrom(r io.Reader) (n int64, err error) { |
245 |
| - if rf, ok := h.genericWrapper.inner.(io.ReaderFrom); ok { |
246 |
| - return rf.ReadFrom(r) |
247 |
| - } |
248 |
| - |
249 |
| - return h.genericWrapper.inner.Unwrap().(io.ReaderFrom).ReadFrom(r) |
250 |
| -} |
251 |
| - |
252 |
| -var _ http.ResponseWriter = &httpGenericWrapper{} |
253 |
| -var _ http.CloseNotifier = &httpGenericWrapper{} |
254 |
| -var _ http.Flusher = &httpGenericWrapper{} |
255 |
| -var _ http.Hijacker = &httpGenericWrapper{} |
256 |
| -var _ io.ReaderFrom = &httpGenericWrapper{} |
0 commit comments