Skip to content

Commit

Permalink
[client] Move checking proxy settings to the BaseClientHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
csordasmarton committed Mar 4, 2021
1 parent 804cefd commit 71501e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
38 changes: 36 additions & 2 deletions web/client/codechecker_client/helpers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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:
Expand Down
36 changes: 0 additions & 36 deletions web/client/codechecker_client/thrift_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand All @@ -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:
Expand Down

0 comments on commit 71501e7

Please sign in to comment.