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

Never write to parent ResponseWriter if we were not written to #49

Merged
merged 1 commit into from
Jun 23, 2017

Conversation

EmielM
Copy link
Contributor

@EmielM EmielM commented Jun 21, 2017

When gzip is used as middleware in a parent -> gzip -> inner setup, sometimes the parent handler wants to call ResponseWriter.WriteHeader knowing the inner handler hasn't called it yet.

However, gziphandler currently calls WriteHeader(200) when wrapping up, and because the go stdlib only uses the first call, this breaks things (parent's call becomes void). The same reasoning goes for calling .Write(): gziphandler should not call .Write() when no .Write() was ever called on itself.

I've updated the logic to never call WriteHeader() when .code is still 0 (and removed the one-liner writeHeader indirection while I was at it), and to make the distinction between Write() being called or not by keeping .buf to nil. This way even a .Write([]byte{}) will result in the same call to the parent.

I've added a test TestDontWriteWhenNotWrittenTo that confirms the new behavior for .WriteHeader() at least.

@jprobinson
Copy link
Contributor

Seems to be pretty straightforward. Thanks for contributing!

🐇

@jprobinson jprobinson merged commit 56545f4 into nytimes:master Jun 23, 2017
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.

2 participants