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 2 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
8 changes: 7 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.path import getsize

import aiohttp
from . import hdrs, helpers, streams
Expand Down Expand Up @@ -383,7 +384,12 @@ 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
self.headers[hdrs.CONTENT_LENGTH] = str(getsize(data.name))
Copy link
Member

Choose a reason for hiding this comment

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

What if file was read a bit before it's going to be sent with a request?

with open('foo.bar', 'rb') as f:
  magic = f.read(3)
  ...
  yield from aiohttp.request('post', 'http://localhost/upload', data=f)

In this case Content-Length header will declare more bytes than actually could be sent. Use tell() to figure out how much were already read. Also, since you have file description here, you can do just os.fstat(data.fileno()).st_size what is a bit more efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx, I'll have a look at that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed both seeking and more efficient file size access.

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
23 changes: 23 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,28 @@ 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_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