2014-01-13 14:08:43 -05:00
|
|
|
# Copyright (C) 2013 Google Inc.
|
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
|
|
|
|
from future import standard_library
|
|
|
|
standard_library.install_aliases()
|
|
|
|
from builtins import * # noqa
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
import requests
|
2016-02-27 19:12:24 -05:00
|
|
|
import urllib.parse
|
2016-02-19 14:02:58 -05:00
|
|
|
import json
|
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-10-02 20:09:25 -04:00
|
|
|
from retries import retries
|
2013-10-01 19:21:17 -04:00
|
|
|
from requests_futures.sessions import FuturesSession
|
2013-10-30 21:08:35 -04:00
|
|
|
from ycm.unsafe_thread_pool_executor import UnsafeThreadPoolExecutor
|
2013-09-20 20:24:34 -04:00
|
|
|
from ycm import vimsupport
|
2016-02-19 14:02:58 -05:00
|
|
|
from ycmd.utils import ToBytes
|
|
|
|
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'}
|
|
|
|
_EXECUTOR = UnsafeThreadPoolExecutor( max_workers = 30 )
|
2013-10-17 17:21:37 -04:00
|
|
|
# Setting this to None seems to screw up the Requests/urllib3 libs.
|
2014-03-07 14:28:53 -05:00
|
|
|
_DEFAULT_TIMEOUT_SEC = 30
|
2014-04-25 14:07:08 -04:00
|
|
|
_HMAC_HEADER = 'x-ycm-hmac'
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
class BaseRequest( object ):
|
|
|
|
def __init__( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def Start( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def Done( self ):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def Response( self ):
|
|
|
|
return {}
|
|
|
|
|
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).
|
|
|
|
@staticmethod
|
2014-03-07 14:28:53 -05:00
|
|
|
def GetDataFromHandler( handler, timeout = _DEFAULT_TIMEOUT_SEC ):
|
2013-11-20 15:33:57 -05:00
|
|
|
return JsonFromFuture( BaseRequest._TalkToHandlerAsync( '',
|
|
|
|
handler,
|
|
|
|
'GET',
|
|
|
|
timeout ) )
|
|
|
|
|
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).
|
2013-09-24 15:53:44 -04:00
|
|
|
@staticmethod
|
2014-03-07 14:28:53 -05:00
|
|
|
def PostDataToHandler( data, handler, timeout = _DEFAULT_TIMEOUT_SEC ):
|
2013-10-01 19:21:17 -04:00
|
|
|
return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data,
|
2013-10-14 14:08:15 -04:00
|
|
|
handler,
|
|
|
|
timeout ) )
|
2013-09-23 17:34:26 -04:00
|
|
|
|
2013-09-27 16:52:04 -04:00
|
|
|
|
2013-10-01 19:21:17 -04:00
|
|
|
# This returns a future! Use JsonFromFuture 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
|
2014-03-07 14:28:53 -05:00
|
|
|
def PostDataToHandlerAsync( data, handler, timeout = _DEFAULT_TIMEOUT_SEC ):
|
2013-11-20 15:33:57 -05:00
|
|
|
return BaseRequest._TalkToHandlerAsync( data, handler, 'POST', timeout )
|
|
|
|
|
|
|
|
|
|
|
|
# This returns a future! Use JsonFromFuture to get the value.
|
|
|
|
# |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,
|
2014-03-07 14:28:53 -05:00
|
|
|
timeout = _DEFAULT_TIMEOUT_SEC ):
|
2013-11-20 15:33:57 -05:00
|
|
|
def SendRequest( data, handler, method, timeout ):
|
2015-04-17 18:00:58 -04:00
|
|
|
request_uri = _BuildUri( handler )
|
2013-11-20 15:33:57 -05:00
|
|
|
if method == 'POST':
|
2016-02-19 14:02:58 -05:00
|
|
|
sent_data = _ToUtf8Json( data )
|
2014-04-25 14:07:08 -04:00
|
|
|
return BaseRequest.session.post(
|
2015-04-17 18:00:58 -04:00
|
|
|
request_uri,
|
2014-04-25 14:07:08 -04:00
|
|
|
data = sent_data,
|
2015-04-17 18:00:58 -04:00
|
|
|
headers = BaseRequest._ExtraHeaders( method,
|
|
|
|
request_uri,
|
|
|
|
sent_data ),
|
2014-04-25 14:07:08 -04:00
|
|
|
timeout = timeout )
|
2013-11-20 15:33:57 -05:00
|
|
|
if method == 'GET':
|
2014-04-25 14:07:08 -04:00
|
|
|
return BaseRequest.session.get(
|
2015-04-17 18:00:58 -04:00
|
|
|
request_uri,
|
|
|
|
headers = BaseRequest._ExtraHeaders( method, request_uri ),
|
2014-04-25 14:07:08 -04:00
|
|
|
timeout = timeout )
|
2013-10-01 19:21:17 -04:00
|
|
|
|
2013-10-17 17:45:16 -04:00
|
|
|
@retries( 5, delay = 0.5, backoff = 1.5 )
|
2013-11-20 15:33:57 -05:00
|
|
|
def DelayedSendRequest( data, handler, method ):
|
2015-04-17 18:00:58 -04:00
|
|
|
request_uri = _BuildUri( handler )
|
2013-11-20 15:33:57 -05:00
|
|
|
if method == 'POST':
|
2016-02-19 14:02:58 -05:00
|
|
|
sent_data = _ToUtf8Json( data )
|
2015-04-17 18:00:58 -04:00
|
|
|
return requests.post(
|
|
|
|
request_uri,
|
|
|
|
data = sent_data,
|
|
|
|
headers = BaseRequest._ExtraHeaders( method,
|
|
|
|
request_uri,
|
|
|
|
sent_data ) )
|
2013-11-20 15:33:57 -05:00
|
|
|
if method == 'GET':
|
2015-04-17 18:00:58 -04:00
|
|
|
return requests.get(
|
|
|
|
request_uri,
|
|
|
|
headers = BaseRequest._ExtraHeaders( method, request_uri ) )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2013-10-02 20:09:25 -04:00
|
|
|
if not _CheckServerIsHealthyWithCache():
|
2014-03-07 14:28:53 -05:00
|
|
|
return _EXECUTOR.submit( DelayedSendRequest, data, handler, method )
|
2013-10-02 20:09:25 -04:00
|
|
|
|
2013-11-20 15:33:57 -05:00
|
|
|
return SendRequest( data, handler, method, 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 ),
|
|
|
|
ToBytes( urllib.parse.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
|
|
|
|
|
2014-03-07 14:28:53 -05:00
|
|
|
session = FuturesSession( executor = _EXECUTOR )
|
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
|
|
|
|
|
|
|
|
2014-05-27 17:29:19 -04:00
|
|
|
def BuildRequestData( include_buffer_data = True ):
|
2013-09-20 20:24:34 -04:00
|
|
|
line, column = vimsupport.CurrentLineAndColumn()
|
2013-10-06 22:45:47 -04:00
|
|
|
filepath = vimsupport.GetCurrentBufferFilepath()
|
2013-09-20 20:24:34 -04:00
|
|
|
request_data = {
|
2014-05-09 18:57:04 -04:00
|
|
|
'line_num': line + 1,
|
|
|
|
'column_num': column + 1,
|
2013-10-14 16:29:28 -04:00
|
|
|
'filepath': filepath
|
2013-09-20 20:24:34 -04:00
|
|
|
}
|
|
|
|
|
2013-10-14 16:29:28 -04:00
|
|
|
if include_buffer_data:
|
|
|
|
request_data[ 'file_data' ] = vimsupport.GetUnsavedAndCurrentBufferData()
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
return request_data
|
|
|
|
|
|
|
|
|
2013-10-01 19:21:17 -04:00
|
|
|
def JsonFromFuture( future ):
|
|
|
|
response = future.result()
|
2014-04-25 14:07:08 -04:00
|
|
|
_ValidateResponseObject( response )
|
2013-10-01 19:21:17 -04:00
|
|
|
if response.status_code == 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
|
|
|
|
|
|
|
|
|
2014-12-09 18:22:58 -05:00
|
|
|
def HandleServerException( exception ):
|
|
|
|
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
|
|
|
|
vimsupport.PostVimMessage( serialized_exception )
|
|
|
|
|
|
|
|
|
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 ):
|
2016-02-27 20:38:38 -05:00
|
|
|
return native( ToBytes( urllib.parse.urljoin( BaseRequest.server_location,
|
|
|
|
handler ) ) )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
2013-10-02 20:09:25 -04:00
|
|
|
SERVER_HEALTHY = False
|
|
|
|
|
|
|
|
def _CheckServerIsHealthyWithCache():
|
|
|
|
global SERVER_HEALTHY
|
|
|
|
|
|
|
|
def _ServerIsHealthy():
|
2015-04-17 18:00:58 -04:00
|
|
|
request_uri = _BuildUri( 'healthy' )
|
|
|
|
response = requests.get( request_uri,
|
|
|
|
headers = BaseRequest._ExtraHeaders(
|
2016-02-27 20:38:38 -05:00
|
|
|
'GET', request_uri, bytes( b'' ) ) )
|
2014-04-25 14:07:08 -04:00
|
|
|
_ValidateResponseObject( response )
|
2013-10-02 20:09:25 -04:00
|
|
|
response.raise_for_status()
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
if SERVER_HEALTHY:
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
|
|
|
SERVER_HEALTHY = _ServerIsHealthy()
|
|
|
|
return SERVER_HEALTHY
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2016-02-19 14:02:58 -05: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' ] ) )
|