From 0a1328ed3731fcbe9712bf3a4ac7ae5a11e69f02 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Sun, 22 Dec 2024 18:35:32 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20method=20`HTT?= =?UTF-8?q?PAdapter.request=5Furl`=20by=2013%=20Here=20is=20a=20rewritten?= =?UTF-8?q?=20and=20optimized=20version=20of=20the=20given=20Python=20prog?= =?UTF-8?q?ram.=20The=20changes=20are=20highlighted=20in=20comments.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Key optimizations include. - Reduced multiple calls to `urlparse(url)` by reusing the parsed URL components. - Used shortcuts for conditional assignments and early returns to simplify logic. - Removed redundant check for `proxy_keys` (iteration over an `in` check automatically stops on first match). - Simplified repeated assignment evaluations inside loop conditions and returns. --- src/requests/adapters.py | 24 +++++++----------------- src/requests/utils.py | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 28 deletions(-) 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