Skip to content

Commit

Permalink
[PR #8252/8f237126 backport][3.9] Fix handling of unsupported upgrade…
Browse files Browse the repository at this point in the history
…s with the pure python http parser (#8254)

Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
patchback[bot] and bdraco authored Mar 28, 2024
1 parent a459459 commit 6e8f63c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES/8252.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed content not being read when an upgrade request was not supported with the pure Python implementation.
-- by :user:`bdraco`.
14 changes: 10 additions & 4 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ def parse_headers(
return (CIMultiDictProxy(headers), tuple(raw_headers))


def _is_supported_upgrade(headers: CIMultiDictProxy[str]) -> bool:
"""Check if the upgrade header is supported."""
return headers.get(hdrs.UPGRADE, "").lower() in {"tcp", "websocket"}


class HttpParser(abc.ABC, Generic[_MsgT]):
lax: ClassVar[bool] = False

Expand Down Expand Up @@ -354,7 +359,9 @@ def get_content_length() -> Optional[int]:
if SEC_WEBSOCKET_KEY1 in msg.headers:
raise InvalidHeader(SEC_WEBSOCKET_KEY1)

self._upgraded = msg.upgrade
self._upgraded = msg.upgrade and _is_supported_upgrade(
msg.headers
)

method = getattr(msg, "method", self.method)
# code is only present on responses
Expand All @@ -366,9 +373,8 @@ def get_content_length() -> Optional[int]:
method and method_must_be_empty_body(method)
)
if not empty_body and (
(length is not None and length > 0)
or msg.chunked
and not msg.upgrade
((length is not None and length > 0) or msg.chunked)
and not self._upgraded
):
payload = StreamReader(
self.protocol,
Expand Down

0 comments on commit 6e8f63c

Please sign in to comment.