Skip to content

Keep the request method across a trailers block so the HEAD exemption still applies - #1331

Open
feiiiiii5 wants to merge 1 commit into
python-hyper:masterfrom
feiiiiii5:fix/keep-request-method-across-trailers
Open

feiiiiii5 wants to merge 1 commit into
python-hyper:masterfrom
feiiiiii5:fix/keep-request-method-across-trailers

Conversation

@feiiiiii5

Copy link
Copy Markdown

A request that was closed with a trailers block lost the :method it was sent with, so the HEAD exemption in _initialize_content_length stopped applying and a bodyless response was rejected.

send_headers stored extract_method_header(bytes_headers) on every header block it sent. A trailers block carries no :method, so the helper returns None and the remembered b"HEAD" was overwritten. The next 200 with content-length: 1234 and an empty DATA frame carrying END_STREAM then raised:

h2.exceptions.InvalidBodyLengthError: Expected 1234 bytes, received 0

RFC 9113 § 8.1.1 lists 204, 304 and the response to HEAD as messages defined to have no content. Without the exemption the check has nothing to stand on, and one frame of framing noise on the request side decides whether a legitimate response is accepted.

Storing the method only when the block carries one keeps the exemption for the whole request. This is the send_headers half of the two smaller defects reported in #1330; the remotely_pushed half (a pushed stream never records its method at all) is a separate change and is not touched here.

Verification

On bc239af the new test fails with the error above, on 5ac90d3 it passes.

  • pytest tests/ -q → 1664 passed.
  • pytest tests/ -q --cov=h2 → 100.00%, the enforced threshold, with stream.py at 100% statements and branches.
  • ruff check src/ and mypy --strict src/h2/stream.py both clean.

send_headers stored extract_method_header() on every header block, including
trailers, which carry no :method, so the remembered b"HEAD" was replaced by
None and _initialize_content_length stopped applying the HEAD exemption. A
response to a HEAD request that was closed with trailers then raised
InvalidBodyLengthError on a bodyless message that RFC 9113 8.1.1 permits.

Store the method only when the block carries one.
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