From 6076c17c517e1926b35330105e04c23ccc5900c9 Mon Sep 17 00:00:00 2001 From: Catherine Date: Fri, 5 Dec 2025 16:37:49 +0000 Subject: [PATCH] Rename HTTP negotiation items. NFC --- src/http.go | 30 +++++++++++++++--------------- src/pages.go | 14 ++++++++------ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/http.go b/src/http.go index d6da818..4e6d1ef 100644 --- a/src/http.go +++ b/src/http.go @@ -8,7 +8,7 @@ import ( "strings" ) -var httpAcceptEncodingRegexp = regexp.MustCompile(`` + +var httpAcceptRegexp = regexp.MustCompile(`` + // token optionally prefixed by whitespace `^[ \t]*([a-zA-Z0-9$!#$%&'*+.^_\x60|~-]+)` + // quality value prefixed by a semicolon optionally surrounded by whitespace @@ -17,22 +17,22 @@ var httpAcceptEncodingRegexp = regexp.MustCompile(`` + `[ \t]*(?:,|$)`, ) -type httpEncoding struct { +type httpAcceptOffer struct { code string qval float64 } -type httpEncodings struct { - encodings []httpEncoding +type HTTPEncodings struct { + encodings []httpAcceptOffer } -func parseHTTPEncodings(headerValue string) (result httpEncodings) { +func ParseHTTPAcceptEncoding(headerValue string) (result HTTPEncodings) { for headerValue != "" { - matches := httpAcceptEncodingRegexp.FindStringSubmatch(headerValue) + matches := httpAcceptRegexp.FindStringSubmatch(headerValue) if matches == nil { - return httpEncodings{} + return HTTPEncodings{} } - enc := httpEncoding{strings.ToLower(matches[1]), 1.0} + enc := httpAcceptOffer{strings.ToLower(matches[1]), 1.0} if matches[2] != "" { enc.qval, _ = strconv.ParseFloat(matches[2], 64) } @@ -51,9 +51,9 @@ func parseHTTPEncodings(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(codes ...string) string { - prefs := make(map[string]float64, len(codes)) - for _, code := range codes { +func (e *HTTPEncodings) Negotiate(offers ...string) string { + prefs := make(map[string]float64, len(offers)) + for _, code := range offers { prefs[code] = 0 } implicitIdentity := true @@ -73,11 +73,11 @@ func (e *httpEncodings) Negotiate(codes ...string) string { if _, ok := prefs["identity"]; ok && implicitIdentity { prefs["identity"] = -1 // sort last } - encs := make([]httpEncoding, len(codes)) - for idx, code := range codes { - encs[idx] = httpEncoding{code, prefs[code]} + encs := make([]httpAcceptOffer, len(offers)) + for idx, code := range offers { + encs[idx] = httpAcceptOffer{code, prefs[code]} } - slices.SortStableFunc(encs, func(a, b httpEncoding) int { + slices.SortStableFunc(encs, func(a, b httpAcceptOffer) int { return -cmp.Compare(a.qval, b.qval) }) for _, enc := range encs { diff --git a/src/pages.go b/src/pages.go index 85ad62b..6f43a03 100644 --- a/src/pages.go +++ b/src/pages.go @@ -214,7 +214,7 @@ func getPage(w http.ResponseWriter, r *http.Request) error { // we only offer `/.git-pages/archive.tar` and not the `.tar.gz`/`.tar.zst` variants // because HTTP can already request compression using the `Content-Encoding` mechanism - acceptedEncodings := parseHTTPEncodings(r.Header.Get("Accept-Encoding")) + acceptedEncodings := ParseHTTPAcceptEncoding(r.Header.Get("Accept-Encoding")) negotiated := acceptedEncodings.Negotiate("zstd", "gzip", "identity") if negotiated != "" { w.Header().Set("Content-Encoding", negotiated) @@ -322,11 +322,13 @@ func getPage(w http.ResponseWriter, r *http.Request) error { defer closer.Close() } - acceptedEncodings := parseHTTPEncodings(r.Header.Get("Accept-Encoding")) + offeredEncodings := []string{} + acceptedEncodings := ParseHTTPAcceptEncoding(r.Header.Get("Accept-Encoding")) negotiatedEncoding := true switch entry.GetTransform() { case Transform_Identity: - switch acceptedEncodings.Negotiate("identity") { + offeredEncodings = []string{"identity"} + switch acceptedEncodings.Negotiate(offeredEncodings...) { case "identity": serveEncodingCount. With(prometheus.Labels{"transform": "identity", "negotiated": "identity"}). @@ -338,13 +340,13 @@ func getPage(w http.ResponseWriter, r *http.Request) error { Inc() } case Transform_Zstd: - supported := []string{"zstd", "identity"} + offeredEncodings = []string{"zstd", "identity"} if entry.ContentType == nil { // If Content-Type is unset, `http.ServeContent` will try to sniff // the file contents. That won't work if it's compressed. - supported = []string{"identity"} + offeredEncodings = []string{"identity"} } - switch acceptedEncodings.Negotiate(supported...) { + switch acceptedEncodings.Negotiate(offeredEncodings...) { case "zstd": // Set Content-Length ourselves since `http.ServeContent` only sets // it if Content-Encoding is unset or if it's a range request.