Skip to content
Myr

Add support for no-proxy configurations (#1099)

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

* Add internal URLMatcher class * Use URLMatcher for proxy lookups in transport_for_url * Docstring * Pin pytest * Add support for no-proxies configurations

authored by Tom Christie · committed by GitHub · parent df54890 · Browse files · 3 files+25 added−14 removed
This commit

State

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

Diff stats

3 files+25 added−14 removed

  • tests:+1 added−0 removed
  • other:+24 added−14 removed

3 files+25 added−14 removed

modified:httpx/_client.py+23 added-13 removed
84 unmodified lines
85 self._netrc = NetRCInfo()8687 def _get_proxy_map(88 self, proxies: typing.Optional[ProxiesTypes], trust_env: bool,89 ) -> typing.Dict[str, Proxy]:89 ) -> typing.Dict[str, typing.Optional[Proxy]]:90 if proxies is None:91 if trust_env:92 return {93 key: Proxy(url=url)94 for key, url in get_environment_proxies().items()95 }96 return {}97 elif isinstance(proxies, (str, URL, Proxy)):98 proxy = Proxy(url=proxies) if isinstance(proxies, (str, URL)) else proxies99 return {"all": proxy}100 else:97 if isinstance(proxies, dict):98 new_proxies = {}99 for key, value in proxies.items():100 proxy = Proxy(url=value) if isinstance(value, (str, URL)) else value101 new_proxies[str(key)] = proxy102 return new_proxies103 else:104 proxy = Proxy(url=proxies) if isinstance(proxies, (str, URL)) else proxies105 return {"all": proxy}106107 @property108 def headers(self) -> Headers:109 """
361 unmodified lines
471 transport=transport,472 app=app,473 trust_env=trust_env,474 )475 self._proxies: typing.Dict[URLMatcher, httpcore.SyncHTTPTransport] = {476 URLMatcher(key): self._init_proxy_transport(475 self._proxies: typing.Dict[476 URLMatcher, typing.Optional[httpcore.SyncHTTPTransport]477 ] = {478 URLMatcher(key): None479 if proxy is None480 else self._init_proxy_transport(481 proxy,482 verify=verify,483 cert=cert,484 http2=http2,
61 unmodified lines
546547 if self._proxies and not should_not_be_proxied(url):548 for matcher, transport in self._proxies.items():549 if matcher.matches(url):546 return transport550 return self._transport if transport is None else transport551552 return self._transport553554 def request(
333 unmodified lines
888889 def close(self) -> None:890 self._transport.close()891 for proxy in self._proxies.values():892 if proxy is not None:888 proxy.close()893 proxy.close()894895 def __enter__(self) -> "Client":896 return self897
95 unmodified lines
993 transport=transport,994 app=app,995 trust_env=trust_env,996 )992 self._proxies: typing.Dict[URLMatcher, httpcore.AsyncHTTPTransport] = {993 URLMatcher(key): self._init_proxy_transport(997 self._proxies: typing.Dict[998 URLMatcher, typing.Optional[httpcore.AsyncHTTPTransport]999 ] = {1000 URLMatcher(key): None1001 if proxy is None1002 else self._init_proxy_transport(1003 proxy,1004 verify=verify,1005 cert=cert,1006 http2=http2,
61 unmodified lines
10681069 if self._proxies and not should_not_be_proxied(url):1070 for matcher, transport in self._proxies.items():1071 if matcher.matches(url):1063 return transport1072 return self._transport if transport is None else transport10731074 return self._transport10751076 async def request(
333 unmodified lines
14101411 async def aclose(self) -> None:1412 await self._transport.aclose()1413 for proxy in self._proxies.values():1414 if proxy is not None:1405 await proxy.aclose()1415 await proxy.aclose()14161417 async def __aenter__(self) -> "AsyncClient":1418 return self1419
68 unmodified lines
modified:httpx/_types.py+1 added-1 removed
52 unmodified lines
53 Optional[float],54 Tuple[Optional[float], Optional[float], Optional[float], Optional[float]],55 "Timeout",56]57ProxiesTypes = Union[URLTypes, "Proxy", Dict[URLTypes, Union[URLTypes, "Proxy"]]]57ProxiesTypes = Union[URLTypes, "Proxy", Dict[URLTypes, Union[None, URLTypes, "Proxy"]]]5859AuthTypes = Union[60 Tuple[Union[str, bytes], Union[str, bytes]],61 Callable[["Request"], "Request"],
15 unmodified lines
modified:tests/client/test_proxies.py+1 added
56 unmodified lines
57 ("http://example.com", {"https": PROXY_URL}, None),58 ("http://example.com", {"http://example.net": PROXY_URL}, None),59 ("http://example.com:443", {"http://example.com": PROXY_URL}, PROXY_URL),60 ("http://example.com", {"all": PROXY_URL}, PROXY_URL),61 ("http://example.com", {"all": PROXY_URL, "http://example.com": None}, None),62 ("http://example.com", {"http": PROXY_URL}, PROXY_URL),63 ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),64 ("http://example.com", {"all://example.com:80": PROXY_URL}, None),65 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),
84 unmodified lines