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)
}
When
httputil.NewSingleHostReverseProxy
is wrapped withhttpgzip.NewHandler
, i.e.the call to
w.Writer.Write
ingzipResponseWriter.Write
returns0, ErrContentLength
("Conn.Write wrote more than the declared Content-Length"). I believe this is becauseReverseProxy.ServeHTTP
callsResponseWriter.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
: