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

Add HTTP range headers to FileResponse #2367

Closed
wants to merge 3 commits into from
Closed
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
144 changes: 131 additions & 13 deletions starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from starlette._compat import md5_hexdigest
from starlette.background import BackgroundTask
from starlette.concurrency import iterate_in_threadpool
from starlette.datastructures import URL, MutableHeaders
from starlette.datastructures import URL, Headers, MutableHeaders
from starlette.types import Receive, Scope, Send


Expand Down Expand Up @@ -288,6 +288,7 @@ def __init__(
self.media_type = media_type
self.background = background
self.init_headers(headers)
self.headers.setdefault("accept-ranges", "bytes")
if self.filename is not None:
content_disposition_filename = quote(self.filename)
if content_disposition_filename != self.filename:
Expand All @@ -310,6 +311,7 @@ def set_stat_headers(self, stat_result: os.stat_result) -> None:
self.headers.setdefault("etag", etag)

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
send_header_only: bool = scope["method"].upper() == "HEAD"
if self.stat_result is None:
try:
stat_result = await anyio.to_thread.run_sync(os.stat, self.path)
Expand All @@ -320,27 +322,143 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
mode = stat_result.st_mode
if not stat.S_ISREG(mode):
raise RuntimeError(f"File at path {self.path} is not a file.")
await send(
{
"type": "http.response.start",
"status": self.status_code,
"headers": self.raw_headers,
}
)
if scope["method"].upper() == "HEAD":
else:
stat_result = self.stat_result

headers = Headers(scope=scope)
http_range = headers.get("range")
# http_if_range = headers.get("if-range")

if http_range is None:
await self._handle_simple(send, send_header_only)
else:
ranges = self._parse_range_header(http_range, stat_result.st_size)

if len(ranges) == 1:
start, end = ranges[0]
await self._handle_single_range(send, start, end, stat_result.st_size, send_header_only)
else:
await self._handle_multiple_ranges(send, ranges, stat_result.st_size, send_header_only)

if self.background is not None:
await self.background()

async def _handle_simple(self, send: Send, send_header_only: bool) -> None:
await send({"type": "http.response.start", "status": self.status_code, "headers": self.raw_headers})
if send_header_only:
await send({"type": "http.response.body", "body": b"", "more_body": False})
else:
async with await anyio.open_file(self.path, mode="rb") as file:
more_body = True
while more_body:
chunk = await file.read(self.chunk_size)
more_body = len(chunk) == self.chunk_size
await send({"type": "http.response.body", "body": chunk, "more_body": more_body})

async def _handle_single_range(
self, send: Send, start: int, end: int, file_size: int, send_header_only: bool
) -> None:
self.headers["content-range"] = f"bytes {start}-{end - 1}/{file_size}"
await send({"type": "http.response.start", "status": 206, "headers": self.raw_headers})
if send_header_only:
await send({"type": "http.response.body", "body": b"", "more_body": False})
else:
async with await anyio.open_file(self.path, mode="rb") as file:
await file.seek(start)
more_body = True
while more_body:
chunk = await file.read(min(self.chunk_size, end - start))
start += len(chunk)
more_body = len(chunk) == self.chunk_size and start < end
await send({"type": "http.response.body", "body": chunk, "more_body": more_body})

async def _handle_multiple_ranges(
self,
send: Send,
ranges: list[tuple[int, int]],
file_size: int,
send_header_only: bool,
) -> None:
boundary = md5_hexdigest(os.urandom(16), usedforsecurity=False)
content_type = f"multipart/byteranges; boundary={boundary}"
self.headers["content-type"] = content_type
await send({"type": "http.response.start", "status": 206, "headers": self.raw_headers})
if send_header_only:
await send({"type": "http.response.body", "body": b"", "more_body": False})
else:
async with await anyio.open_file(self.path, mode="rb") as file:
for start, end in ranges:
await send(
{
"type": "http.response.body",
"body": chunk,
"more_body": more_body,
"body": b"\r\n--" + boundary.encode("ascii") + b"\r\n",
"more_body": True,
}
)
if self.background is not None:
await self.background()
await send(
{
"type": "http.response.body",
"body": f"Content-Type: {self.media_type}\r\n".encode(),
"more_body": True,
}
)
await send(
{
"type": "http.response.body",
"body": f"Content-Range: bytes {start}-{end - 1}/{file_size}\r\n\r\n".encode(),
"more_body": True,
}
)
await file.seek(start)
more_body = True
while more_body:
chunk = await file.read(min(self.chunk_size, end - start))
start += len(chunk)
more_body = len(chunk) == self.chunk_size and start < end
await send({"type": "http.response.body", "body": chunk, "more_body": more_body})
await send(
{
"type": "http.response.body",
"body": b"\r\n--" + boundary.encode("ascii") + b"--\r\n",
"more_body": False,
}
)

def _parse_range_header(self, http_range: str | None, file_size: int) -> list[tuple[int, int]]:
ranges: list[tuple[int, int]] = []
if http_range is None:
return ranges

if http_range.strip() == "":
return ranges

units, range_ = http_range.split("=", 1)
units = units.strip().lower()

if units != "bytes":
Copy link
Member Author

@Kludex Kludex Dec 10, 2023

Choose a reason for hiding this comment

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

If other unit is provided, should error for the time being.

return ranges

for val in range_.split(","):
val = val.strip()
if "-" not in val:
return []
if val.startswith("-"):
suffix_length = int(val[1:])
if suffix_length == 0:
return []
ranges.append((file_size - suffix_length, file_size))
elif val.endswith("-"):
start = int(val[:-1])
if start >= file_size:
return []
ranges.append((start, file_size))
else:
start, end = (int(v) for v in val.split("-", 1))
start = int(start)
end = int(end) + 1
if start >= end:
return []
if end > file_size:
return []
ranges.append((start, end))
return ranges
2 changes: 1 addition & 1 deletion tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ async def send(message: Message) -> None:

# Since the TestClient drops the response body on HEAD requests, we need to test
# this directly.
await app({"type": "http", "method": "head"}, receive, send)
await app({"type": "http", "method": "head", "headers": [(b"key", b"value")]}, receive, send)


def test_file_response_set_media_type(tmp_path: Path, test_client_factory: TestClientFactory) -> None:
Expand Down
Loading