From 0d1987bb536eb7921cab0241b9279e28050f2cb8 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Thu, 11 Apr 2024 16:16:36 +0100 Subject: [PATCH 1/4] Fix Python parser to mark responses without length as closing --- aiohttp/http_parser.py | 11 ++++++++++- tests/test_http_parser.py | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index c568c7574f4..e4e8b88dafe 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -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 headers.keys() & {hdrs.CONTENT_LENGTH, hdrs.TRANSFER_ENCODING}: + close = False + else: + # https://www.rfc-editor.org/rfc/rfc9112.html#section-6.3-2.8 + close = True return RawResponseMessage( version_o, diff --git a/tests/test_http_parser.py b/tests/test_http_parser.py index fcfee480cf8..9688663a307 100644 --- a/tests/test_http_parser.py +++ b/tests/test_http_parser.py @@ -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 From 3cf56ccd3e4de103717ac9e3773dde138cc5cef5 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Thu, 11 Apr 2024 16:19:34 +0100 Subject: [PATCH 2/4] Create 8320.bugfix.rest --- CHANGES/8320.bugfix.rest | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/8320.bugfix.rest diff --git a/CHANGES/8320.bugfix.rest b/CHANGES/8320.bugfix.rest new file mode 100644 index 00000000000..027074f743b --- /dev/null +++ b/CHANGES/8320.bugfix.rest @@ -0,0 +1 @@ +Fixed the pure python parser to mark a connection as closing when a response has no length -- by :user:`Dreamsorcerer`. From 71512d37f972b239fd361653578aa88ed9ab49af Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Thu, 11 Apr 2024 16:21:01 +0100 Subject: [PATCH 3/4] Rename 8320.bugfix.rest to 8320.bugfix.rst --- CHANGES/{8320.bugfix.rest => 8320.bugfix.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CHANGES/{8320.bugfix.rest => 8320.bugfix.rst} (100%) diff --git a/CHANGES/8320.bugfix.rest b/CHANGES/8320.bugfix.rst similarity index 100% rename from CHANGES/8320.bugfix.rest rename to CHANGES/8320.bugfix.rst From 1ea53e2440dcaf8514f2656d291e6215e2db6447 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Thu, 11 Apr 2024 16:50:20 +0100 Subject: [PATCH 4/4] Update aiohttp/http_parser.py --- aiohttp/http_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index e4e8b88dafe..aaa6fe654ed 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -711,7 +711,7 @@ def parse_message(self, lines: List[bytes]) -> RawResponseMessage: # 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 headers.keys() & {hdrs.CONTENT_LENGTH, hdrs.TRANSFER_ENCODING}: + 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