From: David Wolever Date: Thu, 16 May 2013 22:45:06 +0000 (-0400) Subject: Use 'get_version' to calculate static file hashes. X-Git-Tag: v3.1.0~68^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5dd18d30544edf83bdec0408a0c033c4253981bc;p=thirdparty%2Ftornado.git Use 'get_version' to calculate static file hashes. --- diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index a1df2eb83..3afdae5da 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -753,6 +753,10 @@ class ErrorResponseTest(WebTestCase): @wsgi_safe class StaticFileTest(WebTestCase): + # The expected MD5 hash of robots.txt, used in tests that call + # StaticFileHandler.get_version + robots_txt_hash = b"f71d20196d4caf35b6a670db8c70b03d" + def get_handlers(self): class StaticUrlHandler(RequestHandler): def get(self, path): @@ -802,12 +806,15 @@ class StaticFileTest(WebTestCase): def test_static_url(self): response = self.fetch("/static_url/robots.txt") - self.assertEqual(response.body, b"/static/robots.txt?v=f71d2") + self.assertEqual(response.body, + b"/static/robots.txt?v=" + self.robots_txt_hash) def test_absolute_static_url(self): response = self.fetch("/abs_static_url/robots.txt") self.assertEqual(response.body, - utf8(self.get_url("/") + "static/robots.txt?v=f71d2")) + utf8(self.get_url("/") + + "static/robots.txt?v=" + + self.robots_txt_hash)) def test_include_host_override(self): self._trigger_include_host_check(False) @@ -855,6 +862,11 @@ class StaticFileTest(WebTestCase): 'If-Modified-Since': format_timestamp(stat.st_mtime + 1)}) self.assertEqual(response.code, 304) + def test_static_etag(self): + response = self.fetch('/static/robots.txt') + self.assertEqual(response.headers.get("Etag"), + b'"%s"' %(self.robots_txt_hash, )) + @wsgi_safe class CustomStaticFileTest(WebTestCase): diff --git a/tornado/web.py b/tornado/web.py index af5ea5127..8b4d31738 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -1822,7 +1822,7 @@ class StaticFileHandler(RequestHandler): hashes[abs_path] = None hsh = hashes.get(abs_path) if hsh: - return hsh[:5] + return hsh return None def parse_url_path(self, url_path): @@ -1836,6 +1836,15 @@ class StaticFileHandler(RequestHandler): url_path = url_path.replace("/", os.path.sep) return url_path + def compute_etag(self): + # Note: compute the etag for static files using get_version so that the + # entire file is always considered, even when the request includes a + # Range header, so the response will only be a portion of the file. + version_hash = self.get_version(self.settings, self.path_args[0]) + if not version_hash: + return None + return '"%s"' %(version_hash, ) + class FallbackHandler(RequestHandler): """A `RequestHandler` that wraps another HTTP server callback.