diff options
author | darkweak <[email protected]> | 2022-12-20 21:26:53 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2022-12-20 13:26:53 -0700 |
commit | dac7cacd4df35e96a4d8c7d09b925bb432509884 (patch) | |
tree | 8854735df53b6d0a00050b95cf1ad6fadcbf79ed /modules/caddyhttp/encode/encode.go | |
parent | af93517c2d0e6ad11f8803b59768f0f9d0e11fe1 (diff) | |
download | caddy-dac7cacd4df35e96a4d8c7d09b925bb432509884.tar.gz caddy-dac7cacd4df35e96a4d8c7d09b925bb432509884.zip |
encode: Respect Cache-Control no-transform (#5257)
* encode: respect Cache-Control HTTP header no-transform
* encode: switch to strings.Contains
Diffstat (limited to 'modules/caddyhttp/encode/encode.go')
-rw-r--r-- | modules/caddyhttp/encode/encode.go | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go index 91e7060a6..e3a6267de 100644 --- a/modules/caddyhttp/encode/encode.go +++ b/modules/caddyhttp/encode/encode.go @@ -117,14 +117,20 @@ 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 { - for _, encName := range AcceptedEncodings(r, enc.Prefer) { - if _, ok := enc.writerPools[encName]; !ok { - continue // encoding not offered + 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) + defer w.(*responseWriter).Close() + break } - w = enc.openResponseWriter(encName, w) - defer w.(*responseWriter).Close() - break } return next.ServeHTTP(w, r) } @@ -281,7 +287,7 @@ func (rw *responseWriter) Close() error { // init should be called before we write a response, if rw.buf has contents. func (rw *responseWriter) init() { - if rw.Header().Get("Content-Encoding") == "" && + if rw.Header().Get("Content-Encoding") == "" && isEncodeAllowed(rw.Header()) && rw.config.Match(rw) { rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder) |