Handler returns a new middleware that will compress the response based on the current Compressor.
(next http.Handler)
| 185 | // Handler returns a new middleware that will compress the response based on the |
| 186 | // current Compressor. |
| 187 | func (c *Compressor) Handler(next http.Handler) http.Handler { |
| 188 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 189 | encoder, encoding, cleanup := c.selectEncoder(r.Header, w) |
| 190 | |
| 191 | cw := &compressResponseWriter{ |
| 192 | ResponseWriter: w, |
| 193 | w: w, |
| 194 | contentTypes: c.allowedTypes, |
| 195 | contentWildcards: c.allowedWildcards, |
| 196 | encoding: encoding, |
| 197 | compressible: false, // determined in post-handler |
| 198 | } |
| 199 | if encoder != nil { |
| 200 | cw.w = encoder |
| 201 | } |
| 202 | // Re-add the encoder to the pool if applicable. |
| 203 | defer cleanup() |
| 204 | defer cw.Close() |
| 205 | |
| 206 | next.ServeHTTP(cw, r) |
| 207 | }) |
| 208 | } |
| 209 | |
| 210 | // selectEncoder returns the encoder, the name of the encoder, and a closer function. |
| 211 | func (c *Compressor) selectEncoder(h http.Header, w io.Writer) (io.Writer, string, func()) { |