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

Bugfix Edge Case Handling for ResponseParser for missing reason value #7776

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/7776.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Edge Case Handling for ResponseParser for missing reason value
1 change: 1 addition & 0 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ def parse_message(self, lines: List[bytes]) -> RawResponseMessage:
try:
status, reason = status.split(maxsplit=1)
except ValueError:
status = status.strip()
reason = ""

if len(reason) > self.max_line_size:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,22 @@ def test_http_response_parser_utf8(response: Any) -> None:
assert not tail


def test_http_response_parser_utf8_without_reason(response: Any) -> None:
text = "HTTP/1.1 200 \r\nx-test:тест\r\n\r\n".encode()

messages, upgraded, tail = response.feed_data(text)
assert len(messages) == 1
msg = messages[0][0]

assert msg.version == (1, 1)
assert msg.code == 200
assert msg.reason == ""
assert msg.headers == CIMultiDict([("X-TEST", "тест")])
assert msg.raw_headers == ((b"x-test", "тест".encode()),)
assert not upgraded
assert not tail


@pytest.mark.parametrize("size", [40962, 8191])
def test_http_response_parser_bad_status_line_too_long(
response: Any, size: Any
Expand Down