Exemple #1
0
    def validate(self, _):
        """
        Component validation
        """
        # Check if we'll use an SSL connection
        self._uses_ssl = self._cert_file is not None

        if not self._address:
            # No address given, use the localhost address
            self._address = LOCALHOST_ADDRESS

        if self._port is None:
            # Random port
            self._port = 0
        else:
            # Ensure we have an integer
            self._port = int(self._port)
            if self._port < 0:
                # Random port
                self._port = 0

        # Normalize the request queue size
        try:
            self._request_queue_size = int(self._request_queue_size)
        except (ValueError, TypeError):
            self._request_queue_size = 5

        if self._request_queue_size <= 0:
            self._request_queue_size = 5

        # Normalize the extra properties
        if not isinstance(self._extra, dict):
            self._extra = {}

        # Set up the logger
        if self._logger_name is not None:
            if not self._logger:
                # Empty name, use the instance name
                self._logger_name = self._instance_name

            self._logger = logging.getLogger(self._logger_name)

            if self._logger_level is None:
                self._logger.level = logging.INFO
            else:
                self._logger.level = int(self._logger_level)

        self.log(
            logging.INFO,
            "Starting HTTP%s server: [%s]:%d ...",
            "S" if self._uses_ssl else "",
            self._address,
            self._port,
        )

        # Create the server
        self._server = _HttpServerFamily(
            (self._address, self._port),
            lambda *x: _RequestHandler(self, *x),
            self._request_queue_size,
            self._logger,
        )

        if self._uses_ssl:
            # Activate HTTPS if required
            self._server.socket = ssl_wrap.wrap_socket(
                self._server.socket,
                self._cert_file,
                self._key_file,
                self._key_password,
            )

        # Property update (if port was 0)
        self._port = self._server.server_port

        # Run it in a separate thread
        self._thread = threading.Thread(
            target=self._server.serve_forever,
            name="HttpService-{0}-Server".format(self._port),
        )
        self._thread.daemon = True
        self._thread.start()

        with self._binding_lock:
            # Set the validation flag up, once the server is ready
            self._validated = True

            # Register bound servlets
            for service, svc_ref in self._servlets_refs.items():
                self.__register_servlet_service(service, svc_ref)

        self.log(
            logging.INFO,
            "HTTP%s server started: [%s]:%d",
            "S" if self._uses_ssl else "",
            self._address,
            self._port,
        )
Exemple #2
0
    def validate(self, _):
        """
        Component validation
        """
        # Check if we'll use an SSL connection
        self._uses_ssl = self._cert_file is not None

        if not self._address:
            # No address given, use the localhost address
            self._address = LOCALHOST_ADDRESS

        if self._port is None:
            # Random port
            self._port = 0
        else:
            # Ensure we have an integer
            self._port = int(self._port)
            if self._port < 0:
                # Random port
                self._port = 0

        # Normalize the request queue size
        try:
            self._request_queue_size = int(self._request_queue_size)
        except (ValueError, TypeError):
            self._request_queue_size = 5

        if self._request_queue_size <= 0:
            self._request_queue_size = 5

        # Normalize the extra properties
        if not isinstance(self._extra, dict):
            self._extra = {}

        # Set up the logger
        if self._logger_name is not None:
            if not self._logger:
                # Empty name, use the instance name
                self._logger_name = self._instance_name

            self._logger = logging.getLogger(self._logger_name)

            if self._logger_level is None:
                self._logger.level = logging.INFO
            else:
                self._logger.level = int(self._logger_level)

        self.log(logging.INFO, "Starting HTTP%s server: [%s]:%d ...",
                 "S" if self._uses_ssl else "", self._address, self._port)

        # Create the server
        self._server = _HttpServerFamily(
            (self._address, self._port), lambda *x: _RequestHandler(self, *x),
            self._request_queue_size, self._logger)

        if self._uses_ssl:
            # Activate HTTPS if required
            self._server.socket = ssl_wrap.wrap_socket(self._server.socket,
                                                       self._cert_file,
                                                       self._key_file,
                                                       self._key_password)

        # Property update (if port was 0)
        self._port = self._server.server_port

        # Run it in a separate thread
        self._thread = threading.Thread(target=self._server.serve_forever,
                                        name="HttpService-{0}-Server".format(
                                            self._port))
        self._thread.daemon = True
        self._thread.start()

        with self._binding_lock:
            # Set the validation flag up, once the server is ready
            self._validated = True

            # Register bound servlets
            for service, svc_ref in self._servlets_refs.items():
                self.__register_servlet_service(service, svc_ref)

        self.log(logging.INFO, "HTTP%s server started: [%s]:%d",
                 "S" if self._uses_ssl else "", self._address, self._port)