From c2928f483f7ddc3cfce6c3c2900a1b2d49cfb694 Mon Sep 17 00:00:00 2001 From: miyuko Date: Thu, 18 Jun 2026 20:45:52 +0100 Subject: [PATCH] Fix 406 Not Acceptable when Accept-Encoding ends with `*;q=0`. Fixes: https://codeberg.org/git-pages/git-pages/issues/203 --- src/http.go | 18 +++++++----------- src/pages.go | 4 +++- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/http.go b/src/http.go index 6095fec..f2b9d0e 100644 --- a/src/http.go +++ b/src/http.go @@ -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) } diff --git a/src/pages.go b/src/pages.go index 05d89fc..b03a29a 100644 --- a/src/pages.go +++ b/src/pages.go @@ -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 {