Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_idna_encoded_host(host):
import idna
try:
host = idna.encode(host, uts46=True).decode('utf-8')
except idna.IDNAError:
raise UnicodeError
return host
def is_valid_domain(domain):
"""Validate a domain - including unicode domains."""
try:
if len(domain) > 255:
return False
encoded_domain = idna.encode(domain)
if domain != encoded_domain and len(domain) == encoded_domain:
return False
return VALID_DOMAIN_RE.match(encoded_domain) is not None
except (UnicodeError, TypeError, idna.IDNAError):
pass
return False
def decode(self, label):
if not self.strict_decode:
return super(IDNA2008Codec, self).decode(label)
if label == b'':
return ''
if not have_idna_2008:
raise NoIDNA2008
try:
if self.uts_46:
label = idna.uts46_remap(label, False, False)
return _escapify(idna.ulabel(label))
except idna.IDNAError as e:
raise IDNAException(idna_exception=e)
# trailing dot is allowed, strip it
parts = parts[0:-1]
if len(parts) <= 1:
# do not accept single-word hosts, must be at least `domain.tld'
return None
if len(parts) > 2 and parts[0] == 'www':
# strip leading 'www' to reduce number of "duplicate" hosts,
# but leave at least 2 trailing parts (www.com is a valid domain)
parts = parts[1:]
for (i, part) in enumerate(parts):
if len(part) > 63:
return None
if not ExtractHostLinksJob.host_part_pattern.match(part):
try:
idn = idna.encode(part).decode('ascii')
except (idna.IDNAError, UnicodeError, IndexError, Exception):
# self.get_logger().debug("Invalid host name: {}".format(url))
return None
if ExtractHostLinksJob.host_part_pattern.match(idn):
parts[i] = idn
else:
# self.get_logger().debug("Invalid host name: {}".format(url))
return None
parts.reverse()
return '.'.join(parts)
def _get_idna_encoded_host(host):
import idna
try:
host = idna.encode(host, uts46=True).decode('utf-8')
except idna.IDNAError:
raise UnicodeError
return host
def _get_idna_encoded_host(host):
try:
from .packages import idna
except ImportError:
# tolerate the possibility of downstream repackagers unvendoring `requests`
# For more information, read: packages/__init__.py
import idna
sys.modules['requests.packages.idna'] = idna
try:
host = idna.encode(host, uts46=True).decode('utf-8')
except idna.IDNAError:
raise UnicodeError
return host
def idna_encode(name):
if name and any([ord(x) > 128 for x in name]):
try:
import idna
except ImportError:
raise LocationParseError("Unable to parse URL without the 'idna' module")
try:
return idna.encode(name.lower(), strict=True, std3_rules=True)
except idna.IDNAError:
raise LocationParseError(u"Name '%s' is not a valid IDNA label" % name)
return name
def _get_idna_encoded_host(host):
import idna
try:
host = idna.encode(host, uts46=True).decode('utf-8')
except idna.IDNAError:
raise UnicodeError
return host
def _get_idna_encoded_host(host):
try:
from .packages import idna
except ImportError:
# tolerate the possibility of downstream repackagers unvendoring `requests`
# For more information, read: packages/__init__.py
import idna
sys.modules['requests.packages.idna'] = idna
try:
host = idna.encode(host, uts46=True).decode('utf-8')
except idna.IDNAError:
raise UnicodeError
return host