Skip to content

Commit

Permalink
Fix handling of unsupported upgrades with the pure python http parser (
Browse files Browse the repository at this point in the history
…#8252)

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
bdraco and Dreamsorcerer committed Mar 28, 2024
1 parent aa014a9 commit 8f23712
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 @@ -225,6 +225,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 @@ -347,7 +352,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 @@ -359,9 +366,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 8f23712

Please sign in to comment.