]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Allow add_handlers() to append to existing hosts.
authorJon Parise <jon@indelible.org>
Wed, 7 Nov 2012 01:33:24 +0000 (17:33 -0800)
committerJon Parise <jon@indelible.org>
Wed, 7 Nov 2012 01:33:24 +0000 (17:33 -0800)
This change removes the restriction that all handlers for a given host
pattern be registered in a single call to add_handlers().

tornado/web.py

index 68bf78b1672d398b6164edff2fbe41fc185f9f45..e1e2e75995755d87ca290dd067b8bc6672cc54a3 100644 (file)
@@ -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(()):