Skip to content

Commit 65d2dfd

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 bfc88d3 commit 65d2dfd

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
@@ -2672,22 +2672,26 @@ def getproxies_registry():
26722672
# Returned as Unicode but problems if not converted to ASCII
26732673
proxyServer = str(winreg.QueryValueEx(internetSettings,
26742674
'ProxyServer')[0])
2675-
if '=' in proxyServer:
2676-
# Per-protocol settings
2677-
for p in proxyServer.split(';'):
2678-
protocol, address = p.split('=', 1)
2679-
# See if address has a type:// prefix
2680-
if not re.match('(?:[^/:]+)://', address):
2681-
address = '%s://%s' % (protocol, address)
2682-
proxies[protocol] = address
2683-
else:
2684-
# Use one setting for all protocols
2685-
if proxyServer[:5] == 'http:':
2686-
proxies['http'] = proxyServer
2687-
else:
2688-
proxies['http'] = 'http://%s' % proxyServer
2689-
proxies['https'] = 'https://%s' % proxyServer
2690-
proxies['ftp'] = 'ftp://%s' % proxyServer
2675+
if '=' not in proxyServer and ';' not in proxyServer:
2676+
# Use one setting for all protocols.
2677+
proxyServer = 'http={0};https={0};ftp={0}'.format(proxyServer)
2678+
for p in proxyServer.split(';'):
2679+
protocol, address = p.split('=', 1)
2680+
# See if address has a type:// prefix
2681+
if not re.match('(?:[^/:]+)://', address):
2682+
# Add type:// prefix to address without specifying type
2683+
if protocol in ('http', 'https', 'ftp'):
2684+
# The default proxy type of Windows is HTTP
2685+
address = 'http://' + address
2686+
elif protocol == 'socks':
2687+
address = 'socks://' + address
2688+
proxies[protocol] = address
2689+
# Use SOCKS proxy for HTTP(S) protocols
2690+
if proxies.get('socks'):
2691+
# The default SOCKS proxy type of Windows is SOCKS4
2692+
address = re.sub(r'^socks://', 'socks4://', proxies['socks'])
2693+
proxies['http'] = proxies.get('http') or address
2694+
proxies['https'] = proxies.get('https') or address
26912695
internetSettings.Close()
26922696
except (OSError, ValueError, TypeError):
26932697
# 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)