]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Allow override of include_host in static_url. 406/head
authorBirk Nilson <me@birknilson.se>
Wed, 30 Nov 2011 20:51:33 +0000 (20:51 +0000)
committerBirk Nilson <me@birknilson.se>
Wed, 30 Nov 2011 20:51:33 +0000 (20:51 +0000)
Thereby supporting the generation of absolute URLs even though the
handler - by default - would have generated a relative URL for instance.
The reverse scenario is of course supported also.

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

index c5ae99a37784007031676824581f7677bab0953b..69bff6c6810dff526d9cf5ba9ce5db6a46b9690a 100644 (file)
@@ -528,8 +528,30 @@ class StaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
             def get(self, path):
                 self.write(self.static_url(path))
 
+        class OverrideStaticUrlHandler(RequestHandler):
+            def get(self, path):
+                do_include = bool(self.get_argument("include_host"))
+                self.include_host = not do_include
+
+                regular_url = self.static_url(path)
+                override_url = self.static_url(path, include_host=do_include)
+                if override_url == regular_url:
+                    return self.write(str(False))
+
+                protocol = self.request.protocol + "://"
+                protocol_length = len(protocol)
+                check_regular = regular_url.find(protocol, 0, protocol_length)
+                check_override = override_url.find(protocol, 0, protocol_length)
+
+                if do_include:
+                    result = (check_override == 0 and check_regular == -1)
+                else:
+                    result = (check_override == -1 and check_regular == 0)
+                self.write(str(result))
+
         return Application([('/static_url/(.*)', StaticUrlHandler),
-                            ('/abs_static_url/(.*)', AbsoluteStaticUrlHandler)],
+                            ('/abs_static_url/(.*)', AbsoluteStaticUrlHandler),
+                            ('/override_static_url/(.*)', OverrideStaticUrlHandler)],
                            static_path=os.path.join(os.path.dirname(__file__), 'static'))
 
     def test_static_files(self):
@@ -548,6 +570,15 @@ class StaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
         self.assertEqual(response.body,
                          utf8(self.get_url("/") + "static/robots.txt?v=f71d2"))
 
+    def test_include_host_override(self):
+        self._trigger_include_host_check(False)
+        self._trigger_include_host_check(True)
+
+    def _trigger_include_host_check(self, include_host):
+        path = "/override_static_url/robots.txt?include_host=%s"
+        response = self.fetch(path % int(include_host))
+        self.assertEqual(response.body, utf8(str(True)))
+
 class CustomStaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         class MyStaticFileHandler(StaticFileHandler):
index c7801a4fefb2d3a93b5a18451017a15e90b362f5..7ff1326dbed3a578f77e72d46616060d37101d62 100644 (file)
@@ -862,7 +862,7 @@ class RequestHandler(object):
         return '<input type="hidden" name="_xsrf" value="' + \
             escape.xhtml_escape(self.xsrf_token) + '"/>'
 
-    def static_url(self, path):
+    def static_url(self, path, include_host=None):
         """Returns a static URL for the given relative static file path.
 
         This method requires you set the 'static_path' setting in your
@@ -877,12 +877,19 @@ class RequestHandler(object):
         If this handler has a "include_host" attribute, we include the
         full host for every static URL, including the "http://". Set
         this attribute for handlers whose output needs non-relative static
-        path names.
+        path names. However, in case the "include_host" argument to this
+        method is given a value other than None it will override the
+        attribute value when determening whether to generate a relative
+        or absolute URL.
         """
         self.require_setting("static_path", "static_url")
         static_handler_class = self.settings.get(
             "static_handler_class", StaticFileHandler)
-        if getattr(self, "include_host", False):
+
+        if include_host is None:
+            include_host = getattr(self, "include_host", False)
+
+        if include_host:
             base = self.request.protocol + "://" + self.request.host
         else:
             base = ""