From: Birk Nilson Date: Wed, 30 Nov 2011 20:51:33 +0000 (+0000) Subject: Allow override of include_host in static_url. X-Git-Tag: v2.2.0~12^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9484eb08cd9944f09f2dd4d961c466c499d8bb01;p=thirdparty%2Ftornado.git Allow override of include_host in static_url. 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. --- diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index c5ae99a37..69bff6c68 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -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): diff --git a/tornado/web.py b/tornado/web.py index c7801a4fe..7ff1326db 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -862,7 +862,7 @@ class RequestHandler(object): return '' - 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 = ""