Skip to content

Not possible to do requests with pre-compressed data #619

@mwfrojdman

Description

@mwfrojdman

Currently aiohttp always compresses the request data if the Content-Encoding header is set. If the data is already compressed, aiohttp will compress it a second time. Example:

url = 'http://some.host/'
deflated_data = zlib.compress(b'mydata')
headers = {'Content-Encoding': 'deflate'}
yield from aiohttp.request('POST', url, data=deflated_data, headers=headers)

Looking at client_reqrep, self.update_content_encoding() is called in ClientRequest.init() on line 88. The method first checks if the Content-Encoding header is set, and if it is, self.compress is set to that value and chunking turned on. Otherwise, self.compress (originally set in init() from the constructor's argument with the same name) is used.

Later in send(), request.add_compression_filter(self.compress) is called on line 460 if self.compress is set. This leads to the double compression.

Suggestion: Only do compression of data in aiohttp if init() is called with the compress argument. If the Content-Encoding header is set, leave it to the user to supply compressed data.

I ran into this user when looking at the raven-python's aiohttp transport (used to send exceptions & al to Sentry at https://github.com/getsentry/raven-aiohttp/blob/c35ca66379f00d58cb8404b51f845f0e754c91b5/raven_aiohttp.py#L41 which gets the data already compressed and Content-Encoding header set.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions