Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python parser to mark responses without length as closing #8320

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/8320.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the pure python parser to mark a connection as closing when a response has no length -- by :user:`Dreamsorcerer`.
11 changes: 10 additions & 1 deletion aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,16 @@ def parse_message(self, lines: List[bytes]) -> RawResponseMessage:
) = self.parse_headers(lines)

if close is None:
close = version_o <= HttpVersion10
if version_o <= HttpVersion10:
close = True
# https://www.rfc-editor.org/rfc/rfc9112.html#name-message-body-length
elif 100 <= status_i < 200 or status_i in {204, 304}:
close = False
elif hdrs.CONTENT_LENGTH in headers or hdrs.TRANSFER_ENCODING in headers:
close = False
else:
# https://www.rfc-editor.org/rfc/rfc9112.html#section-6.3-2.8
close = True

return RawResponseMessage(
version_o,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def test_max_header_value_size_continuation_under_limit(response: Any) -> None:
assert msg.version == (1, 1)
assert msg.headers == CIMultiDict({"data": "test " + value.decode()})
assert msg.raw_headers == ((b"data", b"test " + value),)
# assert not msg.should_close # TODO: https://github.com/nodejs/llhttp/issues/354
assert msg.should_close
assert msg.compression is None
assert not msg.upgrade
assert not msg.chunked
Expand Down