Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure zero byte files can be sent #5125

Merged
merged 1 commit into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -44,6 +44,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