no_proxy can now be upper case
ProxyInfo.from_environment now honors no_proxy=*
ProxyInfo.applies_to now matches the domain of the host (consistent with Python's urllib)
diff --git a/python2/httplib2/__init__.py b/python2/httplib2/__init__.py
index b1b882a..ff7e384 100644
--- a/python2/httplib2/__init__.py
+++ b/python2/httplib2/__init__.py
@@ -728,6 +728,7 @@
name/password are mapped to key/cert."""
pass
+class AllHosts(object): pass
class ProxyInfo(object):
"""Collect information required to use a proxy."""
@@ -767,8 +768,10 @@
if not url: return
pi = cls.from_url(url, method)
- no_proxy = os.environ.get('no_proxy', '')
+ no_proxy = os.environ.get('no_proxy', os.environ.get('NO_PROXY', ''))
bypass_hosts = no_proxy.split(',') if no_proxy else []
+ # special case, no_proxy=* means all hosts bypassed
+ if no_proxy == '*': bypass_hosts = AllHosts
pi.bypass_hosts = bypass_hosts
return pi
@@ -796,7 +799,14 @@
)
def applies_to(self, hostname):
- return hostname not in self.bypass_hosts
+ return not self.bypass_host(hostname)
+
+ def bypass_host(self, hostname):
+ """Has this host been excluded from the proxy config"""
+ return self.bypass_hosts is AllHosts or any(
+ hostname.endswith(domain)
+ for domain in self.bypass_hosts
+ )
class HTTPConnectionWithTimeout(httplib.HTTPConnection):
diff --git a/python2/httplib2test.py b/python2/httplib2test.py
index 705cc38..48b2360 100755
--- a/python2/httplib2test.py
+++ b/python2/httplib2test.py
@@ -1599,10 +1599,18 @@
def test_applies_to(self):
os.environ['http_proxy'] = 'http://myproxy.example.com:80'
os.environ['https_proxy'] = 'http://myproxy.example.com:81'
- os.environ['no_proxy'] = 'localhost,otherhost.domain.local'
+ os.environ['no_proxy'] = 'localhost,otherhost.domain.local,example.com'
pi = httplib2.ProxyInfo.from_environment()
self.assertFalse(pi.applies_to('localhost'))
self.assertTrue(pi.applies_to('www.google.com'))
+ self.assertFalse(pi.applies_to('www.example.com'))
+
+ def test_no_proxy_star(self):
+ os.environ['http_proxy'] = 'http://myproxy.example.com:80'
+ os.environ['NO_PROXY'] = '*'
+ pi = httplib2.ProxyInfo.from_environment()
+ for host in ('localhost', '169.254.38.192', 'www.google.com'):
+ self.assertFalse(pi.applies_to(host))
if __name__ == '__main__':