diff --git a/src/requests/adapters.py b/src/requests/adapters.py index 9a58b16025..3ec9a782a6 100644 --- a/src/requests/adapters.py +++ b/src/requests/adapters.py @@ -546,12 +546,9 @@ def close(self): def request_url(self, request, proxies): """Obtain the url to use when making the final request. - If the message is being sent through a HTTP proxy, the full URL has to - be used. Otherwise, we should only use the path portion of the URL. + If the message is being sent through a HTTP proxy, the full URL has to be used. Otherwise, we should only use the path portion of the URL. - This should not be called from user code, and is only exposed for use - when subclassing the - :class:`HTTPAdapter `. + This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param request: The :class:`PreparedRequest ` being sent. :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs. @@ -560,20 +557,13 @@ def request_url(self, request, proxies): proxy = select_proxy(request.url, proxies) scheme = urlparse(request.url).scheme - is_proxied_http_request = proxy and scheme != "https" - using_socks_proxy = False - if proxy: + if proxy and scheme != "https": proxy_scheme = urlparse(proxy).scheme.lower() - using_socks_proxy = proxy_scheme.startswith("socks") - + if not proxy_scheme.startswith("socks"): + return urldefragauth(request.url) + url = request.path_url - if url.startswith("//"): # Don't confuse urllib3 - url = f"/{url.lstrip('/')}" - - if is_proxied_http_request and not using_socks_proxy: - url = urldefragauth(request.url) - - return url + return f"/{url.lstrip('/')}" if url.startswith("//") else url def add_headers(self, request, **kwargs): """Add any headers needed by the connection. As of v2.0 this does diff --git a/src/requests/utils.py b/src/requests/utils.py index 699683e5d9..c737fc75d3 100644 --- a/src/requests/utils.py +++ b/src/requests/utils.py @@ -844,24 +844,25 @@ def select_proxy(url, proxies): :param url: The url being for the request :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs """ - proxies = proxies or {} + if not proxies: + return None # Early return if proxies are not provided + urlparts = urlparse(url) - if urlparts.hostname is None: + if not urlparts.hostname: return proxies.get(urlparts.scheme, proxies.get("all")) - proxy_keys = [ - urlparts.scheme + "://" + urlparts.hostname, + proxy_keys = ( + f"{urlparts.scheme}://{urlparts.hostname}", urlparts.scheme, - "all://" + urlparts.hostname, + f"all://{urlparts.hostname}", "all", - ] - proxy = None + ) + for proxy_key in proxy_keys: if proxy_key in proxies: - proxy = proxies[proxy_key] - break + return proxies[proxy_key] # Early return if proxy found - return proxy + return None # If no proxy is found def resolve_proxies(request, proxies, trust_env=True): @@ -1072,7 +1073,6 @@ def urldefragauth(url): """ scheme, netloc, path, params, query, fragment = urlparse(url) - # see func:`prepend_scheme_if_needed` if not netloc: netloc, path = path, netloc