selectEncoder returns the encoder, the name of the encoder, and a closer function.
(h http.Header, w io.Writer)
| 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()) { |
| 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 | |
| 240 | func matchAcceptEncoding(accepted []string, encoding string) bool { |
| 241 | for _, v := range accepted { |
no test coverage detected