diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2010-04-16 03:06:19 (GMT) |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2010-04-16 03:06:19 (GMT) |
commit | 2176ad5f304880bcc2341106ba4ed943ded8a120 (patch) | |
tree | 09c0b5e01bb06ccc2608f29827dfe68454a2f812 /Lib/urllib/parse.py | |
parent | 0e3e48545295d23e2dae081a01b6e62fb2dfe65a (diff) | |
download | cpython-2176ad5f304880bcc2341106ba4ed943ded8a120.zip cpython-2176ad5f304880bcc2341106ba4ed943ded8a120.tar.gz cpython-2176ad5f304880bcc2341106ba4ed943ded8a120.tar.bz2 |
Merged revisions 80102 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r80102 | senthil.kumaran | 2010-04-16 08:32:13 +0530 (Fri, 16 Apr 2010) | 9 lines
Merged revisions 80101 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r80101 | senthil.kumaran | 2010-04-16 08:16:46 +0530 (Fri, 16 Apr 2010) | 3 lines
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
........
................
Diffstat (limited to 'Lib/urllib/parse.py')
-rw-r--r-- | Lib/urllib/parse.py | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index c1ae5ff..1affc69 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -69,22 +69,26 @@ class ResultMixin(object): @property def hostname(self): - netloc = self.netloc - if "@" in netloc: - netloc = netloc.rsplit("@", 1)[1] - if ":" in netloc: - netloc = netloc.split(":", 1)[0] - return netloc.lower() or None + netloc = self.netloc.split('@')[-1] + if '[' in netloc and ']' in netloc: + return netloc.split(']')[0][1:].lower() + elif '[' in netloc or ']' in netloc: + raise ValueError("Invalid IPv6 hostname") + elif ':' in netloc: + return netloc.split(':')[0].lower() + elif netloc == '': + return None + else: + return netloc.lower() @property def port(self): - netloc = self.netloc - if "@" in netloc: - netloc = netloc.rsplit("@", 1)[1] - if ":" in netloc: - port = netloc.split(":", 1)[1] + netloc = self.netloc.split('@')[-1].split(']')[-1] + if ':' in netloc: + port = netloc.split(':')[1] return int(port, 10) - return None + else: + return None from collections import namedtuple @@ -129,6 +133,10 @@ def _splitparams(url): def _splitnetloc(url, start=0): delim = len(url) # position of end of domain part of url, default is end + if '[' in url: # check for invalid IPv6 URL + if not ']' in url: raise ValueError("Invalid IPv6 URL") + elif ']' in url: + if not '[' in url: raise ValueError("Invalid IPv6 URL") for c in '/?#': # look for delimiters; the order is NOT important wdelim = url.find(c, start) # find first of this delim if wdelim >= 0: # if found |