Skip to content

Commit 658353d

Browse files
committed
Keep original host on IDNA decode errors
1 parent def4778 commit 658353d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

httpx/_urls.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def host(self) -> str:
170170
"""
171171
The URL host as a string.
172172
Always normalized to lowercase, with IDNA hosts decoded into unicode.
173+
Invalid IDNA encodings are kept in their original xn-- form.
173174
174175
Examples:
175176
@@ -182,13 +183,19 @@ def host(self) -> str:
182183
url = httpx.URL("http://xn--fiqs8s.icom.museum")
183184
assert url.host == "中国.icom.museum"
184185
186+
url = httpx.URL("https://xn--ls8h.la/")
187+
assert url.host == "xn--ls8h.la"
188+
185189
url = httpx.URL("https://[::ffff:192.168.0.1]")
186190
assert url.host == "::ffff:192.168.0.1"
187191
"""
188192
host: str = self._uri_reference.host
189193

190194
if host.startswith("xn--"):
191-
host = idna.decode(host)
195+
try:
196+
host = idna.decode(host)
197+
except idna.IDNAError:
198+
pass
192199

193200
return host
194201

tests/models/test_url.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,12 @@ def test_url_invalid_idna_host():
802802
assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"
803803

804804

805+
def test_url_invalid_idna_encoding_kept():
806+
url = httpx.URL("https://xn--ls8h.la/")
807+
assert url.host == "xn--ls8h.la"
808+
assert url.raw_host == b"xn--ls8h.la"
809+
810+
805811
# Tests for IPv4 hostname support.
806812

807813

0 commit comments

Comments
 (0)