Bugfix: fix mishandling of POST/PUT requests with empty body, causing net/http to send request without a Content-Length
header
#194
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.
This PR addresses a mishandling of POST and PUT requests having an empty body.
In a few places within
net/http
, in order to determine whether the request has a body or not,Request.Body
which is anio.ReadCloser
, does not have itsRead()
method called but is instead simply nil checked or compared to thehttp.NoBody
value - so even if itsRead()
method returns0, io.EOF
, it's still considered to represent a nonempty request body.(Note this docstring also suggests that you should set
Request.Body
tonil
orhttp.NoBody
)For HTTP/1.1, even when the request body is an
io.ReadCloser
with a zero length buffer, the resulting request on the wire then contains neither aContent-Length
nor aTransfer-Encoding
header and is therefore invalid (incurring a 411 response from a compliant server).For HTTP/2.0, the
http.http2actualContentLength()
function, if it isn't specificallyhttp.NoBody
, theContent-Length
is incorrectly discarded instead of being set to zero, which has the same effect.