123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- # Copyright (C) 2013 Google Inc.
- #
- # 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/>.
- 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
- import requests
- import urllib.parse
- import json
- from base64 import b64decode, b64encode
- from retries import retries
- from requests_futures.sessions import FuturesSession
- from ycm.unsafe_thread_pool_executor import UnsafeThreadPoolExecutor
- from ycm import vimsupport
- from ycmd.utils import ToBytes
- from ycmd.hmac_utils import CreateRequestHmac, CreateHmac, SecureBytesEqual
- from ycmd.responses import ServerError, UnknownExtraConf
- _HEADERS = {'content-type': 'application/json'}
- _EXECUTOR = UnsafeThreadPoolExecutor( max_workers = 30 )
- # Setting this to None seems to screw up the Requests/urllib3 libs.
- _DEFAULT_TIMEOUT_SEC = 30
- _HMAC_HEADER = 'x-ycm-hmac'
- class BaseRequest( object ):
- def __init__( self ):
- pass
- def Start( self ):
- pass
- def Done( self ):
- return True
- def Response( self ):
- return {}
- # 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
- def GetDataFromHandler( handler, timeout = _DEFAULT_TIMEOUT_SEC ):
- return JsonFromFuture( BaseRequest._TalkToHandlerAsync( '',
- handler,
- 'GET',
- timeout ) )
- # This is the blocking version of the method. See below for async.
- # |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 PostDataToHandler( data, handler, timeout = _DEFAULT_TIMEOUT_SEC ):
- return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data,
- handler,
- timeout ) )
- # This returns a future! Use JsonFromFuture to get the value.
- # |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 PostDataToHandlerAsync( data, handler, timeout = _DEFAULT_TIMEOUT_SEC ):
- 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,
- timeout = _DEFAULT_TIMEOUT_SEC ):
- def SendRequest( data, handler, method, timeout ):
- request_uri = _BuildUri( handler )
- if method == 'POST':
- sent_data = _ToUtf8Json( data )
- return BaseRequest.session.post(
- request_uri,
- data = sent_data,
- headers = BaseRequest._ExtraHeaders( method,
- request_uri,
- sent_data ),
- timeout = timeout )
- if method == 'GET':
- return BaseRequest.session.get(
- request_uri,
- headers = BaseRequest._ExtraHeaders( method, request_uri ),
- timeout = timeout )
- @retries( 5, delay = 0.5, backoff = 1.5 )
- def DelayedSendRequest( data, handler, method ):
- request_uri = _BuildUri( handler )
- if method == 'POST':
- sent_data = _ToUtf8Json( data )
- return requests.post(
- request_uri,
- data = sent_data,
- headers = BaseRequest._ExtraHeaders( method,
- request_uri,
- sent_data ) )
- if method == 'GET':
- return requests.get(
- request_uri,
- headers = BaseRequest._ExtraHeaders( method, request_uri ) )
- if not _CheckServerIsHealthyWithCache():
- return _EXECUTOR.submit( DelayedSendRequest, data, handler, method )
- return SendRequest( data, handler, method, timeout )
- @staticmethod
- def _ExtraHeaders( method, request_uri, request_body = None ):
- if not request_body:
- request_body = ''
- headers = dict( _HEADERS )
- headers[ _HMAC_HEADER ] = b64encode(
- CreateRequestHmac( method,
- urllib.parse.urlparse( request_uri ).path,
- request_body,
- BaseRequest.hmac_secret ) )
- return headers
- session = FuturesSession( executor = _EXECUTOR )
- server_location = ''
- hmac_secret = ''
- def BuildRequestData( include_buffer_data = True ):
- line, column = vimsupport.CurrentLineAndColumn()
- filepath = vimsupport.GetCurrentBufferFilepath()
- request_data = {
- 'line_num': line + 1,
- 'column_num': column + 1,
- 'filepath': filepath
- }
- if include_buffer_data:
- request_data[ 'file_data' ] = vimsupport.GetUnsavedAndCurrentBufferData()
- return request_data
- def JsonFromFuture( future ):
- response = future.result()
- _ValidateResponseObject( response )
- if response.status_code == requests.codes.server_error:
- raise MakeServerException( response.json() )
- # 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
- 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 )
- def _ToUtf8Json( data ):
- return ToBytes( json.dumps( data ) if data else None )
- def _ValidateResponseObject( response ):
- our_hmac = CreateHmac( response.content, BaseRequest.hmac_secret )
- their_hmac = ToBytes( b64decode( response.headers[ _HMAC_HEADER ] ) )
- if not SecureBytesEqual( our_hmac, their_hmac ):
- raise RuntimeError( 'Received invalid HMAC for response!' )
- return True
- def _BuildUri( handler ):
- return urllib.parse.urljoin( BaseRequest.server_location, handler )
- SERVER_HEALTHY = False
- def _CheckServerIsHealthyWithCache():
- global SERVER_HEALTHY
- def _ServerIsHealthy():
- request_uri = _BuildUri( 'healthy' )
- response = requests.get( request_uri,
- headers = BaseRequest._ExtraHeaders(
- 'GET', request_uri, '' ) )
- _ValidateResponseObject( response )
- response.raise_for_status()
- return response.json()
- if SERVER_HEALTHY:
- return True
- try:
- SERVER_HEALTHY = _ServerIsHealthy()
- return SERVER_HEALTHY
- except:
- return False
- def MakeServerException( data ):
- if data[ 'exception' ][ 'TYPE' ] == UnknownExtraConf.__name__:
- return UnknownExtraConf( data[ 'exception' ][ 'extra_conf_file' ] )
- return ServerError( '{0}: {1}'.format( data[ 'exception' ][ 'TYPE' ],
- data[ 'message' ] ) )
|