2018-01-06 07:16:30 -05:00
|
|
|
# Copyright (C) 2013-2018 YouCompleteMe contributors
|
2013-09-20 20:24:34 -04:00
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import absolute_import
|
2017-03-09 09:57:27 -05:00
|
|
|
# Not installing aliases from python-future; it's unreliable and slow.
|
2016-02-27 19:12:24 -05:00
|
|
|
from builtins import * # noqa
|
|
|
|
|
2016-11-05 09:57:02 -04:00
|
|
|
import logging
|
2016-02-19 14:02:58 -05:00
|
|
|
import json
|
2018-01-06 07:16:30 -05:00
|
|
|
import vim
|
2016-02-27 20:38:38 -05:00
|
|
|
from future.utils import native
|
2014-05-09 13:37:20 -04:00
|
|
|
from base64 import b64decode, b64encode
|
2013-09-20 20:24:34 -04:00
|
|
|
from ycm import vimsupport
|
2017-09-16 19:22:36 -04:00
|
|
|
from ycmd.utils import ToBytes, urljoin, urlparse, GetCurrentDirectory
|
2016-02-19 14:02:58 -05:00
|
|
|
from ycmd.hmac_utils import CreateRequestHmac, CreateHmac, SecureBytesEqual
|
2014-05-13 16:09:19 -04:00
|
|
|
from ycmd.responses import ServerError, UnknownExtraConf
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2014-03-07 14:28:53 -05:00
|
|
|
_HEADERS = {'content-type': 'application/json'}
|
2016-11-28 07:21:28 -05:00
|
|
|
_CONNECT_TIMEOUT_SEC = 0.01
|
2013-10-17 17:21:37 -04:00
|
|
|
# Setting this to None seems to screw up the Requests/urllib3 libs.
|
2016-11-28 07:21:28 -05:00
|
|
|
_READ_TIMEOUT_SEC = 30
|
2014-04-25 14:07:08 -04:00
|
|
|
_HMAC_HEADER = 'x-ycm-hmac'
|
2016-11-05 09:57:02 -04:00
|
|
|
_logger = logging.getLogger( __name__ )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2016-02-28 17:38:23 -05:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
class BaseRequest( object ):
|
2016-03-06 12:13:43 -05:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
def __init__( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def Start( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def Done( self ):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def Response( self ):
|
|
|
|
return {}
|
|
|
|
|
2017-05-17 05:12:00 -04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def HandleFuture( future, display_message = True, truncate_message = False ):
|
|
|
|
"""Get the server response from a |future| object and catch any exception
|
|
|
|
while doing so. If an exception is raised because of a unknown
|
|
|
|
.ycm_extra_conf.py file, load the file or ignore it after asking the user.
|
|
|
|
For other exceptions, log the exception and display its message to the user
|
|
|
|
on the Vim status line. Unset the |display_message| parameter to hide the
|
|
|
|
message from the user. Set the |truncate_message| parameter to avoid
|
|
|
|
hit-enter prompts from this message."""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return _JsonFromFuture( future )
|
|
|
|
except UnknownExtraConf as e:
|
|
|
|
if vimsupport.Confirm( str( e ) ):
|
|
|
|
_LoadExtraConfFile( e.extra_conf_file )
|
|
|
|
else:
|
|
|
|
_IgnoreExtraConfFile( e.extra_conf_file )
|
|
|
|
except BaseRequest.Requests().exceptions.ConnectionError:
|
|
|
|
# We don't display this exception to the user since it is likely to happen
|
|
|
|
# for each subsequent request (typically if the server crashed) and we
|
|
|
|
# don't want to spam the user with it.
|
|
|
|
_logger.exception( 'Unable to connect to server' )
|
|
|
|
except Exception as e:
|
|
|
|
_logger.exception( 'Error while handling server response' )
|
|
|
|
if display_message:
|
|
|
|
DisplayServerException( e, truncate_message )
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-11-20 15:33:57 -05:00
|
|
|
# This method blocks
|
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
2017-05-17 05:12:00 -04:00
|
|
|
# See the HandleFuture method for the |display_message| and |truncate_message|
|
|
|
|
# parameters.
|
2013-11-20 15:33:57 -05:00
|
|
|
@staticmethod
|
2017-05-17 05:12:00 -04:00
|
|
|
def GetDataFromHandler( handler,
|
|
|
|
timeout = _READ_TIMEOUT_SEC,
|
|
|
|
display_message = True,
|
|
|
|
truncate_message = False ):
|
|
|
|
return BaseRequest.HandleFuture(
|
|
|
|
BaseRequest._TalkToHandlerAsync( '', handler, 'GET', timeout ),
|
|
|
|
display_message,
|
|
|
|
truncate_message )
|
2013-11-20 15:33:57 -05:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2013-10-01 19:21:17 -04:00
|
|
|
# This is the blocking version of the method. See below for async.
|
2013-10-14 14:08:15 -04:00
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
2017-05-17 05:12:00 -04:00
|
|
|
# See the HandleFuture method for the |display_message| and |truncate_message|
|
|
|
|
# parameters.
|
2013-09-24 15:53:44 -04:00
|
|
|
@staticmethod
|
2017-05-17 05:12:00 -04:00
|
|
|
def PostDataToHandler( data,
|
|
|
|
handler,
|
|
|
|
timeout = _READ_TIMEOUT_SEC,
|
|
|
|
display_message = True,
|
|
|
|
truncate_message = False ):
|
|
|
|
return BaseRequest.HandleFuture(
|
|
|
|
BaseRequest.PostDataToHandlerAsync( data, handler, timeout ),
|
|
|
|
display_message,
|
|
|
|
truncate_message )
|
|
|
|
|
|
|
|
|
|
|
|
# This returns a future! Use HandleFuture to get the value.
|
2013-10-14 14:08:15 -04:00
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
2013-10-01 19:21:17 -04:00
|
|
|
@staticmethod
|
2016-11-28 07:21:28 -05:00
|
|
|
def PostDataToHandlerAsync( data, handler, timeout = _READ_TIMEOUT_SEC ):
|
2013-11-20 15:33:57 -05:00
|
|
|
return BaseRequest._TalkToHandlerAsync( data, handler, 'POST', timeout )
|
|
|
|
|
|
|
|
|
2017-05-17 05:12:00 -04:00
|
|
|
# This returns a future! Use HandleFuture to get the value.
|
2013-11-20 15:33:57 -05:00
|
|
|
# |method| is either 'POST' or 'GET'.
|
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
|
|
|
@staticmethod
|
|
|
|
def _TalkToHandlerAsync( data,
|
|
|
|
handler,
|
|
|
|
method,
|
2016-11-28 07:21:28 -05:00
|
|
|
timeout = _READ_TIMEOUT_SEC ):
|
2017-01-24 22:40:47 -05:00
|
|
|
request_uri = _BuildUri( handler )
|
|
|
|
if method == 'POST':
|
|
|
|
sent_data = _ToUtf8Json( data )
|
2017-01-26 12:54:57 -05:00
|
|
|
return BaseRequest.Session().post(
|
2017-01-24 22:40:47 -05:00
|
|
|
request_uri,
|
|
|
|
data = sent_data,
|
|
|
|
headers = BaseRequest._ExtraHeaders( method,
|
|
|
|
request_uri,
|
|
|
|
sent_data ),
|
|
|
|
timeout = ( _CONNECT_TIMEOUT_SEC, timeout ) )
|
2017-01-26 12:54:57 -05:00
|
|
|
return BaseRequest.Session().get(
|
2017-01-24 22:40:47 -05:00
|
|
|
request_uri,
|
|
|
|
headers = BaseRequest._ExtraHeaders( method, request_uri ),
|
|
|
|
timeout = ( _CONNECT_TIMEOUT_SEC, timeout ) )
|
2013-10-02 20:09:25 -04:00
|
|
|
|
|
|
|
|
2014-04-25 14:07:08 -04:00
|
|
|
@staticmethod
|
2015-04-17 18:00:58 -04:00
|
|
|
def _ExtraHeaders( method, request_uri, request_body = None ):
|
2014-04-25 14:07:08 -04:00
|
|
|
if not request_body:
|
2016-02-27 20:38:38 -05:00
|
|
|
request_body = bytes( b'' )
|
2014-04-25 14:07:08 -04:00
|
|
|
headers = dict( _HEADERS )
|
2014-05-09 13:37:20 -04:00
|
|
|
headers[ _HMAC_HEADER ] = b64encode(
|
2016-02-27 20:38:38 -05:00
|
|
|
CreateRequestHmac( ToBytes( method ),
|
2017-03-09 09:57:27 -05:00
|
|
|
ToBytes( urlparse( request_uri ).path ),
|
2015-04-17 18:00:58 -04:00
|
|
|
request_body,
|
|
|
|
BaseRequest.hmac_secret ) )
|
2014-04-25 14:07:08 -04:00
|
|
|
return headers
|
|
|
|
|
2017-01-26 12:54:57 -05:00
|
|
|
|
|
|
|
# These two methods exist to avoid importing the requests module at startup;
|
|
|
|
# reducing loading time since this module is slow to import.
|
|
|
|
@classmethod
|
|
|
|
def Requests( cls ):
|
|
|
|
try:
|
|
|
|
return cls.requests
|
|
|
|
except AttributeError:
|
|
|
|
import requests
|
|
|
|
cls.requests = requests
|
|
|
|
return requests
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def Session( cls ):
|
|
|
|
try:
|
|
|
|
return cls.session
|
|
|
|
except AttributeError:
|
|
|
|
from ycm.unsafe_thread_pool_executor import UnsafeThreadPoolExecutor
|
|
|
|
from requests_futures.sessions import FuturesSession
|
|
|
|
executor = UnsafeThreadPoolExecutor( max_workers = 30 )
|
|
|
|
cls.session = FuturesSession( executor = executor )
|
|
|
|
return cls.session
|
|
|
|
|
|
|
|
|
2014-08-22 14:25:58 -04:00
|
|
|
server_location = ''
|
2014-04-25 14:07:08 -04:00
|
|
|
hmac_secret = ''
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
2018-01-06 07:16:30 -05:00
|
|
|
def BuildRequestData( buffer_number = None ):
|
|
|
|
"""Build request for the current buffer or the buffer with number
|
|
|
|
|buffer_number| if specified."""
|
2017-09-16 19:22:36 -04:00
|
|
|
working_dir = GetCurrentDirectory()
|
2018-01-06 07:16:30 -05:00
|
|
|
current_buffer = vim.current.buffer
|
2016-09-05 11:33:30 -04:00
|
|
|
|
2018-01-06 07:16:30 -05:00
|
|
|
if buffer_number and current_buffer.number != buffer_number:
|
2016-09-05 11:33:30 -04:00
|
|
|
# Cursor position is irrelevant when filepath is not the current buffer.
|
2018-01-06 07:16:30 -05:00
|
|
|
buffer_object = vim.buffers[ buffer_number ]
|
|
|
|
filepath = vimsupport.GetBufferFilepath( buffer_object )
|
2016-09-05 11:33:30 -04:00
|
|
|
return {
|
|
|
|
'filepath': filepath,
|
|
|
|
'line_num': 1,
|
|
|
|
'column_num': 1,
|
2017-09-16 19:22:36 -04:00
|
|
|
'working_dir': working_dir,
|
2018-01-06 07:16:30 -05:00
|
|
|
'file_data': vimsupport.GetUnsavedAndSpecifiedBufferData( buffer_object,
|
|
|
|
filepath )
|
2016-09-05 11:33:30 -04:00
|
|
|
}
|
|
|
|
|
2018-01-06 07:16:30 -05:00
|
|
|
current_filepath = vimsupport.GetBufferFilepath( current_buffer )
|
2013-09-20 20:24:34 -04:00
|
|
|
line, column = vimsupport.CurrentLineAndColumn()
|
2016-09-05 11:33:30 -04:00
|
|
|
|
|
|
|
return {
|
|
|
|
'filepath': current_filepath,
|
2014-05-09 18:57:04 -04:00
|
|
|
'line_num': line + 1,
|
|
|
|
'column_num': column + 1,
|
2017-09-16 19:22:36 -04:00
|
|
|
'working_dir': working_dir,
|
2018-01-06 07:16:30 -05:00
|
|
|
'file_data': vimsupport.GetUnsavedAndSpecifiedBufferData( current_buffer,
|
|
|
|
current_filepath )
|
2013-09-20 20:24:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-17 05:12:00 -04:00
|
|
|
def _JsonFromFuture( future ):
|
2013-10-01 19:21:17 -04:00
|
|
|
response = future.result()
|
2014-04-25 14:07:08 -04:00
|
|
|
_ValidateResponseObject( response )
|
2017-01-26 12:54:57 -05:00
|
|
|
if response.status_code == BaseRequest.Requests().codes.server_error:
|
2015-08-07 14:16:07 -04:00
|
|
|
raise MakeServerException( response.json() )
|
2013-10-01 19:21:17 -04:00
|
|
|
|
|
|
|
# We let Requests handle the other status types, we only handle the 500
|
|
|
|
# error code.
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
if response.text:
|
|
|
|
return response.json()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-11-05 09:57:02 -04:00
|
|
|
def _LoadExtraConfFile( filepath ):
|
|
|
|
BaseRequest.PostDataToHandler( { 'filepath': filepath },
|
|
|
|
'load_extra_conf_file' )
|
|
|
|
|
|
|
|
|
|
|
|
def _IgnoreExtraConfFile( filepath ):
|
|
|
|
BaseRequest.PostDataToHandler( { 'filepath': filepath },
|
|
|
|
'ignore_extra_conf_file' )
|
|
|
|
|
|
|
|
|
2017-05-17 05:12:00 -04:00
|
|
|
def DisplayServerException( exception, truncate_message = False ):
|
2014-12-09 18:22:58 -05:00
|
|
|
serialized_exception = str( exception )
|
|
|
|
|
|
|
|
# We ignore the exception about the file already being parsed since it comes
|
|
|
|
# up often and isn't something that's actionable by the user.
|
|
|
|
if 'already being parsed' in serialized_exception:
|
|
|
|
return
|
2017-05-17 05:12:00 -04:00
|
|
|
vimsupport.PostVimMessage( serialized_exception, truncate = truncate_message )
|
2014-12-09 18:22:58 -05:00
|
|
|
|
|
|
|
|
2016-02-19 14:02:58 -05:00
|
|
|
def _ToUtf8Json( data ):
|
|
|
|
return ToBytes( json.dumps( data ) if data else None )
|
|
|
|
|
|
|
|
|
2014-04-25 14:07:08 -04:00
|
|
|
def _ValidateResponseObject( response ):
|
2016-02-19 14:02:58 -05:00
|
|
|
our_hmac = CreateHmac( response.content, BaseRequest.hmac_secret )
|
|
|
|
their_hmac = ToBytes( b64decode( response.headers[ _HMAC_HEADER ] ) )
|
|
|
|
if not SecureBytesEqual( our_hmac, their_hmac ):
|
2014-04-25 14:07:08 -04:00
|
|
|
raise RuntimeError( 'Received invalid HMAC for response!' )
|
|
|
|
return True
|
|
|
|
|
2016-02-19 14:02:58 -05:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
def _BuildUri( handler ):
|
2017-03-09 09:57:27 -05:00
|
|
|
return native( ToBytes( urljoin( BaseRequest.server_location, handler ) ) )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
2015-08-07 14:16:07 -04:00
|
|
|
def MakeServerException( data ):
|
2013-10-08 19:21:43 -04:00
|
|
|
if data[ 'exception' ][ 'TYPE' ] == UnknownExtraConf.__name__:
|
2015-08-07 14:16:07 -04:00
|
|
|
return UnknownExtraConf( data[ 'exception' ][ 'extra_conf_file' ] )
|
2013-10-08 19:21:43 -04:00
|
|
|
|
2015-08-07 14:16:07 -04:00
|
|
|
return ServerError( '{0}: {1}'.format( data[ 'exception' ][ 'TYPE' ],
|
|
|
|
data[ 'message' ] ) )
|