Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# behaviour. For strings containing only ASCII characters, we need to also verify
# it doesn't start with a wildcard (*), before allowing the unencoded hostname.
if not unicode_is_ascii(uri.host):
try:
uri = uri.copy_with(host=self._get_idna_encoded_host(uri.host))
except UnicodeError:
raise InvalidURL('URL has an invalid label.')
elif uri.host.startswith(u'*'):
raise InvalidURL('URL has an invalid label.')
# Bare domains aren't valid URLs.
if not uri.path:
uri = uri.copy_with(path='/')
if isinstance(params, (str, bytes)):
params = to_native_string(params)
enc_params = self._encode_params(params)
if enc_params:
if uri.query:
uri = uri.copy_with(query=f'{uri.query}&{enc_params}')
else:
uri = uri.copy_with(query=enc_params)
# url = requote_uri(
# urlunparse([uri.scheme, uri.authority, uri.path, None, uri.query, uri.fragment])
# )
# Normalize the URI.
self.url = rfc3986.normalize_uri(uri.unsplit())
def prepare_headers(self, headers):
"""Prepares the given HTTP headers."""
self.headers = CaseInsensitiveDict()
if headers:
for header in headers.items():
# Raise exception on invalid header value.
check_header_validity(header)
name, value = header
self.headers[to_native_string(name)] = value
self.url = url
return
# Support for unicode domain names and paths.
try:
uri = rfc3986.urlparse(url)
if validate:
rfc3986.normalize_uri(url)
except rfc3986.exceptions.RFC3986Exception:
raise InvalidURL(f"Invalid URL {url!r}: URL is imporoper.")
if not uri.scheme:
error = (
"Invalid URL {0!r}: No scheme supplied. Perhaps you meant http://{0}?"
)
error = error.format(to_native_string(url, 'utf8'))
raise MissingScheme(error)
if not uri.host:
raise InvalidURL(f"Invalid URL {url!r}: No host supplied")
# In general, we want to try IDNA encoding the hostname if the string contains
# non-ASCII characters. This allows users to automatically get the correct IDNA
# behaviour. For strings containing only ASCII characters, we need to also verify
# it doesn't start with a wildcard (*), before allowing the unencoded hostname.
if not unicode_is_ascii(uri.host):
try:
uri = uri.copy_with(host=self._get_idna_encoded_host(uri.host))
except UnicodeError:
raise InvalidURL('URL has an invalid label.')
elif uri.host.startswith(u'*'):
if response.is_redirect:
if not is_valid_location(response):
raise InvalidHeader(
'Response contains multiple Location headers. '
'Unable to perform redirect.'
)
location = response.headers['location']
# Currently the underlying http module on py3 decode headers
# in latin1, but empirical evidence suggests that latin1 is very
# rarely used with non-ASCII characters in HTTP headers.
# It is more likely to get UTF8 header rather than latin1.
# This causes incorrect handling of UTF8 encoded location headers.
# To solve this, we re-encode the location in latin1.
location = location.encode('latin1')
return to_native_string(location, 'utf8')
return None
def prepare_method(self, method):
"""Prepares the given HTTP method."""
self.method = method
if self.method is None:
raise ValueError('Request method cannot be "None"')
self.method = to_native_string(self.method.upper())