lib/ycmd/server.py
def _send_request(self, handler,
                      request_params=None, method=None, timeout=None):
        '''
        Sends a request to the associated ycmd server and returns the response.
        The `handler` should be one of the ycmd handler constants.
        If `request_params` are supplied, it should be an instance of
        `RequestParameters`. Most handlers require these parameters, and ycmd
        will reject any requests that are missing them.
        If `method` is provided, it should be an HTTP verb (e.g. 'GET',
        'POST'). If omitted, it is set to 'GET' when no parameters are given,
        and 'POST' otherwise.
        '''
        with self._lock:
            if self._status == Server.STOPPING:
                self._logger.warning(
                    'server is shutting down, cannot send request to it',
                )
                return None

        try:
            wait_status_timeout = (
                timeout if timeout is not None
                else QUEUED_REQUEST_MAX_WAIT_TIME
            )
            self.wait_for_status(Server.RUNNING, timeout=wait_status_timeout)
        except TimeoutError as e:
            self._logger.warning(
                'server not ready, dropping due to timeout: %r', e, exc_info=e,
            )
            raise

        assert request_params is None or \
            isinstance(request_params, (RequestParameters, dict)), \
            '[internal] request parameters is not RequestParameters: %r' % \
            (request_params)
        assert method is None or isinstance(method, str), \
            '[internal] method is not a str: %r' % (method)

        has_params = request_params is not None

        if has_params:
            self._logger.debug('generating json body from parameters')
            if isinstance(request_params, dict):
                json_params = request_params.copy()
            elif isinstance(request_params, RequestParameters):
                json_params = request_params.to_json()
            else:
                raise TypeError(
                    'request parameters must be RequestParameters: %r' %
                    (request_params)
                )
            body = json_serialize(json_params)
        else:
            json_params = None
            body = None

        if isinstance(body, str):
            body = str_to_bytes(body)

        if not method:
            method = 'GET' if not has_params else 'POST'

        hmac_headers = self._generate_hmac_header(method, handler, body)
        content_type_headers = \
            {'Content-Type': 'application/json'} if has_params else None

        headers = {}
        if hmac_headers:
            headers.update(hmac_headers)
        if content_type_headers:
            headers.update(content_type_headers)

        self._logger.debug(
            'about to send a request with '
            'method, handler, params, headers: %s, %s, %s, %s',
            method, handler, truncate(json_params), truncate(headers),
        )

        with self._lock:
            host = self.hostname
            port = self.port

        response_status = None
        response_reason = None
        response_headers = None
        response_data = None
        try:
            connection = http.client.HTTPConnection(
                host=host, port=port, timeout=timeout,
            )
            connection.request(
                method=method,
                url=handler,
                body=body,
                headers=headers,
            )

            response = connection.getresponse()

            response_status = response.status
            response_reason = response.reason

            # verify response hmac before using it
            response_headers = response.getheaders()
            response_content_type = response.getheader('Content-Type')
            response_content_length = response.getheader('Content-Length', 0)
            response_content_hmac = response.getheader(YCMD_HMAC_HEADER)

            try:
                response_content_length = int(response_content_length)
            except ValueError:
                response_content_length = 0

            has_content = response_content_length > 0
            is_content_json = response_content_type == 'application/json'

            # extract response to check hmac
            if has_content:
                response_content = response.read()
                with self._lock:
                    hmac = self._hmac

                expected_content_hmac = calculate_hmac(
                    hmac, response_content,
                )

                if response_content_hmac != expected_content_hmac:
                    self._logger.error(
                        'server responded with incorrect hmac, '
                        'dropping response - expected, received: %r, %r',
                        expected_content_hmac, response_content_hmac,
                    )
                    response_content = None
                else:
                    self._logger.debug(
                        'response hmac matches, response is valid',
                    )
            else:
                response_content = None

            # parse response as json
            if response_content and is_content_json:
                response_data = json_parse(response_content)
            else:
                if has_content or response_content:
                    self._logger.warning(
                        'ycmd server response is not json, content type: %r',
                        response_content_type,
                    )
                response_data = None

        except http.client.HTTPException as e:
            self._logger.error('error during ycmd request: %s', e, exc_info=e)
        except ConnectionError as e:
            self._logger.debug(
                'connection error, ycmd server may be dead: %s', e,
            )

        self._logger.debug(
            'parsed status, reason, headers, data: %s, %s, %s, %s',
            response_status, response_reason, response_headers, response_data,
        )

        return response_data

        Sends a request to the associated ycmd server and returns the response.
        The `handler` should be one of the ycmd handler constants.
        If `request_params` are supplied, it should be an instance of
        `RequestParameters`. Most handlers require these parameters, and ycmd
        will reject any requests that are missing them.
        If `method` is provided, it should be an HTTP verb (e.g. 'GET',
        'POST'). If omitted, it is set to 'GET' when no parameters are given,
        and 'POST' otherwise.
        
Sends a request to the associated ycmd server and returns the response.
The `handler` should be one of the ycmd handler constants.
If `request_params` are supplied, it should be an instance of
`RequestParameters`. Most handlers require these parameters, and ycmd
will reject any requests that are missing them.
If `method` is provided, it should be an HTTP verb . If omitted, it is set to 'GET' when no parameters are given,
and 'POST' otherwise.
Sends a request to the associated ycmd server and returns the response . The ` handler ` should be one of the ycmd handler constants . If ` request_params ` are supplied it should be an instance of ` RequestParameters ` . Most handlers require these parameters and ycmd will reject any requests that are missing them . If ` method ` is provided it should be an HTTP verb . If omitted it is set to ' GET ' when no parameters are given and ' POST ' otherwise .
def _send_request(self, handler,
                      request_params=None, method=None, timeout=None):
        with self._lock:
            if self._status == Server.STOPPING:
                self._logger.warning(
                    'server is shutting down, cannot send request to it',
                )
                return None
        try:
            wait_status_timeout = (
                timeout if timeout is not None
                else QUEUED_REQUEST_MAX_WAIT_TIME
            )
            self.wait_for_status(Server.RUNNING, timeout=wait_status_timeout)
        except TimeoutError as e:
            self._logger.warning(
                'server not ready, dropping due to timeout: %r', e, exc_info=e,
            )
            raise
        assert request_params is None or \
            isinstance(request_params, (RequestParameters, dict)), \
            '[internal] request parameters is not RequestParameters: %r' % \
            (request_params)
        assert method is None or isinstance(method, str), \
            '[internal] method is not a str: %r' % (method)
        has_params = request_params is not None
        if has_params:
            self._logger.debug('generating json body from parameters')
            if isinstance(request_params, dict):
                json_params = request_params.copy()
            elif isinstance(request_params, RequestParameters):
                json_params = request_params.to_json()
            else:
                raise TypeError(
                    'request parameters must be RequestParameters: %r' %
                    (request_params)
                )
            body = json_serialize(json_params)
        else:
            json_params = None
            body = None
        if isinstance(body, str):
            body = str_to_bytes(body)
        if not method:
            method = 'GET' if not has_params else 'POST'
        hmac_headers = self._generate_hmac_header(method, handler, body)
        content_type_headers = \
            {'Content-Type': 'application/json'} if has_params else None
        headers = {}
        if hmac_headers:
            headers.update(hmac_headers)
        if content_type_headers:
            headers.update(content_type_headers)
        self._logger.debug(
            'about to send a request with '
            'method, handler, params, headers: %s, %s, %s, %s',
            method, handler, truncate(json_params), truncate(headers),
        )
        with self._lock:
            host = self.hostname
            port = self.port
        response_status = None
        response_reason = None
        response_headers = None
        response_data = None
        try:
            connection = http.client.HTTPConnection(
                host=host, port=port, timeout=timeout,
            )
            connection.request(
                method=method,
                url=handler,
                body=body,
                headers=headers,
            )
            response = connection.getresponse()
            response_status = response.status
            response_reason = response.reason
            response_headers = response.getheaders()
            response_content_type = response.getheader('Content-Type')
            response_content_length = response.getheader('Content-Length', 0)
            response_content_hmac = response.getheader(YCMD_HMAC_HEADER)
            try:
                response_content_length = int(response_content_length)
            except ValueError:
                response_content_length = 0
            has_content = response_content_length > 0
            is_content_json = response_content_type == 'application/json'
            if has_content:
                response_content = response.read()
                with self._lock:
                    hmac = self._hmac
                expected_content_hmac = calculate_hmac(
                    hmac, response_content,
                )
                if response_content_hmac != expected_content_hmac:
                    self._logger.error(
                        'server responded with incorrect hmac, '
                        'dropping response - expected, received: %r, %r',
                        expected_content_hmac, response_content_hmac,
                    )
                    response_content = None
                else:
                    self._logger.debug(
                        'response hmac matches, response is valid',
                    )
            else:
                response_content = None
            if response_content and is_content_json:
                response_data = json_parse(response_content)
            else:
                if has_content or response_content:
                    self._logger.warning(
                        'ycmd server response is not json, content type: %r',
                        response_content_type,
                    )
                response_data = None
        except http.client.HTTPException as e:
            self._logger.error('error during ycmd request: %s', e, exc_info=e)
        except ConnectionError as e:
            self._logger.debug(
                'connection error, ycmd server may be dead: %s', e,
            )
        self._logger.debug(
            'parsed status, reason, headers, data: %s, %s, %s, %s',
            response_status, response_reason, response_headers, response_data,
        )
        return response_data
def _send_request ( self , handler , request_params = None , method = None , timeout = None ) : with self . _lock : if self . _status == Server . STOPPING : self . _logger . warning ( 'server is shutting down, cannot send request to it' , ) return None try : wait_status_timeout = ( timeout if timeout is not None else QUEUED_REQUEST_MAX_WAIT_TIME ) self . wait_for_status ( Server . RUNNING , timeout = wait_status_timeout ) except TimeoutError as e : self . _logger . warning ( 'server not ready, dropping due to timeout: %r' , e , exc_info = e , ) raise assert request_params is None or isinstance ( request_params , ( RequestParameters , dict ) ) , '[internal] request parameters is not RequestParameters: %r' % ( request_params ) assert method is None or isinstance ( method , str ) , '[internal] method is not a str: %r' % ( method ) has_params = request_params is not None if has_params : self . _logger . debug ( 'generating json body from parameters' ) if isinstance ( request_params , dict ) : json_params = request_params . copy ( ) elif isinstance ( request_params , RequestParameters ) : json_params = request_params . to_json ( ) else : raise TypeError ( 'request parameters must be RequestParameters: %r' % ( request_params ) ) body = json_serialize ( json_params ) else : json_params = None body = None if isinstance ( body , str ) : body = str_to_bytes ( body ) if not method : method = 'GET' if not has_params else 'POST' hmac_headers = self . _generate_hmac_header ( method , handler , body ) content_type_headers = { 'Content-Type' : 'application/json' } if has_params else None headers = { } if hmac_headers : headers . update ( hmac_headers ) if content_type_headers : headers . update ( content_type_headers ) self . _logger . debug ( 'about to send a request with ' 'method, handler, params, headers: %s, %s, %s, %s' , method , handler , truncate ( json_params ) , truncate ( headers ) , ) with self . _lock : host = self . hostname port = self . port response_status = None response_reason = None response_headers = None response_data = None try : connection = http . client . HTTPConnection ( host = host , port = port , timeout = timeout , ) connection . request ( method = method , url = handler , body = body , headers = headers , ) response = connection . getresponse ( ) response_status = response . status response_reason = response . reason response_headers = response . getheaders ( ) response_content_type = response . getheader ( 'Content-Type' ) response_content_length = response . getheader ( 'Content-Length' , 0 ) response_content_hmac = response . getheader ( YCMD_HMAC_HEADER ) try : response_content_length = int ( response_content_length ) except ValueError : response_content_length = 0 has_content = response_content_length > 0 is_content_json = response_content_type == 'application/json' if has_content : response_content = response . read ( ) with self . _lock : hmac = self . _hmac expected_content_hmac = calculate_hmac ( hmac , response_content , ) if response_content_hmac != expected_content_hmac : self . _logger . error ( 'server responded with incorrect hmac, ' 'dropping response - expected, received: %r, %r' , expected_content_hmac , response_content_hmac , ) response_content = None else : self . _logger . debug ( 'response hmac matches, response is valid' , ) else : response_content = None if response_content and is_content_json : response_data = json_parse ( response_content ) else : if has_content or response_content : self . _logger . warning ( 'ycmd server response is not json, content type: %r' , response_content_type , ) response_data = None except http . client . HTTPException as e : self . _logger . error ( 'error during ycmd request: %s' , e , exc_info = e ) except ConnectionError as e : self . _logger . debug ( 'connection error, ycmd server may be dead: %s' , e , ) self . _logger . debug ( 'parsed status, reason, headers, data: %s, %s, %s, %s' , response_status , response_reason , response_headers , response_data , ) return response_data
Sends a request to the associated ycmd server and returns the response.
Sends a request to the associated ycmd server and returns the response .
'''
        Sends a request to the associated ycmd server and returns the response.
        The `handler` should be one of the ycmd handler constants.
        If `request_params` are supplied, it should be an instance of
        `RequestParameters`. Most handlers require these parameters, and ycmd
        will reject any requests that are missing them.
        If `method` is provided, it should be an HTTP verb (e.g. 'GET',
        'POST'). If omitted, it is set to 'GET' when no parameters are given,
        and 'POST' otherwise.
        ''' # verify response hmac before using it # extract response to check hmac # parse response as json
self
handler
request_params
method
timeout