6 from twisted.internet import defer
7 from twisted.python.failure import Failure
8 from twisted.web import http, html
9 from nevow import url, rend, tags as T
10 from nevow.inevow import IRequest
12 from foolscap.eventual import fireEventually
14 from allmydata.util import base32
15 from allmydata.uri import from_string_verifier, from_string_dirnode, \
17 from allmydata.interfaces import IDirectoryNode, IFileNode, IMutableFileNode, \
19 from allmydata.web.common import text_plain, WebError, IClient, \
20 boolean_of_arg, get_arg, should_create_intermediate_directories, \
21 getxmlfile, RenderMixin
22 from allmydata.web.filenode import ReplaceMeMixin, \
23 FileNodeHandler, PlaceHolderNodeHandler
24 from allmydata.web.checker_results import CheckerResults, DeepCheckResults, \
25 DeepCheckAndRepairResults
27 class BlockingFileError(Exception):
28 # TODO: catch and transform
29 """We cannot auto-create a parent directory, because there is a file in
32 def make_handler_for(node, parentnode=None, name=None):
34 assert IDirectoryNode.providedBy(parentnode)
35 if IFileNode.providedBy(node):
36 return FileNodeHandler(node, parentnode, name)
37 if IMutableFileNode.providedBy(node):
38 return FileNodeHandler(node, parentnode, name)
39 if IDirectoryNode.providedBy(node):
40 return DirectoryNodeHandler(node, parentnode, name)
41 raise WebError("Cannot provide handler for '%s'" % node)
43 class DirectoryNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin):
46 def __init__(self, node, parentnode=None, name=None):
47 rend.Page.__init__(self)
50 self.parentnode = parentnode
53 def childFactory(self, ctx, name):
55 name = name.decode("utf-8")
56 d = self.node.get(name)
57 d.addBoth(self.got_child, ctx, name)
58 # got_child returns a handler resource: FileNodeHandler or
59 # DirectoryNodeHandler
62 def got_child(self, node_or_failure, ctx, name):
64 if DEBUG: print "GOT_CHILD", name, node_or_failure
67 nonterminal = len(req.postpath) > 1
68 t = get_arg(req, "t", "").strip()
69 if isinstance(node_or_failure, Failure):
72 # No child by this name. What should we do about it?
73 if DEBUG: print "no child", name
74 if DEBUG: print "postpath", req.postpath
76 if DEBUG: print " intermediate"
77 if should_create_intermediate_directories(req):
78 # create intermediate directories
79 if DEBUG: print " making intermediate directory"
80 d = self.node.create_empty_directory(name)
81 d.addCallback(make_handler_for, self.node, name)
84 if DEBUG: print " terminal"
86 if (method,t) in [ ("POST","mkdir"), ("PUT","mkdir") ]:
87 if DEBUG: print " making final directory"
89 d = self.node.create_empty_directory(name)
90 d.addCallback(make_handler_for, self.node, name)
92 if (method,t) in ( ("PUT",""), ("PUT","uri"), ):
93 if DEBUG: print " PUT, making leaf placeholder"
94 # we were trying to find the leaf filenode (to put a new
95 # file in its place), and it didn't exist. That's ok,
96 # since that's the leaf node that we're about to create.
97 # We make a dummy one, which will respond to the PUT
98 # request by replacing itself.
99 return PlaceHolderNodeHandler(self.node, name)
100 if DEBUG: print " 404"
101 # otherwise, we just return a no-such-child error
102 return rend.FourOhFour()
104 node = node_or_failure
105 if nonterminal and should_create_intermediate_directories(req):
106 if not IDirectoryNode.providedBy(node):
107 # we would have put a new directory here, but there was a
109 if DEBUG: print "blocking"
110 raise WebError("Unable to create directory '%s': "
111 "a file was in the way" % name,
113 if DEBUG: print "good child"
114 return make_handler_for(node, self.node, name)
116 def render_DELETE(self, ctx):
117 assert self.parentnode and self.name
118 d = self.parentnode.delete(self.name)
119 d.addCallback(lambda res: self.node.get_uri())
122 def render_GET(self, ctx):
123 client = IClient(ctx)
125 # This is where all of the directory-related ?t=* code goes.
126 t = get_arg(req, "t", "").strip()
128 # render the directory as HTML, using the docFactory and Nevow's
129 # whole templating thing.
130 return DirectoryAsHTML(self.node)
133 return DirectoryJSONMetadata(ctx, self.node)
135 return DirectoryURI(ctx, self.node)
136 if t == "readonly-uri":
137 return DirectoryReadonlyURI(ctx, self.node)
139 return Manifest(self.node)
141 return DeepSize(ctx, self.node)
142 if t == "deep-stats":
143 return DeepStats(ctx, self.node)
144 if t == 'rename-form':
145 return RenameForm(self.node)
147 raise WebError("GET directory: bad t=%s" % t)
149 def render_PUT(self, ctx):
151 t = get_arg(req, "t", "").strip()
152 replace = boolean_of_arg(get_arg(req, "replace", "true"))
154 # our job was done by the traversal/create-intermediate-directory
155 # process that got us here.
156 return text_plain(self.node.get_uri(), ctx) # TODO: urlencode
159 # they're trying to set_uri and that name is already occupied
161 raise ExistingChildError()
162 d = self.parentnode.replace_me_with_a_childcap(ctx, replace)
166 raise WebError("PUT to a directory")
168 def render_POST(self, ctx):
170 t = get_arg(req, "t", "").strip()
172 d = self._POST_mkdir(req)
175 d = self._POST_mkdir_p(req)
177 d = self._POST_upload(ctx) # this one needs the context
179 d = self._POST_uri(req)
181 d = self._POST_delete(req)
183 d = self._POST_rename(req)
185 d = self._POST_check(req)
186 elif t == "deep-check":
187 d = self._POST_deep_check(req)
188 elif t == "set_children":
190 d = self._POST_set_children(req)
192 raise WebError("POST to a directory with bad t=%s" % t)
194 when_done = get_arg(req, "when_done", None)
196 d.addCallback(lambda res: url.URL.fromString(when_done))
199 def _POST_mkdir(self, req):
200 name = get_arg(req, "name", "")
202 # our job is done, it was handled by the code in got_child
203 # which created the final directory (i.e. us)
204 return defer.succeed(self.node.get_uri()) # TODO: urlencode
205 name = name.decode("utf-8")
206 replace = boolean_of_arg(get_arg(req, "replace", "true"))
207 d = self.node.create_empty_directory(name, overwrite=replace)
208 d.addCallback(lambda child: child.get_uri()) # TODO: urlencode
211 def _POST_mkdir_p(self, req):
212 path = get_arg(req, "path")
214 raise WebError("mkdir-p requires a path")
215 path_ = tuple([seg.decode("utf-8") for seg in path.split('/') if seg ])
217 d = self._get_or_create_directories(self.node, path_)
218 d.addCallback(lambda node: node.get_uri())
221 def _get_or_create_directories(self, node, path):
222 if not IDirectoryNode.providedBy(node):
223 # unfortunately it is too late to provide the name of the
224 # blocking directory in the error message.
225 raise BlockingFileError("cannot create directory because there "
226 "is a file in the way")
228 return defer.succeed(node)
229 d = node.get(path[0])
230 def _maybe_create(f):
232 return node.create_empty_directory(path[0])
233 d.addErrback(_maybe_create)
234 d.addCallback(self._get_or_create_directories, path[1:])
237 def _POST_upload(self, ctx):
239 charset = get_arg(req, "_charset", "utf-8")
240 contents = req.fields["file"]
241 assert contents.filename is None or isinstance(contents.filename, str)
242 name = get_arg(req, "name")
243 name = name or contents.filename
247 # this prohibts empty, missing, and all-whitespace filenames
248 raise WebError("upload requires a name")
249 assert isinstance(name, str)
250 name = name.decode(charset)
252 raise WebError("name= may not contain a slash", http.BAD_REQUEST)
253 assert isinstance(name, unicode)
255 # since POST /uri/path/file?t=upload is equivalent to
256 # POST /uri/path/dir?t=upload&name=foo, just do the same thing that
257 # childFactory would do. Things are cleaner if we only do a subset of
258 # them, though, so we don't do: d = self.childFactory(ctx, name)
260 d = self.node.get(name)
261 def _maybe_got_node(node_or_failure):
262 if isinstance(node_or_failure, Failure):
265 # create a placeholder which will see POST t=upload
266 return PlaceHolderNodeHandler(self.node, name)
268 node = node_or_failure
269 return make_handler_for(node, self.node, name)
270 d.addBoth(_maybe_got_node)
271 # now we have a placeholder or a filenodehandler, and we can just
272 # delegate to it. We could return the resource back out of
273 # DirectoryNodeHandler.renderHTTP, and nevow would recurse into it,
274 # but the addCallback() that handles when_done= would break.
275 d.addCallback(lambda child: child.renderHTTP(ctx))
278 def _POST_uri(self, req):
279 childcap = get_arg(req, "uri")
281 raise WebError("set-uri requires a uri")
282 name = get_arg(req, "name")
284 raise WebError("set-uri requires a name")
285 charset = get_arg(req, "_charset", "utf-8")
286 name = name.decode(charset)
287 replace = boolean_of_arg(get_arg(req, "replace", "true"))
288 d = self.node.set_uri(name, childcap, overwrite=replace)
289 d.addCallback(lambda res: childcap)
292 def _POST_delete(self, req):
293 name = get_arg(req, "name")
295 # apparently an <input type="hidden" name="name" value="">
296 # won't show up in the resulting encoded form.. the 'name'
297 # field is completely missing. So to allow deletion of an
298 # empty file, we have to pretend that None means ''. The only
299 # downide of this is a slightly confusing error message if
300 # someone does a POST without a name= field. For our own HTML
301 # thisn't a big deal, because we create the 'delete' POST
304 charset = get_arg(req, "_charset", "utf-8")
305 name = name.decode(charset)
306 d = self.node.delete(name)
307 d.addCallback(lambda res: "thing deleted")
310 def _POST_rename(self, req):
311 charset = get_arg(req, "_charset", "utf-8")
312 from_name = get_arg(req, "from_name")
313 if from_name is not None:
314 from_name = from_name.strip()
315 from_name = from_name.decode(charset)
316 assert isinstance(from_name, unicode)
317 to_name = get_arg(req, "to_name")
318 if to_name is not None:
319 to_name = to_name.strip()
320 to_name = to_name.decode(charset)
321 assert isinstance(to_name, unicode)
322 if not from_name or not to_name:
323 raise WebError("rename requires from_name and to_name")
324 for k,v in [ ('from_name', from_name), ('to_name', to_name) ]:
326 raise WebError("%s= may not contain a slash" % k,
329 replace = boolean_of_arg(get_arg(req, "replace", "true"))
330 d = self.node.move_child_to(from_name, self.node, to_name, replace)
331 d.addCallback(lambda res: "thing renamed")
334 def _POST_check(self, req):
335 # check this directory
336 d = self.node.check()
337 d.addCallback(lambda res: CheckerResults(res))
340 def _POST_deep_check(self, req):
341 # check this directory and everything reachable from it
342 verify = boolean_of_arg(get_arg(req, "verify", "false"))
343 repair = boolean_of_arg(get_arg(req, "repair", "false"))
345 d = self.node.deep_check_and_repair(verify)
346 d.addCallback(lambda res: DeepCheckAndRepairResults(res))
348 d = self.node.deep_check(verify)
349 d.addCallback(lambda res: DeepCheckResults(res))
352 def _POST_set_children(self, req):
353 replace = boolean_of_arg(get_arg(req, "replace", "true"))
355 body = req.content.read()
357 children = simplejson.loads(body)
358 except ValueError, le:
359 le.args = tuple(le.args + (body,))
360 # TODO test handling of bad JSON
363 for name, (file_or_dir, mddict) in children.iteritems():
364 cap = str(mddict.get('rw_uri') or mddict.get('ro_uri'))
365 cs.append((name, cap, mddict.get('metadata')))
366 d = self.node.set_children(cs, replace)
367 d.addCallback(lambda res: "Okay so I did it.")
371 def abbreviated_dirnode(dirnode):
372 u = from_string_dirnode(dirnode.get_uri())
373 si = u.get_filenode_uri().storage_index
374 si_s = base32.b2a(si)
377 class DirectoryAsHTML(rend.Page):
378 # The remainder of this class is to render the directory into
379 # human+browser -oriented HTML.
380 docFactory = getxmlfile("directory.xhtml")
383 def __init__(self, node):
384 rend.Page.__init__(self)
387 def render_title(self, ctx, data):
388 si_s = abbreviated_dirnode(self.node)
389 header = ["Directory SI=%s" % si_s]
390 return ctx.tag[header]
392 def render_header(self, ctx, data):
393 si_s = abbreviated_dirnode(self.node)
394 header = ["Directory SI=%s" % si_s]
395 if self.node.is_readonly():
396 header.append(" (readonly)")
397 return ctx.tag[header]
399 def get_root(self, ctx):
401 # the addSlash=True gives us one extra (empty) segment
402 depth = len(req.prepath) + len(req.postpath) - 1
403 link = "/".join([".."] * depth)
406 def render_welcome(self, ctx, data):
407 link = self.get_root(ctx)
408 return T.div[T.a(href=link)["Return to Welcome page"]]
410 def data_children(self, ctx, data):
412 d.addCallback(lambda dict: sorted(dict.items()))
413 def _stall_some(items):
414 # Deferreds don't optimize out tail recursion, and the way
415 # Nevow's flattener handles Deferreds doesn't take this into
416 # account. As a result, large lists of Deferreds that fire in the
417 # same turn (i.e. the output of defer.succeed) will cause a stack
418 # overflow. To work around this, we insert a turn break after
419 # every 100 items, using foolscap's fireEventually(). This gives
420 # the stack a chance to be popped. It would also work to put
421 # every item in its own turn, but that'd be a lot more
422 # inefficient. This addresses ticket #237, for which I was never
423 # able to create a failing unit test.
425 for i,item in enumerate(items):
427 output.append(fireEventually(item))
431 d.addCallback(_stall_some)
434 def render_row(self, ctx, data):
435 name, (target, metadata) = data
436 name = name.encode("utf-8")
437 assert not isinstance(name, unicode)
439 root = self.get_root(ctx)
440 here = "%s/uri/%s/" % (root, urllib.quote(self.node.get_uri()))
441 if self.node.is_readonly():
445 # this creates a button which will cause our child__delete method
446 # to be invoked, which deletes the file and then redirects the
447 # browser back to this directory
448 delete = T.form(action=here, method="post")[
449 T.input(type='hidden', name='t', value='delete'),
450 T.input(type='hidden', name='name', value=name),
451 T.input(type='hidden', name='when_done', value="."),
452 T.input(type='submit', value='del', name="del"),
455 rename = T.form(action=here, method="get")[
456 T.input(type='hidden', name='t', value='rename-form'),
457 T.input(type='hidden', name='name', value=name),
458 T.input(type='hidden', name='when_done', value="."),
459 T.input(type='submit', value='rename', name="rename"),
462 ctx.fillSlots("delete", delete)
463 ctx.fillSlots("rename", rename)
464 if IDirectoryNode.providedBy(target):
465 check_url = "%s/uri/%s/" % (root, urllib.quote(target.get_uri()))
466 check_done_url = "../../uri/%s/" % urllib.quote(self.node.get_uri())
468 check_url = "%s/uri/%s" % (root, urllib.quote(target.get_uri()))
469 check_done_url = "../uri/%s/" % urllib.quote(self.node.get_uri())
470 check = T.form(action=check_url, method="post")[
471 T.input(type='hidden', name='t', value='check'),
472 T.input(type='hidden', name='return_to', value=check_done_url),
473 T.input(type='submit', value='check', name="check"),
475 ctx.fillSlots("overwrite",
476 self.build_overwrite_form(ctx, name, target))
477 ctx.fillSlots("check", check)
480 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
481 if "ctime" in metadata:
482 ctime = time.strftime(TIME_FORMAT,
483 time.localtime(metadata["ctime"]))
484 times.append("c: " + ctime)
485 if "mtime" in metadata:
486 mtime = time.strftime(TIME_FORMAT,
487 time.localtime(metadata["mtime"]))
490 times.append("m: " + mtime)
491 ctx.fillSlots("times", times)
493 assert (IFileNode.providedBy(target)
494 or IDirectoryNode.providedBy(target)
495 or IMutableFileNode.providedBy(target)), target
497 quoted_uri = urllib.quote(target.get_uri())
499 if IMutableFileNode.providedBy(target):
500 # to prevent javascript in displayed .html files from stealing a
501 # secret directory URI from the URL, send the browser to a URI-based
502 # page that doesn't know about the directory at all
503 dlurl = "%s/file/%s/@@named=/%s" % (root, quoted_uri, urllib.quote(name))
505 ctx.fillSlots("filename",
506 T.a(href=dlurl)[html.escape(name)])
507 ctx.fillSlots("type", "SSK")
509 ctx.fillSlots("size", "?")
511 text_plain_url = "%s/file/%s/@@named=/foo.txt" % (root, quoted_uri)
512 text_plain_tag = T.a(href=text_plain_url)["text/plain"]
514 elif IFileNode.providedBy(target):
515 dlurl = "%s/file/%s/@@named=/%s" % (root, quoted_uri, urllib.quote(name))
517 ctx.fillSlots("filename",
518 T.a(href=dlurl)[html.escape(name)])
519 ctx.fillSlots("type", "FILE")
521 ctx.fillSlots("size", target.get_size())
523 text_plain_url = "%s/file/%s/@@named=/foo.txt" % (root, quoted_uri)
524 text_plain_tag = T.a(href=text_plain_url)["text/plain"]
527 elif IDirectoryNode.providedBy(target):
529 uri_link = "%s/uri/%s/" % (root, urllib.quote(target.get_uri()))
530 ctx.fillSlots("filename",
531 T.a(href=uri_link)[html.escape(name)])
532 if target.is_readonly():
536 ctx.fillSlots("type", dirtype)
537 ctx.fillSlots("size", "-")
538 text_plain_tag = None
540 childdata = [T.a(href="%s?t=json" % name)["JSON"], ", ",
541 T.a(href="%s?t=uri" % name)["URI"], ", ",
542 T.a(href="%s?t=readonly-uri" % name)["readonly-URI"],
545 childdata.extend([", ", text_plain_tag])
547 ctx.fillSlots("data", childdata)
550 # TODO: include a link to see more results, including timestamps
551 # TODO: use a sparkline
552 ctx.fillSlots("checker_results", results)
556 def render_forms(self, ctx, data):
558 deep_check = T.form(action=".", method="post",
559 enctype="multipart/form-data")[
561 T.input(type="hidden", name="t", value="deep-check"),
562 T.input(type="hidden", name="return_to", value="."),
563 T.legend(class_="freeform-form-label")["Run a deep-check operation (EXPENSIVE)"],
564 T.input(type="submit", value="Deep-Check"),
566 "Verify every bit? (EVEN MORE EXPENSIVE):",
567 T.input(type="checkbox", name="verify"),
569 forms.append(T.div(class_="freeform-form")[deep_check])
571 if self.node.is_readonly():
572 forms.append(T.div["No upload forms: directory is read-only"])
575 mkdir = T.form(action=".", method="post",
576 enctype="multipart/form-data")[
578 T.input(type="hidden", name="t", value="mkdir"),
579 T.input(type="hidden", name="when_done", value="."),
580 T.legend(class_="freeform-form-label")["Create a new directory"],
581 "New directory name: ",
582 T.input(type="text", name="name"), " ",
583 T.input(type="submit", value="Create"),
585 forms.append(T.div(class_="freeform-form")[mkdir])
587 upload = T.form(action=".", method="post",
588 enctype="multipart/form-data")[
590 T.input(type="hidden", name="t", value="upload"),
591 T.input(type="hidden", name="when_done", value="."),
592 T.legend(class_="freeform-form-label")["Upload a file to this directory"],
593 "Choose a file to upload: ",
594 T.input(type="file", name="file", class_="freeform-input-file"),
596 T.input(type="submit", value="Upload"),
598 T.input(type="checkbox", name="mutable"),
600 forms.append(T.div(class_="freeform-form")[upload])
602 mount = T.form(action=".", method="post",
603 enctype="multipart/form-data")[
605 T.input(type="hidden", name="t", value="uri"),
606 T.input(type="hidden", name="when_done", value="."),
607 T.legend(class_="freeform-form-label")["Attach a file or directory"
611 T.input(type="text", name="name"), " ",
612 "URI of new child: ",
613 T.input(type="text", name="uri"), " ",
614 T.input(type="submit", value="Attach"),
616 forms.append(T.div(class_="freeform-form")[mount])
619 def build_overwrite_form(self, ctx, name, target):
620 if IMutableFileNode.providedBy(target) and not target.is_readonly():
621 root = self.get_root(ctx)
622 action = "%s/uri/%s" % (root, urllib.quote(target.get_uri()))
623 done_url = "../uri/%s/" % urllib.quote(self.node.get_uri())
624 overwrite = T.form(action=action, method="post",
625 enctype="multipart/form-data")[
627 T.input(type="hidden", name="t", value="upload"),
628 T.input(type='hidden', name='when_done', value=done_url),
629 T.legend(class_="freeform-form-label")["Overwrite"],
631 T.input(type="file", name="file", class_="freeform-input-file"),
633 T.input(type="submit", value="Overwrite")
635 return [T.div(class_="freeform-form")[overwrite],]
639 def render_results(self, ctx, data):
641 return get_arg(req, "results", "")
644 def DirectoryJSONMetadata(ctx, dirnode):
648 for name, (childnode, metadata) in children.iteritems():
649 if childnode.is_readonly():
651 ro_uri = childnode.get_uri()
653 rw_uri = childnode.get_uri()
654 ro_uri = childnode.get_readonly_uri()
655 if IFileNode.providedBy(childnode):
656 kiddata = ("filenode", {'size': childnode.get_size(),
657 'metadata': metadata,
660 assert IDirectoryNode.providedBy(childnode), (childnode,
662 kiddata = ("dirnode", {'metadata': metadata})
664 kiddata[1]["ro_uri"] = ro_uri
666 kiddata[1]["rw_uri"] = rw_uri
667 kiddata[1]['mutable'] = childnode.is_mutable()
669 if dirnode.is_readonly():
671 dro_uri = dirnode.get_uri()
673 drw_uri = dirnode.get_uri()
674 dro_uri = dirnode.get_readonly_uri()
675 contents = { 'children': kids }
677 contents['ro_uri'] = dro_uri
679 contents['rw_uri'] = drw_uri
680 contents['mutable'] = dirnode.is_mutable()
681 data = ("dirnode", contents)
682 return simplejson.dumps(data, indent=1)
684 d.addCallback(text_plain, ctx)
687 def DirectoryURI(ctx, dirnode):
688 return text_plain(dirnode.get_uri(), ctx)
690 def DirectoryReadonlyURI(ctx, dirnode):
691 return text_plain(dirnode.get_readonly_uri(), ctx)
693 class RenameForm(rend.Page):
695 docFactory = getxmlfile("rename-form.xhtml")
697 def render_title(self, ctx, data):
698 return ctx.tag["Directory SI=%s" % abbreviated_dirnode(self.original)]
700 def render_header(self, ctx, data):
702 "in directory SI=%s" % abbreviated_dirnode(self.original),
705 if self.original.is_readonly():
706 header.append(" (readonly!)")
708 return ctx.tag[header]
710 def render_when_done(self, ctx, data):
711 return T.input(type="hidden", name="when_done", value=".")
713 def render_get_name(self, ctx, data):
715 name = get_arg(req, "name", "")
716 ctx.tag.attributes['value'] = name
720 class Manifest(rend.Page):
721 docFactory = getxmlfile("manifest.xhtml")
723 def render_title(self, ctx):
724 return T.title["Manifest of SI=%s" % abbreviated_dirnode(self.original)]
726 def render_header(self, ctx):
727 return T.p["Manifest of SI=%s" % abbreviated_dirnode(self.original)]
729 def data_items(self, ctx, data):
730 return self.original.build_manifest()
732 def render_row(self, ctx, refresh_cap):
733 ctx.fillSlots("refresh_capability", refresh_cap)
736 def DeepSize(ctx, dirnode):
737 d = dirnode.build_manifest()
738 def _measure_size(manifest):
740 for verifiercap in manifest:
741 u = from_string_verifier(verifiercap)
742 if isinstance(u, CHKFileVerifierURI):
745 d.addCallback(_measure_size)
746 d.addCallback(text_plain, ctx)
749 def DeepStats(ctx, dirnode):
750 d = dirnode.deep_stats()
751 d.addCallback(simplejson.dumps, indent=1)
752 d.addCallback(text_plain, ctx)