Skip to content

Commit ced8133

Browse files
authored
Fix read_chunk() on body parts with a zero Content-Length (aio-libs#13760)
1 parent ed74d78 commit ced8133

5 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎CHANGES/13758.bugfix.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fixed a crash in :meth:`~aiohttp.BodyPartReader.read_chunk` on a body part
2+
with an explicit ``Content-Length: 0``: the part fell through to the
3+
streaming read strategy, whose minimum chunk size assertion then failed for
4+
chunk sizes below the boundary length. Such parts now yield an immediate
5+
empty chunk, like any other part with a known length
6+
-- by :user:`istoolsfox`.

‎CHANGES/13760.bugfix.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
13758.bugfix.rst

‎CONTRIBUTORS.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ Illia Volochii
188188
Ilya Chichak
189189
Ilya Gruzinov
190190
Ingmar Steen
191+
istoolsfox
191192
Ivan Lakovic
192193
Ivan Larin
193194
J. Nick Koston

‎aiohttp/multipart.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ async def read_chunk(self, size: int = chunk_size) -> bytes:
386386
if carry:
387387
self._b64_carry = b""
388388
want = max(want, self._boundary_len)
389-
if self._length:
389+
if self._length is not None:
390390
fresh = await self._read_chunk_from_length(want)
391391
else:
392392
fresh = await self._read_chunk_from_stream(want)

‎tests/test_multipart.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ async def test_read_chunk_without_content_length(self) -> None:
206206
assert c1 + c2 == b"Hello, world!"
207207
assert c3 == b""
208208

209+
async def test_read_chunk_with_zero_content_length(self) -> None:
210+
with Stream(b"\r\n--:--\r\n") as stream:
211+
d = HeadersDictProxy(CIMultiDict({"Content-Length": "0"}))
212+
obj = aiohttp.BodyPartReader(BOUNDARY, d, stream)
213+
result = await obj.read_chunk(4)
214+
assert obj.at_eof()
215+
assert b"" == result
216+
209217
async def test_read_incomplete_chunk(self) -> None:
210218
with Stream(b"") as stream:
211219

0 commit comments

Comments
 (0)