Skip to content

Commit 5a33643

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 a2c8180 commit 5a33643

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