Skip to content
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

Fix HTTPS websites with system-wide HTTP proxy on Windows #6162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,25 @@ def get_proxy(key):
return False


def getproxies_fix_system_proxies_windows():
"""Fix for urllib getproxies() bug on Windows with Python < 3.10.5
See: https://bugs.python.org/issue42627
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing precludes this from running on the versions unaffected according to you. This comment is aside from whether this is the correct fix

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a no-op for a fixed Python/urllib version. This commit checks whether getproxies() returns the same proxy host and port for three protocols, each prefixed with the protocol, and rewrites it to "http://". The fixed urllib already returns proxies with "http://", so this commit does nothing in this case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sigmavirus24, do you think the code needs more checks?


:rtype: dict
"""
proxy_hosts = []
protocols = {"http", "https", "ftp"}
proxies = getproxies()
if proxies.keys() != protocols:
return proxies
for protocol in proxies:
proxy_hosts.append(proxies[protocol].lstrip(protocol + "://"))
proxy_hosts = list(set(proxy_hosts))
if len(proxy_hosts) == 1:
return {protocol: "http://" + proxy_hosts[0] for protocol in protocols}
return proxies


def get_environ_proxies(url, no_proxy=None):
"""
Return a dict of environment proxies.
Expand All @@ -824,6 +843,8 @@ def get_environ_proxies(url, no_proxy=None):
"""
if should_bypass_proxies(url, no_proxy=no_proxy):
return {}
elif os.name == 'nt':
return getproxies_fix_system_proxies_windows()
else:
return getproxies()

Expand Down