Preserve non-default ports in Host headers (#6171)

Co-authored-by: cuteNBD <[email protected]>
This commit is contained in:
cuteNBDandcuteNBD authored and GitHub committed 2026-11-03 22:03:38 +01:00
1 parent c15f4bf055
commit 3130623afc
2 files changed
+20 -5

No files matched your search

+5 -5
View File
@@ -5138,12 +5138,12 @@ def getHostHeader(url):
retVal = url
if url:
retVal = _urllib.parse.urlparse(url).netloc
parsed = _urllib.parse.urlparse(url)
retVal = parsed.netloc
defaultPort = {"http": 80, "https": 443}.get(parsed.scheme.lower())
# Note: netloc keeps the IPv6 brackets (and any port), so only the default ports are
# stripped here - mirroring the hostname/IPv4 branch and preserving non-default ports
# (e.g. '[::1]:8080') as required by RFC 7230
if any(retVal.endswith(':%d' % _) for _ in (80, 443)):
# Keep non-default ports, including 80 on HTTPS and 443 on HTTP.
if defaultPort is not None and retVal.endswith(':%d' % defaultPort):
retVal = retVal[:retVal.rfind(':')]
if retVal and retVal.count(':') > 1 and not any(_ in retVal for _ in ('[', ']')):
+15
View File
@@ -70,6 +70,21 @@ class TestGetHostHeader(unittest.TestCase):
def test_without_port(self):
self.assertEqual(getHostHeader("http://example.com/path"), "example.com")
def test_default_ports_depend_on_scheme(self):
cases = (
("http://example.com:80/path", "example.com"),
("https://example.com:443/path", "example.com"),
("http://example.com:443/path", "example.com:443"),
("https://example.com:80/path", "example.com:80"),
("HTTP://example.com:443/path", "example.com:443"),
("http://[::1]:80/path", "[::1]"),
("https://[::1]:443/path", "[::1]"),
("http://[::1]:443/path", "[::1]:443"),
("https://[::1]:80/path", "[::1]:80"),
)
for url, expected in cases:
self.assertEqual(getHostHeader(url), expected, msg=url)
if __name__ == "__main__":
unittest.main(verbosity=2)