diff --git a/aiohttp/multipart.py b/aiohttp/multipart.py index 005257761d2..35f0f8f1164 100644 --- a/aiohttp/multipart.py +++ b/aiohttp/multipart.py @@ -305,14 +305,18 @@ def _read_chunk_from_stream(self, size): """ assert size >= len(self._boundary) + 2, \ 'Chunk size must be greater or equal than boundary length + 2' - if self._prev_chunk is None: + first_chunk = self._prev_chunk is None + if first_chunk: self._prev_chunk = yield from self._content.read(size) chunk = yield from self._content.read(size) window = self._prev_chunk + chunk sub = b'\r\n' + self._boundary - idx = window.find(sub, len(self._prev_chunk) - len(sub)) + if first_chunk: + idx = window.find(sub) + else: + idx = window.find(sub, len(self._prev_chunk) - len(sub)) if idx >= 0: # pushing boundary back to content self._content.unread_data(window[idx:])