2 from twisted.web import http, server
3 from twisted.python import log
4 from zope.interface import Interface
5 from nevow import loaders, appserver
6 from nevow.inevow import IRequest
7 from nevow.util import resource_filename
8 from allmydata.interfaces import ExistingChildError, NoSuchChildError, \
9 FileTooLargeError, NotEnoughSharesError, NoSharesError
10 from allmydata.mutable.common import UnrecoverableFileError
11 from allmydata.util import abbreviate # TODO: consolidate
13 class IOpHandleTable(Interface):
17 return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
19 def boolean_of_arg(arg):
21 assert arg.lower() in ("true", "t", "1", "false", "f", "0", "on", "off")
22 return arg.lower() in ("true", "t", "1", "on")
24 def parse_replace_arg(replace):
25 if replace == "only-files":
28 return boolean_of_arg(replace)
30 def get_root(ctx_or_req):
31 req = IRequest(ctx_or_req)
32 # the addSlash=True gives us one extra (empty) segment
33 depth = len(req.prepath) + len(req.postpath) - 1
34 link = "/".join([".."] * depth)
37 def get_arg(ctx_or_req, argname, default=None, multiple=False):
38 """Extract an argument from either the query args (req.args) or the form
39 body fields (req.fields). If multiple=False, this returns a single value
40 (or the default, which defaults to None), and the query args take
41 precedence. If multiple=True, this returns a tuple of arguments (possibly
42 empty), starting with all those in the query args.
44 req = IRequest(ctx_or_req)
46 if argname in req.args:
47 results.extend(req.args[argname])
48 if req.fields and argname in req.fields:
49 results.append(req.fields[argname].value)
56 def abbreviate_time(data):
62 return abbreviate.abbreviate_time(data)
66 return "%dms" % (1000*s)
68 return "%.1fms" % (1000*s)
69 return "%dus" % (1000000*s)
71 def abbreviate_rate(data):
72 # 21.8kBps, 554.4kBps 4.37MBps
77 return "%1.2fMBps" % (r/1000000)
79 return "%.1fkBps" % (r/1000)
82 def abbreviate_size(data):
83 # 21.8kB, 554.4kB 4.37MB
88 return "%1.2fGB" % (r/1000000000)
90 return "%1.2fMB" % (r/1000000)
92 return "%.1fkB" % (r/1000)
95 def plural(sequence_or_length):
96 if isinstance(sequence_or_length, int):
97 length = sequence_or_length
99 length = len(sequence_or_length)
104 def text_plain(text, ctx):
106 req.setHeader("content-type", "text/plain")
107 req.setHeader("content-length", len(text))
110 class WebError(Exception):
111 def __init__(self, text, code=http.BAD_REQUEST):
115 # XXX: to make UnsupportedMethod return 501 NOT_IMPLEMENTED instead of 500
116 # Internal Server Error, we either need to do that ICanHandleException trick,
117 # or make sure that childFactory returns a WebErrorResource (and never an
118 # actual exception). The latter is growing increasingly annoying.
120 def should_create_intermediate_directories(req):
121 t = get_arg(req, "t", "").strip()
122 return bool(req.method in ("PUT", "POST") and
123 t not in ("delete", "rename", "rename-form", "check"))
125 def humanize_failure(f):
126 # return text, responsecode
127 if f.check(ExistingChildError):
128 return ("There was already a child by that name, and you asked me "
129 "to not replace it.", http.CONFLICT)
130 if f.check(NoSuchChildError):
131 name = f.value.args[0]
132 return ("No such child: %s" % name.encode("utf-8"), http.NOT_FOUND)
133 if f.check(NotEnoughSharesError):
134 t = ("NotEnoughSharesError: This indicates that some "
135 "servers were unavailable, or that shares have been "
136 "lost to server departure, hard drive failure, or disk "
137 "corruption. You should perform a filecheck on "
138 "this object to learn more.\n\nThe full error message is:\n"
140 return (t, http.GONE)
141 if f.check(NoSharesError):
142 t = ("NoSharesError: no shares could be found. "
143 "Zero shares usually indicates a corrupt URI, or that "
144 "no servers were connected, but it might also indicate "
145 "severe corruption. You should perform a filecheck on "
146 "this object to learn more.\n\nThe full error message is:\n"
148 return (t, http.GONE)
149 if f.check(UnrecoverableFileError):
150 t = ("UnrecoverableFileError: the directory (or mutable file) could "
151 "not be retrieved, because there were insufficient good shares. "
152 "This might indicate that no servers were connected, "
153 "insufficient servers were connected, the URI was corrupt, or "
154 "that shares have been lost due to server departure, hard drive "
155 "failure, or disk corruption. You should perform a filecheck on "
156 "this object to learn more.")
157 return (t, http.GONE)
158 if f.check(WebError):
159 return (f.value.text, f.value.code)
160 if f.check(FileTooLargeError):
161 return (f.getTraceback(), http.REQUEST_ENTITY_TOO_LARGE)
162 return (str(f), None)
164 class MyExceptionHandler(appserver.DefaultExceptionHandler):
165 def simple(self, ctx, text, code=http.BAD_REQUEST):
167 req.setResponseCode(code)
168 #req.responseHeaders.setRawHeaders("content-encoding", [])
169 #req.responseHeaders.setRawHeaders("content-disposition", [])
170 req.setHeader("content-type", "text/plain;charset=utf-8")
171 if isinstance(text, unicode):
172 text = text.encode("utf-8")
173 req.setHeader("content-length", str(len(text)))
175 # TODO: consider putting the requested URL here
176 req.finishRequest(False)
178 def renderHTTP_exception(self, ctx, f):
180 text, code = humanize_failure(f)
182 log.msg("exception in humanize_failure")
183 log.msg("argument was %s" % (f,))
185 text, code = str(f), None
187 return self.simple(ctx, text, code)
188 if f.check(server.UnsupportedMethod):
189 # twisted.web.server.Request.render() has support for transforming
190 # this into an appropriate 501 NOT_IMPLEMENTED or 405 NOT_ALLOWED
191 # return code, but nevow does not.
194 return self.simple(ctx,
195 "I don't know how to treat a %s request." % method,
196 http.NOT_IMPLEMENTED)
198 accept = req.getHeader("accept")
201 if "*/*" in accept or "text/*" in accept or "text/html" in accept:
202 super = appserver.DefaultExceptionHandler
203 return super.renderHTTP_exception(self, ctx, f)
205 traceback = f.getTraceback()
206 return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR)
208 class NeedOperationHandleError(WebError):
213 def renderHTTP(self, ctx):
214 request = IRequest(ctx)
216 # if we were using regular twisted.web Resources (and the regular
217 # twisted.web.server.Request object) then we could implement
218 # render_PUT and render_GET. But Nevow's request handler
219 # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy
220 # some code from the Resource.render method that Nevow bypasses, to
222 m = getattr(self, 'render_' + request.method, None)
224 from twisted.web.server import UnsupportedMethod
225 raise UnsupportedMethod(getattr(self, 'allowedMethods', ()))