Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compress] Vary: Accept-Encoding must always be set #175

Merged
merged 2 commits into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func CompressHandlerLevel(h http.Handler, level int) http.Handler {
}
}

// always add Accept-Encoding to Vary to prevent intermediate caches corruption
w.Header().Add("Vary", "Accept-Encoding")

// if we weren't able to identify an encoding we're familiar with, pass on the
// request to the handler and return
if encoding == "" {
Expand All @@ -108,7 +111,6 @@ func CompressHandlerLevel(h http.Handler, level int) http.Handler {

w.Header().Set("Content-Encoding", encoding)
r.Header.Del("Accept-Encoding")
w.Header().Add("Vary", "Accept-Encoding")

hijacker, ok := w.(http.Hijacker)
if !ok { /* w is not Hijacker... oh well... */
Expand Down
3 changes: 3 additions & 0 deletions compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func TestCompressHandlerNoCompression(t *testing.T) {
if l := w.HeaderMap.Get("Content-Length"); l != "9216" {
t.Errorf("wrong content-length. got %q expected %d", l, 1024*9)
}
if v := w.HeaderMap.Get("Vary"); v != "Accept-Encoding" {
t.Errorf("wrong vary. got %s expected %s", v, "Accept-Encoding")
}
}

func TestAcceptEncodingIsDropped(t *testing.T) {
Expand Down