Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
encode: use httpguts to look for Accept-Encoding in Vary (#8329)
Kévin Dunglas committed
Nov 1, 2026 at 22:59 UTC
1a870400e684156f240f8db6e225c980b4357a80
3 files changed
+9
-27
modules/caddyhttp/encode/encode.go
+2
-13
@@ -287,7 +287,7 @@ func (rw *responseWriter) WriteHeader(status int) {
287
// Not Modified must have certain headers set as if it was a 200 response, and according to the issue
288
// we would miss the Vary header in this case when compression was also enabled; note that we set this
289
// header in the responseWriter.init() method but that is only called if we are writing a response body
290
- if status == http.StatusNotModified && !hasVaryValue(h, "Accept-Encoding") {
290
+ if status == http.StatusNotModified && !httpguts.HeaderValuesContainsToken(h["Vary"], "Accept-Encoding") {
291
h["Vary"] = append(h["Vary"], "Accept-Encoding")
292
}
293
@@ -541,7 +541,7 @@ func (rw *responseWriter) init() {
541
if !hasNonEmptyValue(hdr, "Content-Encoding") &&
542
!httpguts.HeaderValuesContainsToken(hdr["Cache-Control"], "no-transform") &&
543
rw.config.Match(rw) {
544
- if !hasVaryValue(hdr, "Accept-Encoding") {
544
+ if !httpguts.HeaderValuesContainsToken(hdr["Vary"], "Accept-Encoding") {
545
hdr["Vary"] = append(hdr["Vary"], "Accept-Encoding")
546
}
547
if rw.encodingName == "" {
@@ -576,17 +576,6 @@ func firstValue(values []string) string {
576
return ""
577
}
578
579
-func hasVaryValue(hdr http.Header, target string) bool {
580
- for _, vary := range hdr["Vary"] {
581
- for val := range strings.SplitSeq(vary, ",") {
582
- if strings.EqualFold(strings.TrimSpace(val), target) {
583
- return true
584
- }
585
- }
586
- }
587
- return false
588
-}
589
-
579
// AcceptedEncodings returns the list of encodings that the
580
// client supports, in descending order of preference.
581
// The client preference via q-factor and the server
modules/caddyhttp/encode/encode_conformance_test.go
+7
-6
@@ -11,8 +11,9 @@ import (
11
"testing"
12
"time"
13
14
+ "golang.org/x/net/http/httpguts"
15
+
16
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
15
- "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
17
)
18
19
const conformanceContentType = "text/plain"
@@ -404,7 +405,7 @@ func checkEncodedCorpusResponse(t *testing.T, w *httptest.ResponseRecorder, encC
405
if got := w.Header().Get("Content-Encoding"); got != encName {
406
t.Fatalf("Content-Encoding = %q, want %q", got, encName)
407
}
407
- if !encode.HasVaryValue(w.Header(), "Accept-Encoding") {
408
+ if !httpguts.HeaderValuesContainsToken(w.Header()["Vary"], "Accept-Encoding") {
409
t.Fatalf("Vary = %q, want Accept-Encoding", w.Header().Values("Vary"))
410
}
411
if got := w.Header().Get("Content-Length"); got != "" {
@@ -440,7 +441,7 @@ func checkNotModifiedVary(t *testing.T, w *httptest.ResponseRecorder, encCase en
441
if got := w.Header().Get("Content-Encoding"); got != "" {
442
t.Fatalf("Content-Encoding = %q, want empty", got)
443
}
443
- if !encode.HasVaryValue(w.Header(), "Accept-Encoding") {
444
+ if !httpguts.HeaderValuesContainsToken(w.Header()["Vary"], "Accept-Encoding") {
445
t.Fatalf("Vary = %q, want Accept-Encoding", w.Header().Values("Vary"))
446
}
447
if got := w.Body.Len(); got != 0 {
@@ -496,7 +497,7 @@ func checkStripsEncodedETagSuffix(t *testing.T, w *httptest.ResponseRecorder, en
497
if got := w.Code; got != http.StatusNotModified {
498
t.Fatalf("status = %d, want %d", got, http.StatusNotModified)
499
}
499
- if !encode.HasVaryValue(w.Header(), "Accept-Encoding") {
500
+ if !httpguts.HeaderValuesContainsToken(w.Header()["Vary"], "Accept-Encoding") {
501
t.Fatalf("Vary = %q, want Accept-Encoding", w.Header().Values("Vary"))
502
}
503
}
@@ -530,7 +531,7 @@ func checkBypassesEncoding(t *testing.T, w *httptest.ResponseRecorder, encCase e
531
if !bytes.Equal(w.Body.Bytes(), want) {
532
t.Fatalf("body len = %d, want len = %d", w.Body.Len(), len(want))
533
}
533
- if encode.HasVaryValue(w.Header(), "Accept-Encoding") {
534
+ if httpguts.HeaderValuesContainsToken(w.Header()["Vary"], "Accept-Encoding") {
535
t.Fatalf("Vary = %q, want no Accept-Encoding", w.Header().Values("Vary"))
536
}
537
}
@@ -545,7 +546,7 @@ func checkIdentityVaries(t *testing.T, w *httptest.ResponseRecorder, encCase enc
546
if !bytes.Equal(w.Body.Bytes(), want) {
547
t.Fatalf("body len = %d, want len = %d", w.Body.Len(), len(want))
548
}
548
- if !encode.HasVaryValue(w.Header(), "Accept-Encoding") {
549
+ if !httpguts.HeaderValuesContainsToken(w.Header()["Vary"], "Accept-Encoding") {
550
t.Fatalf("Vary = %q, want Accept-Encoding", w.Header().Values("Vary"))
551
}
552
}
modules/caddyhttp/encode/export_test.go
deleted
-8
@@ -1,8 +0,0 @@
1
-package encode
2
-
3
-import "net/http"
4
-
5
-// HasVaryValue exposes hasVaryValue for external tests in encode_test.
6
-func HasVaryValue(hdr http.Header, target string) bool {
7
- return hasVaryValue(hdr, target)
8
-}