Skip to content

Commit

Permalink
Handle range flag cleanly, for #12
Browse files Browse the repository at this point in the history
  • Loading branch information
anjackson committed Mar 24, 2022
1 parent 24d805a commit e2ec0ca
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
7 changes: 1 addition & 6 deletions warc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ def hello_world():
@app.route('/by-filename/<path:warc_filename>')
def warc_by_filename(warc_filename):
# Work out the range:
offset, length = get_byte_range()
if offset == None:
is_range = False
offset = 0
else:
is_range = True
offset, length, is_range = get_byte_range()
app.logger.debug(f"Looking for range {is_range} ({offset}-{length}) of {warc_filename}...")
# Look up the file:
path_to_file = find_file(warc_filename)
Expand Down
8 changes: 5 additions & 3 deletions warcserver/flask_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ def get_byte_range():
"""
Determines the byte range for this request, either via parameters or HTTP Range requests.
:return: the (offset, length) tuple, using None if unspecified
:return: the (offset, length, is_range) tuple, using None if unspecified
"""

# Default:
offset = None
offset = 0
length = None
is_range = False

# Get any range header:
range_header = request.headers.get('Range', None)
Expand All @@ -33,6 +34,7 @@ def get_byte_range():

# Otherwise, check for Range header:
elif range_header:
is_range = True
m = re.search('(\d+)-(\d*)', range_header)
g = m.groups()
if g[0]: offset = int(g[0])
Expand All @@ -45,7 +47,7 @@ def get_byte_range():
if offset2 is not None:
length = offset2 + 1 - offset

return offset, length
return offset, length, is_range


def send_file_partial(path, offset, length, is_range):
Expand Down

0 comments on commit e2ec0ca

Please sign in to comment.