Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Content-Length deletion for reverse proxies in Martini #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Romanowiczmarek
Copy link

It turns out that deleting Content-Length header field at the end of
serveGzip function may not be enough for gzip. According to the spec
of compress/gzip, a Write() call may flush the writer before closing
it. This means that in certain cases, gzip writer flushes compressed
data to the underlying martini.ResponseWriter with a wrong header
Content-Length field, i.e before it is deleted by serveGzip resulting
in a "Conn.Write wrote more than the declared Content-Length" error.
This change adds a defensive measure of deleting Content-Length
before a potential write.

It turns out that deleting Content-Length header field at the end of
serveGzip function may not be enough for gzip. According to the spec
of compress/gzip, a Write() call may flush the writer before closing
it. This means that in certain cases, gzip writer flushes compressed
data to the underlying martini.ResponseWriter with a wrong header
Content-Length field, i.e before it is deleted by serveGzip resulting
in a "Conn.Write wrote more than the declared Content-Length" error.
This change adds a defensive measure of deleting Content-Length
before a potential write.
@@ -88,7 +88,9 @@ func (grw gzipResponseWriter) Write(p []byte) (int, error) {
if len(grw.Header().Get(HeaderContentType)) == 0 {
grw.Header().Set(HeaderContentType, http.DetectContentType(p))
}

// removing content length before writing in case gzip.Writer writes to the
// underlying http.ResponseWriter before closing. See notes in compress/gzip.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the link to compress/gzip.

Extracted comment:
Write writes a compressed form of p to the underlying io.Writer. The compressed bytes are not necessarily flushed until the Writer is closed.

In some cases, WriteHeader() can be called explicitly by the user
which may result in the Content-Length field of a header to be
written to the underlying ResponseWriter. This CL adds one more
defensive removal of Content-Length for the reverse proxy issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant