2014-03-10 05:18:05 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import cherrypy
|
|
|
|
|
|
|
|
|
|
|
|
def process_body():
|
|
|
|
"""Return (params, method) from request body."""
|
|
|
|
try:
|
2014-06-06 04:24:43 +00:00
|
|
|
import xmlrpclib
|
|
|
|
return xmlrpclib.loads(cherrypy.request.body.read())
|
2014-03-10 05:18:05 +00:00
|
|
|
except Exception:
|
2014-06-06 04:24:43 +00:00
|
|
|
return ('ERROR PARAMS',), 'ERRORMETHOD'
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def patched_path(path):
|
|
|
|
"""Return 'path', doctored for RPC."""
|
|
|
|
if not path.endswith('/'):
|
|
|
|
path += '/'
|
|
|
|
if path.startswith('/RPC2/'):
|
|
|
|
# strip the first /rpc2
|
|
|
|
path = path[5:]
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def _set_response(body):
|
|
|
|
# The XML-RPC spec (http://www.xmlrpc.com/spec) says:
|
|
|
|
# "Unless there's a lower-level error, always return 200 OK."
|
|
|
|
# Since Python's xmlrpclib interprets a non-200 response
|
|
|
|
# as a "Protocol Error", we'll just return 200 every time.
|
|
|
|
response = cherrypy.response
|
|
|
|
response.status = '200 OK'
|
2014-06-06 04:24:43 +00:00
|
|
|
response.body = body
|
2014-03-10 05:18:05 +00:00
|
|
|
response.headers['Content-Type'] = 'text/xml'
|
|
|
|
response.headers['Content-Length'] = len(body)
|
|
|
|
|
|
|
|
|
|
|
|
def respond(body, encoding='utf-8', allow_none=0):
|
2014-06-06 04:24:43 +00:00
|
|
|
from xmlrpclib import Fault, dumps
|
|
|
|
if not isinstance(body, Fault):
|
2014-03-10 05:18:05 +00:00
|
|
|
body = (body,)
|
2014-06-06 04:24:43 +00:00
|
|
|
_set_response(dumps(body, methodresponse=1,
|
|
|
|
encoding=encoding,
|
|
|
|
allow_none=allow_none))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def on_error(*args, **kwargs):
|
|
|
|
body = str(sys.exc_info()[1])
|
2014-06-06 04:24:43 +00:00
|
|
|
from xmlrpclib import Fault, dumps
|
|
|
|
_set_response(dumps(Fault(1, body)))
|
|
|
|
|