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

Fix handling of unsupported upgrades with the pure python http parser #8252

Merged
merged 9 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdraco it's best not to put a trailing period at the end of the sentence if the byline is right after — RST will render it on the same line. If you want a separate line, add an extra empty separator line in between. Look how it's rendered now: https://docs.aiohttp.org/en/latest/changes.html#bug-fixes.

Also, I suggest not using any period after the byline if it's on a dedicated line as opposed to being at the end of a sentence.

Examples:

-- 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
Loading