~ubuntu-branches/ubuntu/jaunty/calibre/jaunty-backports

« back to all changes in this revision

Viewing changes to src/cherrypy/lib/xmlrpc.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-01-20 17:14:02 UTC
  • Revision ID: james.westby@ubuntu.com-20090120171402-8y3znf6nokwqe80k
Tags: upstream-0.4.125+dfsg
ImportĀ upstreamĀ versionĀ 0.4.125+dfsg

Show diffs side-by-side

added added

removed removed

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