Skip to content

Commit

Permalink
if_range_check should return True if no header is present. Refactor f…
Browse files Browse the repository at this point in the history
…unction usage in static.py.
  • Loading branch information
ian-otto committed Jan 4, 2019
1 parent 9af77aa commit 2da1314
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cherrypy/lib/httputil.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def protocol_from_http(protocol_str):


def passes_if_range_check(request):
"""Return true if the request has If-Range header, and it passes the conditions contained in the header.
"""Return true if the request does not have an If-Range header, or it passes the conditions contained in the header.
In this case, the Range header should be observed. False if other, with the Range header discarded."""
if_range_header = request.headers.get('If-Range')
if if_range_header:
Expand All @@ -84,10 +84,14 @@ def passes_if_range_check(request):
try:
if datetime(*parsedate(if_range_header)[:6]) < datetime.now():
return True
else:
return False
except TypeError:
# Fixme: TypeError indicates that the value is an ETag. We don't support ETag at the moment.
return False
return False
else:
# Return True when there is no If-Range, since it technically fufills all conditions
return True

def get_ranges(headervalue, content_length):
"""Return a list of (start, stop) indices from a Range header, or None.
Expand Down
6 changes: 3 additions & 3 deletions cherrypy/lib/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False):
request = cherrypy.serving.request
if request.protocol >= (1, 1):
response.headers['Accept-Ranges'] = 'bytes'
r = httputil.get_ranges(request.headers.get('Range'), content_length)
r = httputil.passes_if_range_check(request) and httputil.get_ranges(request.headers.get('Range'), content_length)
if r == []:
response.headers['Content-Range'] = 'bytes */%s' % content_length
message = ('Invalid Range (first-byte-pos greater than '
Expand All @@ -176,7 +176,7 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False):
cherrypy.log(message, 'TOOLS.STATIC')
raise cherrypy.HTTPError(416, message)

if r and not httputil.passes_if_range_check(request):
if r:
if len(r) == 1:
# Return a single-part response.
start, stop = r[0]
Expand Down Expand Up @@ -235,7 +235,7 @@ def file_ranges():
else:
if debug:
log_msg = (
'If-Range is in the past' if is_if_range_in_past
'If-Range is in the past' if resource_expired
else 'No byteranges requested'
)
cherrypy.log(log_msg, 'TOOLS.STATIC')
Expand Down

1 comment on commit 2da1314

@webknjaz
Copy link
Member

Choose a reason for hiding this comment

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

Please sign in to comment.