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 file upload not chunked #327

Merged
merged 3 commits into from
Apr 15, 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
9 changes: 8 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import weakref
import warnings
import chardet
from os import fstat

import aiohttp
from . import hdrs, helpers, streams
Expand Down Expand Up @@ -383,7 +384,13 @@ def update_body_from_data(self, data):
assert not isinstance(data, io.StringIO), \
'attempt to send text data instead of binary'
self.body = data
self.chunked = True
if not self.chunked and isinstance(data, io.BufferedReader):
# Not chunking if content-length can be determined
size = fstat(data.fileno()).st_size - data.tell()
Copy link
Member

Choose a reason for hiding this comment

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

I would use full os.fstat here since it doesn't simply to figure out what fstat is and where it comes from. Also look on the other imports from stdlib.

self.headers[hdrs.CONTENT_LENGTH] = str(size)
self.chunked = False
else:
self.chunked = True
if hasattr(data, 'mode'):
if data.mode == 'r':
raise ValueError('file {!r} should be open in binary mode'
Expand Down
34 changes: 34 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
import unittest.mock
import urllib.parse
import os.path

import aiohttp
from aiohttp.client import ClientRequest, ClientResponse
Expand Down Expand Up @@ -542,6 +543,39 @@ def test_chunked_length(self):
self.assertEqual(req.headers['TRANSFER-ENCODING'], 'chunked')
self.assertNotIn('CONTENT-LENGTH', req.headers)

def test_file_upload_not_chunked(self):
here = os.path.dirname(__file__)
fname = os.path.join(here, 'sample.key')
with open(fname, 'rb') as f:
req = ClientRequest(
'post', 'http://python.org/',
data=f)
self.assertFalse(req.chunked)
self.assertEqual(req.headers['CONTENT-LENGTH'],
str(os.path.getsize(fname)))

def test_file_upload_not_chunked_seek(self):
here = os.path.dirname(__file__)
fname = os.path.join(here, 'sample.key')
with open(fname, 'rb') as f:
f.seek(100)
req = ClientRequest(
'post', 'http://python.org/',
data=f)
self.assertEqual(req.headers['CONTENT-LENGTH'],
str(os.path.getsize(fname) - 100))

def test_file_upload_force_chunked(self):
here = os.path.dirname(__file__)
fname = os.path.join(here, 'sample.key')
with open(fname, 'rb') as f:
req = ClientRequest(
'post', 'http://python.org/',
data=f,
chunked=True)
self.assertTrue(req.chunked)
self.assertNotIn('CONTENT-LENGTH', req.headers)

def test_expect100(self):
req = ClientRequest('get', 'http://python.org/',
expect100=True, loop=self.loop)
Expand Down