Skip to content

Don't send Content-Length alongside Transfer-Encoding in requests - #219

Open
qianeric-backup wants to merge 1 commit into
python-hyper:masterfrom
qianeric-backup:request-framing-headers
Open

qianeric-backup wants to merge 1 commit into
python-hyper:masterfrom
qianeric-backup:request-framing-headers

Conversation

@qianeric-backup

Copy link
Copy Markdown

A request that carries both Transfer-Encoding and Content-Length goes out with both headers, even though h11 frames it as chunked:

>>> c = Connection(CLIENT)
>>> c.send(Request(method="POST", target="/", headers=[
...     ("Host", "example.com"),
...     ("Content-Length", "5"),
...     ("Transfer-Encoding", "chunked")]))
b'POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 5\r\nTransfer-Encoding: chunked\r\n\r\n'

_body_framing() lets Transfer-Encoding win, so the Content-Length: 5 on 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, and test_automatic_transfer_encoding_in_response asserts that the same header pair produces a response with only Transfer-Encoding left. Requests never had the equivalent.

Change

send_with_data_passthrough() gains an elif next to the existing Response branch, and a private _clean_up_request_headers_for_sending() drops Content-Length when Transfer-Encoding is 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-Encoding win, as the framing algorithm requires, so gh-90 is untouched.

Tests

test_request_framing_headers covers four header sets: neither framing header, Content-Length only, Transfer-Encoding only, and both.

tree suite
master 78 passed
master + the test, _connection.py untouched 1 failed, 78 passed
with this patch 79 passed

Comparing the sorted failure lists of master and 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-Encoding combinations 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.py goes 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, and mypy 1.8.0 --strict are clean on both trees. A news fragment is prepared and renders under towncrier 23.11.0; I will rename it to the PR number once this is opened.

_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
qianeric-backup marked this pull request as ready for review November 28, 2026 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant