go.httpgzip
go.httpgzip copied to clipboard
httpgzip.NewHandler breaks when used with httputil.NewSingleHostReverseProxy
When httputil.NewSingleHostReverseProxy
is wrapped with httpgzip.NewHandler
, i.e.
http.Handle("/", httpgzip.NewHandler(httputil.NewSingleHostReverseProxy(target)))
the call to w.Writer.Write
in gzipResponseWriter.Write
returns 0, ErrContentLength
("Conn.Write wrote more than the declared Content-Length"). I believe this is because ReverseProxy.ServeHTTP
calls ResponseWriter.WriteHeader
which stores the current (pre-gzip) Content-Length value in the response before it writes the body out.
I was able to fix this problem by adding this method to gzipResponseWriter
:
func (w *gzipResponseWriter) WriteHeader(code int) {
w.Header().Del("Content-Length")
w.ResponseWriter.WriteHeader(code)
}