]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/webish.py
webish: add POST /uri?t=upload&mutable=true
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / webish.py
1
2 import time, os.path
3 from twisted.application import service, strports, internet
4 from twisted.web import static, resource, server, html, http
5 from twisted.python import log
6 from twisted.internet import defer, address
7 from twisted.internet.interfaces import IConsumer
8 from nevow import inevow, rend, loaders, appserver, url, tags as T
9 from nevow.static import File as nevow_File # TODO: merge with static.File?
10 from allmydata.util import fileutil, idlib
11 import simplejson
12 from allmydata.interfaces import IDownloadTarget, IDirectoryNode, IFileNode, \
13      IMutableFileNode
14 import allmydata # to display import path
15 from allmydata import download
16 from allmydata.upload import FileHandle, FileName
17 from allmydata import provisioning
18 from allmydata import get_package_versions_string
19 from zope.interface import implements, Interface
20 import urllib
21 from formless import webform
22
23 from nevow.util import resource_filename
24
25 def getxmlfile(name):
26     return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
27
28 class IClient(Interface):
29     pass
30 class ILocalAccess(Interface):
31     def local_access_is_allowed():
32         """Return True if t=upload&localdir= is allowed, giving anyone who
33         can talk to the webserver control over the local (disk) filesystem."""
34
35 def boolean_of_arg(arg):
36     assert arg.lower() in ("true", "t", "1", "false", "f", "0")
37     return arg.lower() in ("true", "t", "1")
38
39 def get_arg(req, argname, default=None, multiple=False):
40     """Extract an argument from either the query args (req.args) or the form
41     body fields (req.fields). If multiple=False, this returns a single value
42     (or the default, which defaults to None), and the query args take
43     precedence. If multiple=True, this returns a tuple of arguments (possibly
44     empty), starting with all those in the query args.
45     """
46     results = []
47     if argname in req.args:
48         results.extend(req.args[argname])
49     if req.fields and argname in req.fields:
50         results.append(req.fields[argname].value)
51     if multiple:
52         return tuple(results)
53     if results:
54         return results[0]
55     return default
56
57 # we must override twisted.web.http.Request.requestReceived with a version
58 # that doesn't use cgi.parse_multipart() . Since we actually use Nevow, we
59 # override the nevow-specific subclass, nevow.appserver.NevowRequest . This
60 # is an exact copy of twisted.web.http.Request (from SVN HEAD on 10-Aug-2007)
61 # that modifies the way form arguments are parsed. Note that this sort of
62 # surgery may induce a dependency upon a particular version of twisted.web
63
64 parse_qs = http.parse_qs
65 class MyRequest(appserver.NevowRequest):
66     fields = None
67     def requestReceived(self, command, path, version):
68         """Called by channel when all data has been received.
69
70         This method is not intended for users.
71         """
72         self.content.seek(0,0)
73         self.args = {}
74         self.stack = []
75
76         self.method, self.uri = command, path
77         self.clientproto = version
78         x = self.uri.split('?', 1)
79
80         if len(x) == 1:
81             self.path = self.uri
82         else:
83             self.path, argstring = x
84             self.args = parse_qs(argstring, 1)
85
86         # cache the client and server information, we'll need this later to be
87         # serialized and sent with the request so CGIs will work remotely
88         self.client = self.channel.transport.getPeer()
89         self.host = self.channel.transport.getHost()
90
91         # Argument processing.
92
93 ##      The original twisted.web.http.Request.requestReceived code parsed the
94 ##      content and added the form fields it found there to self.args . It
95 ##      did this with cgi.parse_multipart, which holds the arguments in RAM
96 ##      and is thus unsuitable for large file uploads. The Nevow subclass
97 ##      (nevow.appserver.NevowRequest) uses cgi.FieldStorage instead (putting
98 ##      the results in self.fields), which is much more memory-efficient.
99 ##      Since we know we're using Nevow, we can anticipate these arguments
100 ##      appearing in self.fields instead of self.args, and thus skip the
101 ##      parse-content-into-self.args step.
102
103 ##      args = self.args
104 ##      ctype = self.getHeader('content-type')
105 ##      if self.method == "POST" and ctype:
106 ##          mfd = 'multipart/form-data'
107 ##          key, pdict = cgi.parse_header(ctype)
108 ##          if key == 'application/x-www-form-urlencoded':
109 ##              args.update(parse_qs(self.content.read(), 1))
110 ##          elif key == mfd:
111 ##              try:
112 ##                  args.update(cgi.parse_multipart(self.content, pdict))
113 ##              except KeyError, e:
114 ##                  if e.args[0] == 'content-disposition':
115 ##                      # Parse_multipart can't cope with missing
116 ##                      # content-dispostion headers in multipart/form-data
117 ##                      # parts, so we catch the exception and tell the client
118 ##                      # it was a bad request.
119 ##                      self.channel.transport.write(
120 ##                              "HTTP/1.1 400 Bad Request\r\n\r\n")
121 ##                      self.channel.transport.loseConnection()
122 ##                      return
123 ##                  raise
124
125         self.process()
126
127 class Directory(rend.Page):
128     addSlash = True
129     docFactory = getxmlfile("directory.xhtml")
130
131     def __init__(self, rootname, dirnode, dirpath):
132         self._rootname = rootname
133         self._dirnode = dirnode
134         self._dirpath = dirpath
135
136     def dirpath_as_string(self):
137         return "/" + "/".join(self._dirpath)
138
139     def render_title(self, ctx, data):
140         return ctx.tag["Directory '%s':" % self.dirpath_as_string()]
141
142     def render_header(self, ctx, data):
143         parent_directories = ("<%s>" % self._rootname,) + self._dirpath
144         num_dirs = len(parent_directories)
145
146         header = ["Directory '"]
147         for i,d in enumerate(parent_directories):
148             upness = num_dirs - i - 1
149             if upness:
150                 link = "/".join( ("..",) * upness )
151             else:
152                 link = "."
153             header.append(T.a(href=link)[d])
154             if upness != 0:
155                 header.append("/")
156         header.append("'")
157
158         if self._dirnode.is_readonly():
159             header.append(" (readonly)")
160         header.append(":")
161         return ctx.tag[header]
162
163     def render_welcome(self, ctx, data):
164         depth = len(self._dirpath) + 2
165         link = "/".join([".."] * depth)
166         return T.div[T.a(href=link)["Return to Welcome page"]]
167
168     def data_children(self, ctx, data):
169         d = self._dirnode.list()
170         d.addCallback(lambda dict: sorted(dict.items()))
171         return d
172
173     def render_row(self, ctx, data):
174         name, (target, metadata) = data
175
176         if self._dirnode.is_readonly():
177             delete = "-"
178             rename = "-"
179         else:
180             # this creates a button which will cause our child__delete method
181             # to be invoked, which deletes the file and then redirects the
182             # browser back to this directory
183             delete = T.form(action=url.here, method="post")[
184                 T.input(type='hidden', name='t', value='delete'),
185                 T.input(type='hidden', name='name', value=name),
186                 T.input(type='hidden', name='when_done', value=url.here),
187                 T.input(type='submit', value='del', name="del"),
188                 ]
189
190             rename = T.form(action=url.here, method="get")[
191                 T.input(type='hidden', name='t', value='rename-form'),
192                 T.input(type='hidden', name='name', value=name),
193                 T.input(type='hidden', name='when_done', value=url.here),
194                 T.input(type='submit', value='rename', name="rename"),
195                 ]
196
197         ctx.fillSlots("delete", delete)
198         ctx.fillSlots("rename", rename)
199         check = T.form(action=url.here, method="post")[
200             T.input(type='hidden', name='t', value='check'),
201             T.input(type='hidden', name='name', value=name),
202             T.input(type='hidden', name='when_done', value=url.here),
203             T.input(type='submit', value='check', name="check"),
204             ]
205         ctx.fillSlots("overwrite", self.build_overwrite(ctx, (name, target)))
206         ctx.fillSlots("check", check)
207
208         # build the base of the uri_link link url
209         uri_link = "/uri/" + urllib.quote(target.get_uri())
210
211         assert (IFileNode.providedBy(target)
212                 or IDirectoryNode.providedBy(target)
213                 or IMutableFileNode.providedBy(target)), target
214
215         if IMutableFileNode.providedBy(target):
216             # file
217
218             # add the filename to the uri_link url
219             uri_link += '?%s' % (urllib.urlencode({'filename': name}),)
220
221             # to prevent javascript in displayed .html files from stealing a
222             # secret directory URI from the URL, send the browser to a URI-based
223             # page that doesn't know about the directory at all
224             #dlurl = urllib.quote(name)
225             dlurl = uri_link
226
227             ctx.fillSlots("filename",
228                           T.a(href=dlurl)[html.escape(name)])
229             ctx.fillSlots("type", "SSK")
230
231             ctx.fillSlots("size", "?")
232
233             text_plain_link = uri_link + "?filename=foo.txt"
234             text_plain_tag = T.a(href=text_plain_link)["text/plain"]
235
236         elif IFileNode.providedBy(target):
237             # file
238
239             # add the filename to the uri_link url
240             uri_link += '?%s' % (urllib.urlencode({'filename': name}),)
241
242             # to prevent javascript in displayed .html files from stealing a
243             # secret directory URI from the URL, send the browser to a URI-based
244             # page that doesn't know about the directory at all
245             #dlurl = urllib.quote(name)
246             dlurl = uri_link
247
248             ctx.fillSlots("filename",
249                           T.a(href=dlurl)[html.escape(name)])
250             ctx.fillSlots("type", "FILE")
251
252             ctx.fillSlots("size", target.get_size())
253
254             text_plain_link = uri_link + "?filename=foo.txt"
255             text_plain_tag = T.a(href=text_plain_link)["text/plain"]
256
257         elif IDirectoryNode.providedBy(target):
258             # directory
259             ctx.fillSlots("filename",
260                           T.a(href=uri_link)[html.escape(name)])
261             if target.is_readonly():
262                 dirtype = "DIR-RO"
263             else:
264                 dirtype = "DIR"
265             ctx.fillSlots("type", dirtype)
266             ctx.fillSlots("size", "-")
267             text_plain_tag = None
268
269         childdata = [T.a(href="%s?t=json" % name)["JSON"], ", ",
270                      T.a(href="%s?t=uri" % name)["URI"], ", ",
271                      T.a(href="%s?t=readonly-uri" % name)["readonly-URI"],
272                      ]
273         if text_plain_tag:
274             childdata.extend([", ", text_plain_tag])
275
276         ctx.fillSlots("data", childdata)
277
278         try:
279             checker = IClient(ctx).getServiceNamed("checker")
280         except KeyError:
281             checker = None
282         if checker:
283             d = defer.maybeDeferred(checker.checker_results_for,
284                                     target.get_verifier())
285             def _got(checker_results):
286                 recent_results = reversed(checker_results[-5:])
287                 if IFileNode.providedBy(target):
288                     results = ("[" +
289                                ", ".join(["%d/%d" % (found, needed)
290                                           for (when,
291                                                (needed, total, found, sharemap))
292                                           in recent_results]) +
293                                "]")
294                 elif IDirectoryNode.providedBy(target):
295                     results = ("[" +
296                                "".join([{True:"+",False:"-"}[res]
297                                         for (when, res) in recent_results]) +
298                                "]")
299                 else:
300                     results = "%d results" % len(checker_results)
301                 return results
302             d.addCallback(_got)
303             results = d
304         else:
305             results = "--"
306         # TODO: include a link to see more results, including timestamps
307         # TODO: use a sparkline
308         ctx.fillSlots("checker_results", results)
309
310         return ctx.tag
311
312     def render_forms(self, ctx, data):
313         if self._dirnode.is_readonly():
314             return T.div["No upload forms: directory is read-only"]
315         mkdir = T.form(action=".", method="post",
316                        enctype="multipart/form-data")[
317             T.fieldset[
318             T.input(type="hidden", name="t", value="mkdir"),
319             T.input(type="hidden", name="when_done", value=url.here),
320             T.legend(class_="freeform-form-label")["Create a new directory"],
321             "New directory name: ",
322             T.input(type="text", name="name"), " ",
323             T.input(type="submit", value="Create"),
324             ]]
325
326         upload = T.form(action=".", method="post",
327                         enctype="multipart/form-data")[
328             T.fieldset[
329             T.input(type="hidden", name="t", value="upload"),
330             T.input(type="hidden", name="when_done", value=url.here),
331             T.legend(class_="freeform-form-label")["Upload a file to this directory"],
332             "Choose a file to upload: ",
333             T.input(type="file", name="file", class_="freeform-input-file"),
334             " ",
335             T.input(type="submit", value="Upload"),
336             " Mutable?:",
337             T.input(type="checkbox", name="mutable"),
338             ]]
339
340         mount = T.form(action=".", method="post",
341                         enctype="multipart/form-data")[
342             T.fieldset[
343             T.input(type="hidden", name="t", value="uri"),
344             T.input(type="hidden", name="when_done", value=url.here),
345             T.legend(class_="freeform-form-label")["Attach a file or directory"
346                                                    " (by URI) to this"
347                                                    " directory"],
348             "New child name: ",
349             T.input(type="text", name="name"), " ",
350             "URI of new child: ",
351             T.input(type="text", name="uri"), " ",
352             T.input(type="submit", value="Attach"),
353             ]]
354         return [T.div(class_="freeform-form")[mkdir],
355                 T.div(class_="freeform-form")[upload],
356                 T.div(class_="freeform-form")[mount],
357                 ]
358
359     def build_overwrite(self, ctx, data):
360         name, target = data
361         if IMutableFileNode.providedBy(target) and not target.is_readonly():
362             action="/uri/" + urllib.quote(target.get_uri())
363             overwrite = T.form(action=action, method="post",
364                                enctype="multipart/form-data")[
365                 T.fieldset[
366                 T.input(type="hidden", name="t", value="overwrite"),
367                 T.input(type='hidden', name='name', value=name),
368                 T.input(type='hidden', name='when_done', value=url.here),
369                 T.legend(class_="freeform-form-label")["Overwrite"],
370                 "Choose new file: ",
371                 T.input(type="file", name="file", class_="freeform-input-file"),
372                 " ",
373                 T.input(type="submit", value="Overwrite")
374                 ]]
375             return [T.div(class_="freeform-form")[overwrite],]
376         else:
377             return []
378
379     def render_results(self, ctx, data):
380         req = inevow.IRequest(ctx)
381         return get_arg(req, "results", "")
382
383 class WebDownloadTarget:
384     implements(IDownloadTarget, IConsumer)
385     def __init__(self, req, content_type, content_encoding, save_to_file):
386         self._req = req
387         self._content_type = content_type
388         self._content_encoding = content_encoding
389         self._opened = False
390         self._producer = None
391         self._save_to_file = save_to_file
392
393     def registerProducer(self, producer, streaming):
394         self._req.registerProducer(producer, streaming)
395     def unregisterProducer(self):
396         self._req.unregisterProducer()
397
398     def open(self, size):
399         self._opened = True
400         self._req.setHeader("content-type", self._content_type)
401         if self._content_encoding:
402             self._req.setHeader("content-encoding", self._content_encoding)
403         self._req.setHeader("content-length", str(size))
404         if self._save_to_file is not None:
405             # tell the browser to save the file rather display it
406             # TODO: quote save_to_file properly
407             self._req.setHeader("content-disposition",
408                                 'attachment; filename="%s"'
409                                 % self._save_to_file)
410
411     def write(self, data):
412         self._req.write(data)
413     def close(self):
414         self._req.finish()
415
416     def fail(self, why):
417         if self._opened:
418             # The content-type is already set, and the response code
419             # has already been sent, so we can't provide a clean error
420             # indication. We can emit text (which a browser might interpret
421             # as something else), and if we sent a Size header, they might
422             # notice that we've truncated the data. Keep the error message
423             # small to improve the chances of having our error response be
424             # shorter than the intended results.
425             #
426             # We don't have a lot of options, unfortunately.
427             self._req.write("problem during download\n")
428         else:
429             # We haven't written anything yet, so we can provide a sensible
430             # error message.
431             msg = str(why.type)
432             msg.replace("\n", "|")
433             self._req.setResponseCode(http.GONE, msg)
434             self._req.setHeader("content-type", "text/plain")
435             # TODO: HTML-formatted exception?
436             self._req.write(str(why))
437         self._req.finish()
438
439     def register_canceller(self, cb):
440         pass
441     def finish(self):
442         pass
443
444 class FileDownloader(resource.Resource):
445     def __init__(self, filenode, name):
446         assert (IFileNode.providedBy(filenode)
447                 or IMutableFileNode.providedBy(filenode))
448         self._filenode = filenode
449         self._name = name
450
451     def render(self, req):
452         gte = static.getTypeAndEncoding
453         type, encoding = gte(self._name,
454                              static.File.contentTypes,
455                              static.File.contentEncodings,
456                              defaultType="text/plain")
457         save_to_file = None
458         if get_arg(req, "save", False):
459             # TODO: make the API specification clear: should "save=" or
460             # "save=false" count?
461             save_to_file = self._name
462         wdt = WebDownloadTarget(req, type, encoding, save_to_file)
463         d = self._filenode.download(wdt)
464         # exceptions during download are handled by the WebDownloadTarget
465         d.addErrback(lambda why: None)
466         return server.NOT_DONE_YET
467
468 class BlockingFileError(Exception):
469     """We cannot auto-create a parent directory, because there is a file in
470     the way"""
471 class NoReplacementError(Exception):
472     """There was already a child by that name, and you asked me to not replace it"""
473 class NoLocalDirectoryError(Exception):
474     """The localdir= directory didn't exist"""
475
476 LOCALHOST = "127.0.0.1"
477
478 class NeedLocalhostError:
479     implements(inevow.IResource)
480
481     def renderHTTP(self, ctx):
482         req = inevow.IRequest(ctx)
483         req.setResponseCode(http.FORBIDDEN)
484         req.setHeader("content-type", "text/plain")
485         return "localfile= or localdir= requires a local connection"
486
487 class NeedAbsolutePathError:
488     implements(inevow.IResource)
489
490     def renderHTTP(self, ctx):
491         req = inevow.IRequest(ctx)
492         req.setResponseCode(http.FORBIDDEN)
493         req.setHeader("content-type", "text/plain")
494         return "localfile= or localdir= requires an absolute path"
495
496 class LocalAccessDisabledError:
497     implements(inevow.IResource)
498
499     def renderHTTP(self, ctx):
500         req = inevow.IRequest(ctx)
501         req.setResponseCode(http.FORBIDDEN)
502         req.setHeader("content-type", "text/plain")
503         return "local file access is disabled"
504
505
506 class LocalFileDownloader(resource.Resource):
507     def __init__(self, filenode, local_filename):
508         self._local_filename = local_filename
509         IFileNode(filenode)
510         self._filenode = filenode
511
512     def render(self, req):
513         target = download.FileName(self._local_filename)
514         d = self._filenode.download(target)
515         def _done(res):
516             req.write(self._filenode.get_uri())
517             req.finish()
518         d.addCallback(_done)
519         return server.NOT_DONE_YET
520
521
522 class FileJSONMetadata(rend.Page):
523     def __init__(self, filenode):
524         self._filenode = filenode
525
526     def renderHTTP(self, ctx):
527         req = inevow.IRequest(ctx)
528         req.setHeader("content-type", "text/plain")
529         return self.renderNode(self._filenode)
530
531     def renderNode(self, filenode):
532         file_uri = filenode.get_uri()
533         data = ("filenode",
534                 {'ro_uri': file_uri,
535                  'size': filenode.get_size(),
536                  })
537         return simplejson.dumps(data, indent=1)
538
539 class FileURI(FileJSONMetadata):
540     def renderNode(self, filenode):
541         file_uri = filenode.get_uri()
542         return file_uri
543
544 class FileReadOnlyURI(FileJSONMetadata):
545     def renderNode(self, filenode):
546         if filenode.is_readonly():
547             return filenode.get_uri()
548         else:
549             return filenode.get_readonly().get_uri()
550
551 class DirnodeWalkerMixin:
552     """Visit all nodes underneath (and including) the rootnode, one at a
553     time. For each one, call the visitor. The visitor will see the
554     IDirectoryNode before it sees any of the IFileNodes inside. If the
555     visitor returns a Deferred, I do not call the visitor again until it has
556     fired.
557     """
558
559 ##    def _walk_if_we_could_use_generators(self, rootnode, rootpath=()):
560 ##        # this is what we'd be doing if we didn't have the Deferreds and
561 ##        # thus could use generators
562 ##        yield rootpath, rootnode
563 ##        for childname, childnode in rootnode.list().items():
564 ##            childpath = rootpath + (childname,)
565 ##            if IFileNode.providedBy(childnode):
566 ##                yield childpath, childnode
567 ##            elif IDirectoryNode.providedBy(childnode):
568 ##                for res in self._walk_if_we_could_use_generators(childnode,
569 ##                                                                 childpath):
570 ##                    yield res
571
572     def walk(self, rootnode, visitor, rootpath=()):
573         d = rootnode.list()
574         def _listed(listing):
575             return listing.items()
576         d.addCallback(_listed)
577         d.addCallback(self._handle_items, visitor, rootpath)
578         return d
579
580     def _handle_items(self, items, visitor, rootpath):
581         if not items:
582             return
583         childname, (childnode, metadata) = items[0]
584         childpath = rootpath + (childname,)
585         d = defer.maybeDeferred(visitor, childpath, childnode, metadata)
586         if IDirectoryNode.providedBy(childnode):
587             d.addCallback(lambda res: self.walk(childnode, visitor, childpath))
588         d.addCallback(lambda res:
589                       self._handle_items(items[1:], visitor, rootpath))
590         return d
591
592 class LocalDirectoryDownloader(resource.Resource, DirnodeWalkerMixin):
593     def __init__(self, dirnode, localdir):
594         self._dirnode = dirnode
595         self._localdir = localdir
596
597     def _handle(self, path, node, metadata):
598         localfile = os.path.join(self._localdir, os.sep.join(path))
599         if IDirectoryNode.providedBy(node):
600             fileutil.make_dirs(localfile)
601         elif IFileNode.providedBy(node):
602             target = download.FileName(localfile)
603             return node.download(target)
604
605     def render(self, req):
606         d = self.walk(self._dirnode, self._handle)
607         def _done(res):
608             req.setHeader("content-type", "text/plain")
609             return "operation complete"
610         d.addCallback(_done)
611         return d
612
613 class DirectoryJSONMetadata(rend.Page):
614     def __init__(self, dirnode):
615         self._dirnode = dirnode
616
617     def renderHTTP(self, ctx):
618         req = inevow.IRequest(ctx)
619         req.setHeader("content-type", "text/plain")
620         return self.renderNode(self._dirnode)
621
622     def renderNode(self, node):
623         d = node.list()
624         def _got(children):
625             kids = {}
626             for name, (childnode, metadata) in children.iteritems():
627                 if IFileNode.providedBy(childnode):
628                     kiduri = childnode.get_uri()
629                     kiddata = ("filenode",
630                                {'ro_uri': kiduri,
631                                 'size': childnode.get_size(),
632                                 })
633                 else:
634                     assert IDirectoryNode.providedBy(childnode), (childnode, children,)
635                     kiddata = ("dirnode",
636                                {'ro_uri': childnode.get_readonly_uri(),
637                                 })
638                     if not childnode.is_readonly():
639                         kiddata[1]['rw_uri'] = childnode.get_uri()
640                 kids[name] = kiddata
641             contents = { 'children': kids,
642                          'ro_uri': node.get_readonly_uri(),
643                          }
644             if not node.is_readonly():
645                 contents['rw_uri'] = node.get_uri()
646             data = ("dirnode", contents)
647             return simplejson.dumps(data, indent=1)
648         d.addCallback(_got)
649         return d
650
651 class DirectoryURI(DirectoryJSONMetadata):
652     def renderNode(self, node):
653         return node.get_uri()
654
655 class DirectoryReadonlyURI(DirectoryJSONMetadata):
656     def renderNode(self, node):
657         return node.get_readonly_uri()
658
659 class RenameForm(rend.Page):
660     addSlash = True
661     docFactory = getxmlfile("rename-form.xhtml")
662
663     def __init__(self, rootname, dirnode, dirpath):
664         self._rootname = rootname
665         self._dirnode = dirnode
666         self._dirpath = dirpath
667
668     def dirpath_as_string(self):
669         return "/" + "/".join(self._dirpath)
670
671     def render_title(self, ctx, data):
672         return ctx.tag["Directory '%s':" % self.dirpath_as_string()]
673
674     def render_header(self, ctx, data):
675         parent_directories = ("<%s>" % self._rootname,) + self._dirpath
676         num_dirs = len(parent_directories)
677
678         header = [ "Rename in directory '",
679                    "<%s>/" % self._rootname,
680                    "/".join(self._dirpath),
681                    "':", ]
682
683         if self._dirnode.is_readonly():
684             header.append(" (readonly)")
685         return ctx.tag[header]
686
687     def render_when_done(self, ctx, data):
688         return T.input(type="hidden", name="when_done", value=url.here)
689
690     def render_get_name(self, ctx, data):
691         req = inevow.IRequest(ctx)
692         name = get_arg(req, "name", "")
693         ctx.tag.attributes['value'] = name
694         return ctx.tag
695
696 class POSTHandler(rend.Page):
697     def __init__(self, node, replace):
698         self._node = node
699         self._replace = replace
700
701     def _check_replacement(self, name):
702         if self._replace:
703             return defer.succeed(None)
704         d = self._node.has_child(name)
705         def _got(present):
706             if present:
707                 raise NoReplacementError("There was already a child by that "
708                                          "name, and you asked me to not "
709                                          "replace it.")
710             return None
711         d.addCallback(_got)
712         return d
713
714     def renderHTTP(self, ctx):
715         req = inevow.IRequest(ctx)
716
717         t = get_arg(req, "t")
718         assert t is not None
719
720         name = get_arg(req, "name", None)
721         if name and "/" in name:
722             req.setResponseCode(http.BAD_REQUEST)
723             req.setHeader("content-type", "text/plain")
724             return "name= may not contain a slash"
725         if name is not None:
726             name = name.strip()
727         # we allow the user to delete an empty-named file, but not to create
728         # them, since that's an easy and confusing mistake to make
729
730         when_done = get_arg(req, "when_done", None)
731         if not boolean_of_arg(get_arg(req, "replace", "true")):
732             self._replace = False
733
734         if t == "mkdir":
735             if not name:
736                 raise RuntimeError("mkdir requires a name")
737             d = self._check_replacement(name)
738             d.addCallback(lambda res: self._node.create_empty_directory(name))
739             d.addCallback(lambda res: "directory created")
740         elif t == "uri":
741             if not name:
742                 raise RuntimeError("set-uri requires a name")
743             newuri = get_arg(req, "uri")
744             assert newuri is not None
745             d = self._check_replacement(name)
746             d.addCallback(lambda res: self._node.set_uri(name, newuri))
747             d.addCallback(lambda res: newuri)
748         elif t == "delete":
749             if name is None:
750                 # apparently an <input type="hidden" name="name" value="">
751                 # won't show up in the resulting encoded form.. the 'name'
752                 # field is completely missing. So to allow deletion of an
753                 # empty file, we have to pretend that None means ''. The only
754                 # downide of this is a slightly confusing error message if
755                 # someone does a POST without a name= field. For our own HTML
756                 # thisn't a big deal, because we create the 'delete' POST
757                 # buttons ourselves.
758                 name = ''
759             d = self._node.delete(name)
760             d.addCallback(lambda res: "thing deleted")
761         elif t == "rename":
762             from_name = 'from_name' in req.fields and req.fields["from_name"].value
763             if from_name is not None:
764                 from_name = from_name.strip()
765             to_name = 'to_name' in req.fields and req.fields["to_name"].value
766             if to_name is not None:
767                 to_name = to_name.strip()
768             if not from_name or not to_name:
769                 raise RuntimeError("rename requires from_name and to_name")
770             if not IDirectoryNode.providedBy(self._node):
771                 raise RuntimeError("rename must only be called on directories")
772             for k,v in [ ('from_name', from_name), ('to_name', to_name) ]:
773                 if v and "/" in v:
774                     req.setResponseCode(http.BAD_REQUEST)
775                     req.setHeader("content-type", "text/plain")
776                     return "%s= may not contain a slash" % (k,)
777             d = self._check_replacement(to_name)
778             d.addCallback(lambda res: self._node.get(from_name))
779             def add_dest(child):
780                 uri = child.get_uri()
781                 # now actually do the rename
782                 return self._node.set_uri(to_name, uri)
783             d.addCallback(add_dest)
784             def rm_src(junk):
785                 return self._node.delete(from_name)
786             d.addCallback(rm_src)
787             d.addCallback(lambda res: "thing renamed")
788
789         elif t == "upload":
790             if "mutable" in req.fields:
791                 contents = req.fields["file"]
792                 name = name or contents.filename
793                 if name is not None:
794                     name = name.strip()
795                 if not name:
796                     raise RuntimeError("upload-mutable requires a name")
797                 # SDMF: files are small, and we can only upload data.
798                 contents.file.seek(0)
799                 data = contents.file.read()
800                 #uploadable = FileHandle(contents.file)
801                 d = self._check_replacement(name)
802                 d.addCallback(lambda res: self._node.has_child(name))
803                 def _checked(present):
804                     if present:
805                         # modify the existing one instead of creating a new
806                         # one
807                         d2 = self._node.get(name)
808                         def _got_newnode(newnode):
809                             d3 = newnode.replace(data)
810                             d3.addCallback(lambda res: newnode.get_uri())
811                             return d3
812                         d2.addCallback(_got_newnode)
813                     else:
814                         d2 = IClient(ctx).create_mutable_file(data)
815                         def _uploaded(newnode):
816                             d1 = self._node.set_node(name, newnode)
817                             d1.addCallback(lambda res: newnode.get_uri())
818                             return d1
819                         d2.addCallback(_uploaded)
820                     return d2
821                 d.addCallback(_checked)
822             else:
823                 contents = req.fields["file"]
824                 name = name or contents.filename
825                 if name is not None:
826                     name = name.strip()
827                 if not name:
828                     raise RuntimeError("upload requires a name")
829                 uploadable = FileHandle(contents.file)
830                 d = self._check_replacement(name)
831                 d.addCallback(lambda res: self._node.add_file(name, uploadable))
832                 def _done(newnode):
833                     return newnode.get_uri()
834                 d.addCallback(_done)
835
836         elif t == "overwrite":
837             contents = req.fields["file"]
838             # SDMF: files are small, and we can only upload data.
839             contents.file.seek(0)
840             data = contents.file.read()
841             # TODO: 'name' handling needs review
842             d = defer.succeed(self._node)
843             def _got_child_overwrite(child_node):
844                 child_node.replace(data)
845                 return child_node.get_uri()
846             d.addCallback(_got_child_overwrite)
847
848         elif t == "check":
849             d = self._node.get(name)
850             def _got_child_check(child_node):
851                 d2 = child_node.check()
852                 def _done(res):
853                     log.msg("checked %s, results %s" % (child_node, res))
854                     return str(res)
855                 d2.addCallback(_done)
856                 return d2
857             d.addCallback(_got_child_check)
858         else:
859             print "BAD t=%s" % t
860             return "BAD t=%s" % t
861         if when_done:
862             d.addCallback(lambda res: url.URL.fromString(when_done))
863         def _check_replacement(f):
864             # TODO: make this more human-friendly: maybe send them to the
865             # when_done page but with an extra query-arg that will display
866             # the error message in a big box at the top of the page. The
867             # directory page that when_done= usually points to accepts a
868             # result= argument.. use that.
869             f.trap(NoReplacementError)
870             req.setResponseCode(http.CONFLICT)
871             req.setHeader("content-type", "text/plain")
872             return str(f.value)
873         d.addErrback(_check_replacement)
874         return d
875
876 class DELETEHandler(rend.Page):
877     def __init__(self, node, name):
878         self._node = node
879         self._name = name
880
881     def renderHTTP(self, ctx):
882         req = inevow.IRequest(ctx)
883         d = self._node.delete(self._name)
884         def _done(res):
885             # what should this return??
886             return "%s deleted" % self._name
887         d.addCallback(_done)
888         def _trap_missing(f):
889             f.trap(KeyError)
890             req.setResponseCode(http.NOT_FOUND)
891             req.setHeader("content-type", "text/plain")
892             return "no such child %s" % self._name
893         d.addErrback(_trap_missing)
894         return d
895
896 class PUTHandler(rend.Page):
897     def __init__(self, node, path, t, localfile, localdir, replace):
898         self._node = node
899         self._path = path
900         self._t = t
901         self._localfile = localfile
902         self._localdir = localdir
903         self._replace = replace
904
905     def renderHTTP(self, ctx):
906         req = inevow.IRequest(ctx)
907         t = self._t
908         localfile = self._localfile
909         localdir = self._localdir
910
911         if t == "upload" and not (localfile or localdir):
912             req.setResponseCode(http.BAD_REQUEST)
913             req.setHeader("content-type", "text/plain")
914             return "t=upload requires localfile= or localdir="
915
916         # we must traverse the path, creating new directories as necessary
917         d = self._get_or_create_directories(self._node, self._path[:-1])
918         name = self._path[-1]
919         d.addCallback(self._check_replacement, name, self._replace)
920         if t == "upload":
921             if localfile:
922                 d.addCallback(self._upload_localfile, localfile, name)
923             else:
924                 # localdir
925                 # take the last step
926                 d.addCallback(self._get_or_create_directories, self._path[-1:])
927                 d.addCallback(self._upload_localdir, localdir)
928         elif t == "uri":
929             d.addCallback(self._attach_uri, req.content, name)
930         elif t == "mkdir":
931             d.addCallback(self._mkdir, name)
932         else:
933             d.addCallback(self._upload_file, req.content, name)
934
935         def _transform_error(f):
936             errors = {BlockingFileError: http.BAD_REQUEST,
937                       NoReplacementError: http.CONFLICT,
938                       NoLocalDirectoryError: http.BAD_REQUEST,
939                       }
940             for k,v in errors.items():
941                 if f.check(k):
942                     req.setResponseCode(v)
943                     req.setHeader("content-type", "text/plain")
944                     return str(f.value)
945             return f
946         d.addErrback(_transform_error)
947         return d
948
949     def _get_or_create_directories(self, node, path):
950         if not IDirectoryNode.providedBy(node):
951             # unfortunately it is too late to provide the name of the
952             # blocking directory in the error message.
953             raise BlockingFileError("cannot create directory because there "
954                                     "is a file in the way")
955         if not path:
956             return defer.succeed(node)
957         d = node.get(path[0])
958         def _maybe_create(f):
959             f.trap(KeyError)
960             return node.create_empty_directory(path[0])
961         d.addErrback(_maybe_create)
962         d.addCallback(self._get_or_create_directories, path[1:])
963         return d
964
965     def _check_replacement(self, node, name, replace):
966         if replace:
967             return node
968         d = node.has_child(name)
969         def _got(present):
970             if present:
971                 raise NoReplacementError("There was already a child by that "
972                                          "name, and you asked me to not "
973                                          "replace it.")
974             return node
975         d.addCallback(_got)
976         return d
977
978     def _mkdir(self, node, name):
979         d = node.create_empty_directory(name)
980         def _done(newnode):
981             return newnode.get_uri()
982         d.addCallback(_done)
983         return d
984
985     def _upload_file(self, node, contents, name):
986         uploadable = FileHandle(contents)
987         d = node.add_file(name, uploadable)
988         def _done(filenode):
989             log.msg("webish upload complete")
990             return filenode.get_uri()
991         d.addCallback(_done)
992         return d
993
994     def _upload_localfile(self, node, localfile, name):
995         uploadable = FileName(localfile)
996         d = node.add_file(name, uploadable)
997         d.addCallback(lambda filenode: filenode.get_uri())
998         return d
999
1000     def _attach_uri(self, parentnode, contents, name):
1001         newuri = contents.read().strip()
1002         d = parentnode.set_uri(name, newuri)
1003         def _done(res):
1004             return newuri
1005         d.addCallback(_done)
1006         return d
1007
1008     def _upload_localdir(self, node, localdir):
1009         # build up a list of files to upload
1010         all_files = []
1011         all_dirs = []
1012         msg = "No files to upload! %s is empty" % localdir
1013         if not os.path.exists(localdir):
1014             msg = "%s doesn't exist!" % localdir
1015             raise NoLocalDirectoryError(msg)
1016         for root, dirs, files in os.walk(localdir):
1017             if root == localdir:
1018                 path = ()
1019             else:
1020                 relative_root = root[len(localdir)+1:]
1021                 path = tuple(relative_root.split(os.sep))
1022             for d in dirs:
1023                 all_dirs.append(path + (d,))
1024             for f in files:
1025                 all_files.append(path + (f,))
1026         d = defer.succeed(msg)
1027         for dir in all_dirs:
1028             if dir:
1029                 d.addCallback(self._makedir, node, dir)
1030         for f in all_files:
1031             d.addCallback(self._upload_one_file, node, localdir, f)
1032         return d
1033
1034     def _makedir(self, res, node, dir):
1035         d = defer.succeed(None)
1036         # get the parent. As long as os.walk gives us parents before
1037         # children, this ought to work
1038         d.addCallback(lambda res: node.get_child_at_path(dir[:-1]))
1039         # then create the child directory
1040         d.addCallback(lambda parent: parent.create_empty_directory(dir[-1]))
1041         return d
1042
1043     def _upload_one_file(self, res, node, localdir, f):
1044         # get the parent. We can be sure this exists because we already
1045         # went through and created all the directories we require.
1046         localfile = os.path.join(localdir, *f)
1047         d = node.get_child_at_path(f[:-1])
1048         d.addCallback(self._upload_localfile, localfile, f[-1])
1049         return d
1050
1051
1052 class Manifest(rend.Page):
1053     docFactory = getxmlfile("manifest.xhtml")
1054     def __init__(self, dirnode, dirpath):
1055         self._dirnode = dirnode
1056         self._dirpath = dirpath
1057
1058     def dirpath_as_string(self):
1059         return "/" + "/".join(self._dirpath)
1060
1061     def render_title(self, ctx):
1062         return T.title["Manifest of %s" % self.dirpath_as_string()]
1063
1064     def render_header(self, ctx):
1065         return T.p["Manifest of %s" % self.dirpath_as_string()]
1066
1067     def data_items(self, ctx, data):
1068         return self._dirnode.build_manifest()
1069
1070     def render_row(self, ctx, refresh_cap):
1071         ctx.fillSlots("refresh_capability", refresh_cap)
1072         return ctx.tag
1073
1074 class ChildError:
1075     implements(inevow.IResource)
1076     def renderHTTP(self, ctx):
1077         req = inevow.IRequest(ctx)
1078         req.setResponseCode(http.BAD_REQUEST)
1079         req.setHeader("content-type", "text/plain")
1080         return self.text
1081
1082 def child_error(text):
1083     ce = ChildError()
1084     ce.text = text
1085     return ce, ()
1086
1087 class VDrive(rend.Page):
1088
1089     def __init__(self, node, name):
1090         self.node = node
1091         self.name = name
1092
1093     def get_child_at_path(self, path):
1094         if path:
1095             return self.node.get_child_at_path(path)
1096         return defer.succeed(self.node)
1097
1098     def locateChild(self, ctx, segments):
1099         req = inevow.IRequest(ctx)
1100         method = req.method
1101         path = segments
1102
1103         t = get_arg(req, "t", "")
1104         localfile = get_arg(req, "localfile", None)
1105         if localfile is not None:
1106             if localfile != os.path.abspath(localfile):
1107                 return NeedAbsolutePathError(), ()
1108         localdir = get_arg(req, "localdir", None)
1109         if localdir is not None:
1110             if localdir != os.path.abspath(localdir):
1111                 return NeedAbsolutePathError(), ()
1112         if localfile or localdir:
1113             if not ILocalAccess(ctx).local_access_is_allowed():
1114                 return LocalAccessDisabledError(), ()
1115             if req.getHost().host != LOCALHOST:
1116                 return NeedLocalhostError(), ()
1117         # TODO: think about clobbering/revealing config files and node secrets
1118
1119         replace = boolean_of_arg(get_arg(req, "replace", "true"))
1120
1121         if method == "GET":
1122             # the node must exist, and our operation will be performed on the
1123             # node itself.
1124             d = self.get_child_at_path(path)
1125             def file_or_dir(node):
1126                 if (IFileNode.providedBy(node)
1127                     or IMutableFileNode.providedBy(node)):
1128                     filename = "unknown"
1129                     if path:
1130                         filename = path[-1]
1131                     filename = get_arg(req, "filename", filename)
1132                     if t == "download":
1133                         if localfile:
1134                             # write contents to a local file
1135                             return LocalFileDownloader(node, localfile), ()
1136                         # send contents as the result
1137                         return FileDownloader(node, filename), ()
1138                     elif t == "":
1139                         # send contents as the result
1140                         return FileDownloader(node, filename), ()
1141                     elif t == "json":
1142                         return FileJSONMetadata(node), ()
1143                     elif t == "uri":
1144                         return FileURI(node), ()
1145                     elif t == "readonly-uri":
1146                         return FileReadOnlyURI(node), ()
1147                     else:
1148                         return child_error("bad t=%s" % t)
1149                 elif IDirectoryNode.providedBy(node):
1150                     if t == "download":
1151                         if localdir:
1152                             # recursive download to a local directory
1153                             return LocalDirectoryDownloader(node, localdir), ()
1154                         return child_error("t=download requires localdir=")
1155                     elif t == "":
1156                         # send an HTML representation of the directory
1157                         return Directory(self.name, node, path), ()
1158                     elif t == "json":
1159                         return DirectoryJSONMetadata(node), ()
1160                     elif t == "uri":
1161                         return DirectoryURI(node), ()
1162                     elif t == "readonly-uri":
1163                         return DirectoryReadonlyURI(node), ()
1164                     elif t == "manifest":
1165                         return Manifest(node, path), ()
1166                     elif t == 'rename-form':
1167                         return RenameForm(self.name, node, path), ()
1168                     else:
1169                         return child_error("bad t=%s" % t)
1170                 else:
1171                     return child_error("unknown node type")
1172             d.addCallback(file_or_dir)
1173         elif method == "POST":
1174             # the node must exist, and our operation will be performed on the
1175             # node itself.
1176             d = self.get_child_at_path(path)
1177             def _got_POST(node):
1178                 return POSTHandler(node, replace), ()
1179             d.addCallback(_got_POST)
1180         elif method == "DELETE":
1181             # the node must exist, and our operation will be performed on its
1182             # parent node.
1183             assert path # you can't delete the root
1184             d = self.get_child_at_path(path[:-1])
1185             def _got_DELETE(node):
1186                 return DELETEHandler(node, path[-1]), ()
1187             d.addCallback(_got_DELETE)
1188         elif method in ("PUT",):
1189             # the node may or may not exist, and our operation may involve
1190             # all the ancestors of the node.
1191             return PUTHandler(self.node, path, t, localfile, localdir, replace), ()
1192         else:
1193             return rend.NotFound
1194         return d
1195
1196 class URIPUTHandler(rend.Page):
1197     def renderHTTP(self, ctx):
1198         req = inevow.IRequest(ctx)
1199         assert req.method == "PUT"
1200
1201         t = get_arg(req, "t", "")
1202
1203         if t == "":
1204             # "PUT /uri", to create an unlinked file. This is like PUT but
1205             # without the associated set_uri.
1206             uploadable = FileHandle(req.content)
1207             d = IClient(ctx).upload(uploadable)
1208             d.addCallback(lambda results: results.uri)
1209             # that fires with the URI of the new file
1210             return d
1211
1212         if t == "mkdir":
1213             # "PUT /uri?t=mkdir", to create an unlinked directory.
1214             d = IClient(ctx).create_empty_dirnode()
1215             d.addCallback(lambda dirnode: dirnode.get_uri())
1216             # XXX add redirect_to_result
1217             return d
1218
1219         req.setResponseCode(http.BAD_REQUEST)
1220         req.setHeader("content-type", "text/plain")
1221         return "/uri only accepts PUT and PUT?t=mkdir"
1222
1223 class URIPOSTHandler(rend.Page):
1224     def renderHTTP(self, ctx):
1225         req = inevow.IRequest(ctx)
1226         assert req.method == "POST"
1227
1228         t = get_arg(req, "t", "").strip()
1229
1230         if t in ("", "upload"):
1231             # "POST /uri", to create an unlinked file.
1232             mutable = bool(get_arg(req, "mutable", "").strip())
1233             if mutable:
1234                 # SDMF: files are small, and we can only upload data
1235                 contents = req.fields["file"]
1236                 contents.file.seek(0)
1237                 data = contents.file.read()
1238                 d = IClient(ctx).create_mutable_file(data)
1239                 d.addCallback(lambda n: n.get_uri())
1240             else:
1241                 fileobj = req.fields["file"].file
1242                 uploadable = FileHandle(fileobj)
1243                 d = IClient(ctx).upload(uploadable)
1244                 d.addCallback(lambda results: results.uri)
1245                 # that fires with the URI of the new file
1246             return d
1247
1248         if t == "mkdir":
1249             # "POST /uri?t=mkdir", to create an unlinked directory.
1250             d = IClient(ctx).create_empty_dirnode()
1251             redirect = get_arg(req, "redirect_to_result", "false")
1252             if boolean_of_arg(redirect):
1253                 def _then_redir(res):
1254                     new_url = "uri/" + urllib.quote(res.get_uri())
1255                     req.setResponseCode(http.SEE_OTHER) # 303
1256                     req.setHeader('location', new_url)
1257                     req.finish()
1258                     return ''
1259                 d.addCallback(_then_redir)
1260             else:
1261                 d.addCallback(lambda dirnode: dirnode.get_uri())
1262             return d
1263
1264         req.setResponseCode(http.BAD_REQUEST)
1265         req.setHeader("content-type", "text/plain")
1266         err = "/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, and POST?t=mkdir"
1267         return err
1268
1269
1270 class Root(rend.Page):
1271
1272     addSlash = True
1273     docFactory = getxmlfile("welcome.xhtml")
1274
1275     def locateChild(self, ctx, segments):
1276         client = IClient(ctx)
1277         req = inevow.IRequest(ctx)
1278
1279         segments = list(segments) # XXX HELP I AM YUCKY!
1280         while segments and not segments[-1]:
1281             segments.pop()
1282         if not segments:
1283             segments.append('')
1284         segments = tuple(segments)
1285         if segments:
1286             if segments[0] == "uri":
1287                 if len(segments) == 1 or segments[1] == '':
1288                     uri = get_arg(req, "uri", None)
1289                     if uri is not None:
1290                         there = url.URL.fromContext(ctx)
1291                         there = there.clear("uri")
1292                         there = there.child("uri").child(uri)
1293                         return there, ()
1294                 if len(segments) == 1:
1295                     # /uri
1296                     if req.method == "PUT":
1297                         # either "PUT /uri" to create an unlinked file, or
1298                         # "PUT /uri?t=mkdir" to create an unlinked directory
1299                         return URIPUTHandler(), ()
1300                     elif req.method == "POST":
1301                         # "POST /uri?t=upload&file=newfile" to upload an unlinked
1302                         # file or "POST /uri?t=mkdir" to create a new directory
1303                         return URIPOSTHandler(), ()
1304                 if len(segments) < 2:
1305                     return rend.NotFound
1306                 uri = segments[1]
1307                 d = defer.maybeDeferred(client.create_node_from_uri, uri)
1308                 d.addCallback(lambda node: VDrive(node, uri))
1309                 d.addCallback(lambda vd: vd.locateChild(ctx, segments[2:]))
1310                 def _trap_KeyError(f):
1311                     f.trap(KeyError)
1312                     return rend.FourOhFour(), ()
1313                 d.addErrback(_trap_KeyError)
1314                 return d
1315             elif segments[0] == "xmlrpc":
1316                 raise NotImplementedError()
1317         return rend.Page.locateChild(self, ctx, segments)
1318
1319     child_webform_css = webform.defaultCSS
1320     child_tahoe_css = nevow_File(resource_filename('allmydata.web', 'tahoe.css'))
1321
1322     child_provisioning = provisioning.ProvisioningTool()
1323
1324     def data_version(self, ctx, data):
1325         return get_package_versions_string()
1326     def data_import_path(self, ctx, data):
1327         return str(allmydata)
1328     def data_my_nodeid(self, ctx, data):
1329         return idlib.nodeid_b2a(IClient(ctx).nodeid)
1330     def data_storage(self, ctx, data):
1331         client = IClient(ctx)
1332         try:
1333             ss = client.getServiceNamed("storage")
1334         except KeyError:
1335             return "Not running"
1336         allocated = ss.allocated_size()
1337         return "about %d bytes allocated" % allocated
1338
1339     def data_introducer_furl(self, ctx, data):
1340         return IClient(ctx).introducer_furl
1341     def data_connected_to_introducer(self, ctx, data):
1342         if IClient(ctx).connected_to_introducer():
1343             return "yes"
1344         return "no"
1345
1346     def data_helper_furl(self, ctx, data):
1347         try:
1348             uploader = IClient(ctx).getServiceNamed("uploader")
1349         except KeyError:
1350             return None
1351         furl, connected = uploader.get_helper_info()
1352         return furl
1353     def data_connected_to_helper(self, ctx, data):
1354         try:
1355             uploader = IClient(ctx).getServiceNamed("uploader")
1356         except KeyError:
1357             return "no" # we don't even have an Uploader
1358         furl, connected = uploader.get_helper_info()
1359         if connected:
1360             return "yes"
1361         return "no"
1362
1363     def data_known_storage_servers(self, ctx, data):
1364         ic = IClient(ctx).introducer_client
1365         servers = [c
1366                    for c in ic.get_all_connectors().values()
1367                    if c.service_name == "storage"]
1368         return len(servers)
1369
1370     def data_connected_storage_servers(self, ctx, data):
1371         ic = IClient(ctx).introducer_client
1372         return len(ic.get_all_connections_for("storage"))
1373
1374     def data_services(self, ctx, data):
1375         ic = IClient(ctx).introducer_client
1376         c = [ (service_name, nodeid, rsc)
1377               for (nodeid, service_name), rsc
1378               in ic.get_all_connectors().items() ]
1379         c.sort()
1380         return c
1381
1382     def render_service_row(self, ctx, data):
1383         (service_name, nodeid, rsc) = data
1384         ctx.fillSlots("peerid", "%s %s" % (idlib.nodeid_b2a(nodeid),
1385                                            rsc.nickname))
1386         if rsc.rref:
1387             rhost = rsc.remote_host
1388             if nodeid == IClient(ctx).nodeid:
1389                 rhost_s = "(loopback)"
1390             elif isinstance(rhost, address.IPv4Address):
1391                 rhost_s = "%s:%d" % (rhost.host, rhost.port)
1392             else:
1393                 rhost_s = str(rhost)
1394             connected = "Yes: to " + rhost_s
1395             since = rsc.last_connect_time
1396         else:
1397             connected = "No"
1398             since = rsc.last_loss_time
1399
1400         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
1401         ctx.fillSlots("connected", connected)
1402         ctx.fillSlots("since", time.strftime(TIME_FORMAT, time.localtime(since)))
1403         ctx.fillSlots("announced", time.strftime(TIME_FORMAT,
1404                                                  time.localtime(rsc.announcement_time)))
1405         ctx.fillSlots("version", rsc.version)
1406         ctx.fillSlots("service_name", rsc.service_name)
1407
1408         return ctx.tag
1409
1410     def render_download_form(self, ctx, data):
1411         # this is a form where users can download files by URI
1412         form = T.form(action="uri", method="get",
1413                       enctype="multipart/form-data")[
1414             T.fieldset[
1415             T.legend(class_="freeform-form-label")["Download a file"],
1416             "URI to download: ",
1417             T.input(type="text", name="uri"), " ",
1418             "Filename to download as: ",
1419             T.input(type="text", name="filename"), " ",
1420             T.input(type="submit", value="Download!"),
1421             ]]
1422         return T.div[form]
1423
1424     def render_view_form(self, ctx, data):
1425         # this is a form where users can download files by URI, or jump to a
1426         # named directory
1427         form = T.form(action="uri", method="get",
1428                       enctype="multipart/form-data")[
1429             T.fieldset[
1430             T.legend(class_="freeform-form-label")["View a file or directory"],
1431             "URI to view: ",
1432             T.input(type="text", name="uri"), " ",
1433             T.input(type="submit", value="View!"),
1434             ]]
1435         return T.div[form]
1436
1437     def render_upload_form(self, ctx, data):
1438         # this is a form where users can upload unlinked files
1439         form = T.form(action="uri", method="post",
1440                       enctype="multipart/form-data")[
1441             T.fieldset[
1442             T.legend(class_="freeform-form-label")["Upload a file"],
1443             "Choose a file: ",
1444             T.input(type="file", name="file", class_="freeform-input-file"),
1445             T.input(type="hidden", name="t", value="upload"),
1446             " Mutable?:", T.input(type="checkbox", name="mutable"),
1447             T.input(type="submit", value="Upload!"),
1448             ]]
1449         return T.div[form]
1450
1451     def render_mkdir_form(self, ctx, data):
1452         # this is a form where users can create new directories
1453         form = T.form(action="uri", method="post",
1454                       enctype="multipart/form-data")[
1455             T.fieldset[
1456             T.legend(class_="freeform-form-label")["Create a directory"],
1457             T.input(type="hidden", name="t", value="mkdir"),
1458             T.input(type="hidden", name="redirect_to_result", value="true"),
1459             T.input(type="submit", value="Create Directory!"),
1460             ]]
1461         return T.div[form]
1462
1463
1464 class LocalAccess:
1465     implements(ILocalAccess)
1466     def __init__(self):
1467         self.local_access = False
1468     def local_access_is_allowed(self):
1469         return self.local_access
1470
1471 class WebishServer(service.MultiService):
1472     name = "webish"
1473
1474     def __init__(self, webport, nodeurl_path=None):
1475         service.MultiService.__init__(self)
1476         self.webport = webport
1477         self.root = Root()
1478         self.site = site = appserver.NevowSite(self.root)
1479         self.site.requestFactory = MyRequest
1480         self.allow_local = LocalAccess()
1481         self.site.remember(self.allow_local, ILocalAccess)
1482         s = strports.service(webport, site)
1483         s.setServiceParent(self)
1484         self.listener = s # stash it so the tests can query for the portnum
1485         self._started = defer.Deferred()
1486         if nodeurl_path:
1487             self._started.addCallback(self._write_nodeurl_file, nodeurl_path)
1488
1489     def allow_local_access(self, enable=True):
1490         self.allow_local.local_access = enable
1491
1492     def startService(self):
1493         service.MultiService.startService(self)
1494         # to make various services available to render_* methods, we stash a
1495         # reference to the client on the NevowSite. This will be available by
1496         # adapting the 'context' argument to a special marker interface named
1497         # IClient.
1498         self.site.remember(self.parent, IClient)
1499         # I thought you could do the same with an existing interface, but
1500         # apparently 'ISite' does not exist
1501         #self.site._client = self.parent
1502         self._started.callback(None)
1503
1504     def _write_nodeurl_file(self, junk, nodeurl_path):
1505         # what is our webport?
1506         s = self.listener
1507         if isinstance(s, internet.TCPServer):
1508             base_url = "http://localhost:%d" % s._port.getHost().port
1509         elif isinstance(s, internet.SSLServer):
1510             base_url = "https://localhost:%d" % s._port.getHost().port
1511         else:
1512             base_url = None
1513         if base_url:
1514             f = open(nodeurl_path, 'wb')
1515             # this file is world-readable
1516             f.write(base_url + "\n")
1517             f.close()
1518