Beispiel #1
0
 def serve_forever(self):
     _, host, port, _, _, _, _ = parse_addr_url(self._url)
     self._http_server = make_server(host, port, self.get_wsgi_application())
     if self._ssl_args:
         self._http_server.socket = ssl.wrap_socket(
             self._http_server.socket, keyfile=self._ssl_args['keyfile'],
             certfile=self._ssl_args['certfile'], server_side=True)
     logger.info('Listening on: %s', self._url)
     self._http_server.serve_forever()
Beispiel #2
0
    def register_handler(self,
                         addr,
                         msg_type,
                         protocol_handler,
                         ssl_args=None):
        """
        Register protocol handler

        :type  addr: :class:`str`
        :param addr: addr url
        :type  msg_type: :class:`str`
        :param msg_type: protocol message type
        :type  protocol_handler: :class:`vmware.vapi.protocol.server.transport.\
            async_protocol_handler.AsyncProtocolHandler`
        :param protocol_handler: protocol handler for this addr
        :type  ssl_args: :class:`dict`
        :param ssl_args: ssl arguments
        """

        assert (protocol_handler)
        # addr is url
        scheme, host, port, _, _, path, _ = parse_addr_url(addr)
        # https / vmwares
        if scheme not in self.SUPPORTED_SCHEMES:
            logger.error('Unsupported url scheme: %s', addr)
            return

        # Get ssl context
        ssl_context = None
        if scheme == 'https' or scheme == 'vmwares':
            ssl_context = self.get_ssl_context(ssl_args)

        with self.lock:
            if scheme == 'http' or scheme == 'https':
                protocol_factory = self.protocol_factories.get((host, port))
                if not protocol_factory:
                    protocol_factory = TwistedHttpSite()
                    self.protocol_factories[(host, port)] = protocol_factory

                if not protocol_factory.add_res_handler(
                        path, msg_type, protocol_handler):
                    logger.error('Failed to add resource handler: %s', addr)
                    return
            elif scheme == 'vmware' or scheme == 'vmwares':
                protocol_factory = TwistedVmwareProtocolFactory(
                    protocol_handler)
            else:
                raise ValueError("Scheme %s is not supported" % scheme)

            self.handlers_map[(host, port)] = (protocol_factory, ssl_context)
Beispiel #3
0
    def test_urls(self):
        for scheme in self.schemes:
            for host in self.hosts:
                substitution_vars = {'${scheme}': scheme, '${host}': host}
                for url, answer in self.urls:
                    test_url = self.sub_vars(url, substitution_vars)
                    expected_answer = [self.sub_vars(token, substitution_vars)
                                                           for token in answer]

                    test_answer = parse_addr_url(test_url)
                    if list(test_answer) != expected_answer:
                        logging.info('url: %s' % test_url)
                        logging.info('got     : %s' % list(test_answer))
                        logging.info('expected: %s' % expected_answer)
                        self.assertTrue(False)
    def __init__(self, ssl_args, url, disable_conn_pool=False):
        """
        http rpc provider init

        :type  ssl_args: :class:`dict`
        :param ssl_args: ssl arguments
        :type  url: :class:`str`
        :param url: url to connected to
        :type disable_conn_pool: :class: 'bool'
        :param disable_conn_pool: disable connection pooling
        """
        HTTPProvider.__init__(self)
        self.ssl_enabled = False
        self.ssl_args = ssl_args

        scheme, host, port, user, password, path, _ = parse_addr_url(url)
        assert (scheme in ['http', 'https'])
        if scheme == 'https':
            self.ssl_enabled = True
        assert (user is None and password is None)  # NYI
        if host.startswith('!'):
            # Unix domain socket: hostname is '!' followed by
            # the URL-encoded socket path
            self.host = None
            self.uds = urllib.parse.unquote(host[1:])
            # SSL currently not supported for Unix domain sockets
            if self.ssl_enabled:
                raise Exception('SSL not supported on Unix domain sockets')
        else:
            self.host = host
            self.port = port
            self.uds = None
        self.path = path
        self.cookie = ''
        self.accept_compress_response = True

        global use_connection_pool
        if disable_conn_pool:
            use_connection_pool = False

        if self.uds is None and use_connection_pool:
            self.manager = PoolManager(num_pools=NUM_OF_POOL,
                                       maxsize=POOL_SIZE,
                                       timeout=CONNECTION_POOL_TIMEOUT,
                                       **self.ssl_args)
    def register_handler(self,
                         addr,
                         msg_type,
                         protocol_handler,
                         ssl_args=None):
        """
        Register protocol handler

        :type  addr: :class:`str`
        :param addr: addr url
        :type  msg_type: :class:`str`
        :param msg_type: protocol message type
        :type  protocol_handler: :class:`vmware.vapi.protocol.server.transport.\
            async_protocol_handler.AsyncProtocolHandler`
        :param protocol_handler: protocol handler for this addr
        :type  ssl_args: :class:`dict`
        :param ssl_args: ssl arguments
        """

        logger.info("register_handler: msg: %s addr: %s", msg_type, addr)

        # addr is in the form of url
        scheme, host, port, _, _, path, _ = parse_addr_url(addr)
        if host is None:
            host = ''

        if scheme not in self.SUPPORTED_SCHEMES:
            logger.error('Unsupported url scheme: %s', addr)
            return

        if not protocol_handler:
            logger.error('No protocol handler: %s', addr)
            return

        if scheme not in ('https'):
            ssl_args = None

        if scheme in ('http', 'https'):
            protocol_factory = self.protocol_factories.get(('http', port))
            if not protocol_factory:
                protocol_factory = HttpFactory()
                self.protocol_factories[('http', port)] = protocol_factory
            protocol_factory.add_handler(path, msg_type, protocol_handler)

        self.handlers_map[(host, port)] = (protocol_factory, ssl_args)
Beispiel #6
0
    def __init__(self, ssl_args, url):
        """
        http rpc provider init

        :type  ssl_args: :class:`dict`
        :param ssl_args: ssl arguments
        :type  url: :class:`str`
        :param url: url to connected to
        """
        HTTPProvider.__init__(self)
        self.lock = threading.RLock()
        self.ssl_enabled = False
        self.ssl_args = ssl_args

        scheme, host, port, user, password, path, _ = parse_addr_url(url)
        assert (scheme == 'http' or scheme == 'https')
        if scheme == 'https':
            self.ssl_enabled = True
        assert (user is None and password is None)  # NYI
        if host.startswith('!'):
            # Unix domain socket: hostname is '!' followed by
            # the URL-encoded socket path
            self.host = None
            self.uds = urllib.parse.unquote(host[1:])
            # SSL currently not supported for Unix domain sockets
            if self.ssl_enabled:
                raise Exception('SSL not supported on Unix domain sockets')
        else:
            self.host = host
            if port:
                self.host += ':%d' % port
            self.uds = None
        self.path = path
        self.cookie = ''
        self.accept_compress_response = True

        self.pool = []
        self.pool_size = 8
        self.connection_pool_timeout = 8 * 60  # 8 minutes