Skip to content

Commit

Permalink
Support for IDNA / Punycode Unicode encoding for host names.
Browse files Browse the repository at this point in the history
Unicode version is natively stored, accessible via `URI.host`, however string and repr will present the IDNA-encoded versions. Tests TBD.
  • Loading branch information
amcgregor committed Mar 25, 2021
1 parent 30dbbc4 commit 9c6fce3
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions uri/part/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class HostPart(ProxyPart):
def render(self, obj, value):
result = super(HostPart, self).render(obj, value)

try:
result.encode('ascii')
except UnicodeEncodeError:
result = result.encode('idna').decode('ascii')

if result:
try: # Identify and armour IPv6 address literals.
inet_pton(AF_INET6, value)
Expand All @@ -20,3 +25,11 @@ def render(self, obj, value):
result = '[' + result + ']'

return result

def __set__(self, obj, value):
if isinstance(value, bytes):
value = value.decode('idna')
elif value.startswith('xn--'):
value = value.encode('ascii').decode('idna')

super().__set__(obj, value)

1 comment on commit 9c6fce3

@amcgregor
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's transparent and automatic, permitting both use/assignment of true Unicode strings, as well as use/assignment of already IDNA-encoded byte or Unicode strings.

Screen Shot 2021-04-01 at 10 39 25

Please sign in to comment.