Gets proxy information from the environment
()
| 28 | |
| 29 | |
| 30 | def get_environment_proxies() -> dict[str, str | None]: |
| 31 | """Gets proxy information from the environment""" |
| 32 | |
| 33 | # urllib.request.getproxies() falls back on System |
| 34 | # Registry and Config for proxies on Windows and macOS. |
| 35 | # We don't want to propagate non-HTTP proxies into |
| 36 | # our configuration such as 'TRAVIS_APT_PROXY'. |
| 37 | proxy_info = getproxies() |
| 38 | mounts: dict[str, str | None] = {} |
| 39 | |
| 40 | for scheme in ("http", "https", "all"): |
| 41 | if proxy_info.get(scheme): |
| 42 | hostname = proxy_info[scheme] |
| 43 | mounts[f"{scheme}://"] = ( |
| 44 | hostname if "://" in hostname else f"http://{hostname}" |
| 45 | ) |
| 46 | |
| 47 | no_proxy_hosts = [host.strip() for host in proxy_info.get("no", "").split(",")] |
| 48 | for hostname in no_proxy_hosts: |
| 49 | # See https://curl.haxx.se/libcurl/c/CURLOPT_NOPROXY.html for details |
| 50 | # on how names in `NO_PROXY` are handled. |
| 51 | if hostname == "*": |
| 52 | # If NO_PROXY=* is used or if "*" occurs as any one of the comma |
| 53 | # separated hostnames, then we should just bypass any information |
| 54 | # from HTTP_PROXY, HTTPS_PROXY, ALL_PROXY, and always ignore |
| 55 | # proxies. |
| 56 | return {} |
| 57 | elif hostname: |
| 58 | # NO_PROXY=.google.com is marked as "all://*.google.com, |
| 59 | # which disables "www.google.com" but not "google.com" |
| 60 | # NO_PROXY=google.com is marked as "all://*google.com, |
| 61 | # which disables "www.google.com" and "google.com". |
| 62 | # (But not "wwwgoogle.com") |
| 63 | # NO_PROXY can include domains, IPv6, IPv4 addresses and "localhost" |
| 64 | # NO_PROXY=example.com,::1,localhost,192.168.0.0/16 |
| 65 | if "://" in hostname: |
| 66 | mounts[hostname] = None |
| 67 | elif is_ipv4_hostname(hostname): |
| 68 | mounts[f"all://{hostname}"] = None |
| 69 | elif is_ipv6_hostname(hostname): |
| 70 | mounts[f"all://[{hostname}]"] = None |
| 71 | elif hostname.lower() == "localhost": |
| 72 | mounts[f"all://{hostname}"] = None |
| 73 | else: |
| 74 | mounts[f"all://*{hostname}"] = None |
| 75 | |
| 76 | return mounts |
| 77 | |
| 78 | |
| 79 | def to_bytes(value: str | bytes, encoding: str = "utf-8") -> bytes: |