From d738a88bc70de6a8e530c617a3671ec7b8d454c7 Mon Sep 17 00:00:00 2001 From: nebularazer Date: Fri, 24 May 2019 12:27:42 +0200 Subject: [PATCH] removed transfer-encoding header from ws #3798 --- CHANGES/3798.feature | 1 + CONTRIBUTORS.txt | 1 + aiohttp/web_response.py | 14 ++++++++++++-- aiohttp/web_ws.py | 1 - 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 CHANGES/3798.feature diff --git a/CHANGES/3798.feature b/CHANGES/3798.feature new file mode 100644 index 00000000000..175ce49fe05 --- /dev/null +++ b/CHANGES/3798.feature @@ -0,0 +1 @@ +Removed `Transfer-Encoding: chunked` header from websocket responses to be compatible with more http proxy servers. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index f580fa78db4..ee7dc299058 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -90,6 +90,7 @@ Eugene Naydenov Eugene Tolmachev Evert Lammerts FichteFoll +Florian Scheffler Frederik Gladhorn Frederik Peter Aalund Gabriel Tremblay diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index 750e040afbc..d83f87d43c5 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -371,13 +371,22 @@ async def _start(self, request: 'BaseRequest') -> AbstractStreamWriter: if self._compression: await self._start_compression(request) + if all([ + 'websocket' == headers.get(hdrs.UPGRADE, '').lower().strip(), + 'upgrade' == headers.get(hdrs.CONNECTION, '').lower() + ]): + websocket_response = True + else: + websocket_response = False + if self._chunked: if version != HttpVersion11: raise RuntimeError( "Using chunked encoding is forbidden " "for HTTP/{0.major}.{0.minor}".format(request.version)) writer.enable_chunking() - headers[hdrs.TRANSFER_ENCODING] = 'chunked' + if websocket_response is False: + headers[hdrs.TRANSFER_ENCODING] = 'chunked' if hdrs.CONTENT_LENGTH in headers: del headers[hdrs.CONTENT_LENGTH] elif self._length_check: @@ -385,7 +394,8 @@ async def _start(self, request: 'BaseRequest') -> AbstractStreamWriter: if writer.length is None: if version >= HttpVersion11: writer.enable_chunking() - headers[hdrs.TRANSFER_ENCODING] = 'chunked' + if websocket_response is False: + headers[hdrs.TRANSFER_ENCODING] = 'chunked' if hdrs.CONTENT_LENGTH in headers: del headers[hdrs.CONTENT_LENGTH] else: diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index faa05ab3e5a..a0795366ca3 100644 --- a/aiohttp/web_ws.py +++ b/aiohttp/web_ws.py @@ -179,7 +179,6 @@ def _handshake(self, request: BaseRequest) -> Tuple['CIMultiDict[str]', response_headers = CIMultiDict( # type: ignore {hdrs.UPGRADE: 'websocket', hdrs.CONNECTION: 'upgrade', - hdrs.TRANSFER_ENCODING: 'chunked', hdrs.SEC_WEBSOCKET_ACCEPT: accept_val}) notakeover = False