From 26802e06eba5ef21e1f510e4372f307121626025 Mon Sep 17 00:00:00 2001 From: Yegor Roganov Date: Tue, 17 Apr 2018 23:00:47 +0300 Subject: [PATCH] Fix bug in FileResponse when compression wouldn't work (#2944) Closes #2942 --- CHANGES/2942.bugfix | 1 + aiohttp/web_fileresponse.py | 5 +++-- aiohttp/web_response.py | 2 +- tests/test_web_sendfile_functional.py | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 CHANGES/2942.bugfix diff --git a/CHANGES/2942.bugfix b/CHANGES/2942.bugfix new file mode 100644 index 00000000000..cf603f6b90f --- /dev/null +++ b/CHANGES/2942.bugfix @@ -0,0 +1 @@ +Fix compression of FileResponse diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 1d05f7d4977..7475501a5c4 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -107,7 +107,8 @@ async def _sendfile_system(self, request, fobj, count): transport = request.transport if (transport.get_extra_info("sslcontext") or - transport.get_extra_info("socket") is None): + transport.get_extra_info("socket") is None or + self.compression): writer = await self._sendfile_fallback(request, fobj, count) else: writer = SendfileStreamWriter( @@ -131,7 +132,7 @@ async def _sendfile_fallback(self, request, fobj, count): # fobj is transferred in chunks controlled by the # constructor's chunk_size argument. - writer = (await super().prepare(request)) + writer = await super().prepare(request) chunk_size = self._chunk_size diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index fb07712965a..0157f92d022 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -43,7 +43,7 @@ def __init__(self, *, status=200, reason=None, headers=None): self._keep_alive = None self._chunked = False self._compression = False - self._compression_force = False + self._compression_force = None self._cookies = SimpleCookie() self._req = None diff --git a/tests/test_web_sendfile_functional.py b/tests/test_web_sendfile_functional.py index 51c6a9de940..203d44dfdf2 100644 --- a/tests/test_web_sendfile_functional.py +++ b/tests/test_web_sendfile_functional.py @@ -1,6 +1,7 @@ import asyncio import os import pathlib +import zlib import pytest @@ -729,3 +730,25 @@ async def handler(request): resp = await client.get('/', headers={'If-Range': lastmod}) assert 200 == resp.status resp.close() + + +async def test_static_file_compression(aiohttp_client, sender): + filepath = pathlib.Path(__file__).parent / 'data.unknown_mime_type' + + async def handler(request): + ret = sender(filepath) + ret.enable_compression() + return ret + + app = web.Application() + app.router.add_get('/', handler) + client = await aiohttp_client(app, auto_decompress=False) + + resp = await client.get('/') + assert resp.status == 200 + zcomp = zlib.compressobj(wbits=-zlib.MAX_WBITS) + expected_body = zcomp.compress(b'file content\n') + zcomp.flush() + assert expected_body == await resp.read() + assert 'application/octet-stream' == resp.headers['Content-Type'] + assert resp.headers.get('Content-Encoding') == 'deflate' + await resp.release()