-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Static range requests #1382
Static range requests #1382
Changes from 4 commits
3ba96f6
3ddd69d
5fe0de3
bb9c909
7b477f3
943a3f6
6d0f017
f82a5ef
7575b21
96f92f2
1347985
6e3a09f
fec9162
61b94c2
27d3950
78d3fcf
88d98e0
0a2c642
c884825
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import asyncio | ||
import mimetypes | ||
import os | ||
import re | ||
|
||
from . import hdrs | ||
from .helpers import create_future | ||
|
@@ -152,16 +153,48 @@ def send(self, request, filepath): | |
if not ct: | ||
ct = 'application/octet-stream' | ||
|
||
resp = self._response_factory() | ||
file_size = st.st_size | ||
|
||
status = 200 | ||
start = 0 | ||
end = file_size - 1 | ||
|
||
# Handle 206 range response if requested | ||
if 'range' in request.headers: | ||
from .web_exceptions import HTTPPartialContent, HTTPRequestRangeNotSatisfiable | ||
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. move it to module level import. 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. I normally would by standard, but was following what had been done a few lines earlier in the same function. I'll fix both. |
||
status = HTTPPartialContent.status_code | ||
|
||
range_header = request.headers['range'].lower() | ||
if not range_header.startswith('bytes='): | ||
raise HTTPRequestRangeNotSatisfiable | ||
|
||
try: | ||
range_start, range_end = re.findall(r'bytes=(\d*)-(\d*)', request.headers['range'].lower())[0] | ||
except IndexError: | ||
raise HTTPRequestRangeNotSatisfiable | ||
if range_start and range_end: | ||
start = int(range_start) | ||
end = int(range_end) | ||
elif range_start and not range_end: | ||
start = int(range_start) | ||
elif range_end and not range_start: | ||
start = file_size - int(range_end) | ||
else: | ||
raise HTTPRequestRangeNotSatisfiable | ||
|
||
if start >= file_size or end >= file_size or start > end: | ||
raise HTTPRequestRangeNotSatisfiable | ||
|
||
resp = self._response_factory(status=status) | ||
resp.content_type = ct | ||
if encoding: | ||
resp.headers[hdrs.CONTENT_ENCODING] = encoding | ||
resp.last_modified = st.st_mtime | ||
|
||
file_size = st.st_size | ||
|
||
resp.content_length = file_size | ||
count = end - start + 1 | ||
resp.content_length = count | ||
with filepath.open('rb') as f: | ||
yield from self._sendfile(request, resp, f, file_size) | ||
f.seek(start) | ||
yield from self._sendfile(request, resp, f, count) | ||
|
||
return resp |
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.
i think it make sense to move range related functionality to helper module or even to separate module. and create
Range
class, it could be useful for external libraries and applicationsThere 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.
I had thought that but wasn't sure where would make sense to put it.
It could be in a parse_range_header(request) function in helpers, it's not necessarily making things clearer but would certainly would aid in reuse.