Skip to content

Commit a1b8a20

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

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-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

0 commit comments

Comments
 (0)