Skip to content

Commit 6ae2751

Browse files
[3.7] Ensure zero byte files can be sent (#5125) (#5129)
Backports the following commits to 3.7: - Ensure zero byte files can be sent (#5125) Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent e7dc844 commit 6ae2751

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

CHANGES/5124.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure sending a zero byte file does not throw an exception

aiohttp/web_fileresponse.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ async def sendfile(self) -> None:
9898
if hasattr(loop, "sendfile"):
9999
# Python 3.7+
100100
self.transport.write(data)
101-
await loop.sendfile(self.transport, self._fobj, self._offset, self._count)
101+
if self._count != 0:
102+
await loop.sendfile(
103+
self.transport,
104+
self._fobj,
105+
self._offset,
106+
self._count
107+
)
102108
await super().write_eof()
103109
return
104110

tests/data.zero_bytes

Whitespace-only changes.

tests/test_web_sendfile_functional.py

+19
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ async def handler(request):
4545
await resp.release()
4646

4747

48+
async def test_zero_bytes_file_ok(aiohttp_client, sender) -> None:
49+
filepath = pathlib.Path(__file__).parent / "data.zero_bytes"
50+
51+
async def handler(request):
52+
return sender(filepath)
53+
54+
app = web.Application()
55+
app.router.add_get("/", handler)
56+
client = await aiohttp_client(app)
57+
58+
resp = await client.get("/")
59+
assert resp.status == 200
60+
txt = await resp.text()
61+
assert "" == txt.rstrip()
62+
assert "application/octet-stream" == resp.headers["Content-Type"]
63+
assert resp.headers.get("Content-Encoding") is None
64+
await resp.release()
65+
66+
4867
async def test_static_file_ok_string_path(aiohttp_client, sender) -> None:
4968
filepath = pathlib.Path(__file__).parent / "data.unknown_mime_type"
5069

0 commit comments

Comments
 (0)