Fix 406 Not Acceptable when Accept-Encoding ends with *;q=0.

Fixes: https://codeberg.org/git-pages/git-pages/issues/203
This commit is contained in:
miyuko
2026-06-18 20:45:52 +01:00
parent 28012ffa5a
commit c2928f483f
2 changed files with 10 additions and 12 deletions
+7 -11
View File
@@ -105,18 +105,10 @@ func ParseAcceptEncodingHeader(headerValue string) (result HTTPEncodings) {
// Negotiate returns the most preferred encoding that is acceptable by the
// client, or an empty string if no encodings are acceptable.
func (e *HTTPEncodings) Negotiate(offers ...string) string {
prefs := make(map[string]float64, len(offers))
for _, code := range offers {
prefs[code] = 0
}
prefs := make(map[string]float64, len(offers)+1)
implicitIdentity := true
for _, enc := range e.encodings {
if enc.code == "*" {
for code := range prefs {
prefs[code] = enc.qval
}
implicitIdentity = false
} else if _, ok := prefs[enc.code]; ok {
if slices.Contains(offers, enc.code) || enc.code == "*" {
prefs[enc.code] = enc.qval
}
if enc.code == "*" || enc.code == "identity" {
@@ -128,7 +120,11 @@ func (e *HTTPEncodings) Negotiate(offers ...string) string {
}
encs := make([]httpAcceptOffer, len(offers))
for idx, code := range offers {
encs[idx] = httpAcceptOffer{code, prefs[code]}
pref, ok := prefs[code]
if !ok {
pref = prefs["*"]
}
encs[idx] = httpAcceptOffer{code, pref}
}
return preferredAcceptOffer(encs)
}
+3 -1
View File
@@ -431,8 +431,10 @@ func getPage(w http.ResponseWriter, r *http.Request) error {
if !negotiatedEncoding {
w.Header().Set("Accept-Encoding", strings.Join(offeredEncodings, ", "))
w.WriteHeader(http.StatusNotAcceptable)
return fmt.Errorf("no supported content encodings (Accept-Encoding: %s)",
err := fmt.Errorf("no supported content encodings (Accept-Encoding: %s)",
r.Header.Get("Accept-Encoding"))
fmt.Fprintf(w, "%s", err)
return err
}
if entry != nil && entry.ContentType != nil {