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

Skip to content

Commit a5a9a9c

Browse files
committed
Fixes python#10860: Handle empty port after port delimiter in httplib
1 parent 551ba20 commit a5a9a9c

3 files changed

Lines changed: 29 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
resp = h.getresponse()
540542
self.assertEqual(resp.status, 404)
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."""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ Library
137137
- Issue #12650: Fix a race condition where a subprocess.Popen could leak
138138
resources (FD/zombie) when killed at the wrong time.
139139

140+
- Issue #10860: http.client now correctly handles an empty port after port
141+
delimiter in URLs.
142+
140143
Tests
141144
-----
142145

0 commit comments

Comments
 (0)