Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def punycode_special_host(url):
if url.host and url.scheme in urlcanon.SPECIAL_SCHEMES:
# https://github.com/kjd/idna/issues/40#issuecomment-285496926
try:
url.host = idna.encode(url.host.decode('utf-8'), uts46=True)
except:
try:
remapped = idna.uts46_remap(url.host.decode('utf-8'))
labels = remapped.split('.')
punycode_labels = [idna2003.ToASCII(label) for label in labels]
url.host = b'.'.join(punycode_labels)
except:
pass
"""
Clean the fully qualified name, as defined in ENS `EIP-137
`_
This does *not* enforce whether ``name`` is a label or fully qualified domain.
:param str name: the dot-separated ENS name
:raises InvalidName: if ``name`` has invalid syntax
"""
if not name:
return name
elif isinstance(name, (bytes, bytearray)):
name = name.decode('utf-8')
try:
return idna.uts46_remap(name, std3_rules=True)
except idna.IDNAError as exc:
raise InvalidName(f"{name} is an invalid name, because {exc}") from exc
def encode(self, label):
if label == '':
return b''
if self.allow_pure_ascii and is_all_ascii(label):
return label.encode('ascii')
if not have_idna_2008:
raise NoIDNA2008
try:
if self.uts_46:
label = idna.uts46_remap(label, False, self.transitional)
return idna.alabel(label)
except idna.IDNAError as e:
raise IDNAException(idna_exception=e)