Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

fix StaticHandler process http ranges header bug #143

Open
wants to merge 2 commits into
base: v0.1.0
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions vibora/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,28 @@ def parse_response(self, request: Request, cache: CacheEntry):
if range_header:
pieces = self.get_range_pieces(range_header)
if len(pieces) == 2:
start, end = int(pieces[0]), int(pieces[1])
if pieces[0] == "":
start = cache.content_length - int(pieces[1])
end = cache.content_length - 1
elif pieces[1] == "":
start = int(pieces[0])
end = cache.content_length - 1
else:
start, end = int(pieces[0]), int(pieces[1])

if start < 0 or start > end or end >= cache.content_length:
return Response(b"", headers={}, status_code=416)

headers = {
"Content-Range": "bytes {0}-{1}/{2}".format(start, end, cache.content_length),
"Content-Length": str((end - start)),
"Content-Length": str((end - start + 1)),
"Accept-Ranges": "bytes",
}
if request.method == "HEAD":
return Response(b"", headers=headers, status_code=206)
else:
return StreamingResponse(RangeFile(cache.path, start, end).stream, headers=headers, status_code=206)
return StreamingResponse(RangeFile(cache.path, start, end + 1).stream, headers=headers,
status_code=206)

# Handling HEAD requests
if request.method == "HEAD":
Expand Down