Skip to content
Myr

Rely on getproxies for all proxy environment variables (#470)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by camellia256 · committed by Seth Michael Larson · parent c1f5127 · Browse files · 2 files+4 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+4 added−28 removed

  • tests:+1 added−8 removed
  • other:+3 added−20 removed

2 files+4 added−28 removed

modified:httpx/utils.py+3 added-20 removed
217 unmodified lines
218 # urllib.request.getproxies() falls back on System219 # Registry and Config for proxies on Windows and macOS.220 # We don't want to propagate non-HTTP proxies into221 # our configuration such as 'TRAVIS_APT_PROXY'.222 proxies = {222 supported_proxy_schemes = ("http", "https", "all")223 return {224 key: val225 for key, val in getproxies().items()225 if ("://" in key or key in ("http", "https"))226 if ("://" in key or key in supported_proxy_schemes)227 }228228 # Favor lowercase environment variables over uppercase.229 all_proxy = get_environ_lower_and_upper("ALL_PROXY")230 if all_proxy is not None:231 proxies["all"] = all_proxy229233 return proxies234235236def get_environ_lower_and_upper(key: str) -> typing.Optional[str]:237 """Gets a value from os.environ with both the lowercase and uppercase238 environment variable. Prioritizes the lowercase environment variable.239 """240 for key in (key.lower(), key.upper()):241 value = os.environ.get(key, None)242 if value is not None and isinstance(value, str):243 return value244 return None245246230def to_bytes(value: typing.Union[str, bytes], encoding: str = "utf-8") -> bytes:231 return value.encode(encoding) if isinstance(value, str) else value232233
120 unmodified lines
modified:tests/test_utils.py+1 added-8 removed
176 unmodified lines
177 (178 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},179 {"https": "http://127.0.0.1", "http": "https://127.0.0.1"},180 ),181 (182 {"all_proxy": "http://127.0.0.1", "ALL_PROXY": "https://1.1.1.1"},183 {"all": "http://127.0.0.1"},184 ),185 (186 {"https_proxy": "http://127.0.0.1", "HTTPS_PROXY": "https://1.1.1.1"},187 {"https": "http://127.0.0.1"},188 ),181 ({"all_proxy": "http://127.0.0.1"}, {"all": "http://127.0.0.1"}),182 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),183 ],184)185def test_get_environment_proxies(environment, proxies):
18 unmodified lines