Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c4e1ae

Browse files
committed
Merged fix for #10860 from 3.2
2 parents 55c7e00 + a5a9a9c commit 6c4e1ae

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

Lib/http/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,10 @@ def _set_hostport(self, host, port):
678678
try:
679679
port = int(host[i+1:])
680680
except ValueError:
681-
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
681+
if host[i+1:] == "": # http://foo.com:/ == http://foo.com/
682+
port = self.default_port
683+
else:
684+
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
682685
host = host[:i]
683686
else:
684687
port = self.default_port

Lib/test/test_httplib.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,16 @@ def test_partial_reads(self):
161161
def test_host_port(self):
162162
# Check invalid host_port
163163

164-
for hp in ("www.python.org:abc", "www.python.org:"):
164+
for hp in ("www.python.org:abc", "user:password@www.python.org"):
165165
self.assertRaises(client.InvalidURL, client.HTTPConnection, hp)
166166

167167
for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
168168
"fe80::207:e9ff:fe9b", 8000),
169169
("www.python.org:80", "www.python.org", 80),
170+
("www.python.org:", "www.python.org", 80),
170171
("www.python.org", "www.python.org", 80),
171-
("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
172+
("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80),
173+
("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", 80)):
172174
c = client.HTTPConnection(hp)
173175
self.assertEqual(h, c.host)
174176
self.assertEqual(p, c.port)
@@ -539,6 +541,24 @@ def test_local_bad_hostname(self):
539541
self.assertEqual(resp.status, 404)
540542
del server
541543

544+
def test_host_port(self):
545+
# Check invalid host_port
546+
547+
for hp in ("www.python.org:abc", "user:[email protected]"):
548+
self.assertRaises(client.InvalidURL, client.HTTPSConnection, hp)
549+
550+
for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
551+
"fe80::207:e9ff:fe9b", 8000),
552+
("www.python.org:443", "www.python.org", 443),
553+
("www.python.org:", "www.python.org", 443),
554+
("www.python.org", "www.python.org", 443),
555+
("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443),
556+
("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b",
557+
443)):
558+
c = client.HTTPSConnection(hp)
559+
self.assertEqual(h, c.host)
560+
self.assertEqual(p, c.port)
561+
542562

543563
class RequestBodyTest(TestCase):
544564
"""Test cases where a request includes a message body."""

0 commit comments

Comments
 (0)