From 8e02fcec52bb5335af71fa94cfeee3ed27a0d428 Mon Sep 17 00:00:00 2001 From: cdeler Date: Tue, 4 Aug 2020 22:08:16 +0300 Subject: [PATCH] #1105 raise warning if proxy scheme is http, https, all instead of http://, https://, all:// --- httpx/_client.py | 55 +++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index e4b212b4ac..9ccb2f0501 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -6,17 +6,8 @@ import httpcore from ._auth import Auth, BasicAuth, FunctionAuth -from ._config import ( - DEFAULT_LIMITS, - DEFAULT_MAX_REDIRECTS, - DEFAULT_TIMEOUT_CONFIG, - UNSET, - Limits, - Proxy, - Timeout, - UnsetType, - create_ssl_context, -) +from ._config import (DEFAULT_LIMITS, DEFAULT_MAX_REDIRECTS, DEFAULT_TIMEOUT_CONFIG, Limits, Proxy, Timeout, UNSET, + UnsetType, create_ssl_context) from ._content_streams import ContentStream from ._exceptions import ( HTTPCORE_EXC_MAP, @@ -25,7 +16,7 @@ TooManyRedirects, map_exceptions, ) -from ._models import URL, Cookies, Headers, QueryParams, Request, Response +from ._models import Cookies, Headers, QueryParams, Request, Response, URL from ._status_codes import codes from ._transports.asgi import ASGITransport from ._transports.wsgi import WSGITransport @@ -100,9 +91,18 @@ def _get_proxy_map( return {} if isinstance(proxies, dict): new_proxies = {} + filtered_proxy_keys = {"all", "http", "https"} + wrong_proxy_schemes = [] for key, value in proxies.items(): + if key in filtered_proxy_keys: + wrong_proxy_schemes.append(key) proxy = Proxy(url=value) if isinstance(value, (str, URL)) else value new_proxies[str(key)] = proxy + + if wrong_proxy_schemes: + valid_proxies = (f"{prefix}://" for prefix in wrong_proxy_schemes) + logger.warning(f"You should use proxy schemes like {', '.join(valid_proxies)} " + f"instead of using {', '.join(wrong_proxy_schemes)}") return new_proxies else: proxy = Proxy(url=proxies) if isinstance(proxies, (str, URL)) else proxies @@ -485,22 +485,25 @@ def __init__( app=app, 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: 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, - ) - for key, proxy in proxy_map.items() - } - self._proxies = dict(sorted(self._proxies.items())) + ] = dict(sorted(proxies)) def _init_transport( self,