Skip to content

Commit

Permalink
Merge pull request #807 from KeepSafe/handle_empty_body_with_gzip
Browse files Browse the repository at this point in the history
Fix #758: Handle empty body with gzipped encoding
  • Loading branch information
asvetlov committed Feb 28, 2016
2 parents bcd4a85 + bf22763 commit a565ee6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion aiohttp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ def __call__(self, out, buf):
length = 8

# payload decompression wrapper
if self.compression and self.message.compression:
if (self.response_with_body and
self.compression and self.message.compression):
out = DeflateBuffer(out, self.message.compression)

# payload parser
Expand Down
4 changes: 4 additions & 0 deletions aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ def enable_compression(self, force=None):
# Backwards compatibility for when force was a bool <0.17.
if type(force) == bool:
force = ContentCoding.deflate if force else ContentCoding.identity
elif force is not None:
assert isinstance(force, ContentCoding), ("force should one of "
"None, bool or "
"ContentEncoding")

self._compression = True
self._compression_force = force
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ def post(self, path, **kwargs):
url = self._url + path
return self._session.post(url, **kwargs)

def delete(self, path, **kwargs):
while path.startswith('/'):
path = path[1:]
url = self._url + path
return self._session.delete(url)

def ws_connect(self, path, **kwargs):
while path.startswith('/'):
path = path[1:]
Expand Down
19 changes: 19 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,22 @@ def handler(request):
resp = yield from client.get('/', version=aiohttp.HttpVersion11)
assert resp.status == 200
resp.close()


@pytest.mark.run_loop
def test_204_with_gzipped_content_encoding(create_app_and_client):
@asyncio.coroutine
def handler(request):
resp = web.StreamResponse(status=204)
resp.content_length = 0
resp.content_type = 'application/json'
# resp.enable_compression(web.ContentCoding.gzip)
resp.headers['Content-Encoding'] = 'gzip'
yield from resp.prepare(request)
return resp

app, client = yield from create_app_and_client()
app.router.add_route('DELETE', '/', handler)
resp = yield from client.delete('/')
assert resp.status == 204
yield from resp.release()

0 comments on commit a565ee6

Please sign in to comment.