Skip to content

[bug] Compression not working as expected #450

Closed
@sk-

Description

Calling enable_compression on a response does not work as expected at all. The response misses data or the whole handler hangs.

The app below reproduces the problem:

import asyncio

import aiohttp.web


def run_app(app, host='0.0.0.0', port=8080):
    """Run an app locally."""
    loop = asyncio.get_event_loop()
    handler = app.make_handler()
    f = loop.create_server(handler, host, port)
    srv = loop.run_until_complete(f)

    if host == '0.0.0.0':
        host = '127.0.0.1'
    print(' * Running on http://{host}:{port}/ (Press CTRL+C to quit)'.format(
        host=host, port=port))

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(handler.finish_connections(1.0))
        srv.close()
        loop.run_until_complete(srv.wait_closed())
        loop.run_until_complete(app.finish())
    loop.close()


@asyncio.coroutine
def bug_response_small(unused_request):
    r = aiohttp.web.Response(body=b'foo')
    r.enable_compression()
    return r

@asyncio.coroutine
def bug_response_large(unused_request):
    r = aiohttp.web.Response(body=b'foo' * 100)
    r.enable_compression()
    return r

if __name__ == '__main__':
    app = aiohttp.web.Application()
    app.router.add_route('GET', '/response/small', bug_response_small)
    app.router.add_route('GET', '/response/large', bug_response_large)
    run_app(app)

Run the app and then execute the following:

~$ curl localhost:8080/response/small -H "Accept-Encoding: none" --compressed
foo

~$ curl localhost:8080/response/small -H "Accept-Encoding: gzip" --compressed

<NO RESPONSE>
~$ curl localhost:8080/response/small -H "Accept-Encoding: deflate" --compressed
fo
<MISSING 1 CHARACTER>

~$ curl localhost:8080/response/large -H "Accept-Encoding: none" --compressed
foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo

~$ curl localhost:8080/response/large -H "Accept-Encoding: gzip" --compressed
^C
<APP HANGS>

~$ curl localhost:8080/response/large -H "Accept-Encoding: deflate" --compressed
^C
<APP HANGS>

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions