@@ -2492,28 +2492,34 @@ def getproxies_environment():
2492
2492
this seems to be the standard convention. If you need a
2493
2493
different way, you can pass a proxies dictionary to the
2494
2494
[Fancy]URLopener constructor.
2495
-
2496
2495
"""
2497
- proxies = {}
2498
2496
# in order to prefer lowercase variables, process environment in
2499
2497
# two passes: first matches any, second pass matches lowercase only
2500
- for name , value in os .environ .items ():
2501
- name = name .lower ()
2502
- if value and name [- 6 :] == '_proxy' :
2503
- proxies [name [:- 6 ]] = value
2498
+
2499
+ # select only environment variables which end in (after making lowercase) _proxy
2500
+ proxies = {}
2501
+ environment = []
2502
+ for name in os .environ .keys ():
2503
+ # fast screen underscore position before more expensive case-folding
2504
+ if len (name ) > 5 and name [- 6 ] == "_" and name [- 5 :].lower () == "proxy" :
2505
+ value = os .environ [name ]
2506
+ proxy_name = name [:- 6 ].lower ()
2507
+ environment .append ((name , value , proxy_name ))
2508
+ if value :
2509
+ proxies [proxy_name ] = value
2504
2510
# CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
2505
2511
# (non-all-lowercase) as it may be set from the web server by a "Proxy:"
2506
2512
# header from the client
2507
2513
# If "proxy" is lowercase, it will still be used thanks to the next block
2508
2514
if 'REQUEST_METHOD' in os .environ :
2509
2515
proxies .pop ('http' , None )
2510
- for name , value in os .environ .items ():
2516
+ for name , value , proxy_name in environment :
2517
+ # not case-folded, checking here for lower-case env vars only
2511
2518
if name [- 6 :] == '_proxy' :
2512
- name = name .lower ()
2513
2519
if value :
2514
- proxies [name [: - 6 ] ] = value
2520
+ proxies [proxy_name ] = value
2515
2521
else :
2516
- proxies .pop (name [: - 6 ] , None )
2522
+ proxies .pop (proxy_name , None )
2517
2523
return proxies
2518
2524
2519
2525
def proxy_bypass_environment (host , proxies = None ):
0 commit comments