From 5820e11dadd2b227ba907282141122a84ab25bc1 Mon Sep 17 00:00:00 2001 From: a Date: Tue, 10 Mar 2026 14:24:57 -0500 Subject: [PATCH 1/3] remove check --- modules/caddyhttp/encode/encode.go | 45 +++++++++++++----------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go index ac995c37b32..ef50a4cf798 100644 --- a/modules/caddyhttp/encode/encode.go +++ b/modules/caddyhttp/encode/encode.go @@ -147,35 +147,28 @@ func (enc *Encode) Validate() error { return nil } -func isEncodeAllowed(h http.Header) bool { - return !strings.Contains(h.Get("Cache-Control"), "no-transform") -} - func (enc *Encode) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { - if isEncodeAllowed(r.Header) { - for _, encName := range AcceptedEncodings(r, enc.Prefer) { - if _, ok := enc.writerPools[encName]; !ok { - continue // encoding not offered - } - w = enc.openResponseWriter(encName, w, r.Method == http.MethodConnect) - defer w.(*responseWriter).Close() - - // to comply with RFC 9110 section 8.8.3(.3), we modify the Etag when encoding - // by appending a hyphen and the encoder name; the problem is, the client will - // send back that Etag in a If-None-Match header, but upstream handlers that set - // the Etag in the first place don't know that we appended to their Etag! so here - // we have to strip our addition so the upstream handlers can still honor client - // caches without knowing about our changes... - if etag := r.Header.Get("If-None-Match"); etag != "" && !strings.HasPrefix(etag, "W/") { - ourSuffix := "-" + encName + `"` - if before, ok := strings.CutSuffix(etag, ourSuffix); ok { - etag = before + `"` - r.Header.Set("If-None-Match", etag) - } + for _, encName := range AcceptedEncodings(r, enc.Prefer) { + if _, ok := enc.writerPools[encName]; !ok { + continue // encoding not offered + } + w = enc.openResponseWriter(encName, w, r.Method == http.MethodConnect) + defer w.(*responseWriter).Close() + + // to comply with RFC 9110 section 8.8.3(.3), we modify the Etag when encoding + // by appending a hyphen and the encoder name; the problem is, the client will + // send back that Etag in a If-None-Match header, but upstream handlers that set + // the Etag in the first place don't know that we appended to their Etag! so here + // we have to strip our addition so the upstream handlers can still honor client + // caches without knowing about our changes... + if etag := r.Header.Get("If-None-Match"); etag != "" && !strings.HasPrefix(etag, "W/") { + ourSuffix := "-" + encName + `"` + if before, ok := strings.CutSuffix(etag, ourSuffix); ok { + etag = before + `"` + r.Header.Set("If-None-Match", etag) } - - break } + break } err := next.ServeHTTP(w, r) From c3dff47ce71fb9652cacfc8b88da24b12cc68521 Mon Sep 17 00:00:00 2001 From: a Date: Tue, 10 Mar 2026 14:56:20 -0500 Subject: [PATCH 2/3] remove second use --- modules/caddyhttp/encode/encode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go index ef50a4cf798..1803585212f 100644 --- a/modules/caddyhttp/encode/encode.go +++ b/modules/caddyhttp/encode/encode.go @@ -437,7 +437,7 @@ func (rw *responseWriter) init() { hdr := rw.Header() - if hdr.Get("Content-Encoding") == "" && isEncodeAllowed(hdr) && + if hdr.Get("Content-Encoding") == "" && rw.config.Match(rw) { rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder) rw.w.Reset(rw.ResponseWriter) From 761070cdd0e568e0c56c41788563a727a863afdd Mon Sep 17 00:00:00 2001 From: a Date: Tue, 10 Mar 2026 15:27:40 -0500 Subject: [PATCH 3/3] remove the no longer relevant test --- modules/caddyhttp/encode/encode_test.go | 47 ------------------------- 1 file changed, 47 deletions(-) diff --git a/modules/caddyhttp/encode/encode_test.go b/modules/caddyhttp/encode/encode_test.go index 818f7674507..adc01514935 100644 --- a/modules/caddyhttp/encode/encode_test.go +++ b/modules/caddyhttp/encode/encode_test.go @@ -248,50 +248,3 @@ func TestValidate(t *testing.T) { }) } } - -func TestIsEncodeAllowed(t *testing.T) { - testCases := []struct { - name string - headers http.Header - expected bool - }{ - { - name: "Without any headers", - headers: http.Header{}, - expected: true, - }, - { - name: "Without Cache-Control HTTP header", - headers: http.Header{ - "Accept-Encoding": {"gzip"}, - }, - expected: true, - }, - { - name: "Cache-Control HTTP header ending with no-transform directive", - headers: http.Header{ - "Accept-Encoding": {"gzip"}, - "Cache-Control": {"no-cache; no-transform"}, - }, - expected: false, - }, - { - name: "With Cache-Control HTTP header no-transform as Cache-Extension value", - headers: http.Header{ - "Accept-Encoding": {"gzip"}, - "Cache-Control": {`no-store; no-cache; community="no-transform"`}, - }, - expected: false, - }, - } - - for _, test := range testCases { - t.Run(test.name, func(t *testing.T) { - if result := isEncodeAllowed(test.headers); result != test.expected { - t.Errorf("The headers given to the isEncodeAllowed should return %t, %t given.", - result, - test.expected) - } - }) - } -}