diff --git a/ceilometerclient/common/http.py b/ceilometerclient/common/http.py index 1a4b58c7..cfd95cf6 100644 --- a/ceilometerclient/common/http.py +++ b/ceilometerclient/common/http.py @@ -48,6 +48,7 @@ class HTTPClient(object): self.endpoint = endpoint self.auth_token = kwargs.get('token') self.connection_params = self.get_connection_params(endpoint, **kwargs) + self.proxy_url = self.get_proxy_url() @staticmethod def get_connection_params(endpoint, **kwargs): @@ -74,8 +75,13 @@ class HTTPClient(object): def get_connection(self): _class = self.connection_params[0] try: - return _class(*self.connection_params[1][0:2], - **self.connection_params[2]) + if self.proxy_url: + proxy_parts = urlutils.urlparse(self.proxy_url) + return _class(proxy_parts.hostname, proxy_parts.port, + **self.connection_params[2]) + else: + return _class(*self.connection_params[1][0:2], + **self.connection_params[2]) except httplib.InvalidURL: raise exc.InvalidEndpoint() @@ -137,7 +143,10 @@ class HTTPClient(object): conn = self.get_connection() try: - conn_url = self._make_connection_url(url) + if self.proxy_url: + conn_url = self.endpoint + self._make_connection_url(url) + else: + conn_url = self._make_connection_url(url) conn.request(method, conn_url, **kwargs) resp = conn.getresponse() except socket.gaierror as e: @@ -202,6 +211,15 @@ class HTTPClient(object): 'application/octet-stream') return self._http_request(url, method, **kwargs) + def get_proxy_url(self): + scheme = urlutils.urlparse(self.endpoint).scheme + if scheme == 'https': + return os.environ.get('https_proxy') + elif scheme == 'http': + return os.environ.get('http_proxy') + msg = 'Unsupported scheme: %s' % scheme + raise exc.InvalidEndpoint(msg) + class VerifiedHTTPSConnection(httplib.HTTPSConnection): """httplib-compatibile connection using client-side SSL authentication