| 1 |
// Copyright 2013 The Go Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file or at |
| 5 |
// https://developers.google.com/open-source/licenses/bsd. |
| 6 |
|
| 7 |
package httputil |
| 8 |
|
| 9 |
import ( |
| 10 |
"net/http" |
| 11 |
|
| 12 |
"github.com/prometheus/client_golang/internal/github.com/golang/gddo/httputil/header" |
| 13 |
) |
| 14 |
|
| 15 |
// NegotiateContentEncoding returns the best offered content encoding for the |
| 16 |
// request's Accept-Encoding header. If two offers match with equal weight and |
| 17 |
// then the offer earlier in the list is preferred. If no offers are |
| 18 |
// acceptable, then "" is returned. |
| 19 |
func NegotiateContentEncoding(r *http.Request, offers []string) string { |
| 20 |
bestOffer := "identity" |
| 21 |
bestQ := -1.0 |
| 22 |
specs := header.ParseAccept(r.Header, "Accept-Encoding") |
| 23 |
for _, offer := range offers { |
| 24 |
for _, spec := range specs { |
| 25 |
if spec.Q > bestQ && |
| 26 |
(spec.Value == "*" || spec.Value == offer) { |
| 27 |
bestQ = spec.Q |
| 28 |
bestOffer = offer |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
if bestQ == 0 { |
| 33 |
bestOffer = "" |
| 34 |
} |
| 35 |
return bestOffer |
| 36 |
} |