예제 #1
0
def _graceful_relative_url(base_url, url):
    """
    Return a graceful link for a URL relative to a base URL.

    * If they are the same, return an empty string.
    * If the have the same scheme and hostname, return the path & query params.
    * Otherwise return the full URL.
    """
    if url == base_url:
        return ""
    base_prefix = "%s://%s" % urlparse.urlparse(base_url or "")[0:2]
    url_prefix = "%s://%s" % urlparse.urlparse(url or "")[0:2]
    if base_prefix == url_prefix and url_prefix != "://":
        return url[len(url_prefix) :]
    return url
예제 #2
0
파일: http.py 프로젝트: meltygroup/pasee
    def __call__(self, request):
        if not self.credentials:
            return request

        # Include any authorization credentials relevant to this domain.
        url_components = urlparse.urlparse(request.url)
        host = url_components.hostname
        if host in self.credentials:
            request.headers["Authorization"] = self.credentials[host]
        return request
예제 #3
0
def domain_matches(request, domain):
    """
    Domain string matching against an outgoing request.
    Patterns starting with '*' indicate a wildcard domain.
    """
    if (domain is None) or (domain == "*"):
        return True

    host = urlparse.urlparse(request.url).hostname
    if domain.startswith("*"):
        return host.endswith(domain[1:])
    return host == domain
예제 #4
0
def _get_filename_from_url(url, content_type=None):
    """
    Determine an output filename based on the download URL.
    """
    parsed = urlparse.urlparse(url)
    final_path_component = posixpath.basename(parsed.path.rstrip("/"))
    filename = _safe_filename(final_path_component)
    suffix = guess_extension(content_type or "")

    if filename:
        if "." not in filename:
            return filename + suffix
        return filename
    elif suffix:
        return "download" + suffix

    return None
예제 #5
0
def determine_transport(transports, url):
    """
    Given a URL determine the appropriate transport instance.
    """
    url_components = urlparse.urlparse(url)
    scheme = url_components.scheme.lower()
    netloc = url_components.netloc

    if not scheme:
        raise exceptions.NetworkError("URL missing scheme '%s'." % url)

    if not netloc:
        raise exceptions.NetworkError("URL missing hostname '%s'." % url)

    for transport in transports:
        if scheme in transport.schemes:
            return transport

    raise exceptions.NetworkError("Unsupported URL scheme '%s'." % scheme)