]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix exception in StaticFileHandler when range requested is larger than file.
authorBen Darnell <ben@bendarnell.com>
Thu, 11 Jul 2013 18:00:37 +0000 (14:00 -0400)
committerBen Darnell <ben@bendarnell.com>
Thu, 11 Jul 2013 18:00:37 +0000 (14:00 -0400)
tornado/test/web_test.py
tornado/web.py

index 942a8917ea23ab73cd584c1dec800c570c56234c..21035c90845505db70255b2339965bb20903cd55 100644 (file)
@@ -900,6 +900,26 @@ class StaticFileTest(WebTestCase):
         self.assertEqual(response.headers.get("Content-Length"), "26")
         self.assertEqual(response.headers.get("Content-Range"), None)
 
+    def test_static_with_range_full_past_end(self):
+        response = self.fetch('/static/robots.txt', headers={
+            'Range': 'bytes=0-10000000'})
+        self.assertEqual(response.code, 200)
+        robots_file_path = os.path.join(self.static_dir, "robots.txt")
+        with open(robots_file_path) as f:
+            self.assertEqual(response.body, utf8(f.read()))
+        self.assertEqual(response.headers.get("Content-Length"), "26")
+        self.assertEqual(response.headers.get("Content-Range"), None)
+
+    def test_static_with_range_partial_past_end(self):
+        response = self.fetch('/static/robots.txt', headers={
+            'Range': 'bytes=1-10000000'})
+        self.assertEqual(response.code, 206)
+        robots_file_path = os.path.join(self.static_dir, "robots.txt")
+        with open(robots_file_path) as f:
+            self.assertEqual(response.body, utf8(f.read()[1:]))
+        self.assertEqual(response.headers.get("Content-Length"), "25")
+        self.assertEqual(response.headers.get("Content-Range"), "bytes 1-25/26")
+
     def test_static_with_range_end_edge(self):
         response = self.fetch('/static/robots.txt', headers={
             'Range': 'bytes=22-'})
index 363c97c44a9c36d3d6f8fc2c10074fdbbe79f669..63f5514c3fb624dbc9ce100a79cb8c91e15cb9d4 100644 (file)
@@ -1827,6 +1827,10 @@ class StaticFileHandler(RequestHandler):
                 return
             if start is not None and start < 0:
                 start += size
+            if end is not None and end > size:
+                # Clients sometimes blindly use a large range to limit their
+                # download size; cap the endpoint at the actual file size.
+                end = size
             # Note: only return HTTP 206 if less than the entire range has been
             # requested. Not only is this semantically correct, but Chrome
             # refuses to play audio if it gets an HTTP 206 in response to