diff options
-rw-r--r-- | cherrypy/lib/httputil.py | 9 | ||||
-rw-r--r-- | cherrypy/test/test_core.py | 6 |
2 files changed, 14 insertions, 1 deletions
diff --git a/cherrypy/lib/httputil.py b/cherrypy/lib/httputil.py index 3de801d0..69a18d45 100644 --- a/cherrypy/lib/httputil.py +++ b/cherrypy/lib/httputil.py @@ -103,7 +103,14 @@ def get_ranges(headervalue, content_length): # See rfc quote above. return None # Negative subscript (last N bytes) - result.append((content_length - int(stop), content_length)) + # + # RFC 2616 Section 14.35.1: + # If the entity is shorter than the specified suffix-length, + # the entire entity-body is used. + if int(stop) > content_length: + result.append((0, content_length)) + else: + result.append((content_length - int(stop), content_length)) return result diff --git a/cherrypy/test/test_core.py b/cherrypy/test/test_core.py index 6966d325..ae4728de 100644 --- a/cherrypy/test/test_core.py +++ b/cherrypy/test/test_core.py @@ -496,6 +496,12 @@ class CoreRequestHandlingTest(helper.CPWebCase): self.getPage("/ranges/get_ranges?bytes=2-4,-1") self.assertBody("[(2, 5), (7, 8)]") + # Test a suffix-byte-range longer than the content + # length. Note that in this test, the content length + # is 8 bytes. + self.getPage("/ranges/get_ranges?bytes=-100") + self.assertBody("[(0, 8)]") + # Get a partial file. if cherrypy.server.protocol_version == "HTTP/1.1": self.getPage("/ranges/slice_file", [('Range', 'bytes=2-5')]) |