diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2011-08-09 10:30:46 -0400 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2011-08-09 10:30:46 -0400 |
| commit | 43840895b7117184e1edcfbf6b18d50224d1f0fb (patch) | |
| tree | 09d9a7ad9780edc221b05d37aa2d98c87b062316 /python2 | |
| parent | 8a487d0509ec5fe31b00f5f5760c445ce3631b44 (diff) | |
| download | httplib2-43840895b7117184e1edcfbf6b18d50224d1f0fb.tar.gz | |
Http() now detects the proxy configuration
Diffstat (limited to 'python2')
| -rw-r--r-- | python2/httplib2/__init__.py | 37 | ||||
| -rwxr-xr-x | python2/httplib2test.py | 8 |
2 files changed, 38 insertions, 7 deletions
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 @@ class ProxyInfo(object): @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 @@ class ProxyInfo(object): 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 @@ class Http(object): 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 @@ and more. 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 @@ a string that contains the response entity body. 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 @@ a string that contains the response entity body. 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 @@ a string that contains the response entity body. 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 @@ class TestProxyInfo(unittest.TestCase): 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() |
