MCPcopy
hub / github.com/go-chi/chi / Handler

Method Handler

middleware/compress.go:187–208  ·  view source on GitHub ↗

Handler returns a new middleware that will compress the response based on the current Compressor.

(next http.Handler)

Source from the content-addressed store, hash-verified

185// Handler returns a new middleware that will compress the response based on the
186// current Compressor.
187func (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.
211func (c *Compressor) selectEncoder(h http.Header, w io.Writer) (io.Writer, string, func()) {

Callers 2

TestRouteHeadersFunction · 0.45
ProfilerFunction · 0.45

Calls 4

selectEncoderMethod · 0.95
CloseMethod · 0.95
HandlerFuncMethod · 0.80
ServeHTTPMethod · 0.45

Tested by 1

TestRouteHeadersFunction · 0.36