def handle(self) -> Union[Response, tuple]: """ Handle a FastRPC request, returns Flask response """ accept_cts = self._get_accepted_content_types() if not accept_cts.intersection(self.allowed_content_types): logging.warning('No supported content type requested: "%s"', accept_cts) return 'Content types in Accept not supported', 400 if request.headers['Content-Type'] not in self.allowed_content_types: logging.warning('Content-Type "%s" is not supported', request.headers['Content-Type']) return 'Content-Type not supported', 400 if fastrpc: if FRPC_CONTENT_TYPE == request.headers['Content-Type']: # We will be loading binary data, which is sent through chunked transfer encoding - not very friendly. # Werkzeug doesn't recognize the header, so the data should be in request.stream BUT # since chunked transfer encoding isn't sending Content-Length header, as it does not make sense, # werkzeug needs some kind of middleware that handles it. In ideal world, we could use stream # because the middleware would set request.environ['wsgi.input_terminated'] - I found none that do that # which means for now we'll be supporting just uwsgi until I figure out how to do with with the others # like gunicorn etc. big TODO ! if uwsgi is None: raise NotImplementedError("This application needs to be running on uWSGI, I'm sorry! TODO :) ") request_data = uwsgi.chunked_read() else: request_data = request.data args, method_name = fastrpc.loads(request_data) else: args, method_name = xmlrpc.loads(request.data) logging.info('Calling method %s with args: %s', method_name, args) return self._create_response(method_name, args, accept_cts)
def __call__(self, data, request_handler_callback): request = fastrpc.loads(data) f = getattr(self.proxy, request[1]) try: data = (f(*request[0]), ) except fastrpc.Fault, e: data = e
def _process_rpc_response(self, response): try: if fastrpc is not None: response_data = fastrpc.loads(response.body)[0] else: response_data = xmlrpclib.loads(response.body)[0][0] except self.fault_cls as e: raise Fault(e.faultCode, e.faultString) else: return response_data
def __call__(self, data, request_handler_callback): request = fastrpc.loads(data) f = getattr(self.proxy, request[1]) try: data = (f(*request[0]), ) except fastrpc.Fault as e: data = e request_handler_callback( api.Response( fastrpc.dumps(data, methodresponse=True, useBinary=True), [("Content-Type", "application/x-frpc")]))
def post(self): # Kontrola vstupniho content type request_ct = self.request.headers['Content-Type'] if request_ct not in self.supported_ct: self.set_status(400) return # Zpracovani vystupniho content type accept_ct = self.request.headers.get('Accept') if accept_ct: accept_ct_set = set([ct.strip() for ct in accept_ct.split(',')]) else: # Hlavicka Accept neni, pouziju content type z requestu accept_ct_set = set([request_ct]) possible_response_cts = self.supported_ct & accept_ct_set # Kontrola pruniku podporovanych a vystupnich content type if not possible_response_cts: self.set_status(406) return # Vyhledani a zavolani funkce args, method_name = fastrpc.loads(self.request.body) method_func = self.context.rpc_methods[method_name] try: response = (method_func(self.context, *args),) except Exception as e: self.logger.exception("Call '%s() error'" % method_name) msg = "%s: %s" % (e.__class__.__name__, e) response = fastrpc.Fault(-500, msg) # Vyhledani nejlepsiho content type pro odpoved + serializace dat if 'application/x-frpc' in possible_response_cts: response_data = fastrpc.dumps( response, methodresponse=True, useBinary=True) response_ct = 'application/x-frpc' elif 'text/xml' in possible_response_cts: response_data = fastrpc.dumps( response, methodresponse=True, useBinary=False) response_ct = 'text/xml' # Hlavicky odpovedi self.set_header("Content-Type", response_ct) self.set_header("Content-Length", len(response_data)) self.set_header("Server", self.request.host) self.set_header("Date", time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) self.set_header("Accept", ",".join(self.supported_ct)) # Telo odpovedi self.write(response_data)
async def handle(self, request: Any) -> Any: """ Handle FastRPC request, returns aiohttp response """ accept_cts = self._get_accepted_content_types(request) if not accept_cts.intersection(self.allowed_content_types): logger.warn('No supported content type requested: "%s"', accept_cts) return web.Response(status=400, body='Content types in Accept not supported') if request.headers['Content-Type'] not in self.allowed_content_types: logger.warn('Conent-Type "%s" is not supported', request.headers['Content-Type']) return web.Response(status=400, body='Content-Type not supported') (args, method_name) = fastrpc.loads(await request.content.read()) logger.debug('Calling method %s with args: %s', method_name, args) return await self._create_response(method_name, args, accept_cts)
def get_method_name(request_body): try: return fastrpc.loads(request_body)[1] except RuntimeError as e: print(e) return ""
def get_method_name(request_body): try: return fastrpc.loads(request_body)[1] except RuntimeError, e: print e return ""