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

Static range requests #1382

Merged
merged 19 commits into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Alex Lisovoy
Amy Boyle
Andrei Ursulenko
Andrej Antonov
Andrew Leech
Andrew Svetlov
Andrii Soldatenko
Anton Kasyanov
Expand Down
43 changes: 38 additions & 5 deletions aiohttp/file_sender.py
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
Expand Down Expand Up @@ -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:
Copy link
Member

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 applications

Copy link
Contributor Author

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.

from .web_exceptions import HTTPPartialContent, HTTPRequestRangeNotSatisfiable
Copy link
Member

Choose a reason for hiding this comment

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

move it to module level import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
90 changes: 89 additions & 1 deletion tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import os
import pathlib

import pytest

import aiohttp
Expand Down Expand Up @@ -323,3 +322,92 @@ def test_static_file_huge(loop, test_client, tmpdir):
off += len(chunk)
cnt += 1
f.close()


@asyncio.coroutine
def test_static_file_range(loop, test_client, sender):
filepath = (pathlib.Path(__file__).parent /
'software_development_in_picture.jpg')

@asyncio.coroutine
def handler(request):
resp = yield from sender(chunk_size=16).send(request, filepath)
return resp

app = web.Application(loop=loop)
app.router.add_get('/', handler)
client = yield from test_client(lambda loop: app)

with filepath.open('rb') as f:
content = f.read()

# Ensure the whole file requested in parts is correct
resp = yield from client.get('/', headers={'Range': 'bytes=0-999'})
assert resp.status == 206
body1 = yield from resp.read()
assert len(body1) == 1000
resp.close()

resp = yield from client.get('/', headers={'Range': 'bytes=0-999'})
assert resp.status == 206
body1 = yield from resp.read()
assert len(body1) == 1000
resp.close()

resp = yield from client.get('/', headers={'Range': 'bytes=1000-1999'})
assert resp.status == 206
body2 = yield from resp.read()
assert len(body1) == 1000
resp.close()

resp = yield from client.get('/', headers={'Range': 'bytes=2000-'})
assert resp.status == 206
body3 = yield from resp.read()
resp.close()

assert content == (body1 + body2 + body3)

# Ensure the tail of the file is correct
resp = yield from client.get('/', headers={'Range': 'bytes=-500'})
assert resp.status == 206
body4 = yield from resp.read()
resp.close()
assert content[-500:] == body4



@asyncio.coroutine
def test_static_file_invalid_range(loop, test_client, sender):
filepath = (pathlib.Path(__file__).parent /
'software_development_in_picture.jpg')

@asyncio.coroutine
def handler(request):
resp = yield from sender(chunk_size=16).send(request, filepath)
return resp

app = web.Application(loop=loop)
app.router.add_get('/', handler)
client = yield from test_client(lambda loop: app)

file_len = filepath.stat().st_size

# range must be in bytes
resp = yield from client.get('/', headers={'Range': 'blocks=0-10'})
assert resp.status == 416, 'Range must be in bytes'
resp.close()

# Range end is inclusive
resp = yield from client.get('/', headers={'Range': 'bytes=0-%d' % file_len})
assert resp.status == 416, 'Range end must be inclusive'
resp.close()

# start > end
resp = yield from client.get('/', headers={'Range': 'bytes=10-5'})
assert resp.status == 416, "Range start can't be greater than end"
resp.close()

# non-number range
resp = yield from client.get('/', headers={'Range': 'bytes=a-f'})
assert resp.status == 416, 'Range must be integers'
resp.close()