Skip to content

Commit

Permalink
[PR #8320/9ba9a4e5 backport][3.9] Fix Python parser to mark responses…
Browse files Browse the repository at this point in the history
… without length as closing (#8321)

**This is a backport of PR #8320 as merged into master
(9ba9a4e).**

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
patchback[bot] and Dreamsorcerer authored Apr 11, 2024
1 parent 2833552 commit a7e240a
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
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 @@ -718,7 +718,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 @@ -735,7 +735,7 @@ def test_max_header_value_size_continuation_under_limit(response) -> 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 a7e240a

Please # to comment.