Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parseServiceRequest(self, bytestream_from_client):
'''Validate the data stream received from the client.'''
# Convert the client request into a PB Request object
request = rpc_pb.Request()
# Catch anything which isn't a valid PB bytestream
try:
request.MergeFromString(bytestream_from_client)
except Exception, e:
raise error.BadRequestDataError("Invalid request from \
client (decodeError): " + str(e))
# Check the request is correctly initialized
if not request.IsInitialized():
raise error.BadRequestDataError("Client request is missing \
mandatory fields")
log.debug('Request = %s' % request)
return request
def validateAndExecuteRequest(self, input):
'''Match a client request to the corresponding service and method on
the server, and then call the service.'''
# Parse and validate the client's request
try:
request = self.parseServiceRequest(input)
except error.BadRequestDataError, e:
return self.handleError(e)
# Retrieve the requested service
try:
service = self.retrieveService(request.service_name)
except error.ServiceNotFoundError, e:
return self.handleError(e)
# Retrieve the requested method
try:
method = self.retrieveMethod(service, request.method_name)
except error.MethodNotFoundError, e:
return self.handleError(e)
# Retrieve the protocol message
try:
def validateRequest(self, request):
'''Validate the client request against the protocol file.'''
# Check the request is correctly initialized
if not request.IsInitialized():
raise error.BadRequestProtoError('Client request is missing\
mandatory fields')
def retrieveService(self, service_name):
'''Match the service request to a registered service.'''
service = self.socket_rpc_server.serviceMap.get(service_name)
if service is None:
msg = "Could not find service '%s'" % service_name
raise error.ServiceNotFoundError(msg)
return service
def tryToRetrieveServiceResponse(self, response_class):
if self.controller.failed():
return
if self.rpcResponse.HasField('error'):
self.controller.handleError(self.rpcResponse.error_reason,
self.rpcResponse.error)
return
if self.rpcResponse.HasField('response_proto'):
# Extract service response
try:
self.serviceResponse = self.channel.parseResponse(
self.rpcResponse.response_proto, response_class)
except error.BadResponseProtoError, e:
self.controller.handleError(rpc_pb.BAD_RESPONSE_PROTO,
e.message)
def retrieveProtoRequest(self, service, method, request):
''' Retrieve the users protocol message from the RPC message'''
proto_request = service.GetRequestClass(method)()
try:
proto_request.ParseFromString(request.request_proto)
except Exception, e:
raise error.BadRequestProtoError(unicode(e))
# Check the request parsed correctly
if not proto_request.IsInitialized():
raise error.BadRequestProtoError('Invalid protocol request \
from client')
return proto_request
def retrieveMethod(self, service, method_name):
'''Match the method request to a method of a registered service.'''
method = service.DESCRIPTOR.FindMethodByName(method_name)
if method is None:
msg = "Could not find method '%s' in service '%s'"\
% (method_name, service.DESCRIPTOR.name)
raise error.MethodNotFoundError(msg)
return method
def retrieveService(self, service_name):
'''Match the service request to a registered service.'''
service = self.socket_rpc_server.serviceMap.get(service_name)
if service is None:
msg = "Could not find service '%s'" % service_name
raise error.ServiceNotFoundError(msg)
return service
def tryToValidateRequest(self, request):
if self.controller.failed():
return
# Validate the request object
try:
self.channel.validateRequest(request)
except error.BadRequestProtoError, e:
self.controller.handleError(rpc_pb.BAD_REQUEST_PROTO,
e.message)
def parseResponse(self, byte_stream, response_class):
'''Parse a bytestream into a Response object of the requested type.'''
# Instantiate a Response object of the requested type
response = response_class()
# Catch anything which isn't a valid PB bytestream
try:
response.ParseFromString(byte_stream)
except Exception, e:
raise error.BadResponseProtoError("Invalid response \
(decodeError): " + str(e))
# Check the response has all mandatory fields initialized
if not response.IsInitialized():
raise error.BadResponseProtoError("Response not initialized")
return response