mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-27 23:03:37 -05:00
encode: Respect Cache-Control no-transform (#5257)
* encode: respect Cache-Control HTTP header no-transform * encode: switch to strings.Contains
This commit is contained in:
parent
af93517c2d
commit
dac7cacd4d
2 changed files with 60 additions and 7 deletions
|
@ -117,14 +117,20 @@ func (enc *Encode) Validate() error {
|
||||||
return nil
|
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 {
|
func (enc *Encode) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||||
for _, encName := range AcceptedEncodings(r, enc.Prefer) {
|
if isEncodeAllowed(r.Header) {
|
||||||
if _, ok := enc.writerPools[encName]; !ok {
|
for _, encName := range AcceptedEncodings(r, enc.Prefer) {
|
||||||
continue // encoding not offered
|
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)
|
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.
|
// init should be called before we write a response, if rw.buf has contents.
|
||||||
func (rw *responseWriter) init() {
|
func (rw *responseWriter) init() {
|
||||||
if rw.Header().Get("Content-Encoding") == "" &&
|
if rw.Header().Get("Content-Encoding") == "" && isEncodeAllowed(rw.Header()) &&
|
||||||
rw.config.Match(rw) {
|
rw.config.Match(rw) {
|
||||||
|
|
||||||
rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder)
|
rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder)
|
||||||
|
|
|
@ -261,3 +261,50 @@ 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue