From a4ba918180ca292cc2f67a6793c8563cec0d1ac3 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Fri, 26 Mar 2010 15:55:04 -0700 Subject: [PATCH] Make add_handlers (for multiple hostnames) usable with static_path (which forces the creation of a wildcard handler) by maintaining a sensible order for handler groups. --- tornado/web.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tornado/web.py b/tornado/web.py index 4fa2aa301..072b261c4 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -934,7 +934,15 @@ class Application(object): if not host_pattern.endswith("$"): host_pattern += "$" handlers = [] - self.handlers.append((re.compile(host_pattern), handlers)) + # The handlers with the wildcard host_pattern are a special + # case - they're added in the constructor but should have lower + # precedence than the more-precise handlers added later. + # If a wildcard handler group exists, it should always be last + # in the list, so insert new groups just before it. + if self.handlers and self.handlers[-1][0].pattern == '.*$': + self.handlers.insert(-1, (re.compile(host_pattern), handlers)) + else: + self.handlers.append((re.compile(host_pattern), handlers)) for spec in host_handlers: if type(spec) is type(()): -- 2.47.2