From 2c3679566194b09cf009f3475effd7fb7581d8ce Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Sat, 3 Mar 2018 17:44:32 -0500 Subject: [PATCH 1/2] Correctly handle RFC2732 IPv6 addresses with ports --- boltons/urlutils.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/boltons/urlutils.py b/boltons/urlutils.py index c96ec08..88e3760 100644 --- a/boltons/urlutils.py +++ b/boltons/urlutils.py @@ -892,16 +892,19 @@ def parse_url(url_text): if hostinfo: host, sep, port_str = hostinfo.partition(u':') if sep: - if u']' in port_str: - host = hostinfo # wrong split, was an ipv6 - else: - try: - port = int(port_str) - except ValueError: - if port_str: # empty ports ok according to RFC 3986 6.2.3 - raise URLParseError('expected integer for port, not %r' - % port_str) - port = None + if host[0] == u'[' and u']' in port_str: + host_right, _, port_str = port_str.partition(u']') + host = host + u':' + host_right + u']' + if port_str and port_str[0] == u':': + port_str = port_str[1:] + + try: + port = int(port_str) + except ValueError: + if port_str: # empty ports ok according to RFC 3986 6.2.3 + raise URLParseError('expected integer for port, not %r' + % port_str) + port = None family, host = parse_host(host) From d4279cfb18f2d5f0bdefda9a285903bee4a23eee Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Sat, 3 Mar 2018 18:02:06 -0500 Subject: [PATCH 2/2] Fix a bug where the host is an empty string --- boltons/urlutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boltons/urlutils.py b/boltons/urlutils.py index 88e3760..42adb09 100644 --- a/boltons/urlutils.py +++ b/boltons/urlutils.py @@ -892,7 +892,7 @@ def parse_url(url_text): if hostinfo: host, sep, port_str = hostinfo.partition(u':') if sep: - if host[0] == u'[' and u']' in port_str: + if host and host[0] == u'[' and u']' in port_str: host_right, _, port_str = port_str.partition(u']') host = host + u':' + host_right + u']' if port_str and port_str[0] == u':':