Skip to content

Commit

Permalink
Fix content-length if file partly read before upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hoh committed Apr 14, 2015
1 parent 6c27208 commit cd3fa2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import weakref
import warnings
import chardet
from os.path import getsize
from os import fstat

import aiohttp
from . import hdrs, helpers, streams
Expand Down Expand Up @@ -386,7 +386,8 @@ def update_body_from_data(self, data):
self.body = data
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))
size = fstat(data.fileno()).st_size - data.tell()
self.headers[hdrs.CONTENT_LENGTH] = str(size)
self.chunked = False
else:
self.chunked = True
Expand Down
11 changes: 11 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,17 @@ def test_file_upload_not_chunked(self):
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')
Expand Down

0 comments on commit cd3fa2e

Please sign in to comment.