예제 #1
0
 def get_connection(self):
     _class = self.connection_params[0]
     try:
         return _class(*self.connection_params[1][0:2],
                       **self.connection_params[2])
     except httplib.InvalidURL:
         raise exc.InvalidEndpoint()
 def get_proxy_url(self):
     scheme = parse.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)
    def _http_request(self, url, method, **kwargs):
        """Send an http request with the specified characteristics.

        Wrapper around httplib.HTTP(S)Connection.request to handle tasks such
        as setting headers and error handling.
        """
        # Copy the kwargs so we can reuse the original in case of redirects
        kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
        kwargs['headers'].setdefault('User-Agent', USER_AGENT)
        auth_token = self.auth_token()
        if auth_token:
            kwargs['headers'].setdefault('X-Auth-Token', auth_token)

        self.log_curl_request(method, url, kwargs)
        conn = self.get_connection()

        try:
            if self.proxy_url:
                conn_url = (self.endpoint.rstrip('/') +
                            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:
            message = ("Error finding address for %(url)s: %(e)s" %
                       dict(url=url, e=e))
            raise exc.InvalidEndpoint(message=message)
        except (socket.error, socket.timeout) as e:
            endpoint = self.endpoint
            message = ("Error communicating with %(endpoint)s %(e)s" %
                       dict(endpoint=endpoint, e=e))
            raise exc.CommunicationError(message=message)

        body_iter = ResponseBodyIterator(resp)

        # Read body into string if it isn't obviously image data
        if resp.getheader('content-type', None) != 'application/octet-stream':
            body_str = ''.join([chunk for chunk in body_iter])
            self.log_http_response(resp, body_str)
            body_iter = six.StringIO(body_str)
        else:
            self.log_http_response(resp)

        if 400 <= resp.status < 600:
            LOG.warn("Request returned failure status.")
            raise exc.from_response(resp, ''.join(body_iter))
        elif resp.status in (301, 302, 305):
            # Redirected. Reissue the request to the new location.
            return self._http_request(resp['location'], method, **kwargs)
        elif resp.status == 300:
            raise exc.from_response(resp)

        return resp, body_iter
 def get_connection(self):
     _class = self.connection_params[0]
     try:
         if self.proxy_url:
             proxy_parts = parse.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()
    def get_connection_params(endpoint, **kwargs):
        parts = parse.urlparse(endpoint)

        _args = (parts.hostname, parts.port, parts.path)
        _kwargs = {
            'timeout':
            (float(kwargs.get('timeout')) if kwargs.get('timeout') else 600)
        }

        if parts.scheme == 'https':
            _class = VerifiedHTTPSConnection
            _kwargs['cacert'] = kwargs.get('cacert', None)
            _kwargs['cert_file'] = kwargs.get('cert_file', None)
            _kwargs['key_file'] = kwargs.get('key_file', None)
            _kwargs['insecure'] = kwargs.get('insecure', False)
        elif parts.scheme == 'http':
            _class = httplib.HTTPConnection
        else:
            msg = 'Unsupported scheme: %s' % parts.scheme
            raise exc.InvalidEndpoint(msg)

        return (_class, _args, _kwargs)