From: Jon Parise Date: Wed, 7 Nov 2012 01:33:24 +0000 (-0800) Subject: Allow add_handlers() to append to existing hosts. X-Git-Tag: v3.0.0~193^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=68f52c12d6eca66e9404f4e76b69f056f9ee5d43;p=thirdparty%2Ftornado.git Allow add_handlers() to append to existing hosts. This change removes the restriction that all handlers for a given host pattern be registered in a single call to add_handlers(). --- diff --git a/tornado/web.py b/tornado/web.py index 68bf78b16..e1e2e7599 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -1316,21 +1316,30 @@ class Application(object): Note that host patterns are processed sequentially in the order they were added, and only the first matching pattern is - used. This means that all handlers for a given host must be - added in a single add_handlers call. + used. """ if not host_pattern.endswith("$"): 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)) + + # Search for an existing handlers entry for this host pattern. + handlers = None + for entry in self.handlers: + if entry[0].pattern == host_pattern: + handlers = entry[1] + break + + # Otherwise, add a new handlers entry for this host pattern. + if handlers is None: + 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(()):