diff --git a/cherrypy/lib/httputil.py b/cherrypy/lib/httputil.py index 77bc961c7..d4f32abef 100644 --- a/cherrypy/lib/httputil.py +++ b/cherrypy/lib/httputil.py @@ -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: @@ -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. diff --git a/cherrypy/lib/static.py b/cherrypy/lib/static.py index 0574030f4..af74dd32b 100644 --- a/cherrypy/lib/static.py +++ b/cherrypy/lib/static.py @@ -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 ' @@ -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] @@ -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')