diff --git a/web/client/codechecker_client/helpers/base.py b/web/client/codechecker_client/helpers/base.py index 7429fa92ec..3c51da9df3 100644 --- a/web/client/codechecker_client/helpers/base.py +++ b/web/client/codechecker_client/helpers/base.py @@ -9,13 +9,17 @@ Base Helper class for Thrift api calls. """ - +import sys from thrift.transport import THttpClient from thrift.protocol import TJSONProtocol from codechecker_client.credential_manager import SESSION_COOKIE_NAME from codechecker_client.product import create_product_url +from codechecker_common.logger import get_logger + +LOG = get_logger('system') + class BaseClientHelper(object): @@ -28,13 +32,43 @@ def __init__(self, protocol, host, port, uri, session_token=None, self.__port = port url = create_product_url(protocol, host, port, uri) - self.transport = THttpClient.THttpClient(url) + self.transport = None + + try: + self.transport = THttpClient.THttpClient(url) + except ValueError: + # Initalizing THttpClient may raise an exception if proxy settings + # are used but the port number is not a valid integer. + pass + finally: + # Thrift do not handle the use case when invalid proxy format is + # used (e.g.: no schema is specified). For this reason we need to + # verify the proxy format in our side. + self._validate_proxy_format() + self.protocol = TJSONProtocol.TJSONProtocol(self.transport) self.client = None self.get_new_token = get_new_token self._set_token(session_token) + def _validate_proxy_format(self): + """ + Validate the proxy settings. + If the proxy settings are invalid, it will print an error message and + stop the program. + """ + if self.transport and not self.transport.using_proxy(): + return + + if not self.transport or not self.transport.host or \ + not isinstance(self.transport.port, int): + LOG.error("Invalid proxy format! Check your " + "HTTP_PROXY/HTTPS_PROXY environment variables if " + "these are in the right format: " + "'http[s]://host:port'.") + sys.exit(1) + def _set_token(self, session_token): """ Set the given token in the transport layer. """ if not session_token: diff --git a/web/client/codechecker_client/thrift_call.py b/web/client/codechecker_client/thrift_call.py index 82bf99753f..800c74ca86 100644 --- a/web/client/codechecker_client/thrift_call.py +++ b/web/client/codechecker_client/thrift_call.py @@ -21,37 +21,6 @@ LOG = get_logger('system') -PROXY_VALIDATION_NEEDED = True - - -def proxy_settings_are_valid(transport): - """ - Return True if no proxy settings are used or the proxy format it valid. - """ - if not transport.using_proxy(): - return True - - return transport.host and isinstance(transport.port, int) - - -def validate_proxy_format(transport): - """ - It will check the proxy settings once and validate the proxy settings. - If the proxy settings are invalid, it will print an error message and - stop the program. - """ - global PROXY_VALIDATION_NEEDED - - if PROXY_VALIDATION_NEEDED: - if not proxy_settings_are_valid(transport): - LOG.error("Invalid proxy format! Check your " - "HTTP_PROXY/HTTPS_PROXY environment variables if " - "these are in the right format:" - "'http[s]://host:port'.") - sys.exit(1) - - PROXY_VALIDATION_NEEDED = False - def truncate_arg(arg, max_len=100): """ Truncate the given argument if the length is too large. """ @@ -69,11 +38,6 @@ def ThriftClientCall(function): funcName = function.__name__ def wrapper(self, *args, **kwargs): - # Thrift do not handle the use case when invalid proxy format is used - # (e.g.: no schema is specified). For this reason we need to verify - # the proxy format in our side. - validate_proxy_format(self.transport) - self.transport.open() func = getattr(self.client, funcName) try: