Skip to content

Commit 9743524

Browse files
bpo-42627: Fix incorrect parsing of Windows registry proxy settings (GH-26307)
(cherry picked from commit b69297e) Co-authored-by: 狂男风 <[email protected]>
1 parent 9f29bdd commit 9743524

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

Lib/urllib/request.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -2674,22 +2674,26 @@ def getproxies_registry():
26742674
# Returned as Unicode but problems if not converted to ASCII
26752675
proxyServer = str(winreg.QueryValueEx(internetSettings,
26762676
'ProxyServer')[0])
2677-
if '=' in proxyServer:
2678-
# Per-protocol settings
2679-
for p in proxyServer.split(';'):
2680-
protocol, address = p.split('=', 1)
2681-
# See if address has a type:// prefix
2682-
if not re.match('(?:[^/:]+)://', address):
2683-
address = '%s://%s' % (protocol, address)
2684-
proxies[protocol] = address
2685-
else:
2686-
# Use one setting for all protocols
2687-
if proxyServer[:5] == 'http:':
2688-
proxies['http'] = proxyServer
2689-
else:
2690-
proxies['http'] = 'http://%s' % proxyServer
2691-
proxies['https'] = 'https://%s' % proxyServer
2692-
proxies['ftp'] = 'ftp://%s' % proxyServer
2677+
if '=' not in proxyServer and ';' not in proxyServer:
2678+
# Use one setting for all protocols.
2679+
proxyServer = 'http={0};https={0};ftp={0}'.format(proxyServer)
2680+
for p in proxyServer.split(';'):
2681+
protocol, address = p.split('=', 1)
2682+
# See if address has a type:// prefix
2683+
if not re.match('(?:[^/:]+)://', address):
2684+
# Add type:// prefix to address without specifying type
2685+
if protocol in ('http', 'https', 'ftp'):
2686+
# The default proxy type of Windows is HTTP
2687+
address = 'http://' + address
2688+
elif protocol == 'socks':
2689+
address = 'socks://' + address
2690+
proxies[protocol] = address
2691+
# Use SOCKS proxy for HTTP(S) protocols
2692+
if proxies.get('socks'):
2693+
# The default SOCKS proxy type of Windows is SOCKS4
2694+
address = re.sub(r'^socks://', 'socks4://', proxies['socks'])
2695+
proxies['http'] = proxies.get('http') or address
2696+
proxies['https'] = proxies.get('https') or address
26932697
internetSettings.Close()
26942698
except (OSError, ValueError, TypeError):
26952699
# Either registry key not found etc, or the value in an
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect parsing of Windows registry proxy settings

0 commit comments

Comments
 (0)