debuggers.hg

view tools/python/xen/web/SrvBase.py @ 4655:a838a908e38e

bitkeeper revision 1.1327.2.2 (4267a9b3MhPpljnjQ5IbfLdzcW2K3w)

Remove twisted from the HTTP server and replace with a
threaded server. Add classes to provide tcp and unix servers
using threads instead of twisted. Remove use of twisted from
the consoles, event server and HTTP resources

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Thu Apr 21 13:25:07 2005 +0000 (2005-04-21)
parents
children d781b9d08e80
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 import types
5 from xen.xend import sxp
6 from xen.xend import PrettyPrint
7 from xen.xend.Args import ArgError
8 from xen.xend.XendError import XendError
9 from xen.xend.XendLogging import log
11 import resource
12 import http
13 import defer
15 def uri_pathlist(p):
16 """Split a path into a list.
17 p path
18 return list of path elements
19 """
20 l = []
21 for x in p.split('/'):
22 if x == '': continue
23 l.append(x)
24 return l
26 class SrvBase(resource.Resource):
27 """Base class for services.
28 """
31 def use_sxp(self, req):
32 """Determine whether to send an SXP response to a request.
33 Uses SXP if there is no User-Agent, no Accept, or application/sxp is in Accept.
35 req request
36 returns 1 for SXP, 0 otherwise
37 """
38 ok = 0
39 user_agent = req.getHeader('User-Agent')
40 accept = req.getHeader('Accept')
41 if (not user_agent) or (not accept) or (accept.find(sxp.mime_type) >= 0):
42 ok = 1
43 return ok
45 def get_op_method(self, op):
46 """Get the method for an operation.
47 For operation 'foo' looks for 'op_foo'.
49 op operation name
50 returns method or None
51 """
52 op_method_name = 'op_' + op
53 return getattr(self, op_method_name, None)
55 def perform(self, req):
56 """General operation handler for posted operations.
57 For operation 'foo' looks for a method op_foo and calls
58 it with op_foo(op, req). Replies with code 500 if op_foo
59 is not found.
61 The method must return a list when req.use_sxp is true
62 and an HTML string otherwise (or list).
63 Methods may also return a Deferred (for incomplete processing).
65 req request
66 """
67 op = req.args.get('op')
68 if op is None or len(op) != 1:
69 req.setResponseCode(http.NOT_ACCEPTABLE, "Invalid request")
70 return ''
71 op = op[0]
72 op_method = self.get_op_method(op)
73 if op_method is None:
74 req.setResponseCode(http.NOT_IMPLEMENTED, "Operation not implemented: " + op)
75 req.setHeader("Content-Type", "text/plain")
76 req.write("Operation not implemented: " + op)
77 return ''
78 else:
79 return self._perform(op, op_method, req)
81 def _perform(self, op, op_method, req):
82 try:
83 val = op_method(op, req)
84 except Exception, err:
85 self._perform_err(err, op, req)
86 return ''
88 if isinstance(val, defer.Deferred):
89 val.addCallback(self._perform_cb, op, req, dfr=1)
90 val.addErrback(self._perform_err, op, req, dfr=1)
91 return server.NOT_DONE_YET
92 else:
93 self._perform_cb(val, op, req, dfr=0)
94 return ''
96 def _perform_cb(self, val, op, req, dfr=0):
97 """Callback to complete the request.
98 May be called from a Deferred.
100 @param err: the error
101 @param req: request causing the error
102 @param dfr: deferred flag
103 """
104 if isinstance(val, resource.ErrorPage):
105 req.write(val.render(req))
106 elif self.use_sxp(req):
107 req.setHeader("Content-Type", sxp.mime_type)
108 sxp.show(val, out=req)
109 else:
110 req.write('<html><head></head><body>')
111 self.print_path(req)
112 if isinstance(val, types.ListType):
113 req.write('<code><pre>')
114 PrettyPrint.prettyprint(val, out=req)
115 req.write('</pre></code>')
116 else:
117 req.write(str(val))
118 req.write('</body></html>')
119 if dfr:
120 req.finish()
122 def _perform_err(self, err, op, req, dfr=0):
123 """Error callback to complete a request.
124 May be called from a Deferred.
126 @param err: the error
127 @param req: request causing the error
128 @param dfr: deferred flag
129 """
130 if not (isinstance(err, ArgError) or
131 isinstance(err, sxp.ParseError) or
132 isinstance(err, XendError)):
133 if dfr:
134 return err
135 else:
136 raise
137 #log.exception("op=%s: %s", op, str(err))
138 if self.use_sxp(req):
139 req.setHeader("Content-Type", sxp.mime_type)
140 sxp.show(['xend.err', str(err)], out=req)
141 else:
142 req.setHeader("Content-Type", "text/plain")
143 req.write('Error ')
144 req.write(': ')
145 req.write(str(err))
146 if dfr:
147 req.finish()
150 def print_path(self, req):
151 """Print the path with hyperlinks.
152 """
153 pathlist = [x for x in req.prepath if x != '' ]
154 s = "/"
155 req.write('<h1><a href="/">/</a>')
156 for x in pathlist:
157 s += x + "/"
158 req.write(' <a href="%s">%s</a>/' % (s, x))
159 req.write("</h1>")