]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
move code to seperate function 'split_host_and_port' 1261/head
author依云 <lilydjwg@gmail.com>
Sun, 14 Dec 2014 10:12:26 +0000 (18:12 +0800)
committer依云 <lilydjwg@gmail.com>
Sun, 14 Dec 2014 10:30:05 +0000 (18:30 +0800)
tornado/httputil.py
tornado/simple_httpclient.py
tornado/web.py

index f5c9c04fea3e197dafaca2329a797b7ee311fb4a..a3745569cac7cfe85745e1d7ae408d2a413ec232 100644 (file)
@@ -873,3 +873,17 @@ def _encode_header(key, pdict):
 def doctests():
     import doctest
     return doctest.DocTestSuite()
+
+def split_host_and_port(netloc):
+    """Returns ``(host, port)`` tuple from ``netloc``.
+
+    Returned ``port`` will be ``None`` if not present.
+    """
+    match = re.match(r'^(.+):(\d+)$', netloc)
+    if match:
+        host = match.group(1)
+        port = int(match.group(2))
+    else:
+        host = netloc
+        port = None
+    return (host, port)
index e60c434f8e05cf70a357febece10b915d36d7b88..7c915e90a6abf863a095902464841ae9037df489 100644 (file)
@@ -193,12 +193,8 @@ class _HTTPConnection(httputil.HTTPMessageDelegate):
             netloc = self.parsed.netloc
             if "@" in netloc:
                 userpass, _, netloc = netloc.rpartition("@")
-            match = re.match(r'^(.+):(\d+)$', netloc)
-            if match:
-                host = match.group(1)
-                port = int(match.group(2))
-            else:
-                host = netloc
+            host, port = httputil.split_host_and_port(netloc)
+            if port is None:
                 port = 443 if self.parsed.scheme == "https" else 80
             if re.match(r'^\[.*\]$', host):
                 # raw ipv6 addresses in urls are enclosed in brackets
index 91147b6891af2d6f6766e124dbf60bdf4036f0ed..71610b95e8843c5613fadda379a05b152961083e 100644 (file)
@@ -85,6 +85,7 @@ from tornado import stack_context
 from tornado import template
 from tornado.escape import utf8, _unicode
 from tornado.util import import_object, ObjectDict, raise_exc_info, unicode_type, _websocket_mask
+from tornado.httputil import split_host_and_port
 
 
 try:
@@ -1729,9 +1730,7 @@ class Application(httputil.HTTPServerConnectionDelegate):
         self.transforms.append(transform_class)
 
     def _get_host_handlers(self, request):
-        host = request.host.lower()
-        if not host.endswith(']'): # excluding IPv6 addresses without port
-            host = host.rsplit(':', 1)[0]
+        host = split_host_and_port(request.host.lower())[0]
         matches = []
         for pattern, handlers in self.handlers:
             if pattern.match(host):