From 30d3aceb6cf28ef4624c89336ec1e7c2c85b77fa Mon Sep 17 00:00:00 2001 From: cdeler Date: Tue, 4 Aug 2020 22:15:20 +0300 Subject: [PATCH] #1105 raise warning if proxy scheme is http, https, all instead of http://, https://, all:// --- httpx/_client.py | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index e4b212b4ac..8811c7415e 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -100,9 +100,20 @@ def _get_proxy_map( return {} if isinstance(proxies, dict): new_proxies = {} + filtered_proxy_keys = {"all", "http", "https"} + wrong_proxy_schemas = [] for key, value in proxies.items(): + if key in filtered_proxy_keys: + wrong_proxy_schemas.append(key) proxy = Proxy(url=value) if isinstance(value, (str, URL)) else value new_proxies[str(key)] = proxy + + if wrong_proxy_schemas: + valid_proxies = (f"{prefix}://" for prefix in wrong_proxy_schemas) + logger.warning( + f"You should use proxy schemes like {', '.join(valid_proxies)} " + f"instead of using {', '.join(wrong_proxy_schemas)}" + ) return new_proxies else: proxy = Proxy(url=proxies) if isinstance(proxies, (str, URL)) else proxies @@ -485,22 +496,27 @@ def __init__( app=app, trust_env=trust_env, ) - self._proxies: typing.Dict[ - URLPattern, typing.Optional[httpcore.SyncHTTPTransport] - ] = { - URLPattern(key): None - if proxy is None - else self._init_proxy_transport( - proxy, - verify=verify, - cert=cert, - http2=http2, - limits=limits, - trust_env=trust_env, + proxies: typing.List[ + typing.Tuple[URLPattern, typing.Optional[httpcore.SyncHTTPTransport]] + ] = [ + ( + URLPattern(key), + None + if proxy is None + else self._init_proxy_transport( + proxy, + verify=verify, + cert=cert, + http2=http2, + limits=limits, + trust_env=trust_env, + ), ) for key, proxy in proxy_map.items() - } - self._proxies = dict(sorted(self._proxies.items())) + ] + self._proxies: typing.Dict[ + URLPattern, typing.Optional[httpcore.SyncHTTPTransport] + ] = dict(sorted(proxies)) def _init_transport( self,