fix Range header handling for static files (#1402)

This commit fixes the issue in the `Range` header handling that was done
while serving the file contents.

As per the HTTP response standards, a status code of 206 will be used in
case if the Range is returning a partial value and default of 200 in
other cases.

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
This commit is contained in:
Harsha Narayana
2018-11-07 19:06:56 +05:30
committed by Stephen Sadowski
parent b63c06c75a
commit 92b73a6f4f
4 changed files with 17 additions and 15 deletions

View File

@@ -167,17 +167,17 @@ class ContentRangeHandler:
)
else:
# this case represents `Content-Range: bytes 5-`
self.end = self.total
self.end = self.total - 1
else:
if self.start is None:
# this case represents `Content-Range: bytes -5`
self.start = self.total - self.end
self.end = self.total
self.end = self.total - 1
if self.start >= self.end:
raise ContentRangeError(
"Invalid for Content Range parameters", self
)
self.size = self.end - self.start
self.size = self.end - self.start + 1
self.headers = {
"Content-Range": "bytes %s-%s/%s"
% (self.start, self.end, self.total)

View File

@@ -302,6 +302,7 @@ async def file(
_range.end,
_range.total,
)
status = 206
else:
out_stream = await _file.read()
@@ -371,6 +372,7 @@ async def file_stream(
_range.end,
_range.total,
)
status = 206
return StreamingHTTPResponse(
streaming_fn=_streaming_fn,
status=status,