0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Merge pull request #1753 from spacewander/weak_etag_after_gzip

gzip: change ETag to weak etag after gzip
This commit is contained in:
Matt Holt 2017-07-13 22:36:01 -06:00 committed by GitHub
commit 905eb70773
2 changed files with 16 additions and 0 deletions

View file

@ -105,6 +105,10 @@ func (w *gzipResponseWriter) WriteHeader(code int) {
w.Header().Del("Content-Length") w.Header().Del("Content-Length")
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
w.Header().Add("Vary", "Accept-Encoding") w.Header().Add("Vary", "Accept-Encoding")
originalEtag := w.Header().Get("ETag")
if originalEtag != "" && !strings.HasPrefix(originalEtag, "W/") {
w.Header().Set("ETag", "W/"+originalEtag)
}
w.ResponseWriterWrapper.WriteHeader(code) w.ResponseWriterWrapper.WriteHeader(code)
w.statusCodeWritten = true w.statusCodeWritten = true
} }

View file

@ -38,6 +38,14 @@ func TestGzipHandler(t *testing.T) {
t.Error(err) t.Error(err)
} }
r.Header.Set("Accept-Encoding", "gzip") r.Header.Set("Accept-Encoding", "gzip")
w.Header().Set("ETag", `"2n9cd"`)
_, err = gz.ServeHTTP(w, r)
if err != nil {
t.Error(err)
}
// The second pass, test if the ETag is already weak
w.Header().Set("ETag", `W/"2n9cd"`)
_, err = gz.ServeHTTP(w, r) _, err = gz.ServeHTTP(w, r)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -114,6 +122,10 @@ func nextFunc(shouldGzip bool) httpserver.Handler {
if w.Header().Get("Vary") != "Accept-Encoding" { if w.Header().Get("Vary") != "Accept-Encoding" {
return 0, fmt.Errorf("Vary must be Accept-Encoding, found %v", w.Header().Get("Vary")) return 0, fmt.Errorf("Vary must be Accept-Encoding, found %v", w.Header().Get("Vary"))
} }
etag := w.Header().Get("ETag")
if etag != "" && etag != `W/"2n9cd"` {
return 0, fmt.Errorf("ETag must be converted to weak Etag, found %v", w.Header().Get("ETag"))
}
if _, ok := w.(*gzipResponseWriter); !ok { if _, ok := w.(*gzipResponseWriter); !ok {
return 0, fmt.Errorf("ResponseWriter should be gzipResponseWriter, found %T", w) return 0, fmt.Errorf("ResponseWriter should be gzipResponseWriter, found %T", w)
} }