]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
backport ipv6 address fix
authorSkip Montanaro <skip@pobox.com>
Thu, 16 Sep 2004 03:31:21 +0000 (03:31 +0000)
committerSkip Montanaro <skip@pobox.com>
Thu, 16 Sep 2004 03:31:21 +0000 (03:31 +0000)
Lib/httplib.py
Lib/test/test_httplib.py

index 40fad26c874cd5f8bac0e63d834f4d03885095fc..0b6a9d2db3162bcda269647eb34c266c7543d331 100644 (file)
@@ -510,8 +510,9 @@ class HTTPConnection:
 
     def _set_hostport(self, host, port):
         if port is None:
-            i = host.find(':')
-            if i >= 0:
+            i = host.rfind(':')
+            j = host.rfind(']')         # ipv6 addresses have [...]
+            if i > j:
                 try:
                     port = int(host[i+1:])
                 except ValueError:
@@ -519,6 +520,8 @@ class HTTPConnection:
                 host = host[:i]
             else:
                 port = self.default_port
+            if host[0] == '[' and host[-1] == ']':
+                host = host[1:-1]
         self.host = host
         self.port = port
 
index c57793ded2f14b276c5f32826ac9d2d45770bcbe..1676873c9657598a56e4bdc4eb698ad96de15dfd 100644 (file)
@@ -80,6 +80,18 @@ def _test():
         else:
             print "Expect InvalidURL"
 
+    for hp,h,p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", 8000),
+                   ("www.python.org:80", "www.python.org", 80),
+                   ("www.python.org", "www.python.org", 80),
+                   ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
+        try:
+            http = httplib.HTTP(hp)
+        except httplib.InvalidURL:
+            print "InvalidURL raised erroneously"
+        c = http._conn
+        if h != c.host: raise AssertionError, ("Host incorrectly parsed", h, c.host)
+        if p != c.port: raise AssertionError, ("Port incorrectly parsed", p, c.host)
+
     # test response with multiple message headers with the same field name.
     text = ('HTTP/1.1 200 OK\r\n'
             'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r\n'