Skip to content

Commit dd9111b

Browse files
authored
Don't unconditionally lowercase host parts (#52)
1 parent ba6a19a commit dd9111b

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/rfc3986/iri.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def from_string(cls, iri_string, encoding='utf-8'):
9494
encoding,
9595
)
9696

97-
def encode(self, idna_encoder=None):
97+
def encode(self, idna_encoder=None): # noqa: C901
9898
"""Encode an IRIReference into a URIReference instance.
9999
100100
If the ``idna`` module is installed or the ``rfc3986[idna]``
@@ -116,16 +116,20 @@ def encode(self, idna_encoder=None):
116116
"Could not import the 'idna' module "
117117
"and the IRI hostname requires encoding"
118118
)
119-
else:
120-
def idna_encoder(x):
119+
120+
def idna_encoder(name):
121+
if any(ord(c) > 128 for c in name):
121122
try:
122-
return idna.encode(x, strict=True, std3_rules=True)
123+
return idna.encode(name.lower(),
124+
strict=True,
125+
std3_rules=True)
123126
except idna.IDNAError:
124127
raise exceptions.InvalidAuthority(self.authority)
128+
return name
125129

126130
authority = ""
127131
if self.host:
128-
authority = ".".join([compat.to_str(idna_encoder(part.lower()))
132+
authority = ".".join([compat.to_str(idna_encoder(part))
129133
for part in self.host.split(".")])
130134

131135
if self.userinfo is not None:

tests/test_iri.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
(u'http://βόλος.com/β/ό?λ#ος', u'http://xn--nxasmm1c.com/%CE%B2/%CF%8C?%CE%BB#%CE%BF%CF%82'),
2020
(u'http://ශ්\u200dරී.com', u'http://xn--10cl1a0b660p.com'),
2121
(u'http://نامه\u200cای.com', u'http://xn--mgba3gch31f060k.com'),
22-
(u'http://Bü:ẞ@gOoGle.com', u'http://B%C3%BC:%E1%BA%9E@google.com'),
22+
(u'http://Bü:ẞ@gOoGle.com', u'http://B%C3%BC:%E1%BA%9E@gOoGle.com'),
2323
(u'http://ẞ.com:443', u'http://xn--zca.com:443'),
24-
(u'http://ẞ.foo.com', u'http://xn--zca.foo.com')
24+
(u'http://ẞ.foo.com', u'http://xn--zca.foo.com'),
25+
(u'http://Bẞ.com', u'http://xn--b-qfa.com'),
26+
(u'http+unix://%2Ftmp%2FTEST.sock/get', 'http+unix://%2Ftmp%2FTEST.sock/get'),
2527
]
2628
)
2729

@@ -49,8 +51,7 @@ def test_iri_equality_special_cases():
4951
@pytest.mark.parametrize("iri", [
5052
u'http://♥.net',
5153
u'http://\u0378.net',
52-
u'http://㛼.com',
53-
u'http://abc..def'
54+
u'http://㛼.com'
5455
])
5556
def test_encode_invalid_iri(iri):
5657
iri_ref = rfc3986.iri_reference(iri)

0 commit comments

Comments
 (0)