3 from twisted.web import http, server
4 from twisted.python import log
5 from zope.interface import Interface
6 from nevow import loaders, appserver
7 from nevow.inevow import IRequest
8 from nevow.util import resource_filename
9 from allmydata.interfaces import ExistingChildError, NoSuchChildError, \
10 FileTooLargeError, NotEnoughSharesError, NoSharesError, \
11 EmptyPathnameComponentError, MustBeDeepImmutableError, \
12 MustBeReadonlyError, MustNotBeUnknownRWError
13 from allmydata.mutable.common import UnrecoverableFileError
14 from allmydata.util import abbreviate
15 from allmydata.util.encodingutil import to_str
17 class IOpHandleTable(Interface):
21 return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
23 def boolean_of_arg(arg):
25 assert arg.lower() in ("true", "t", "1", "false", "f", "0", "on", "off")
26 return arg.lower() in ("true", "t", "1", "on")
28 def parse_replace_arg(replace):
29 if replace.lower() == "only-files":
32 return boolean_of_arg(replace)
34 def get_root(ctx_or_req):
35 req = IRequest(ctx_or_req)
36 # the addSlash=True gives us one extra (empty) segment
37 depth = len(req.prepath) + len(req.postpath) - 1
38 link = "/".join([".."] * depth)
41 def get_arg(ctx_or_req, argname, default=None, multiple=False):
42 """Extract an argument from either the query args (req.args) or the form
43 body fields (req.fields). If multiple=False, this returns a single value
44 (or the default, which defaults to None), and the query args take
45 precedence. If multiple=True, this returns a tuple of arguments (possibly
46 empty), starting with all those in the query args.
48 req = IRequest(ctx_or_req)
50 if argname in req.args:
51 results.extend(req.args[argname])
52 if req.fields and argname in req.fields:
53 results.append(req.fields[argname].value)
60 def convert_children_json(nodemaker, children_json):
61 """I convert the JSON output of GET?t=json into the dict-of-nodes input
62 to both dirnode.create_subdirectory() and
63 client.create_directory(initial_children=). This is used by
64 t=mkdir-with-children and t=mkdir-immutable"""
67 data = simplejson.loads(children_json)
68 for (namex, (ctype, propdict)) in data.iteritems():
69 namex = unicode(namex)
70 writecap = to_str(propdict.get("rw_uri"))
71 readcap = to_str(propdict.get("ro_uri"))
72 metadata = propdict.get("metadata", {})
73 # name= argument is just for error reporting
74 childnode = nodemaker.create_from_cap(writecap, readcap, name=namex)
75 children[namex] = (childnode, metadata)
78 def abbreviate_time(data):
84 return abbreviate.abbreviate_time(data)
88 return "%.0fms" % (1000*s)
90 return "%.1fms" % (1000*s)
91 return "%.0fus" % (1000000*s)
93 def abbreviate_rate(data):
94 # 21.8kBps, 554.4kBps 4.37MBps
99 return "%1.2fMBps" % (r/1000000)
101 return "%.1fkBps" % (r/1000)
104 def abbreviate_size(data):
105 # 21.8kB, 554.4kB 4.37MB
110 return "%1.2fGB" % (r/1000000000)
112 return "%1.2fMB" % (r/1000000)
114 return "%.1fkB" % (r/1000)
117 def plural(sequence_or_length):
118 if isinstance(sequence_or_length, int):
119 length = sequence_or_length
121 length = len(sequence_or_length)
126 def text_plain(text, ctx):
128 req.setHeader("content-type", "text/plain")
129 req.setHeader("content-length", len(text))
132 class WebError(Exception):
133 def __init__(self, text, code=http.BAD_REQUEST):
137 # XXX: to make UnsupportedMethod return 501 NOT_IMPLEMENTED instead of 500
138 # Internal Server Error, we either need to do that ICanHandleException trick,
139 # or make sure that childFactory returns a WebErrorResource (and never an
140 # actual exception). The latter is growing increasingly annoying.
142 def should_create_intermediate_directories(req):
143 t = get_arg(req, "t", "").strip()
144 return bool(req.method in ("PUT", "POST") and
145 t not in ("delete", "rename", "rename-form", "check"))
147 def humanize_failure(f):
148 # return text, responsecode
149 if f.check(EmptyPathnameComponentError):
150 return ("The webapi does not allow empty pathname components, "
151 "i.e. a double slash", http.BAD_REQUEST)
152 if f.check(ExistingChildError):
153 return ("There was already a child by that name, and you asked me "
154 "to not replace it.", http.CONFLICT)
155 if f.check(NoSuchChildError):
156 name = f.value.args[0]
157 return ("No such child: %s" % name.encode("utf-8"), http.NOT_FOUND)
158 if f.check(NotEnoughSharesError):
159 t = ("NotEnoughSharesError: This indicates that some "
160 "servers were unavailable, or that shares have been "
161 "lost to server departure, hard drive failure, or disk "
162 "corruption. You should perform a filecheck on "
163 "this object to learn more.\n\nThe full error message is:\n"
165 return (t, http.GONE)
166 if f.check(NoSharesError):
167 t = ("NoSharesError: no shares could be found. "
168 "Zero shares usually indicates a corrupt URI, or that "
169 "no servers were connected, but it might also indicate "
170 "severe corruption. You should perform a filecheck on "
171 "this object to learn more.\n\nThe full error message is:\n"
173 return (t, http.GONE)
174 if f.check(UnrecoverableFileError):
175 t = ("UnrecoverableFileError: the directory (or mutable file) could "
176 "not be retrieved, because there were insufficient good shares. "
177 "This might indicate that no servers were connected, "
178 "insufficient servers were connected, the URI was corrupt, or "
179 "that shares have been lost due to server departure, hard drive "
180 "failure, or disk corruption. You should perform a filecheck on "
181 "this object to learn more.")
182 return (t, http.GONE)
183 if f.check(MustNotBeUnknownRWError):
184 name = f.value.args[1]
185 immutable = f.value.args[2]
187 t = ("MustNotBeUnknownRWError: an operation to add a child named "
188 "'%s' to a directory was given an unknown cap in a write slot.\n"
189 "If the cap is actually an immutable readcap, then using a "
190 "webapi server that supports a later version of Tahoe may help.\n\n"
191 "If you are using the webapi directly, then specifying an immutable "
192 "readcap in the read slot (ro_uri) of the JSON PROPDICT, and "
193 "omitting the write slot (rw_uri), would also work in this "
194 "case.") % name.encode("utf-8")
196 t = ("MustNotBeUnknownRWError: an operation to add a child named "
197 "'%s' to a directory was given an unknown cap in a write slot.\n"
198 "Using a webapi server that supports a later version of Tahoe "
200 "If you are using the webapi directly, specifying a readcap in "
201 "the read slot (ro_uri) of the JSON PROPDICT, as well as a "
202 "writecap in the write slot if desired, would also work in this "
203 "case.") % name.encode("utf-8")
204 return (t, http.BAD_REQUEST)
205 if f.check(MustBeDeepImmutableError):
206 name = f.value.args[1]
207 t = ("MustBeDeepImmutableError: a cap passed to this operation for "
208 "the child named '%s', needed to be immutable but was not. Either "
209 "the cap is being added to an immutable directory, or it was "
210 "originally retrieved from an immutable directory as an unknown "
211 "cap." % name.encode("utf-8"))
212 return (t, http.BAD_REQUEST)
213 if f.check(MustBeReadonlyError):
214 name = f.value.args[1]
215 t = ("MustBeReadonlyError: a cap passed to this operation for "
216 "the child named '%s', needed to be read-only but was not. "
217 "The cap is being passed in a read slot (ro_uri), or was retrieved "
218 "from a read slot as an unknown cap." % name.encode("utf-8"))
219 return (t, http.BAD_REQUEST)
220 if f.check(WebError):
221 return (f.value.text, f.value.code)
222 if f.check(FileTooLargeError):
223 return (f.getTraceback(), http.REQUEST_ENTITY_TOO_LARGE)
224 return (str(f), None)
226 class MyExceptionHandler(appserver.DefaultExceptionHandler):
227 def simple(self, ctx, text, code=http.BAD_REQUEST):
229 req.setResponseCode(code)
230 #req.responseHeaders.setRawHeaders("content-encoding", [])
231 #req.responseHeaders.setRawHeaders("content-disposition", [])
232 req.setHeader("content-type", "text/plain;charset=utf-8")
233 if isinstance(text, unicode):
234 text = text.encode("utf-8")
235 req.setHeader("content-length", str(len(text)))
237 # TODO: consider putting the requested URL here
238 req.finishRequest(False)
240 def renderHTTP_exception(self, ctx, f):
242 text, code = humanize_failure(f)
244 log.msg("exception in humanize_failure")
245 log.msg("argument was %s" % (f,))
247 text, code = str(f), None
249 return self.simple(ctx, text, code)
250 if f.check(server.UnsupportedMethod):
251 # twisted.web.server.Request.render() has support for transforming
252 # this into an appropriate 501 NOT_IMPLEMENTED or 405 NOT_ALLOWED
253 # return code, but nevow does not.
256 return self.simple(ctx,
257 "I don't know how to treat a %s request." % method,
258 http.NOT_IMPLEMENTED)
260 accept = req.getHeader("accept")
263 if "*/*" in accept or "text/*" in accept or "text/html" in accept:
264 super = appserver.DefaultExceptionHandler
265 return super.renderHTTP_exception(self, ctx, f)
267 traceback = f.getTraceback()
268 return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR)
270 class NeedOperationHandleError(WebError):
275 def renderHTTP(self, ctx):
276 request = IRequest(ctx)
278 # if we were using regular twisted.web Resources (and the regular
279 # twisted.web.server.Request object) then we could implement
280 # render_PUT and render_GET. But Nevow's request handler
281 # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy
282 # some code from the Resource.render method that Nevow bypasses, to
284 m = getattr(self, 'render_' + request.method, None)
286 from twisted.web.server import UnsupportedMethod
287 raise UnsupportedMethod(getattr(self, 'allowedMethods', ()))