From 19c847d458e60908d43dba4b8c60ab068c7dc6f5 Mon Sep 17 00:00:00 2001 From: Jakob Keller <57402305+jakob-keller@users.noreply.github.com> Date: Sun, 17 Nov 2024 01:31:50 +0100 Subject: [PATCH] Support gzip compression in `ClientSession._ws_connect()` if specified by server --- CHANGES/9933.feature.rst | 1 + aiohttp/client.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 CHANGES/9933.feature.rst diff --git a/CHANGES/9933.feature.rst b/CHANGES/9933.feature.rst new file mode 100644 index 00000000000..47184438471 --- /dev/null +++ b/CHANGES/9933.feature.rst @@ -0,0 +1 @@ +Provided experimental support for communicating with WebSocket servers that require the `permessage-gzip` extension -- by :user:`jakob-keller`. diff --git a/aiohttp/client.py b/aiohttp/client.py index 70ec795164c..47a8cdd703e 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -9,6 +9,7 @@ import sys import traceback import warnings +import zlib from contextlib import suppress from types import TracebackType from typing import ( @@ -1005,7 +1006,9 @@ async def _ws_connect( compress_hdrs = resp.headers.get(hdrs.SEC_WEBSOCKET_EXTENSIONS) if compress_hdrs: try: - compress, notakeover = ws_ext_parse(compress_hdrs) + compress, notakeover = ws_ext_parse( + compress_hdrs, detect_gzip=True + ) except WSHandshakeError as exc: raise WSServerHandshakeError( resp.request_info, @@ -1036,7 +1039,12 @@ async def _ws_connect( transport = conn.transport assert transport is not None reader = WebSocketDataQueue(conn_proto, 2**16, loop=self._loop) - conn_proto.set_parser(WebSocketReader(reader, max_msg_size), reader) + conn_proto.set_parser( + WebSocketReader( + reader, max_msg_size, compress=compress <= zlib.MAX_WBITS or "gzip" + ), + reader, + ) writer = WebSocketWriter( conn_proto, transport,