Skip to content

Commit

Permalink
don't search boundary twice
Browse files Browse the repository at this point in the history
  • Loading branch information
tumb1er committed Jan 27, 2016
1 parent 8c3ec5a commit 7dda8eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 5 additions & 9 deletions aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,17 @@ def _read_chunk_from_stream(self, size):
:rtype: bytearray
"""
assert size >= len(self._boundary), \
'Chunk size must be greater or equal than boundary length'
assert size >= len(self._boundary) + 2, \
'Chunk size must be greater or equal than boundary length + 2'
if self._prev_chunk is None:
self._prev_chunk = yield from self._content.read(size)

chunk = yield from self._content.read(size)

window = self._prev_chunk + chunk

try:
idx = window.index(b'\r\n' + self._boundary)
except ValueError:
# boundary not found
pass
else:
sub = b'\r\n' + self._boundary
idx = window.find(sub, len(self._prev_chunk) - len(sub))
if idx >= 0:
# pushing boundary back to content
self._content.unread_data(window[idx:])
if size > idx:
Expand Down
10 changes: 6 additions & 4 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,10 @@ def test_read_chunk_from_stream_doesnt_breaks_reader(self):
{CONTENT_TYPE: 'multipart/related;boundary=":"'},
Stream(b'--:\r\n'
b'\r\n'
b'test'
b'chunk'
b'\r\n--:\r\n'
b'\r\n'
b'passed'
b'two_chunks'
b'\r\n--:--'))
body_parts = []
while True:
Expand All @@ -610,9 +610,11 @@ def test_read_chunk_from_stream_doesnt_breaks_reader(self):
if part is None:
break
while not part.at_eof():
read_part += yield from part.read_chunk(4)
chunk = yield from part.read_chunk(5)
self.assertTrue(chunk)
read_part += chunk
body_parts.append(read_part)
self.assertListEqual(body_parts, [b'test', b'passed'])
self.assertListEqual(body_parts, [b'chunk', b'two_chunks'])


class BodyPartWriterTestCase(unittest.TestCase):
Expand Down

0 comments on commit 7dda8eb

Please sign in to comment.