Commits

Jason R. Coombs committed a966c67

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)

Comments (0)

Files changed (2)

python2/httplib2/__init__.py

     name/password are mapped to key/cert."""
     pass
 
+class AllHosts(object): pass
 
 class ProxyInfo(object):
     """Collect information required to use a proxy."""
         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
         )
 
     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):

python2/httplib2test.py

     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__':