Skip to content

Commit

Permalink
Fix bug in FileResponse when compression wouldn't work (#2944)
Browse files Browse the repository at this point in the history
Closes #2942
  • Loading branch information
roganov authored and asvetlov committed Apr 17, 2018
1 parent cb17696 commit 26802e0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/2942.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix compression of FileResponse
5 changes: 3 additions & 2 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import os
import pathlib
import zlib

import pytest

Expand Down Expand Up @@ -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()

0 comments on commit 26802e0

Please sign in to comment.