Skip to content

Commit 2b85ad7

Browse files
committed
Merge branch 'z2v-bug/wrong-read-bytes-count' into 0.17
2 parents 2cfe5f3 + ab4bf2e commit 2b85ad7

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGES
66

77
- Don't forget to pass `data` argument forward #462
88

9+
- Fix multipart read bytes count #463
10+
911
0.17.1 (08-10-2015)
1012
---------------------
1113

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ Tolga Tezel
6565
Vaibhav Sagar
6666
Vitaly Haritonsky
6767
Vitaly Magerya
68+
Vladimir Zakharov
6869
W. Trevor King
6970
Yannick Koechlin

aiohttp/multipart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def read_chunk(self, size=chunk_size):
247247
'Content-Length required for chunked read'
248248
chunk_size = min(size, self._length - self._read_bytes)
249249
chunk = yield from self._content.read(chunk_size)
250-
self._read_bytes += chunk_size
250+
self._read_bytes += len(chunk)
251251
if self._read_bytes == self._length:
252252
self._at_eof = True
253253
assert b'\r\n' == (yield from self._content.readline()), \

tests/test_multipart.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ def readline(self):
7474
return self.content.readline()
7575

7676

77+
class StreamWithShortenRead(Stream):
78+
79+
def __init__(self, content):
80+
self._first = True
81+
super().__init__(content)
82+
83+
@asyncio.coroutine
84+
def read(self, size=None):
85+
if size is not None and self._first:
86+
self._first = False
87+
size = size // 2
88+
return (yield from super().read(size))
89+
90+
7791
class MultipartResponseWrapperTestCase(TestCase):
7892

7993
def setUp(self):
@@ -148,6 +162,22 @@ def test_read_chunk_requires_content_length(self):
148162
with self.assertRaises(AssertionError):
149163
yield from obj.read_chunk()
150164

165+
def test_read_chunk_properly_counts_read_bytes(self):
166+
expected = b'.' * 10
167+
size = len(expected)
168+
obj = aiohttp.multipart.BodyPartReader(
169+
self.boundary, {'CONTENT-LENGTH': size},
170+
StreamWithShortenRead(expected + b'\r\n--:--'))
171+
result = bytearray()
172+
while True:
173+
chunk = yield from obj.read_chunk()
174+
if not chunk:
175+
break
176+
result.extend(chunk)
177+
self.assertEqual(size, len(result))
178+
self.assertEqual(b'.' * size, result)
179+
self.assertTrue(obj.at_eof())
180+
151181
def test_read_does_reads_boundary(self):
152182
stream = Stream(b'Hello, world!\r\n--:')
153183
obj = aiohttp.multipart.BodyPartReader(

0 commit comments

Comments
 (0)