Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion httpx/_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def host(self) -> str:
"""
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:

Expand All @@ -182,13 +183,19 @@ def host(self) -> str:
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

Expand Down
6 changes: 6 additions & 0 deletions tests/models/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down