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)
class UpstreamServerProvider(api.UpstreamServerProvider): def __init__(self, upstream_server): super(UpstreamServerProvider, self).__init__(upstream_server) self._proxy = None @property def proxy(self): if self._proxy is None: self._proxy = fastrpc.ServerProxy(self.upstream_server) return self._proxy 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 request_handler_callback( api.Response( fastrpc.dumps(data, methodresponse=True, useBinary=True), [("Content-Type", "application/x-frpc")]))
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 _create_response(self, method_name: str, args: Any, accept_cts: Set[str]) -> Response: response = None if method_name not in self.methods: response = { 'status': 501, 'statusMessage': "Method '{}' not found.".format(method_name) } else: try: method = self.methods[method_name] response = method(*args) except Exception as ex: # .error() instead of .exception(), because that way it doesn't break log parsing when someone # is using that, because it inserts the exception into the message itself, instead of paste after logging.error('In method call %s: \n%s', method_name, format_exc()) response = {'status': 500, 'statusMessage': str(ex)} logging.info('Response: %s', response) _response_autostatus(response) headers = {} if FRPC_CONTENT_TYPE in accept_cts and fastrpc: use_binary = True headers['Content-Type'] = FRPC_CONTENT_TYPE else: use_binary = False headers['Content-Type'] = RPC_CONTENT_TYPE if fastrpc: headers['Accept'] = ','.join(self.allowed_content_types) body = fastrpc.dumps((response, ), methodresponse=True, useBinary=use_binary) else: body = xmlrpc.dumps((response, ), methodresponse=True) headers['Content-Length'] = str(len(body)) resp = Response(body, headers=headers) return resp
def _create_response(self, method_name: str, args: Any, accept_cts: Set[str]) -> Response: response = None if method_name not in self.methods: response = { 'status': 501, 'statusMessage': "Method '{}' not found.".format(method_name) } else: try: method = self.methods[method_name] response = method(*args) except Exception as ex: # .error() instead of .exception(), because that way it doesn't break log parsing when someone # is using that, because it inserts the exception into the message itself, instead of paste after logging.error('In method call %s: \n%s', method_name, format_exc()) response = { 'status': 500, 'statusMessage': str(ex) } logging.info('Response: %s', response) _response_autostatus(response) headers = {} if FRPC_CONTENT_TYPE in accept_cts and fastrpc: use_binary = True headers['Content-Type'] = FRPC_CONTENT_TYPE else: use_binary = False headers['Content-Type'] = RPC_CONTENT_TYPE if fastrpc: headers['Accept'] = ','.join(self.allowed_content_types) body = fastrpc.dumps((response,), methodresponse=True, useBinary=use_binary) else: body = xmlrpc.dumps((response,), methodresponse=True) headers['Content-Length'] = str(len(body)) resp = Response(body, headers=headers) return resp
async def _create_response(self, method_name, args, accept_cts): response = None if method_name not in self.methods: response = { 'status': 501, 'statusMessage': "Method '{}' not found.".format(method_name) } else: try: method = self.methods[method_name] response = await method(*args) except Exception as ex: logger.exception('In method call') response = { 'status': 500, 'statusMessage': str(ex) } logger.debug('Response: %s', response) headers = {} if FRPC_CONTENT_TYPE in accept_cts: use_binary = fastrpc.ALWAYS headers['Content-Type'] = FRPC_CONTENT_TYPE else: use_binary = fastrpc.NEVER headers['Content-Type'] = RPC_CONTENT_TYPE body = fastrpc.dumps((response,), methodresponse=True, useBinary=use_binary) headers['Content-Length'] = str(len(body)) headers['Accept'] = ','.join(self.allowed_content_types) return web.Response( body=body, headers=headers, )
def _dump(self, data): if isinstance(data, fastrpc.Fault): return fastrpc.dumps(data, methodresponse=True, useBinary=True) else: return fastrpc.dumps((data, ), methodresponse=True, useBinary=True)
def _get_post_body(self, name, args): if fastrpc is not None: return fastrpc.dumps(args, name, useBinary=self.use_binary) else: return xmlrpclib.dumps(args, name, allow_none=True)