Skip to content

⚡️ Speed up method SessionRedirectMixin.rebuild_proxies by 7% #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ._internal_utils import to_native_string
from .adapters import HTTPAdapter
from .auth import _basic_auth_str
from .compat import Mapping, cookielib, urljoin, urlparse
from .compat import unquote, Mapping, cookielib, urljoin, urlparse
from .cookies import (
RequestsCookieJar,
cookiejar_from_dict,
Expand Down Expand Up @@ -318,15 +318,16 @@ def rebuild_proxies(self, prepared_request, proxies):
if "Proxy-Authorization" in headers:
del headers["Proxy-Authorization"]

try:
username, password = get_auth_from_url(new_proxies[scheme])
except KeyError:
username, password = None, None
proxy_url = new_proxies.get(scheme)
if proxy_url:
parsed = urlparse(proxy_url)
username = unquote(parsed.username or "")
password = unquote(parsed.password or "")

# urllib3 handles proxy authorization for us in the standard adapter.
# Avoid appending this to TLS tunneled requests where it may be leaked.
if not scheme.startswith("https") and username and password:
headers["Proxy-Authorization"] = _basic_auth_str(username, password)
# urllib3 handles proxy authorization for us in the standard adapter.
# Avoid appending this to TLS tunneled requests where it may be leaked.
if username and password and not scheme.startswith("https"):
headers["Proxy-Authorization"] = _basic_auth_str(username, password)

return new_proxies

Expand Down
25 changes: 11 additions & 14 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
from ._internal_utils import ( # noqa: F401
_HEADER_VALIDATORS_BYTE,
_HEADER_VALIDATORS_STR,
HEADER_VALIDATORS,
to_native_string,
)
HEADER_VALIDATORS)
from .compat import (
Mapping,
basestring,
Expand Down Expand Up @@ -875,20 +873,21 @@ def resolve_proxies(request, proxies, trust_env=True):

:rtype: dict
"""
proxies = proxies if proxies is not None else {}
if proxies is None:
proxies = {}

url = request.url
scheme = urlparse(url).scheme
no_proxy = proxies.get("no_proxy")
new_proxies = proxies.copy()

if trust_env and not should_bypass_proxies(url, no_proxy=no_proxy):
environ_proxies = get_environ_proxies(url, no_proxy=no_proxy)

proxy = environ_proxies.get(scheme, environ_proxies.get("all"))

if proxy:
new_proxies.setdefault(scheme, proxy)
return new_proxies
proxies.setdefault(scheme, proxy)

return proxies


def default_user_agent(name="python-requests"):
Expand Down Expand Up @@ -1026,12 +1025,10 @@ def get_auth_from_url(url):
"""
parsed = urlparse(url)

try:
auth = (unquote(parsed.username), unquote(parsed.password))
except (AttributeError, TypeError):
auth = ("", "")
username = unquote(parsed.username or "")
password = unquote(parsed.password or "")

return auth
return username, password


def check_header_validity(header):
Expand Down