Skip to content

Commit

Permalink
Polish http_range
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 18, 2016
1 parent 10eff37 commit c693473
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CHANGES

- Make `TestServer.make_url` compatible with `yarl.URL` #1389

-
- Implement range requests for static files #1382

-

Expand Down
4 changes: 3 additions & 1 deletion aiohttp/file_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ def send(self, request, filepath):
count = file_size

try:
start, end = request.http_range
rng = request.http_range
start = rng.start
end = rng.stop
except ValueError:
raise HTTPRequestRangeNotSatisfiable

Expand Down
10 changes: 5 additions & 5 deletions aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ def cookies(self):

@property
def http_range(self, *, _RANGE=hdrs.RANGE):
"""The content of Range HTTP header.
Return a slice instance.
"""
The content of Range HTTP header.
:returns tuple (start, end): values that can be used for slice
eg. content[start:end]
"""
rng = self.headers.get(_RANGE)
start, end = None, None
if rng is not None:
Expand All @@ -329,7 +329,7 @@ def http_range(self, *, _RANGE=hdrs.RANGE):

if start is end is None: # No valid range supplied
raise ValueError('No start or end of range specified')
return start, end
return slice(start, end, 1)

@property
def content(self):
Expand Down
29 changes: 27 additions & 2 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@ like one using :meth:`Request.copy`.

Returns :class:`int` or ``None`` if *Content-Length* is absent.

.. attribute:: http_range

Read-only property that returns information about *Range* HTTP header.

Returns a :class:`slice` where ``.start`` is *left inclusive
bound*, ``.stop`` is *right exclusive bound* and ``.step`` is
``1``.

The property might be used in two manners:

1. Attribute-access style (example assumes that both left and
right borders are set, the real logic for case of open bounds
is more complex)::

rng = request.http_rangea
with open(filename, 'rb') as f:
f.seek(rng.start)
return f.read(rng.stop-rng.start)

2. Slice-style::

return buffer[request.http_range]

.. versionadded:: 1.2

.. attribute:: if_modified_since

Read-only property that returns the date specified in the
Expand Down Expand Up @@ -808,8 +833,8 @@ WebSocketResponse
cannot use :meth:`~StreamResponse.write` method but should to
communicate with websocket client by :meth:`send_str`,
:meth:`receive` and others.
:param bool autoping: Automatically send

:param bool autoping: Automatically send
:const:`~aiohttp.WSMsgType.PONG` on
:const:`~aiohttp.WSMsgType.PING`
message from client, and handle
Expand Down

0 comments on commit c693473

Please sign in to comment.