Fix raw_post empty bug when when Transfer-Encoding: chunked #47336
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.
Motivation / Background
This PR fixes the issue with
request.raw_post
property being empty for chunked requests. (Transfer-Encoding header present and without a Content-Length value). It was inspired from this previous PR, but without the need of doing a secondrewind
of the request body.A similar fix was done on Puma webserver, but with this fix,
raw_post
will work as expected for any Rack based http server.Detail
This pull request changes
ActionDispatch::Request#raw_post
andActionDispatch::Request#content_length
methods, to take into account the presence ofTransfer-Encoding
header or not.According to HTTP/1.1 RFC, when
Transfer-Encoding
is present, a sender must not send aContent-Length
header field on a request, so because of that we can't rely on that field when reading the raw request body stream and calculating its content length for the upstream app.So what this change does is to read the entire requests's
body_stream
forraw_post
instead of usingcontent_length
as its length whenTransfer-Encoding
header is present.And it uses the
raw_post#bytesize
value to calculate the request'scontent_length
instead of reading this value from theContent-Length
header, whenTransfer-Encoding
header is present.Additional information
To test this case an integration railties test
http_request_test.rb
was used, in order for it to be similar to real use cases.It is also recommended to test this change manually with a real Rails app running on different Rack servers (e.g. Puma, Unicorn, Webrick or Thin)
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]