]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Send HTTP 200 in response to Range: bytes=0-
authorDavid Wolever <david@wolever.net>
Tue, 21 May 2013 04:14:46 +0000 (00:14 -0400)
committerDavid Wolever <david@wolever.net>
Tue, 21 May 2013 04:14:46 +0000 (00:14 -0400)
tornado/test/web_test.py
tornado/web.py

index 00d0ea0d3d41fb63e8a717accc23d46de36db8d4..3d80b6ba5002b8910ab51a0f6e9ea2d8c890eaba 100644 (file)
@@ -756,6 +756,7 @@ class StaticFileTest(WebTestCase):
     # The expected MD5 hash of robots.txt, used in tests that call
     # StaticFileHandler.get_version
     robots_txt_hash = b"f71d20196d4caf35b6a670db8c70b03d"
+    static_dir = os.path.join(os.path.dirname(__file__), 'static')
 
     def get_handlers(self):
         class StaticUrlHandler(RequestHandler):
@@ -794,8 +795,7 @@ class StaticFileTest(WebTestCase):
                 ('/override_static_url/(.*)', OverrideStaticUrlHandler)]
 
     def get_app_kwargs(self):
-        return dict(static_path=os.path.join(os.path.dirname(__file__),
-                                             'static'))
+        return dict(static_path=self.static_dir)
 
     def test_static_files(self):
         response = self.fetch('/robots.txt')
@@ -879,6 +879,18 @@ class StaticFileTest(WebTestCase):
         self.assertEqual(response.headers.get("Content-Range"),
                          "0-9/26")
 
+    def test_static_with_range_full_file(self):
+        response = self.fetch('/static/robots.txt', headers={
+                'Range': 'bytes=0-'})
+        # Note: Chrome refuses to play audio if it gets an HTTP 206 in response
+        # to ``Range: bytes=0-`` :(
+        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"), "0-25/26")
+
     def test_static_with_range_end_edge(self):
         response = self.fetch('/static/robots.txt', headers={
                 'Range': 'bytes=22-'})
index 690d2ecc44922ed62ae2061f88a59f304c5bd62a..0ffd9e164fadacc6a25018de20223904fe0a5ab9 100644 (file)
@@ -52,6 +52,7 @@ request.
 
 from __future__ import absolute_import, division, print_function, with_statement
 
+
 import base64
 import binascii
 import datetime
@@ -1797,11 +1798,17 @@ class StaticFileHandler(RequestHandler):
         with open(abspath, "rb") as file:
             data = file.read()
             if request_range:
-                # 206: Partial Content
-                self.set_status(206)
+                data_sliced = data[request_range]
+                # 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 ``Range: bytes=0-``.
+                if len(data) != len(data_sliced):
+                    # 206: Partial Content
+                    self.set_status(206)
                 content_range = httputil.get_content_range(data, request_range)
                 self.set_header("Content-Range", content_range)
-                data = data[request_range]
+                data = data_sliced
             if include_body:
                 self.write(data)
             else: