Exemple #1
0
    def decode_netloc(self):
        """Decodes the netloc part into a string."""
        rv = _decode_idna(self.host or '')

        if ':' in rv:
            rv = '[%s]' % rv
        port = self.port
        if port is not None:
            rv = '%s:%d' % (rv, port)
        auth = ':'.join(filter(None, [
            _url_unquote_legacy(self.raw_username or '', '/:%@'),
            _url_unquote_legacy(self.raw_password or '', '/:%@'),
        ]))
        if auth:
            rv = '%s@%s' % (auth, rv)
        return rv
Exemple #2
0
    def decode_netloc(self):
        """Decodes the netloc part into a string."""
        rv = _decode_idna(self.host or '')

        if ':' in rv:
            rv = '[%s]' % rv
        port = self.port
        if port is not None:
            rv = '%s:%d' % (rv, port)
        auth = ':'.join(filter(None, [
            _url_unquote_legacy(self.raw_username or '', '/:%@'),
            _url_unquote_legacy(self.raw_password or '', '/:%@'),
        ]))
        if auth:
            rv = '%s@%s' % (auth, rv)
        return rv
Exemple #3
0
    def decode_netloc(self):
        """Decodes the netloc part into a string."""
        rv = _decode_idna(self.host or "")

        if ":" in rv:
            rv = "[%s]" % rv
        port = self.port
        if port is not None:
            rv = "%s:%d" % (rv, port)
        auth = ":".join(
            filter(
                None,
                [
                    _url_unquote_legacy(self.raw_username or "", "/:%@"),
                    _url_unquote_legacy(self.raw_password or "", "/:%@"),
                ],
            ))
        if auth:
            rv = "%s@%s" % (auth, rv)
        return rv
Exemple #4
0
    def decode_netloc(self):
        """Decodes the netloc part into a string."""
        rv = _decode_idna(self.host or "")

        if ":" in rv:
            rv = "[%s]" % rv
        port = self.port
        if port is not None:
            rv = "%s:%d" % (rv, port)
        auth = ":".join(
            filter(
                None,
                [
                    _url_unquote_legacy(self.raw_username or "", "/:%@"),
                    _url_unquote_legacy(self.raw_password or "", "/:%@"),
                ],
            )
        )
        if auth:
            rv = "%s@%s" % (auth, rv)
        return rv
Exemple #5
0
def uri_to_iri(uri, charset='utf-8', errors='replace'):
    r"""Converts a URI in a given charset to a IRI.

    Examples for URI versus IRI:

    >>> uri_to_iri(b'http://xn--n3h.net/')
    u'http://\u2603.net/'
    >>> uri_to_iri(b'http://%C3%BCser:p%C3%[email protected]/p%C3%A5th')
    u'http://\xfcser:p\xe4ssword@\u2603.net/p\xe5th'

    Query strings are left unchanged:

    >>> uri_to_iri('/?foo=24&x=%26%2f')
    u'/?foo=24&x=%26%2f'

    :param uri:
        The URI to convert.
    :param charset:
        The charset of the URI.
    :param errors:
        The error handling on decode.
    """
    assert isinstance(uri, str)
    uri = urlsplit(to_unicode(uri, charset))

    host = _decode_idna(uri.hostname) if uri.hostname else ''
    if ':' in host:
        host = '[%s]' % host

    netloc = host

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

    if uri.username or uri.password:
        if uri.username:
            username = _safe_urlunquote(uri.username,
                                        charset='utf-8',
                                        errors='strict',
                                        unsafe='/:%')
        else:
            username = ''

        if uri.password:
            password = _safe_urlunquote(uri.password,
                                        charset='utf-8',
                                        errors='strict',
                                        unsafe='/:%')
            auth = '%s:%s' % (username, password)
        else:
            auth = username

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

    path = _safe_urlunquote(uri.path,
                            charset=charset,
                            errors=errors,
                            unsafe='%/;?')
    query = _safe_urlunquote(uri.query,
                             charset=charset,
                             errors=errors,
                             unsafe='%;/?:@&=+,$#')
    fragment = _safe_urlunquote(uri.fragment,
                                charset=charset,
                                errors=errors,
                                unsafe='%;/?:@&=+,$#')
    return urlunsplit((uri.scheme, netloc, path, query, fragment))