encode: prioritize zstd and br over gzip in content negotiation (#7772)
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.26.0, 1.26, aix) (push) Successful in 2m10s
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Failing after 3m19s
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Successful in 1m48s
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Successful in 1m30s
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Successful in 1m49s
Cross-Build / build (~1.26.0, 1.26, linux) (push) Successful in 1m46s
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Successful in 3m3s
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Successful in 1m48s
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Successful in 2m0s
Cross-Build / build (~1.26.0, 1.26, windows) (push) Successful in 1m7s
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Successful in 3m6s
Lint / dependency-review (push) Failing after 36s
Lint / govulncheck (push) Successful in 1m41s
Lint / lint (ubuntu-latest, linux) (push) Successful in 2m42s
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Failing after 6m31s
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-14, 0, 1.26, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled

* fix(encode): prioritize zstd and br over gzip in content negotiation

* test(encode): update unit tests to reflect new default priority ties

* fix(encode): move default preferences to dynamic encode handler and restore generic negotiation helper

* test(encode): call real Provision function in served-response test

* test(encode): rename served-response test to TestServeHTTPDefaultEncodingPreference

* refactor(encode): use slices.SortStableFunc and httptest.NewRecorder as recommended

* refactor(encode): simplify sorting with cmp.Compare and check request error in test

* test(encode): fix variable redeclaration in TestServeHTTPDefaultEncodingPreference

Fix 'no new variables on left side of :=' error by changing 'err :=' to 'err ='
on line 347, since err was already declared on line 332.

This fixes the build failure in the encode module tests.
This commit is contained in:
Muhammad Syafri, S.Kom
2026-05-29 02:26:19 +07:00
committed by GitHub
parent 86121c860f
commit 03e08ee6a9
2 changed files with 88 additions and 5 deletions
+13 -5
View File
@@ -20,12 +20,12 @@
package encode
import (
"cmp"
"fmt"
"io"
"math"
"net/http"
"slices"
"sort"
"strconv"
"strings"
"sync"
@@ -127,6 +127,14 @@ func (enc *Encode) Provision(ctx caddy.Context) error {
}
}
if len(enc.Prefer) == 0 {
for _, encName := range []string{"zstd", "br", "gzip"} {
if _, ok := enc.writerPools[encName]; ok {
enc.Prefer = append(enc.Prefer, encName)
}
}
}
return nil
}
@@ -538,11 +546,11 @@ func AcceptedEncodings(r *http.Request, preferredOrder []string) []string {
}
// sort preferences by descending q-factor first, then by preferOrder
sort.Slice(prefs, func(i, j int) bool {
if math.Abs(prefs[i].q-prefs[j].q) < 0.00001 {
return prefs[i].preferOrder > prefs[j].preferOrder
slices.SortStableFunc(prefs, func(a, b encodingPreference) int {
if math.Abs(a.q-b.q) < 0.00001 {
return cmp.Compare(b.preferOrder, a.preferOrder)
}
return prefs[i].q > prefs[j].q
return cmp.Compare(b.q, a.q)
})
prefEncNames := make([]string, len(prefs))