summaryrefslogtreecommitdiff
path: root/Lib/urlparse.py
diff options
context:
space:
mode:
authorSenthil Kumaran <orsenthil@gmail.com>2010-04-16 02:46:46 +0000
committerSenthil Kumaran <orsenthil@gmail.com>2010-04-16 02:46:46 +0000
commit8c6d9d7c8d6082e2168f2756c12638284d0f9206 (patch)
tree05a28701b5677b459cda9931be1783ad6e237cad /Lib/urlparse.py
parentb7b7c77eb3c62bab5dde55d5ad802df4c90645d5 (diff)
downloadcpython-git-8c6d9d7c8d6082e2168f2756c12638284d0f9206.tar.gz
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Diffstat (limited to 'Lib/urlparse.py')
-rw-r--r--Lib/urlparse.py32
1 files changed, 20 insertions, 12 deletions
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
index f6e20afdb8..c169f38004 100644
--- a/Lib/urlparse.py
+++ b/Lib/urlparse.py
@@ -64,22 +64,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
@@ -124,6 +128,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