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

Enable passing pre-compressed data in requests (attempt 2) #621

Merged
merged 1 commit into from
Nov 4, 2015
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
6 changes: 4 additions & 2 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ def update_content_encoding(self):
"""Set request content encoding."""
enc = self.headers.get(hdrs.CONTENT_ENCODING, '').lower()
if enc:
self.compress = enc
self.chunked = True # enable chunked, no need to deal with length
if self.compress is not False:
self.compress = enc
# enable chunked, no need to deal with length
self.chunked = True
elif self.compress:
if not isinstance(self.compress, str):
self.compress = 'deflate'
Expand Down
17 changes: 17 additions & 0 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ requests together (aka HTTP pipelining)::
data=r.content)


Uploading pre-compressed data
-----------------------------

To upload data that is already compressed before passing it to aiohttp, call
the request function with ``compress=False`` and set the used compression
algorithm name (usually deflate or zlib) as the value of the
``Content-Encoding`` header::

@asyncio.coroutine
def my_coroutine( my_data):
data = zlib.compress(my_data)
headers = {'Content-Encoding': 'deflate'}
yield from aiohttp.post(
'http://httpbin.org/post', data=data, headers=headers,
compress=False)


.. _aiohttp-client-session:

Keep-Alive, connection pooling and cookie sharing
Expand Down
3 changes: 3 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ certification chaining.

:param bool compress: Set to ``True`` if request has to be compressed
with deflate encoding.
``False`` instructs aiohttp to not compress data
even if the Content-Encoding header is set. Use it
when sending pre-compressed data.
``None`` by default (optional).

:param int chunked: Set to chunk size for chunked transfer encoding.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io
import urllib.parse
import os.path
import zlib

from http.cookies import SimpleCookie

Expand Down Expand Up @@ -616,6 +617,20 @@ def test_file_upload_not_chunked(self):
str(os.path.getsize(fname)))
self.loop.run_until_complete(req.close())

def test_precompressed_data_stays_intact(self):
data = zlib.compress(b'foobar')
req = ClientRequest(
'post', 'http://python.org/',
data=data,
headers={'CONTENT-ENCODING': 'deflate'},
compress=False,
loop=self.loop)
self.assertFalse(req.compress)
self.assertFalse(req.chunked)
self.assertEqual(req.headers['CONTENT-ENCODING'],
'deflate')
self.loop.run_until_complete(req.close())

def test_file_upload_not_chunked_seek(self):
here = os.path.dirname(__file__)
fname = os.path.join(here, 'sample.key')
Expand Down