Example #1
0
 def bind(self,
          server_name,
          script_name=None,
          subdomain=None,
          url_scheme='http',
          default_method='GET',
          path_info=None,
          query_args=None,
          request=None,
          version_dict=None):
     server_name = server_name.lower()
     if self.host_matching:
         if subdomain is not None:
             raise RuntimeError('host matching enabled and a '
                                'subdomain was provided')
         elif subdomain is None:
             subdomain = self.default_subdomain
     if script_name is None:
         script_name = '/'
     try:
         server_name = _encode_idna(server_name)
     except UnicodeError:
         raise BadHost()
     return MFMapAdapter(self, server_name, script_name, subdomain,
                         url_scheme, path_info, default_method, query_args,
                         request, version_dict)
Example #2
0
 def ascii_host(self):
     """Works exactly like :attr:`host` but will return a result that
     is restricted to ASCII.  If it finds a netloc that is not ASCII
     it will attempt to idna decode it.  This is useful for socket
     operations when the URL might include internationalized characters.
     """
     rv = self.host
     if rv is not None and isinstance(rv, text_type):
         rv = _encode_idna(rv)
     return to_native(rv, 'ascii', 'ignore')
Example #3
0
 def ascii_host(self):
     """Works exactly like :attr:`host` but will return a result that
     is restricted to ASCII.  If it finds a netloc that is not ASCII
     it will attempt to idna decode it.  This is useful for socket
     operations when the URL might include internationalized characters.
     """
     rv = self.host
     if rv is not None and isinstance(rv, text_type):
         rv = _encode_idna(rv)
     return to_native(rv, 'ascii', 'ignore')
Example #4
0
def _make_cookie_domain(domain):
    if domain is None:
        return None
    domain = _encode_idna(domain)
    if b':' in domain:
        domain = domain.split(b':', 1)[0]
    if b'.' in domain:
        return domain
    raise ValueError(
        'Setting \'domain\' for a cookie on a server running locally (ex: '
        'localhost) is not supported by complying browsers. You should '
        'have something like: \'127.0.0.1 localhost dev.localhost\' on '
        'your hosts file and then point your server to run on '
        '\'dev.localhost\' and also set \'domain\' for \'dev.localhost\'')
Example #5
0
 def _normalize(hostname):
     if ':' in hostname:
         hostname = hostname.rsplit(':', 1)[0]
     return _encode_idna(hostname)
Example #6
0
 def _normalize(hostname):
     if ':' in hostname:
         hostname = hostname.rsplit(':', 1)[0]
     return _encode_idna(hostname)
Example #7
0
 def _normalize(hostname):
     if ":" in hostname:
         hostname = hostname.rsplit(":", 1)[0]
     return _encode_idna(hostname)
Example #8
0
 def _normalize(hostname):
     if ":" in hostname:
         hostname = hostname.rsplit(":", 1)[0]
     return _encode_idna(hostname)
Example #9
0
def iri_to_uri(iri, errors='strict', safe_conversion=False):
    r"""Converts any unicode based IRI to an acceptable ASCII URI. Werkzeug always
    uses utf-8 URLs internally because this is what browsers and HTTP do as
    well. In some places where it accepts an URL it also accepts a unicode IRI
    and converts it into a URI.

    Examples for IRI versus URI:

    >>> iri_to_uri(u'http://☃.net/')
    'http://xn--n3h.net/'
    >>> iri_to_uri(u'http://üser:pässword@☃.net/påth')
    'http://%C3%BCser:p%C3%[email protected]/p%C3%A5th'

    There is a general problem with IRI and URI conversion with some
    protocols that appear in the wild that are in violation of the URI
    specification.  In places where Werkzeug goes through a forced IRI to
    URI conversion it will set the `safe_conversion` flag which will
    not perform a conversion if the end result is already ASCII.  This
    can mean that the return value is not an entirely correct URI but
    it will not destroy such invalid URLs in the process.

    As an example consider the following two IRIs::

        magnet:?xt=uri:whatever
        itms-services://?action=download-manifest

    The internal representation after parsing of those URLs is the same
    and there is no way to reconstruct the original one.  If safe
    conversion is enabled however this function becomes a noop for both of
    those strings as they both can be considered URIs.

    :param iri:
        The IRI to convert.
    :param charset:
        The charset for the URI.
    :param safe_conversion:
        Indicates if a safe conversion should take place.  For more information
        see the explanation above.
    """
    assert isinstance(iri, str)
    if safe_conversion:
        try:
            native_iri = to_native(iri)
            ascii_iri = to_native(iri).encode('ascii')
            if ascii_iri.split() == [ascii_iri]:
                return native_iri
        except UnicodeError:
            pass

    iri = urlsplit(iri)

    host = _encode_idna(iri.hostname).decode('ascii') if iri.hostname else ''
    if ':' in host:
        host = '[%s]' % host

    netloc = host

    if iri.port:
        if not 0 <= int(iri.port) <= 65535:
            raise ValueError('Invalid port')
        netloc = '%s:%s' % (netloc, iri.port)

    if iri.username or iri.password:
        if iri.username:
            username = urlquote(iri.username, safe='/:%')
        else:
            username = ''

        if iri.password:
            password = urlquote(iri.password, safe='/:%')
            auth = '%s:%s' % (username, password)
        else:
            auth = username

        netloc = '%s@%s' % (auth, netloc)

    path = urlquote(iri.path, safe='/:~+%')
    query = urlquote(iri.query, safe='%&[]:;$*()+,!?*/=')
    fragment = urlquote(iri.fragment, safe='=%&[]:;$()+,!?*/')

    return urlunsplit((iri.scheme, netloc, path, query, fragment))