Пример #1
0
def _url_equals(url1, url2):
    """
    Compares two URLs and determines whether they are the equal.

    :param url1:
        First URL.
    :param url2:
        Second URL.
    :returns:
        ``True`` if equal; ``False`` otherwise.

    Usage::

        >>> _url_equals("http://www.google.com/a", "http://www.google.com/a")
        True
        >>> _url_equals("https://www.google.com/a", "http://www.google.com/a")
        False
        >>> _url_equals("http://www.google.com/", "http://www.example.com/")
        False
        >>> _url_equals("http://example.com:80/", "http://example.com:8000/")
        False
        >>> _url_equals("http://[email protected]/", "http://[email protected]/")
        False
        >>> _url_equals("http://[email protected]/request?a=b&b=c&b=d#fragment", "http://[email protected]/request?b=c&b=d&a=b#fragment")
        True
        >>> _url_equals("http://[email protected]/request?a=b&b=c&b=d#fragment", "http://[email protected]/request?b=c&b=d&a=b#fragment2")
        False
        >>> _url_equals("http://www.google.com/request?a=b", "http://www.google.com/request?b=c")
        False
    """
    u1 = urlparse(url1)
    u2 = urlparse(url2)
    return u1.scheme == u2.scheme and \
        u1.path == u2.path and \
        u1.params == u2.params and \
        u1.netloc == u2.netloc and \
        u1.fragment == u2.fragment and \
        parse_qs(u1.query, keep_blank_values=True) == \
            parse_qs(u2.query, keep_blank_values=True)
Пример #2
0
def urlparse_normalized(url):
    """
    Like :func:`urlparse.urlparse` but also normalizes scheme, netloc, port,
    and the path.

    Use with OAuth URLs.

    :see: Base String URI (http://tools.ietf.org/html/rfc5849#section-3.4.1.2)
    :param url:
        The URL to split and normalize.
    :returns:
        Tuple that contains these elements:
        ``(scheme, netloc, path, params, query, fragment)``
    """
    if not url:
        raise InvalidUrlError("Invalid URL `%r`" % (url,))

    parts = urlparse(url)

    scheme      = parts.scheme.lower()
    # Netloc.
    username    = parts.username or SYMBOL_EMPTY_BYTES
    password    = (b(":") + parts.password) if parts.password \
                  else SYMBOL_EMPTY_BYTES
    credentials = username + password
    credentials = (credentials + b("@")) if credentials else SYMBOL_EMPTY_BYTES
    hostname = utf8_encode_if_unicode(parts.hostname.lower())

    # Exclude default port numbers.
    # See:
    if parts.port:
        if (scheme == b("http") and parts.port == 80) \
        or (scheme == b("https") and parts.port == 443):
            port = SYMBOL_EMPTY_BYTES
        else:
            port = (b(":") + str(parts.port).encode("ascii")) if parts.port \
                   else SYMBOL_EMPTY_BYTES
    else:
        port = SYMBOL_EMPTY_BYTES

    netloc        = credentials + hostname + port
    # http://tools.ietf.org/html/rfc3986#section-3
    # and http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.2
    path          = parts.path or b("/")
    matrix_params = parts.params or SYMBOL_EMPTY_BYTES
    fragment      = parts.fragment or SYMBOL_EMPTY_BYTES
    query         = parts.query or SYMBOL_EMPTY_BYTES

    return scheme, netloc, path, matrix_params, query, fragment
Пример #3
0
def is_valid_callback_url(url):
    """
    Determines whether a specified URL is a valid oauth_callback callback
    absolute URL as required by http://tools.ietf.org/html/rfc5849#section-2.1
    (Temporary Credentials) in the OAuth specification.

    :param url:
        The URL to validate.
    :returns:
        ``True`` if valid; ``False`` otherwise.
    """
    if not is_bytes(url):
        return False
    if url == OAUTH_VALUE_CALLBACK_OOB:
        return True
    else:
        scheme, netloc, _, _, _, _ = urlparse(url)
        return scheme.lower() in (b("http"), b("https")) and netloc