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