Commit 905eb707 authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1753 from spacewander/weak_etag_after_gzip

gzip: change ETag to weak etag after gzip
parents ba113221 e2544597
...@@ -105,6 +105,10 @@ func (w *gzipResponseWriter) WriteHeader(code int) { ...@@ -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
} }
......
...@@ -38,6 +38,14 @@ func TestGzipHandler(t *testing.T) { ...@@ -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 { ...@@ -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)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment