]> granicus.if.org Git - python/commitdiff
Issue2987 - Added additional Invalid URL and changed the Invalid URL checking code...
authorSenthil Kumaran <orsenthil@gmail.com>
Tue, 20 Apr 2010 20:37:59 +0000 (20:37 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Tue, 20 Apr 2010 20:37:59 +0000 (20:37 +0000)
Lib/test/test_urlparse.py
Lib/urlparse.py

index 4085de7bf5b35b3a2497afb4604d043792715fa9..c25b6e0dd74d7fb455aeeb6ab6139dbf001ce122 100644 (file)
@@ -272,6 +272,7 @@ class UrlParseTestCase(unittest.TestCase):
         for invalid_url in [
                 'http://::12.34.56.78]/',
                 'http://[::1/foo/',
+                'http://[::1/foo/bad]/bad',
                 'http://[::ffff:12.34.56.78']:
             self.assertRaises(ValueError, lambda : urlparse.urlparse(invalid_url).hostname)
             self.assertRaises(ValueError, lambda : urlparse.urlparse(invalid_url))
index 8c37dff50ca21919f2d33eeb18689d254be36f06..1a8151804d481bd468a957243a3b478599c8e934 100644 (file)
@@ -90,8 +90,6 @@ class ResultMixin(object):
         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 == '':
@@ -151,10 +149,6 @@ 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
@@ -182,6 +176,10 @@ def urlsplit(url, scheme='', allow_fragments=True):
             url = url[i+1:]
             if url[:2] == '//':
                 netloc, url = _splitnetloc(url, 2)
+                if '[' in netloc :
+                    if not ']' in netloc: raise ValueError("Invalid IPv6 URL")
+                if ']' in netloc:
+                    if not '[' in netloc: raise ValueError("Invalid IPv6 URL")
             if allow_fragments and '#' in url:
                 url, fragment = url.split('#', 1)
             if '?' in url:
@@ -197,6 +195,10 @@ def urlsplit(url, scheme='', allow_fragments=True):
 
     if url[:2] == '//':
         netloc, url = _splitnetloc(url, 2)
+        if '[' in netloc:
+            if not ']' in netloc: raise ValueError("Invalid IPv6 URL")
+        if ']' in netloc:
+            if not '[' in netloc: raise ValueError("Invalid IPv6 URL")
     if allow_fragments and scheme in uses_fragment and '#' in url:
         url, fragment = url.split('#', 1)
     if scheme in uses_query and '?' in url: