Skip to content

Commit

Permalink
[PR #7776/0c938122 backport][3.9] Bugfix Edge Case Handling for Respo…
Browse files Browse the repository at this point in the history
…nseParser for missing reason value (#7779)

**This is a backport of PR #7776 as merged into master
(0c93812).**

<!-- Thank you for your contribution! -->

For cases where response stream does not contain a reason, the current
HttpResponseParser throws "Bad Status Line" Error. HTTP/1.1 protocol
does not mandate reason to be present, hence this PR adds logic fix to
handle this edge case.

<!-- Please give a short brief about these changes. -->

## Are there changes in behavior for the user?
No, this is not changing user behavior. Standard "HTTP/1.1 200 OK" and
edge case of "HTTP/1.1 200 " should work after this PR gets merged.

<!-- Outline any notable behaviour for the end users. -->

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this
change? -->
No. This edge case is handled in 3.7 and earlier, but breaks from 3.8
forward versions.

## Checklist

- [x] I think the code is well written
- [x] Unit tests for the changes exist
- [ ] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [ ] Add a new news fragment into the `CHANGES` folder
  * name it `<issue_id>.<type>` for example (588.bugfix)
* if you don't have an `issue_id` change it to the pr id after creating
the pr
  * ensure type is one of the following:
    * `.feature`: Signifying a new feature.
    * `.bugfix`: Signifying a bug fix.
    * `.doc`: Signifying a documentation improvement.
    * `.removal`: Signifying a deprecation or removal of public API.
* `.misc`: A ticket has been closed, but it is not of interest to users.
* Make sure to use full sentences with correct case and punctuation, for
example: "Fix issue with non-ascii contents in doctest text files."

Co-authored-by: jparag <[email protected]>
  • Loading branch information
patchback[bot] and jparag authored Nov 3, 2023
1 parent 379ee7c commit cdfed8b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
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 CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ Olaf Conradi
Pahaz Blinov
Panagiotis Kolokotronis
Pankaj Pandey
Parag Jain
Pau Freixes
Paul Colomiets
Paulius Šileikis
Expand Down
1 change: 1 addition & 0 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,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 @@ -819,6 +819,22 @@ def test_http_response_parser_utf8(response) -> 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, size) -> None:
reason = b"t" * (size - 2)
Expand Down

0 comments on commit cdfed8b

Please sign in to comment.