diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2016-04-25 15:18:07 (GMT) |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2016-04-25 15:18:07 (GMT) |
commit | 0996fa3bd827e68eccf359823c8bc3518075abb4 (patch) | |
tree | 670cb6e3308e63003bb83f61a4bdd65750606799 /Lib/urllib | |
parent | 3211595bef0c13d10c814d7dd35a96810e66c8d3 (diff) | |
parent | a7c0ff2f0b3883109245d6be3083aeeee8c9749c (diff) | |
download | cpython-0996fa3bd827e68eccf359823c8bc3518075abb4.zip cpython-0996fa3bd827e68eccf359823c8bc3518075abb4.tar.gz cpython-0996fa3bd827e68eccf359823c8bc3518075abb4.tar.bz2 |
merge 3.5
Issue #26804: urllib.request will prefer lower_case proxy environment variables
over UPPER_CASE or Mixed_Case ones.
Patch contributed by Hans-Peter Jansen. Reviewed by Martin Panter and Senthil Kumaran.
Diffstat (limited to 'Lib/urllib')
-rw-r--r-- | Lib/urllib/request.py | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 909c2cf..e0a96e0 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2455,19 +2455,35 @@ def getproxies_environment(): """ proxies = {} + # in order to prefer lowercase variables, process environment in + # two passes: first matches any, second pass matches lowercase only for name, value in os.environ.items(): name = name.lower() if value and name[-6:] == '_proxy': proxies[name[:-6]] = value + for name, value in os.environ.items(): + if name[-6:] == '_proxy': + name = name.lower() + if value: + proxies[name[:-6]] = value + else: + proxies.pop(name[:-6], None) return proxies -def proxy_bypass_environment(host): +def proxy_bypass_environment(host, proxies=None): """Test if proxies should not be used for a particular host. - Checks the environment for a variable named no_proxy, which should - be a list of DNS suffixes separated by commas, or '*' for all hosts. + Checks the proxy dict for the value of no_proxy, which should + be a list of comma separated DNS suffixes, or '*' for all hosts. + """ - no_proxy = os.environ.get('no_proxy', '') or os.environ.get('NO_PROXY', '') + if proxies is None: + proxies = getproxies_environment() + # don't bypass, if no_proxy isn't specified + try: + no_proxy = proxies['no'] + except KeyError: + return 0 # '*' is special case for always bypass if no_proxy == '*': return 1 @@ -2562,8 +2578,15 @@ if sys.platform == 'darwin': def proxy_bypass(host): - if getproxies_environment(): - return proxy_bypass_environment(host) + """Return True, if host should be bypassed. + + Checks proxy settings gathered from the environment, if specified, + or from the MacOSX framework SystemConfiguration. + + """ + proxies = getproxies_environment() + if proxies: + return proxy_bypass_environment(host, proxies) else: return proxy_bypass_macosx_sysconf(host) @@ -2677,14 +2700,15 @@ elif os.name == 'nt': return 0 def proxy_bypass(host): - """Return a dictionary of scheme -> proxy server URL mappings. + """Return True, if host should be bypassed. - Returns settings gathered from the environment, if specified, + Checks proxy settings gathered from the environment, if specified, or the registry. """ - if getproxies_environment(): - return proxy_bypass_environment(host) + proxies = getproxies_environment() + if proxies: + return proxy_bypass_environment(host, proxies) else: return proxy_bypass_registry(host) |