Http() now detects the proxy configuration
diff --git a/python2/httplib2/__init__.py b/python2/httplib2/__init__.py
index ce153d6..b1b882a 100644
--- a/python2/httplib2/__init__.py
+++ b/python2/httplib2/__init__.py
@@ -776,7 +776,7 @@
@classmethod
def from_url(cls, url, method='http'):
"""
- Construct a ProxyInfo from a URL
+ Construct a ProxyInfo from a URL (such as http_proxy env var)
"""
url = urlparse.urlparse(url)
ident, sep, host_port = url.netloc.rpartition('@')
@@ -795,6 +795,9 @@
proxy_pass = password or None,
)
+ def applies_to(self, hostname):
+ return hostname not in self.bypass_hosts
+
class HTTPConnectionWithTimeout(httplib.HTTPConnection):
"""
@@ -1088,11 +1091,10 @@
and more.
"""
- def __init__(self, cache=None, timeout=None, proxy_info=None,
+ def __init__(self, cache=None, timeout=None,
+ proxy_info=ProxyInfo.from_environment,
ca_certs=None, disable_ssl_certificate_validation=False):
"""
- The value of proxy_info is a ProxyInfo instance.
-
If 'cache' is a string then it is used as a directory name for
a disk cache. Otherwise it must be an object that supports the
same interface as FileCache.
@@ -1102,6 +1104,13 @@
for example the docs of socket.setdefaulttimeout():
http://docs.python.org/library/socket.html#socket.setdefaulttimeout
+ `proxy_info` may be:
+ - a callable that takes the http scheme ('http' or 'https') and
+ returns a ProxyInfo instance per request. By default, uses
+ ProxyInfo.from_environment.
+ - a ProxyInfo instance (static proxy config).
+ - None (proxy disabled).
+
ca_certs is the path of a file containing root CA certificates for SSL
server certificate validation. By default, a CA cert file bundled with
httplib2 is used.
@@ -1349,6 +1358,8 @@
scheme = 'https'
authority = domain_port[0]
+ proxy_info = self._get_proxy_info(scheme, authority)
+
conn_key = scheme+":"+authority
if conn_key in self.connections:
conn = self.connections[conn_key]
@@ -1361,21 +1372,21 @@
conn = self.connections[conn_key] = connection_type(
authority, key_file=certs[0][0],
cert_file=certs[0][1], timeout=self.timeout,
- proxy_info=self.proxy_info,
+ proxy_info=proxy_info,
ca_certs=self.ca_certs,
disable_ssl_certificate_validation=
self.disable_ssl_certificate_validation)
else:
conn = self.connections[conn_key] = connection_type(
authority, timeout=self.timeout,
- proxy_info=self.proxy_info,
+ proxy_info=proxy_info,
ca_certs=self.ca_certs,
disable_ssl_certificate_validation=
self.disable_ssl_certificate_validation)
else:
conn = self.connections[conn_key] = connection_type(
authority, timeout=self.timeout,
- proxy_info=self.proxy_info)
+ proxy_info=proxy_info)
conn.set_debuglevel(debuglevel)
if 'range' not in headers and 'accept-encoding' not in headers:
@@ -1521,6 +1532,18 @@
return (response, content)
+ def _get_proxy_info(self, scheme, authority):
+ """Return a ProxyInfo instance (or None) based on the scheme
+ and authority.
+ """
+ proxy_info = self.proxy_info
+ if callable(proxy_info):
+ proxy_info = proxy_info(scheme)
+
+ if (hasattr(proxy_info, 'applies_to')
+ and not proxy_info.applies_to(authority)):
+ proxy_info = None
+ return proxy_info
class Response(dict):
diff --git a/python2/httplib2test.py b/python2/httplib2test.py
index bcfc073..705cc38 100755
--- a/python2/httplib2test.py
+++ b/python2/httplib2test.py
@@ -1596,6 +1596,14 @@
pi = httplib2.ProxyInfo.from_environment()
self.assertEquals(pi, None)
+ 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'
+ pi = httplib2.ProxyInfo.from_environment()
+ self.assertFalse(pi.applies_to('localhost'))
+ self.assertTrue(pi.applies_to('www.google.com'))
+
if __name__ == '__main__':
unittest.main()