Skip to content

Commit

Permalink
Fix Python parser to mark responses without length as closing (#8320)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Apr 11, 2024
1 parent ffbc432 commit 9ba9a4e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/8320.bugfix.rst
@@ -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
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
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

0 comments on commit 9ba9a4e

Please sign in to comment.