Skip to content

Commit

Permalink
Ensure ascii host case normalization happens before validation
Browse files Browse the repository at this point in the history
fixes #1441

regression was introduced in #954
  • Loading branch information
bdraco committed Nov 30, 2024
1 parent 65d3aed commit 1e9e9fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/test_url_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,11 @@ def test_build_with_none_query_string():
def test_build_with_none_fragment():
with pytest.raises(TypeError):
URL.build(scheme="http", host="example.com", fragment=None)


def test_build_uppercase_host():
u = URL.build(
host="UPPER.case",
encoded=False,
)
assert u.host == "upper.case"
3 changes: 2 additions & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ def _encode_host(host: str, validate_host: bool) -> str:
if host.isascii():
# Check for invalid characters explicitly; _idna_encode() does this
# for non-ascii host names.
host = host.lower()
if validate_host and (invalid := NOT_REG_NAME.search(host)):
value, pos, extra = invalid.group(), invalid.start(), ""
if value == "@" or (value == ":" and "@" in host[pos:]):
Expand All @@ -1510,7 +1511,7 @@ def _encode_host(host: str, validate_host: bool) -> str:
raise ValueError(
f"Host {host!r} cannot contain {value!r} (at position {pos}){extra}"
) from None
return host.lower()
return host

return _idna_encode(host)

Expand Down

0 comments on commit 1e9e9fa

Please sign in to comment.