5 from twisted.web import http, server
6 from twisted.python import log
7 from zope.interface import Interface
8 from nevow import loaders, appserver
9 from nevow.inevow import IRequest
10 from nevow.util import resource_filename
11 from allmydata import blacklist
12 from allmydata.interfaces import ExistingChildError, NoSuchChildError, \
13 FileTooLargeError, NotEnoughSharesError, NoSharesError, \
14 EmptyPathnameComponentError, MustBeDeepImmutableError, \
15 MustBeReadonlyError, MustNotBeUnknownRWError, SDMF_VERSION, MDMF_VERSION
16 from allmydata.mutable.common import UnrecoverableFileError
17 from allmydata.util import abbreviate
18 from allmydata.util.time_format import format_time, format_delta
19 from allmydata.util.encodingutil import to_str, quote_output
22 def get_filenode_metadata(filenode):
23 metadata = {'mutable': filenode.is_mutable()}
24 if metadata['mutable']:
25 mutable_type = filenode.get_version()
26 assert mutable_type in (SDMF_VERSION, MDMF_VERSION)
27 if mutable_type == MDMF_VERSION:
33 metadata['format'] = file_format
34 size = filenode.get_size()
36 metadata['size'] = size
39 class IOpHandleTable(Interface):
43 return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
45 def boolean_of_arg(arg):
47 if arg.lower() not in ("true", "t", "1", "false", "f", "0", "on", "off"):
48 raise WebError("invalid boolean argument: %r" % (arg,), http.BAD_REQUEST)
49 return arg.lower() in ("true", "t", "1", "on")
51 def parse_replace_arg(replace):
52 if replace.lower() == "only-files":
55 return boolean_of_arg(replace)
57 raise WebError("invalid replace= argument: %r" % (replace,), http.BAD_REQUEST)
60 def get_format(req, default="CHK"):
61 arg = get_arg(req, "format", None)
63 if boolean_of_arg(get_arg(req, "mutable", "false")):
66 if arg.upper() == "CHK":
68 elif arg.upper() == "SDMF":
70 elif arg.upper() == "MDMF":
73 raise WebError("Unknown format: %s, I know CHK, SDMF, MDMF" % arg,
76 def get_mutable_type(file_format): # accepts result of get_format()
77 if file_format == "SDMF":
79 elif file_format == "MDMF":
82 # this is also used to identify which formats are mutable. Use
83 # if get_mutable_type(file_format) is not None:
90 def parse_offset_arg(offset):
91 # XXX: This will raise a ValueError when invoked on something that
92 # is not an integer. Is that okay? Or do we want a better error
93 # message? Since this call is going to be used by programmers and
94 # their tools rather than users (through the wui), it is not
95 # inconsistent to return that, I guess.
96 if offset is not None:
102 def get_root(ctx_or_req):
103 req = IRequest(ctx_or_req)
104 # the addSlash=True gives us one extra (empty) segment
105 depth = len(req.prepath) + len(req.postpath) - 1
106 link = "/".join([".."] * depth)
109 def get_arg(ctx_or_req, argname, default=None, multiple=False):
110 """Extract an argument from either the query args (req.args) or the form
111 body fields (req.fields). If multiple=False, this returns a single value
112 (or the default, which defaults to None), and the query args take
113 precedence. If multiple=True, this returns a tuple of arguments (possibly
114 empty), starting with all those in the query args.
116 req = IRequest(ctx_or_req)
118 if argname in req.args:
119 results.extend(req.args[argname])
120 if req.fields and argname in req.fields:
121 results.append(req.fields[argname].value)
123 return tuple(results)
128 def convert_children_json(nodemaker, children_json):
129 """I convert the JSON output of GET?t=json into the dict-of-nodes input
130 to both dirnode.create_subdirectory() and
131 client.create_directory(initial_children=). This is used by
132 t=mkdir-with-children and t=mkdir-immutable"""
135 data = simplejson.loads(children_json)
136 for (namex, (ctype, propdict)) in data.iteritems():
137 namex = unicode(namex)
138 writecap = to_str(propdict.get("rw_uri"))
139 readcap = to_str(propdict.get("ro_uri"))
140 metadata = propdict.get("metadata", {})
141 # name= argument is just for error reporting
142 childnode = nodemaker.create_from_cap(writecap, readcap, name=namex)
143 children[namex] = (childnode, metadata)
146 def abbreviate_time(data):
147 # 1.23s, 790ms, 132us
152 return abbreviate.abbreviate_time(data)
156 return "%.0fms" % (1000*s)
158 return "%.1fms" % (1000*s)
159 return "%.0fus" % (1000000*s)
161 def compute_rate(bytes, seconds):
165 if seconds is None or seconds == 0:
168 # negative values don't make sense here
172 return 1.0 * bytes / seconds
174 def abbreviate_rate(data):
175 # 21.8kBps, 554.4kBps 4.37MBps
180 return "%1.2fMBps" % (r/1000000)
182 return "%.1fkBps" % (r/1000)
185 def abbreviate_size(data):
186 # 21.8kB, 554.4kB 4.37MB
191 return "%1.2fGB" % (r/1000000000)
193 return "%1.2fMB" % (r/1000000)
195 return "%.1fkB" % (r/1000)
198 def plural(sequence_or_length):
199 if isinstance(sequence_or_length, int):
200 length = sequence_or_length
202 length = len(sequence_or_length)
207 def text_plain(text, ctx):
209 req.setHeader("content-type", "text/plain")
210 req.setHeader("content-length", b"%d" % len(text))
213 def spaces_to_nbsp(text):
214 return unicode(text).replace(u' ', u'\u00A0')
216 def render_time_delta(time_1, time_2):
217 return spaces_to_nbsp(format_delta(time_1, time_2))
220 return spaces_to_nbsp(format_time(time.localtime(t)))
222 def render_time_attr(t):
223 return format_time(time.localtime(t))
225 class WebError(Exception):
226 def __init__(self, text, code=http.BAD_REQUEST):
230 # XXX: to make UnsupportedMethod return 501 NOT_IMPLEMENTED instead of 500
231 # Internal Server Error, we either need to do that ICanHandleException trick,
232 # or make sure that childFactory returns a WebErrorResource (and never an
233 # actual exception). The latter is growing increasingly annoying.
235 def should_create_intermediate_directories(req):
236 t = get_arg(req, "t", "").strip()
237 return bool(req.method in ("PUT", "POST") and
238 t not in ("delete", "rename", "rename-form", "check"))
240 def humanize_failure(f):
241 # return text, responsecode
242 if f.check(EmptyPathnameComponentError):
243 return ("The webapi does not allow empty pathname components, "
244 "i.e. a double slash", http.BAD_REQUEST)
245 if f.check(ExistingChildError):
246 return ("There was already a child by that name, and you asked me "
247 "to not replace it.", http.CONFLICT)
248 if f.check(NoSuchChildError):
249 quoted_name = quote_output(f.value.args[0], encoding="utf-8", quotemarks=False)
250 return ("No such child: %s" % quoted_name, http.NOT_FOUND)
251 if f.check(NotEnoughSharesError):
252 t = ("NotEnoughSharesError: This indicates that some "
253 "servers were unavailable, or that shares have been "
254 "lost to server departure, hard drive failure, or disk "
255 "corruption. You should perform a filecheck on "
256 "this object to learn more.\n\nThe full error message is:\n"
258 return (t, http.GONE)
259 if f.check(NoSharesError):
260 t = ("NoSharesError: no shares could be found. "
261 "Zero shares usually indicates a corrupt URI, or that "
262 "no servers were connected, but it might also indicate "
263 "severe corruption. You should perform a filecheck on "
264 "this object to learn more.\n\nThe full error message is:\n"
266 return (t, http.GONE)
267 if f.check(UnrecoverableFileError):
268 t = ("UnrecoverableFileError: the directory (or mutable file) could "
269 "not be retrieved, because there were insufficient good shares. "
270 "This might indicate that no servers were connected, "
271 "insufficient servers were connected, the URI was corrupt, or "
272 "that shares have been lost due to server departure, hard drive "
273 "failure, or disk corruption. You should perform a filecheck on "
274 "this object to learn more.")
275 return (t, http.GONE)
276 if f.check(MustNotBeUnknownRWError):
277 quoted_name = quote_output(f.value.args[1], encoding="utf-8")
278 immutable = f.value.args[2]
280 t = ("MustNotBeUnknownRWError: an operation to add a child named "
281 "%s to a directory was given an unknown cap in a write slot.\n"
282 "If the cap is actually an immutable readcap, then using a "
283 "webapi server that supports a later version of Tahoe may help.\n\n"
284 "If you are using the webapi directly, then specifying an immutable "
285 "readcap in the read slot (ro_uri) of the JSON PROPDICT, and "
286 "omitting the write slot (rw_uri), would also work in this "
287 "case.") % quoted_name
289 t = ("MustNotBeUnknownRWError: an operation to add a child named "
290 "%s to a directory was given an unknown cap in a write slot.\n"
291 "Using a webapi server that supports a later version of Tahoe "
293 "If you are using the webapi directly, specifying a readcap in "
294 "the read slot (ro_uri) of the JSON PROPDICT, as well as a "
295 "writecap in the write slot if desired, would also work in this "
296 "case.") % quoted_name
297 return (t, http.BAD_REQUEST)
298 if f.check(MustBeDeepImmutableError):
299 quoted_name = quote_output(f.value.args[1], encoding="utf-8")
300 t = ("MustBeDeepImmutableError: a cap passed to this operation for "
301 "the child named %s, needed to be immutable but was not. Either "
302 "the cap is being added to an immutable directory, or it was "
303 "originally retrieved from an immutable directory as an unknown "
304 "cap.") % quoted_name
305 return (t, http.BAD_REQUEST)
306 if f.check(MustBeReadonlyError):
307 quoted_name = quote_output(f.value.args[1], encoding="utf-8")
308 t = ("MustBeReadonlyError: a cap passed to this operation for "
309 "the child named '%s', needed to be read-only but was not. "
310 "The cap is being passed in a read slot (ro_uri), or was retrieved "
311 "from a read slot as an unknown cap.") % quoted_name
312 return (t, http.BAD_REQUEST)
313 if f.check(blacklist.FileProhibited):
314 t = "Access Prohibited: %s" % quote_output(f.value.reason, encoding="utf-8", quotemarks=False)
315 return (t, http.FORBIDDEN)
316 if f.check(WebError):
317 return (f.value.text, f.value.code)
318 if f.check(FileTooLargeError):
319 return (f.getTraceback(), http.REQUEST_ENTITY_TOO_LARGE)
320 return (str(f), None)
322 class MyExceptionHandler(appserver.DefaultExceptionHandler):
323 def simple(self, ctx, text, code=http.BAD_REQUEST):
325 req.setResponseCode(code)
326 #req.responseHeaders.setRawHeaders("content-encoding", [])
327 #req.responseHeaders.setRawHeaders("content-disposition", [])
328 req.setHeader("content-type", "text/plain;charset=utf-8")
329 if isinstance(text, unicode):
330 text = text.encode("utf-8")
331 req.setHeader("content-length", b"%d" % len(text))
333 # TODO: consider putting the requested URL here
334 req.finishRequest(False)
336 def renderHTTP_exception(self, ctx, f):
338 text, code = humanize_failure(f)
340 log.msg("exception in humanize_failure")
341 log.msg("argument was %s" % (f,))
343 text, code = str(f), None
345 return self.simple(ctx, text, code)
346 if f.check(server.UnsupportedMethod):
347 # twisted.web.server.Request.render() has support for transforming
348 # this into an appropriate 501 NOT_IMPLEMENTED or 405 NOT_ALLOWED
349 # return code, but nevow does not.
352 return self.simple(ctx,
353 "I don't know how to treat a %s request." % method,
354 http.NOT_IMPLEMENTED)
356 accept = req.getHeader("accept")
359 if "*/*" in accept or "text/*" in accept or "text/html" in accept:
360 super = appserver.DefaultExceptionHandler
361 return super.renderHTTP_exception(self, ctx, f)
363 traceback = f.getTraceback()
364 return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR)
366 class NeedOperationHandleError(WebError):
371 def renderHTTP(self, ctx):
372 request = IRequest(ctx)
374 # if we were using regular twisted.web Resources (and the regular
375 # twisted.web.server.Request object) then we could implement
376 # render_PUT and render_GET. But Nevow's request handler
377 # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy
378 # some code from the Resource.render method that Nevow bypasses, to
380 m = getattr(self, 'render_' + request.method, None)
382 from twisted.web.server import UnsupportedMethod
383 raise UnsupportedMethod(getattr(self, 'allowedMethods', ()))