Never write to parent ResponseWriter if we were not written to #49
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When gzip is used as middleware in a
parent -> gzip -> inner
setup, sometimes the parent handler wants to callResponseWriter.WriteHeader
knowing the inner handler hasn't called it yet.However,
gziphandler
currently callsWriteHeader(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-linerwriteHeader
indirection while I was at it), and to make the distinction betweenWrite()
being called or not by keeping.buf
tonil
. 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.