]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fixes #10860: Handle empty port after port delimiter in httplib
authorŁukasz Langa <lukasz@langa.pl>
Tue, 18 Oct 2011 19:17:39 +0000 (21:17 +0200)
committerŁukasz Langa <lukasz@langa.pl>
Tue, 18 Oct 2011 19:17:39 +0000 (21:17 +0200)
Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS

index 8400914fca9bec7fc717cc95a3e759bb40559d66..745b999942bc9935f4a539ef77a8d5999f0d96ca 100644 (file)
@@ -678,7 +678,10 @@ class HTTPConnection:
                 try:
                     port = int(host[i+1:])
                 except ValueError:
-                    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
+                    if host[i+1:] == "": # http://foo.com:/ == http://foo.com/
+                        port = self.default_port
+                    else:
+                        raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
                 host = host[:i]
             else:
                 port = self.default_port
index 4899ac7284be00a869454cf103cee6dfe99811bc..8a328a9882ef6cbf3e53e79e607728f9620d49ef 100644 (file)
@@ -161,14 +161,16 @@ class BasicTest(TestCase):
     def test_host_port(self):
         # Check invalid host_port
 
-        for hp in ("www.python.org:abc", "www.python.org:"):
+        for hp in ("www.python.org:abc", "user:password@www.python.org"):
             self.assertRaises(client.InvalidURL, client.HTTPConnection, hp)
 
         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),
                          ("www.python.org", "www.python.org", 80),
-                         ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
+                         ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80),
+                         ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", 80)):
             c = client.HTTPConnection(hp)
             self.assertEqual(h, c.host)
             self.assertEqual(p, c.port)
@@ -539,6 +541,24 @@ class HTTPSTest(TestCase):
         resp = h.getresponse()
         self.assertEqual(resp.status, 404)
 
+    def test_host_port(self):
+        # Check invalid host_port
+
+        for hp in ("www.python.org:abc", "user:password@www.python.org"):
+            self.assertRaises(client.InvalidURL, client.HTTPSConnection, hp)
+
+        for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
+                          "fe80::207:e9ff:fe9b", 8000),
+                         ("www.python.org:443", "www.python.org", 443),
+                         ("www.python.org:", "www.python.org", 443),
+                         ("www.python.org", "www.python.org", 443),
+                         ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443),
+                         ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b",
+                             443)):
+            c = client.HTTPSConnection(hp)
+            self.assertEqual(h, c.host)
+            self.assertEqual(p, c.port)
+
 
 class RequestBodyTest(TestCase):
     """Test cases where a request includes a message body."""
index dd31b4bb59f7133410dec8e4551bb2d2934b93aa..c9804fc265fe8000dddd61d9d1791c9b089909c9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,6 +137,9 @@ Library
 - Issue #12650: Fix a race condition where a subprocess.Popen could leak
   resources (FD/zombie) when killed at the wrong time.
 
+- Issue #10860: http.client now correctly handles an empty port after port
+  delimiter in URLs.
+
 Tests
 -----