Skip to content

Commit

Permalink
Ensure zero byte files can be sent (#5125)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored and asvetlov committed Oct 25, 2020
1 parent e7dc844 commit 23ce198
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/5124.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure sending a zero byte file does not throw an exception
8 changes: 7 additions & 1 deletion aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ async def sendfile(self) -> None:
if hasattr(loop, "sendfile"):
# Python 3.7+
self.transport.write(data)
await loop.sendfile(self.transport, self._fobj, self._offset, self._count)
if self._count != 0:
await loop.sendfile(
self.transport,
self._fobj,
self._offset,
self._count
)
await super().write_eof()
return

Expand Down
Empty file added tests/data.zero_bytes
Empty file.
19 changes: 19 additions & 0 deletions tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ async def handler(request):
await resp.release()


async def test_zero_bytes_file_ok(aiohttp_client, sender) -> None:
filepath = pathlib.Path(__file__).parent / "data.zero_bytes"

async def handler(request):
return sender(filepath)

app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)

resp = await client.get("/")
assert resp.status == 200
txt = await resp.text()
assert "" == txt.rstrip()
assert "application/octet-stream" == resp.headers["Content-Type"]
assert resp.headers.get("Content-Encoding") is None
await resp.release()


async def test_static_file_ok_string_path(aiohttp_client, sender) -> None:
filepath = pathlib.Path(__file__).parent / "data.unknown_mime_type"

Expand Down

0 comments on commit 23ce198

Please sign in to comment.