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

Method selectEncoder

middleware/compress.go:211–238  ·  view source on GitHub ↗

selectEncoder returns the encoder, the name of the encoder, and a closer function.

(h http.Header, w io.Writer)

Source from the content-addressed store, hash-verified

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()) {
212 header := h.Get("Accept-Encoding")
213
214 // Parse the names of all accepted algorithms from the header.
215 accepted := strings.Split(strings.ToLower(header), ",")
216
217 // Find supported encoder by accepted list by precedence
218 for _, name := range c.encodingPrecedence {
219 if matchAcceptEncoding(accepted, name) {
220 if pool, ok := c.pooledEncoders[name]; ok {
221 encoder := pool.Get().(ioResetterWriter)
222 cleanup := func() {
223 pool.Put(encoder)
224 }
225 encoder.Reset(w)
226 return encoder, name, cleanup
227
228 }
229 if fn, ok := c.encoders[name]; ok {
230 return fn(w, c.level), name, func() {}
231 }
232 }
233
234 }
235
236 // No encoder found to match the accepted encoding
237 return nil, "", func() {}
238}
239
240func matchAcceptEncoding(accepted []string, encoding string) bool {
241 for _, v := range accepted {

Callers 1

HandlerMethod · 0.95

Calls 4

matchAcceptEncodingFunction · 0.85
GetMethod · 0.65
PutMethod · 0.65
ResetMethod · 0.65

Tested by

no test coverage detected