lib/httpserver: do not recompress already compressed response

This shoud help with vmauth issue - https://github.com/VictoriaMetrics/VictoriaMetrics/issues/514
This commit is contained in:
Aliaksandr Valialkin 2020-05-22 16:44:09 +03:00
parent bb4a2bf1aa
commit be7253c084

View file

@ -232,10 +232,10 @@ func maybeGzipResponseWriter(w http.ResponseWriter, r *http.Request) http.Respon
ae = strings.ToLower(ae) ae = strings.ToLower(ae)
n := strings.Index(ae, "gzip") n := strings.Index(ae, "gzip")
if n < 0 { if n < 0 {
// Do not apply gzip encoding to the response.
return w return w
} }
h := w.Header() // Apply gzip encoding to the response.
h.Set("Content-Encoding", "gzip")
zw := getGzipWriter(w) zw := getGzipWriter(w)
bw := getBufioWriter(zw) bw := getBufioWriter(zw)
zrw := &gzipResponseWriter{ zrw := &gzipResponseWriter{
@ -250,7 +250,14 @@ func maybeGzipResponseWriter(w http.ResponseWriter, r *http.Request) http.Respon
// //
// The function must be called before the first w.Write* call. // The function must be called before the first w.Write* call.
func DisableResponseCompression(w http.ResponseWriter) { func DisableResponseCompression(w http.ResponseWriter) {
w.Header().Del("Content-Encoding") zrw, ok := w.(*gzipResponseWriter)
if !ok {
return
}
if zrw.firstWriteDone {
logger.Panicf("BUG: DisableResponseCompression must be called before sending the response")
}
zrw.disableCompression = true
} }
// EnableCORS enables https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS // EnableCORS enables https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
@ -292,14 +299,16 @@ type gzipResponseWriter struct {
func (zrw *gzipResponseWriter) Write(p []byte) (int, error) { func (zrw *gzipResponseWriter) Write(p []byte) (int, error) {
if !zrw.firstWriteDone { if !zrw.firstWriteDone {
h := zrw.Header() h := zrw.Header()
if h.Get("Content-Encoding") != "gzip" { if h.Get("Content-Encoding") != "" {
// The request handler disabled gzip encoding.
// Send uncompressed response body.
zrw.disableCompression = true zrw.disableCompression = true
} else if h.Get("Content-Type") == "" { }
// Disable auto-detection of content-type, since it if !zrw.disableCompression {
// is incorrectly detected after the compression. h.Set("Content-Encoding", "gzip")
h.Set("Content-Type", "text/html") if h.Get("Content-Type") == "" {
// Disable auto-detection of content-type, since it
// is incorrectly detected after the compression.
h.Set("Content-Type", "text/html")
}
} }
zrw.firstWriteDone = true zrw.firstWriteDone = true
} }