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 bug in FileResponse when compression wouldn't work #2944

Merged
merged 1 commit into from
Apr 17, 2018
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this so that type is simplified to Optional[ContentCoding], not Union[None, bool, ContentCoding]

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()