Repository navigation
Don't send Content-Length alongside Transfer-Encoding in requests - #219
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
_body_framing() lets Transfer-Encoding win, but the request path sent both headers when the caller set both, putting a framing header on the wire that contradicts the framing actually used. RFC 7230 3.3.2 forbids that for senders and 3.3.3 requires removing an inbound Content-Length before forwarding, which is the relay case. Responses already drop it in _clean_up_response_headers_for_sending(); requests had no equivalent. Add one, leaving _body_framing() alone so the framing decision and the receive side are unchanged.
qianeric-backup
force-pushed
the
request-framing-headers
branch
from
November 28, 2026 04:06
bf0434a to
9d92e63
Compare
qianeric-backup
marked this pull request as ready for review
November 28, 2026 04:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A request that carries both
Transfer-EncodingandContent-Lengthgoes out with both headers, even though h11 frames it as chunked:_body_framing()letsTransfer-Encodingwin, so theContent-Length: 5on the wire contradicts the framing of the message actually being sent. RFC 7230 section 3.3.2 is unconditional for senders: "A sender MUST NOT send a Content-Length header field in any message that contains a Transfer-Encoding header field." Section 3.3.3 adds the forwarding case, "A sender MUST remove the received Content-Length field prior to forwarding such a message downstream", which is what happens whenever h11 is used to relay a request.Responses already get exactly this treatment.
_clean_up_response_headers_for_sending()has the comment spelling out the rule, andtest_automatic_transfer_encoding_in_responseasserts that the same header pair produces a response with onlyTransfer-Encodingleft. Requests never had the equivalent.Change
send_with_data_passthrough()gains anelifnext to the existingResponsebranch, and a private_clean_up_request_headers_for_sending()dropsContent-LengthwhenTransfer-Encodingis present, mirroring the response path. Nothing else is touched:_body_framing()is unchanged, so the framing decision is identical, and a request with only one of the two headers is passed through as before.This is a sending rule only. On the receiving side h11 still accepts both and lets
Transfer-Encodingwin, as the framing algorithm requires, so gh-90 is untouched.Tests
test_request_framing_headerscovers four header sets: neither framing header,Content-Lengthonly,Transfer-Encodingonly, and both.master78 passedmaster+ the test,_connection.pyuntouched1 failed, 78 passed79 passedComparing the sorted failure lists of
masterand this branch gives nothing; against the red tree it gives exactly the one new test.Equivalence: 2122 records (9 methods × 18 legal header combinations × 3 body shapes, plus trailing-header variants, plus 9 methods × 9 statuses × 7 response header sets on the response side, plus 8 fixed messages through both receive paths) produce byte-identical output. Adding the illegal
Content-Length+Transfer-Encodingcombinations to the matrix gives 2284 records, of which 81 differ; all 81 are in that one class and outside it nothing differs. The body bytes are identical in both cases, so framing is unchanged.Coverage of
_connection.pygoes from 99.0% to 99.1%; the uncovered lines before and after are the same...stubs under@overload. The ten new statements and six new branches are all covered.black 23.3.0,isort 5.12.0, andmypy 1.8.0 --strictare clean on both trees. A news fragment is prepared and renders undertowncrier 23.11.0; I will rename it to the PR number once this is opened.