Keep original host on IDNA decode errors

This commit is contained in:
mimi89999 2025-11-14 12:27:42 +01:00
parent def4778d62
commit 658353de06
No known key found for this signature in database
GPG Key ID: 4B054AC4922D7B18
2 changed files with 14 additions and 1 deletions

View File

@ -170,6 +170,7 @@ class URL:
"""
The URL host as a string.
Always normalized to lowercase, with IDNA hosts decoded into unicode.
Invalid IDNA encodings are kept in their original xn-- form.
Examples:
@ -182,13 +183,19 @@ class URL:
url = httpx.URL("http://xn--fiqs8s.icom.museum")
assert url.host == "中国.icom.museum"
url = httpx.URL("https://xn--ls8h.la/")
assert url.host == "xn--ls8h.la"
url = httpx.URL("https://[::ffff:192.168.0.1]")
assert url.host == "::ffff:192.168.0.1"
"""
host: str = self._uri_reference.host
if host.startswith("xn--"):
host = idna.decode(host)
try:
host = idna.decode(host)
except idna.IDNAError:
pass
return host

View File

@ -802,6 +802,12 @@ def test_url_invalid_idna_host():
assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"
def test_url_invalid_idna_encoding_kept():
url = httpx.URL("https://xn--ls8h.la/")
assert url.host == "xn--ls8h.la"
assert url.raw_host == b"xn--ls8h.la"
# Tests for IPv4 hostname support.