Skip to content
Browse files

Merge remote-tracking branch 'birknilson/static_url_override' into work

  • Loading branch information...
2 parents 3bb80eb + 9484eb0 commit 7f7928f2eee30d1d4191cabc0bb41436c6bfdbd8 @bdarnell bdarnell committed
Showing with 42 additions and 4 deletions.
  1. +32 −1 tornado/test/web_test.py
  2. +10 −3 tornado/web.py
View
33 tornado/test/web_test.py
@@ -547,8 +547,30 @@ class AbsoluteStaticUrlHandler(RequestHandler):
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):
@@ -567,6 +589,15 @@ def test_absolute_static_url(self):
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):
View
13 tornado/web.py
@@ -886,7 +886,7 @@ def xsrf_form_html(self):
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
@@ -901,12 +901,19 @@ def static_url(self, path):
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 = ""

0 comments on commit 7f7928f

Please sign in to comment.
Something went wrong with that request. Please try again.