Be more consistent and just always write to the request in `read_range`.

This commit is contained in:
Itamar Turner-Trauring 2022-06-08 13:55:47 -04:00
parent 00381bc24f
commit db42651355
1 changed files with 7 additions and 7 deletions

View File

@ -274,21 +274,20 @@ _SCHEMAS = {
} }
def read_range(request, read_data: Callable[[int, int], bytes]) -> Optional[bytes]: def read_range(request, read_data: Callable[[int, int], bytes]) -> None:
""" """
Read an optional ``Range`` header, reads data appropriately via the given Read an optional ``Range`` header, reads data appropriately via the given
callable, return as result. callable, writes the data to the request.
Only parses a subset of ``Range`` headers that we support: must be set, Only parses a subset of ``Range`` headers that we support: must be set,
bytes only, only a single range, the end must be explicitly specified. bytes only, only a single range, the end must be explicitly specified.
Raises a ``_HTTPError(http.REQUESTED_RANGE_NOT_SATISFIABLE)`` if parsing is Raises a ``_HTTPError(http.REQUESTED_RANGE_NOT_SATISFIABLE)`` if parsing is
not possible or the header isn't set. not possible or the header isn't set.
Returns a result that should be returned from the request handler, and sets
appropriate response headers.
Takes a function that will do the actual reading given the start offset and Takes a function that will do the actual reading given the start offset and
a length to read. a length to read.
The resulting data is written to the request.
""" """
if request.getHeader("range") is None: if request.getHeader("range") is None:
# Return the whole thing. # Return the whole thing.
@ -299,7 +298,7 @@ def read_range(request, read_data: Callable[[int, int], bytes]) -> Optional[byte
data = read_data(start, start + 65536) data = read_data(start, start + 65536)
if not data: if not data:
request.finish() request.finish()
return None return
request.write(data) request.write(data)
start += len(data) start += len(data)
@ -326,7 +325,8 @@ def read_range(request, read_data: Callable[[int, int], bytes]) -> Optional[byte
"content-range", "content-range",
ContentRange("bytes", offset, offset + len(data)).to_header(), ContentRange("bytes", offset, offset + len(data)).to_header(),
) )
return data request.write(data)
request.finish()
class HTTPServer(object): class HTTPServer(object):