go.httpgzip icon indicating copy to clipboard operation
go.httpgzip copied to clipboard

httpgzip.NewHandler breaks when used with httputil.NewSingleHostReverseProxy

Open nwidger opened this issue 9 years ago • 0 comments

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)
}

nwidger avatar Dec 09 '14 18:12 nwidger