Set CONTENT_LENGTH for chunked requests #2287
Merged
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.
Description
Fixes #1839.
Chunked requests don't contain a Content-Length header, but Puma buffers the entire request body upfront, which means it can determine the length before dispatching to the application.
The Rack spec doesn't mandate the presence of the CONTENT_LENGTH header, but it does refer to it as a "CGI key" and draws a distinction between it and the HTTP Content-Length header:
https://github.com/rack/rack/blob/v2.2.2/SPEC.rdoc
RFC 3875, which defines the CGI protocol including CONTENT_LENGTH, says:
https://tools.ietf.org/html/rfc3875#section-4.1.2
"Removing a transfer-coding" is precisely what Puma is doing when it parses a chunked request.
RFC 7230, the most recent specification of HTTP 1.1, includes a pseudo-code algorithm for decoding chunked requests that roughly matches the behaviour implemented here:
https://tools.ietf.org/html/rfc7230#section-4.1.3
I say "roughly" because we don't update the Transfer-Encoding header or parse trailers.
Your checklist for this pull request
[changelog skip]
the pull request title.[ci skip]
to the title of the PR.#issue
" to the PR description or my commit messages.