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