mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2026-05-20 10:16:28 +03:00
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:
@@ -283,10 +283,10 @@ func maybeGzipResponseWriter(w http.ResponseWriter, r *http.Request) http.Respon
|
||||
ae = strings.ToLower(ae)
|
||||
n := strings.Index(ae, "gzip")
|
||||
if n < 0 {
|
||||
// Do not apply gzip encoding to the response.
|
||||
return w
|
||||
}
|
||||
h := w.Header()
|
||||
h.Set("Content-Encoding", "gzip")
|
||||
// Apply gzip encoding to the response.
|
||||
zw := getGzipWriter(w)
|
||||
bw := getBufioWriter(zw)
|
||||
zrw := &gzipResponseWriter{
|
||||
@@ -301,7 +301,14 @@ func maybeGzipResponseWriter(w http.ResponseWriter, r *http.Request) http.Respon
|
||||
//
|
||||
// The function must be called before the first w.Write* call.
|
||||
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
|
||||
@@ -343,14 +350,16 @@ type gzipResponseWriter struct {
|
||||
func (zrw *gzipResponseWriter) Write(p []byte) (int, error) {
|
||||
if !zrw.firstWriteDone {
|
||||
h := zrw.Header()
|
||||
if h.Get("Content-Encoding") != "gzip" {
|
||||
// The request handler disabled gzip encoding.
|
||||
// Send uncompressed response body.
|
||||
if h.Get("Content-Encoding") != "" {
|
||||
zrw.disableCompression = true
|
||||
} else 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")
|
||||
}
|
||||
if !zrw.disableCompression {
|
||||
h.Set("Content-Encoding", "gzip")
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user