daaku / go.httpgzip

Package httpgzip provides a http handler wrapper to transparently add gzip compression.
https://godoc.org/github.com/daaku/go.httpgzip
62 stars 6 forks source link

httpgzip.NewHandler breaks when used with httputil.NewSingleHostReverseProxy #6

Open nwidger opened 9 years ago

nwidger commented 9 years ago

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