Skip to content

Commit f2ffe62

Browse files
committed
few fixes around parse_host, add a bunch of ipv6 test cases now that we're doing that in regex
1 parent 22550ad commit f2ffe62

File tree

4 files changed

+523
-7
lines changed

4 files changed

+523
-7
lines changed

hyperlink/_url_codecs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ class URLParseError(ValueError):
2525

2626
# The following is based on Ian Cordasco's rfc3986 package
2727

28+
# TODO: This pattern isn't perfect, so we double check with inet_pton
29+
# below, this will have to change for windows
2830
IPv4_PATT = '([0-9]{1,3}\.){3}[0-9]{1,3}'
2931
IPv4_RE = re.compile(IPv4_PATT)
3032
# Hexadecimal characters used in each piece of an IPv6 address
3133
HEXDIG_PATT = '[0-9A-Fa-f]{1,4}'
3234
# Least-significant 32 bits of an IPv6 address
33-
LS32_RE = '(%(hex)s:%(hex)s|%(ipv4)s)' % {'hex': HEXDIG_PATT, 'ipv4': IPv4_PATT}
35+
LS32_PATT = '(%(hex)s:%(hex)s|%(ipv4)s)' % {'hex': HEXDIG_PATT, 'ipv4': IPv4_PATT}
3436
# Substitutions into the following patterns for IPv6 patterns defined
3537
# http://tools.ietf.org/html/rfc3986#page-20
36-
_subs = {'hex': HEXDIG_PATT, 'ls32': LS32_RE}
38+
_subs = {'hex': HEXDIG_PATT, 'ls32': LS32_PATT}
3739

3840
# Below: h16 = hexdig, see: https://tools.ietf.org/html/rfc5234 for details
3941
# about ABNF (Augmented Backus-Naur Form) use in the comments
@@ -73,7 +75,7 @@ class URLParseError(ValueError):
7375
ZONE_ID_PATT = '(?:[' + UNRESERVED_CHAR_PATT + ']|' + PERCENT_ENCODED_PATT + ')+'
7476
IPv6_ADDRZ_PATT = IPv6_PATT + '%25' + ZONE_ID_PATT
7577

76-
IP_LITERAL_PATT = ('(%s|(?:%s)|%s)'
78+
IP_LITERAL_PATT = ('^(%s|(?:%s)|%s)\Z'
7779
% (IPv6_PATT, IPv6_ADDRZ_PATT, IPv_FUTURE_PATT))
7880

7981

@@ -103,13 +105,11 @@ def parse_host(host):
103105
ipv6_match = _IP_LITERAL_RE.match(host)
104106
if ipv6_match is None:
105107
raise URLParseError(u'invalid IPv6 host: %r' % host)
106-
if host.startswith('2001'):
107-
import pdb;pdb.set_trace()
108108
ipv4_match = IPv4_RE.search(host)
109109
if ipv4_match:
110110
try:
111111
socket.inet_pton(socket.AF_INET, ipv4_match.group(0))
112-
except socket.error as se: # socket.error _is_ OSError on Py3
112+
except socket.error: # NB: socket.error _is_ OSError on Py3
113113
raise URLParseError(u'invalid IPv6 host with IPv4: %r' % host)
114114
return socket.AF_INET6, host
115115
try:

0 commit comments

Comments
 (0)