]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add ability to parse static path before using it as though it was
authorBirk Nilson <me@birknilson.se>
Fri, 2 Dec 2011 14:28:31 +0000 (14:28 +0000)
committerBirk Nilson <me@birknilson.se>
Fri, 2 Dec 2011 14:28:31 +0000 (14:28 +0000)
a relative filesystem path. Thereby enabling developers to add the versioning
string as a component in the path rather than a query string. Which is required
when working with CloudFront for instance.

tornado/test/web_test.py
tornado/web.py

index c5ae99a37784007031676824581f7677bab0953b..bef6f529adcc044c52b5037eb12fd8389df060af 100644 (file)
@@ -552,12 +552,24 @@ class CustomStaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         class MyStaticFileHandler(StaticFileHandler):
             def get(self, path):
+                path = self.parse_url_path(path)
                 assert path == "foo.txt"
                 self.write("bar")
 
             @classmethod
             def make_static_url(cls, settings, path):
-                return "/static/%s?v=42" % path
+                version_hash = cls.get_version(settings, path)
+                extension_index = path.rindex('.')
+                before_version = path[:extension_index]
+                after_version = path[(extension_index + 1):]
+                return '/static/%s.%s.%s' % (before_version, 42, after_version)
+
+            @classmethod
+            def parse_url_path(cls, url_path):
+                extension_index = url_path.rindex('.')
+                version_index = url_path.rindex('.', 0, extension_index)
+                return '%s%s' % (url_path[:version_index],
+                                 url_path[extension_index:])
 
         class StaticUrlHandler(RequestHandler):
             def get(self, path):
@@ -568,9 +580,9 @@ class CustomStaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
                            static_handler_class=MyStaticFileHandler)
 
     def test_serve(self):
-        response = self.fetch("/static/foo.txt")
+        response = self.fetch("/static/foo.42.txt")
         self.assertEqual(response.body, b("bar"))
 
     def test_static_url(self):
         response = self.fetch("/static_url/foo.txt")
-        self.assertEqual(response.body, b("/static/foo.txt?v=42"))
+        self.assertEqual(response.body, b("/static/foo.42.txt"))
index d6a82d5640c46143743b476bcfbf64aefc1851b0..5a25acdf6794b4208b728f6c0110bee0f81bfc66 100644 (file)
@@ -1446,6 +1446,7 @@ class StaticFileHandler(RequestHandler):
     def get(self, path, include_body=True):
         if os.path.sep != "/":
             path = path.replace("/", os.path.sep)
+        path = self.parse_url_path(path)
         abspath = os.path.abspath(os.path.join(self.root, path))
         # os.path.abspath strips a trailing /
         # it needs to be temporarily added back for requests to root/
@@ -1558,6 +1559,10 @@ class StaticFileHandler(RequestHandler):
                 return hsh[:5]
         return None
 
+    @classmethod
+    def parse_url_path(cls, url_path):
+        return url_path
+
 
 class FallbackHandler(RequestHandler):
     """A RequestHandler that wraps another HTTP server callback.