Repository navigation
Ignore well-formed multi-range on FileResponse instead of 416 #14129
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
base: master
Are you sure you want to change the base?
Changes from all commits
b305ff6
ef05741
45cf0cb
55ed25b
d5fd46d
577fda3
e4d725c
bd25c8a
112d8ca
a81e61c
32917c5
d26bd21
2e2cc33
4cffbcd
d12840a
11d581a
488c3af
83891f5
3cf027f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import math | ||
| import os | ||
| import pathlib | ||
| import re | ||
| import sys | ||
| from collections.abc import Awaitable, Callable | ||
| from contextlib import suppress | ||
|
|
@@ -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+))+$", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A valid header such as 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. | ||
|
|
@@ -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 | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The pattern matches 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AGENTS.mdrequires docs for user-visible API changes and news entries under both the issue and PR numbers. This status-code change has nodocs/update and only the issue-numbered fragment. Before merging, document the new behavior underFileResponseand addCHANGES/14129.bugfix.rstas a symlink to14124.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!