def _dump_docs(self, req): self.log.debug("Rendering docs") # Dump RPC documentation req.perm.require('XML_RPC') # Need at least XML_RPC namespaces = {} for method in XMLRPCSystem(self.env).all_methods(req): namespace = method.namespace.replace('.', '_') if namespace not in namespaces: namespaces[namespace] = { 'description': wiki_to_oneliner(method.namespace_description, self.env, req=req), 'methods': [], 'namespace': method.namespace, } try: namespaces[namespace]['methods'].append( (method.signature, wiki_to_oneliner(method.description, self.env, req=req), method.permission)) except Exception, e: from tracrpc.util import StringIO import traceback out = StringIO() traceback.print_exc(file=out) raise Exception('%s: %s\n%s' % (method.name, str(e), out.getvalue()))
def _rpc_process(self, req, protocol, content_type): """Process incoming RPC request and finalize response.""" proto_id = protocol.rpc_info()[0] rpcreq = req.rpc = {'mimetype': content_type} try: self.log.debug("RPC(%s) call by '%s'", proto_id, req.authname) rpcreq = req.rpc = protocol.parse_rpc_request(req, content_type) rpcreq['mimetype'] = content_type # Important ! Check after parsing RPC request to add # protocol-specific fields in response # (e.g. JSON-RPC response `id`) req.perm.require('XML_RPC') # Need at least XML_RPC method_name = rpcreq.get('method') if method_name is None: raise ProtocolException('Missing method name') args = rpcreq.get('params') or [] self.log.debug("RPC(%s) call by '%s' %s", proto_id, \ req.authname, method_name) try: result = (XMLRPCSystem(self.env).get_method(method_name)( req, args))[0] if isinstance(result, GeneratorType): result = list(result) except (RPCError, PermissionError, ResourceNotFound), e: raise except Exception: e, tb = sys.exc_info()[-2:] raise ServiceException(e), None, tb else: protocol.send_rpc_result(req, result)
def process_request(self, req): # Need at least XML_RPC req.perm.assert_permission('XML_RPC') # Dump RPC functions content_type = req.get_header('Content-Type') if content_type is None or 'text/xml' not in content_type: namespaces = {} for method in XMLRPCSystem(self.env).all_methods(req): namespace = method.namespace.replace('.', '_') if namespace not in namespaces: namespaces[namespace] = { 'description': wiki_to_oneliner(method.namespace_description, self.env), 'methods': [], 'namespace': method.namespace, } try: namespaces[namespace]['methods'].append( (method.signature, wiki_to_oneliner(method.description, self.env), method.permission)) except Exception, e: from StringIO import StringIO import traceback out = StringIO() traceback.print_exc(file=out) raise Exception('%s: %s\n%s' % (method.name, str(e), out.getvalue())) add_stylesheet(req, 'common/css/wiki.css') req.hdf['xmlrpc.functions'] = namespaces return 'xmlrpclist.cs', None
def _rpc_process(self, req, protocol, content_type): """Process incoming RPC request and finalize response.""" proto_id = protocol.rpc_info()[0] rpcreq = req.rpc = {'mimetype': content_type} self.log.debug("RPC(%s) call by '%s'", proto_id, req.authname) try: if req.path_info.startswith('/login/') and \ req.authname == 'anonymous': raise TracError("Authentication information not available") rpcreq = req.rpc = protocol.parse_rpc_request(req, content_type) rpcreq['mimetype'] = content_type # Important ! Check after parsing RPC request to add # protocol-specific fields in response # (e.g. JSON-RPC response `id`) req.perm.require('XML_RPC') # Need at least XML_RPC method_name = rpcreq.get('method') if method_name is None: raise ProtocolException('Missing method name') args = rpcreq.get('params') or [] self.log.debug("RPC(%s) call by '%s' %s", proto_id, req.authname, method_name) try: result = (XMLRPCSystem(self.env).get_method(method_name)( req, args))[0] if isinstance(result, GeneratorType): result = list(result) except (TracError, PermissionError, ResourceNotFound), e: raise except Exception: e, tb = sys.exc_info()[-2:] self.log.error( "RPC(%s) [%s] Exception caught while calling " "%s(*%r) by %s%s", proto_id, req.remote_addr, method_name, args, req.authname, exception_to_unicode(e, traceback=True)) raise ServiceException(e), None, tb else: protocol.send_rpc_result(req, result)
def setUp(self): self.env = EnvironmentStub(default_data=True) from trac.log import logger_factory self.env.log = logger_factory(logtype='syslog', logfile=None, level='DEBUG', logid='Trac', format=None) self.env.path = '/' self.wiki_tag_rpc_engine = WikiTagRPCSystem(self.env) self.ticket_tag_rpc_engine = TicketTagRPCSystem(self.env) self.tag_engine = TagEngine(self.env) self.tag_engine.upgrade_environment(self.env.get_db_cnx()) self.xml_rpc_system = XMLRPCSystem(self.env) # Insert some test tickets from trac.ticket.model import Ticket for id in (1, 2, 3): ticket = Ticket(self.env) ticket['summary'] = 'Test ticket %i' % id ticket['description'] = 'Test ticket %i description' % id ticket.insert()
class XMLRPCWeb(Component): """ Handle XML-RPC calls from HTTP clients, as well as presenting a list of methods available to the currently logged in user. Browsing to <trac>/xmlrpc will display this list. """ implements(IRequestHandler, ITemplateProvider) # IRequestHandler methods def match_request(self, req): return req.path_info in ('/login/xmlrpc', '/xmlrpc') def _send_response(self, req, response): req.send_response(200) req.send_header('Content-Type', 'text/xml') req.send_header('Content-Length', len(response)) req.end_headers() req.write(response) def process_request(self, req): # Need at least XML_RPC req.perm.assert_permission('XML_RPC') # Dump RPC functions content_type = req.get_header('Content-Type') if content_type is None or 'text/xml' not in content_type: namespaces = {} for method in XMLRPCSystem(self.env).all_methods(req): namespace = method.namespace.replace('.', '_') if namespace not in namespaces: namespaces[namespace] = { 'description': wiki_to_oneliner(method.namespace_description, self.env), 'methods': [], 'namespace': method.namespace, } try: namespaces[namespace]['methods'].append( (method.signature, wiki_to_oneliner(method.description, self.env), method.permission)) except Exception, e: from StringIO import StringIO import traceback out = StringIO() traceback.print_exc(file=out) raise Exception('%s: %s\n%s' % (method.name, str(e), out.getvalue())) add_stylesheet(req, 'common/css/wiki.css') req.hdf['xmlrpc.functions'] = namespaces return 'xmlrpclist.cs', None # Handle XML-RPC call args, method = xmlrpclib.loads( req.read(int(req.get_header('Content-Length')))) try: result = XMLRPCSystem(self.env).get_method(method)(req, args) self._send_response(req, xmlrpclib.dumps(result, methodresponse=True)) except xmlrpclib.Fault, e: self.log.error(e) self._send_response(req, xmlrpclib.dumps(e))