Skip to content

Commit a7e240a

Browse files
[PR #8320/9ba9a4e5 backport][3.9] Fix Python parser to mark responses without length as closing (#8321)
**This is a backport of PR #8320 as merged into master (9ba9a4e).** Co-authored-by: Sam Bull <[email protected]>
1 parent 2833552 commit a7e240a

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

CHANGES/8320.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the pure python parser to mark a connection as closing when a response has no length -- by :user:`Dreamsorcerer`.

aiohttp/http_parser.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,16 @@ def parse_message(self, lines: List[bytes]) -> RawResponseMessage:
718718
) = self.parse_headers(lines)
719719

720720
if close is None:
721-
close = version_o <= HttpVersion10
721+
if version_o <= HttpVersion10:
722+
close = True
723+
# https://www.rfc-editor.org/rfc/rfc9112.html#name-message-body-length
724+
elif 100 <= status_i < 200 or status_i in {204, 304}:
725+
close = False
726+
elif hdrs.CONTENT_LENGTH in headers or hdrs.TRANSFER_ENCODING in headers:
727+
close = False
728+
else:
729+
# https://www.rfc-editor.org/rfc/rfc9112.html#section-6.3-2.8
730+
close = True
722731

723732
return RawResponseMessage(
724733
version_o,

tests/test_http_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def test_max_header_value_size_continuation_under_limit(response) -> None:
735735
assert msg.version == (1, 1)
736736
assert msg.headers == CIMultiDict({"data": "test " + value.decode()})
737737
assert msg.raw_headers == ((b"data", b"test " + value),)
738-
# assert not msg.should_close # TODO: https://github.com/nodejs/llhttp/issues/354
738+
assert msg.should_close
739739
assert msg.compression is None
740740
assert not msg.upgrade
741741
assert not msg.chunked

0 commit comments

Comments
 (0)