Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b305ff6
Fix FileResponse 416 on well-formed multi-range requests
vyrnsynx Oct 26, 2026
ef05741
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2026
45cf0cb
Fix mypy possibly-undefined end on multi-range ignore path
vyrnsynx Oct 26, 2026
55ed25b
Fix mypy: init end with start, avoid no-redef
vyrnsynx Oct 26, 2026
d5fd46d
Merge branch 'master' into fix/fileresponse-multi-range-14124
vyrnsynx Oct 29, 2026
577fda3
Merge branch 'master' into fix/fileresponse-multi-range-14124
vyrnsynx Oct 30, 2026
e4d725c
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 19, 2026
bd25c8a
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 20, 2026
112d8ca
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 21, 2026
a81e61c
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 24, 2026
32917c5
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 25, 2026
d26bd21
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 26, 2026
2e2cc33
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 27, 2026
4cffbcd
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 28, 2026
d12840a
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 29, 2026
11d581a
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Nov 30, 2026
488c3af
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Dec 1, 2026
83891f5
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Dec 2, 2026
3cf027f
Merge remote-tracking branch 'upstream/master' into fix/fileresponse-…
vyrnsynx Dec 3, 2026
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
3 changes: 3 additions & 0 deletions CHANGES/14124.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed ``FileResponse`` answering a well-formed multi-range request with
``416 Range Not Satisfiable`` instead of ignoring ``Range`` and returning the
full file -- by :user:`vyrnsynx`.
Comment on lines +1 to +3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Required release updates are missing

AGENTS.md requires docs for user-visible API changes and news entries under both the issue and PR numbers. This status-code change has no docs/ update and only the issue-numbered fragment. Before merging, document the new behavior under FileResponse and add CHANGES/14129.bugfix.rst as a symlink to 14124.bugfix.rst.

Context Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ Vladimir Zakharov
Vladyslav Bohaichuk
Vladyslav Bondar
Vojtěch Boček
Vyrn Synx
W. Trevor King
Wei Lin
Weiwei Wang
Expand Down
45 changes: 32 additions & 13 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os
import pathlib
import re
import sys
from collections.abc import Awaitable, Callable
from contextlib import suppress
Expand Down Expand Up @@ -42,6 +43,14 @@

NOSENDFILE: Final[bool] = bool(os.environ.get("AIOHTTP_NOSENDFILE"))

# Well-formed multi-range set (RFC 9110 §14.1). FileResponse only serves a
# single range; a satisfiable multi-range must not become 416 (RFC 9110 §14.2
# allows ignoring Range and returning 200 with the full representation).
_MULTI_BYTE_RANGE_RE = re.compile(
r"^bytes=(?:\d+-\d*|-\d+)(?:,(?:\d+-\d*|-\d+))+$",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Spaced range lists still fail

A valid header such as Range: bytes=0-1, 4-5 still gets 416 for a file with at least six bytes. HTTP list syntax allows spaces or tabs around commas, but this pattern rejects them. Clients using that spacing still get an error instead of the full file. Allow that whitespace and add a regression test.

Knowledge Base Used: Server routing, responses, and static files

re.ASCII | re.IGNORECASE,
)

CONTENT_TYPES: Final[MimeTypes] = MimeTypes()

# File extension to IANA encodings map that will be checked in the order defined.
Expand Down Expand Up @@ -339,6 +348,7 @@ async def _prepare_open_file(
file_mtime: float = st.st_mtime
count: int = file_size
start: int | None = None
end: int | None = None
etag_value = f"{st.st_mtime_ns:x}-{st.st_size:x}"

# https://www.rfc-editor.org/info/rfc9110/#name-if-range
Expand Down Expand Up @@ -367,20 +377,29 @@ async def _prepare_open_file(
try:
rng = request.http_range
start = rng.start
end: int | None = rng.stop
end = rng.stop
except ValueError:
# https://tools.ietf.org/html/rfc7233:
# A server generating a 416 (Range Not Satisfiable) response to
# a byte-range request SHOULD send a Content-Range header field
# with an unsatisfied-range value.
# The complete-length in a 416 response indicates the current
# length of the selected representation.
#
# Will do the same below. Many servers ignore this and do not
# send a Content-Range header with HTTP 416
self._headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}"
self.set_status(HTTPRequestRangeNotSatisfiable.status_code)
return await super().prepare(request)
# http_range only accepts a single byte range. A well-formed
# multi-range set (e.g. bytes=0-1,4-5) is still a valid Range
# per RFC 9110; answering 416 would be wrong for a satisfiable
# set. Ignore the header and fall through to a full 200 instead
# (RFC 9110 §14.2 / RFC 7233). Malformed ranges keep 416.
range_hdr = request.headers.get(hdrs.RANGE)
if range_hdr is not None and _MULTI_BYTE_RANGE_RE.fullmatch(range_hdr):
pass
Comment on lines +388 to +389

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Backwards ranges download full files

The pattern matches bytes=100-0,0-1, although its first range runs backwards. The new fallback returns 200 with the full file instead of the previous 416, contrary to the promised unchanged behavior for malformed ranges. Check each bounded range's start and end before accepting the set, and add a regression test.

Knowledge Base Used: Server routing, responses, and static files

else:
# https://tools.ietf.org/html/rfc7233:
# A server generating a 416 (Range Not Satisfiable) response to
# a byte-range request SHOULD send a Content-Range header field
# with an unsatisfied-range value.
# The complete-length in a 416 response indicates the current
# length of the selected representation.
#
# Will do the same below. Many servers ignore this and do not
# send a Content-Range header with HTTP 416
self._headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}"
self.set_status(HTTPRequestRangeNotSatisfiable.status_code)
return await super().prepare(request)

# If a range request has been made, convert start, end slice
# notation into file pointer offset and count
Expand Down
39 changes: 39 additions & 0 deletions tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,45 @@ async def handler(request: web.Request) -> web.FileResponse:
await client.close()


async def test_static_file_multi_range_ignored(
aiohttp_client: AiohttpClient, sender: _Sender
) -> None:
"""A well-formed multi-range request gets 200 with the full file.

FileResponse only implements a single byte range. Per RFC 9110 §14.2 a
server may ignore Range; answering 416 for a satisfiable multi-range set
is incorrect (see #14124).
"""
filepath = pathlib.Path(__file__).parent / "aiohttp.png"
body = filepath.read_bytes()

async def handler(request: web.Request) -> web.FileResponse:
return sender(filepath, chunk_size=16)

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

resp = await client.get("/", headers={"Range": "bytes=0-1,4-5"})
assert resp.status == 200
assert "Content-Range" not in resp.headers
assert await resp.read() == body
resp.release()

# Still a multi-range set (suffix + open range); ignore rather than 416.
resp = await client.get("/", headers={"Range": "bytes=-3,0-"})
assert resp.status == 200
assert await resp.read() == body
resp.release()

# Malformed ranges that happen to contain a comma stay 416.
resp = await client.get("/", headers={"Range": "bytes=0-1,a-b"})
assert resp.status == 416
resp.release()

await client.close()


async def test_static_file_if_unmodified_since_past_with_range(
aiohttp_client: AiohttpClient, app_with_static_route: web.Application
) -> None:
Expand Down
Loading