Keep original host on IDNA decode errors
This commit is contained in:
parent
def4778d62
commit
658353de06
@ -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
|
||||
|
||||
|
||||
@ -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.
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user