From: 依云 Date: Sun, 14 Dec 2014 10:12:26 +0000 (+0800) Subject: move code to seperate function 'split_host_and_port' X-Git-Tag: v4.1.0b1~49^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1261%2Fhead;p=thirdparty%2Ftornado.git move code to seperate function 'split_host_and_port' --- diff --git a/tornado/httputil.py b/tornado/httputil.py index f5c9c04fe..a3745569c 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -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) diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index e60c434f8..7c915e90a 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -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 diff --git a/tornado/web.py b/tornado/web.py index 91147b689..71610b95e 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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):