コード例 #1
0
def test_reg_errors_reraise():
    with _patch_winreg_qve(side_effect=WindowsError()):
        assert not autoconfig_url_from_registry()
    with _patch_winreg_qve(side_effect=CalledProcessError(2, "foo")):
        with pytest.raises(CalledProcessError) as exinfo:
            autoconfig_url_from_registry()
        assert exinfo.value.returncode == 2
コード例 #2
0
def get_pac(url=None,
            js=None,
            from_registry=True,
            from_dns=True,
            timeout=2,
            allowed_content_types=None,
            **kwargs):
    """
    Convenience function for finding and getting a parsed PAC file (if any) that's ready to use.

    :param str url: Download PAC from a URL.
        If provided, `from_registry` and `from_dns` are ignored.
    :param str js: Parse the given string as a PAC file.
        If provided, `from_registry` and `from_dns` are ignored.
    :param bool from_registry: Look for a PAC URL or filesystem path from the Windows Registry, and use it if present.
        Doesn't do anything on non-Windows platforms.
    :param bool from_dns: Look for a PAC file using the WPAD protocol.
    :param timeout: Time to wait for host resolution and response for each URL.
    :param allowed_content_types: Consider PAC file response to be valid only
        if the server responds with one of these content types.
        If not specified, the allowed types are
        ``application/x-ns-proxy-autoconfig`` and ``application/x-javascript-config``.
    :return: The first valid parsed PAC file according to the criteria, or `None` if nothing was found.
    :rtype: PACFile|None
    :raises MalformedPacError: If something that claims to be a PAC file was obtained but could not be parsed.
    """
    if url:
        downloaded_pac = download_pac(
            [url],
            timeout=timeout,
            allowed_content_types=allowed_content_types)
        if not downloaded_pac:
            return
        return PACFile(downloaded_pac, **kwargs)
    if js:
        return PACFile(js, **kwargs)

    if from_registry and ON_WINDOWS:
        path = autoconfig_url_from_registry()
        if path and os.path.isfile(path):
            with open(path) as f:
                return PACFile(f.read(), **kwargs)

    pac_candidate_urls = collect_pac_urls(from_registry=True,
                                          from_dns=from_dns)
    downloaded_pac = download_pac(pac_candidate_urls,
                                  timeout=timeout,
                                  allowed_content_types=allowed_content_types)
    if not downloaded_pac:
        return
    return PACFile(downloaded_pac, **kwargs)
コード例 #3
0
ファイル: api.py プロジェクト: LKleinNux/pypac
def collect_pac_urls(from_registry=True, from_dns=True):
    """
    Get all the URLs that potentially yield a PAC file.

    :param bool from_registry: Look for a PAC URL from the Windows Registry.
        If a value is found and is a URL, it comes first in the returned list.
        Doesn't do anything on non-Windows platforms.
    :param bool from_dns: Assemble a list of PAC URL candidates using the WPAD protocol.
    :return: A list of URLs that should be tried in order.
    :rtype: list[str]
    """
    pac_urls = []
    if from_registry and ON_WINDOWS:
        url_or_path = autoconfig_url_from_registry()
        if url_or_path and (url_or_path.lower().startswith('http://') or url_or_path.lower().startswith('https://')):
            pac_urls.append(url_or_path)
    if from_dns:
        pac_urls.extend(proxy_urls_from_dns())
    return pac_urls
コード例 #4
0
def test_mock_autoconfigurl():
    with _patch_winreg_qve(return_value=(test_reg_output_url, 'foo')):
        assert autoconfig_url_from_registry() == test_reg_output_url
コード例 #5
0
def test_autoconfig_url_from_registry():
    value = autoconfig_url_from_registry()
    assert value is None or value.startswith('http://')