Package cherrypy :: Package lib :: Module xmlrpcutil
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.lib.xmlrpcutil

 1  import sys 
 2   
 3  import cherrypy 
 4  from cherrypy._cpcompat import ntob 
 5   
 6   
7 -def get_xmlrpclib():
8 try: 9 import xmlrpc.client as x 10 except ImportError: 11 import xmlrpclib as x 12 return x
13 14
15 -def process_body():
16 """Return (params, method) from request body.""" 17 try: 18 return get_xmlrpclib().loads(cherrypy.request.body.read()) 19 except Exception: 20 return ('ERROR PARAMS', ), 'ERRORMETHOD'
21 22
23 -def patched_path(path):
24 """Return 'path', doctored for RPC.""" 25 if not path.endswith('/'): 26 path += '/' 27 if path.startswith('/RPC2/'): 28 # strip the first /rpc2 29 path = path[5:] 30 return path
31 32
33 -def _set_response(body):
34 # The XML-RPC spec (http://www.xmlrpc.com/spec) says: 35 # "Unless there's a lower-level error, always return 200 OK." 36 # Since Python's xmlrpclib interprets a non-200 response 37 # as a "Protocol Error", we'll just return 200 every time. 38 response = cherrypy.response 39 response.status = '200 OK' 40 response.body = ntob(body, 'utf-8') 41 response.headers['Content-Type'] = 'text/xml' 42 response.headers['Content-Length'] = len(body)
43 44
45 -def respond(body, encoding='utf-8', allow_none=0):
46 xmlrpclib = get_xmlrpclib() 47 if not isinstance(body, xmlrpclib.Fault): 48 body = (body,) 49 _set_response(xmlrpclib.dumps(body, methodresponse=1, 50 encoding=encoding, 51 allow_none=allow_none))
52 53
54 -def on_error(*args, **kwargs):
55 body = str(sys.exc_info()[1]) 56 xmlrpclib = get_xmlrpclib() 57 _set_response(xmlrpclib.dumps(xmlrpclib.Fault(1, body)))
58