Skip to content

Commit cdaa0ad

Browse files
committed
Respect NO_PROXY env var in proxy_info_from_url().
This allows users to specify an override value for the noproxy list, in addition to overriding the proxy url, in proxy_info_from_url(). If no override is given, we should respect the values in the no_proxy/NO_PROXY environment variables, if present. This behavior can be seen in other tools like curl (see curl/curl#1140). I would also tweak this for the python3 module, but its ProxyInfo class doesn't even seem to have a bypass_hosts attribute.
1 parent 7d36dc6 commit cdaa0ad

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

python2/httplib2/__init__.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -841,20 +841,10 @@ def proxy_info_from_environment(method='http'):
841841
url = os.environ.get(env_var, os.environ.get(env_var.upper()))
842842
if not url:
843843
return
844-
pi = proxy_info_from_url(url, method)
844+
return proxy_info_from_url(url, method, None)
845845

846-
no_proxy = os.environ.get('no_proxy', os.environ.get('NO_PROXY', ''))
847-
bypass_hosts = []
848-
if no_proxy:
849-
bypass_hosts = no_proxy.split(',')
850-
# special case, no_proxy=* means all hosts bypassed
851-
if no_proxy == '*':
852-
bypass_hosts = AllHosts
853846

854-
pi.bypass_hosts = bypass_hosts
855-
return pi
856-
857-
def proxy_info_from_url(url, method='http'):
847+
def proxy_info_from_url(url, method='http', noproxy=None):
858848
"""
859849
Construct a ProxyInfo from a URL (such as http_proxy env var)
860850
"""
@@ -881,7 +871,7 @@ def proxy_info_from_url(url, method='http'):
881871
port = dict(https=443, http=80)[method]
882872

883873
proxy_type = 3 # socks.PROXY_TYPE_HTTP
884-
return ProxyInfo(
874+
pi = ProxyInfo(
885875
proxy_type = proxy_type,
886876
proxy_host = host,
887877
proxy_port = port,
@@ -890,6 +880,19 @@ def proxy_info_from_url(url, method='http'):
890880
proxy_headers = None,
891881
)
892882

883+
bypass_hosts = []
884+
# If not given an explicit noproxy value, respect values in env vars.
885+
if noproxy is None:
886+
noproxy = os.environ.get('no_proxy', os.environ.get('NO_PROXY', ''))
887+
# Special case: A single '*' character means all hosts should be bypassed.
888+
if noproxy == '*':
889+
bypass_hosts = httplib2.AllHosts
890+
elif noproxy.strip():
891+
bypass_hosts = noproxy.split(',')
892+
893+
pi.bypass_hosts = bypass_hosts
894+
return pi
895+
893896

894897
class HTTPConnectionWithTimeout(httplib.HTTPConnection):
895898
"""

0 commit comments

Comments
 (0)