3 from twisted.internet import address
4 from twisted.web import http
5 from nevow import rend, url, tags as T
6 from nevow.inevow import IRequest
7 from nevow.static import File as nevow_File # TODO: merge with static.File?
8 from nevow.util import resource_filename
10 import allmydata # to display import path
11 from allmydata import get_package_versions_string
12 from allmydata.util import log
13 from allmydata.interfaces import IFileNode
14 from allmydata.web import filenode, directory, unlinked, status, operations
15 from allmydata.web import storage
16 from allmydata.web.common import abbreviate_size, getxmlfile, WebError, \
17 get_arg, RenderMixin, get_format, get_mutable_type, TIME_FORMAT
20 class URIHandler(RenderMixin, rend.Page):
21 # I live at /uri . There are several operations defined on /uri itself,
22 # mostly involved with creation of unlinked files and directories.
24 def __init__(self, client):
25 rend.Page.__init__(self, client)
28 def render_GET(self, ctx):
30 uri = get_arg(req, "uri", None)
32 raise WebError("GET /uri requires uri=")
33 there = url.URL.fromContext(ctx)
34 there = there.clear("uri")
35 # I thought about escaping the childcap that we attach to the URL
36 # here, but it seems that nevow does that for us.
37 there = there.child(uri)
40 def render_PUT(self, ctx):
42 # either "PUT /uri" to create an unlinked file, or
43 # "PUT /uri?t=mkdir" to create an unlinked directory
44 t = get_arg(req, "t", "").strip()
46 file_format = get_format(req, "CHK")
47 mutable_type = get_mutable_type(file_format)
48 if mutable_type is not None:
49 return unlinked.PUTUnlinkedSSK(req, self.client, mutable_type)
51 return unlinked.PUTUnlinkedCHK(req, self.client)
53 return unlinked.PUTUnlinkedCreateDirectory(req, self.client)
54 errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
56 raise WebError(errmsg, http.BAD_REQUEST)
58 def render_POST(self, ctx):
59 # "POST /uri?t=upload&file=newfile" to upload an
60 # unlinked file or "POST /uri?t=mkdir" to create a
63 t = get_arg(req, "t", "").strip()
64 if t in ("", "upload"):
65 file_format = get_format(req)
66 mutable_type = get_mutable_type(file_format)
67 if mutable_type is not None:
68 return unlinked.POSTUnlinkedSSK(req, self.client, mutable_type)
70 return unlinked.POSTUnlinkedCHK(req, self.client)
72 return unlinked.POSTUnlinkedCreateDirectory(req, self.client)
73 elif t == "mkdir-with-children":
74 return unlinked.POSTUnlinkedCreateDirectoryWithChildren(req,
76 elif t == "mkdir-immutable":
77 return unlinked.POSTUnlinkedCreateImmutableDirectory(req,
79 errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
81 raise WebError(errmsg, http.BAD_REQUEST)
83 def childFactory(self, ctx, name):
84 # 'name' is expected to be a URI
86 node = self.client.create_node_from_uri(name)
87 return directory.make_handler_for(node, self.client)
88 except (TypeError, AssertionError):
89 raise WebError("'%s' is not a valid file- or directory- cap"
92 class FileHandler(rend.Page):
93 # I handle /file/$FILECAP[/IGNORED] , which provides a URL from which a
94 # file can be downloaded correctly by tools like "wget".
96 def __init__(self, client):
97 rend.Page.__init__(self, client)
100 def childFactory(self, ctx, name):
102 if req.method not in ("GET", "HEAD"):
103 raise WebError("/file can only be used with GET or HEAD")
104 # 'name' must be a file URI
106 node = self.client.create_node_from_uri(name)
107 except (TypeError, AssertionError):
108 # I think this can no longer be reached
109 raise WebError("'%s' is not a valid file- or directory- cap"
111 if not IFileNode.providedBy(node):
112 raise WebError("'%s' is not a file-cap" % name)
113 return filenode.FileNodeDownloadHandler(self.client, node)
115 def renderHTTP(self, ctx):
116 raise WebError("/file must be followed by a file-cap and a name",
119 class IncidentReporter(RenderMixin, rend.Page):
120 def render_POST(self, ctx):
122 log.msg(format="User reports incident through web page: %(details)s",
123 details=get_arg(req, "details", ""),
124 level=log.WEIRD, umid="LkD9Pw")
125 req.setHeader("content-type", "text/plain")
126 return "An incident report has been saved to logs/incidents/ in the node directory."
130 class Root(rend.Page):
133 docFactory = getxmlfile("welcome.xhtml")
136 "not-configured": "Not Configured",
138 "no": "Disconnected",
141 def __init__(self, client, clock=None):
142 rend.Page.__init__(self, client)
144 # If set, clock is a twisted.internet.task.Clock that the tests
145 # use to test ophandle expiration.
146 self.child_operations = operations.OphandleTable(clock)
148 s = client.getServiceNamed("storage")
151 self.child_storage = storage.StorageStatus(s, self.client.nickname)
153 self.child_uri = URIHandler(client)
154 self.child_cap = URIHandler(client)
156 self.child_file = FileHandler(client)
157 self.child_named = FileHandler(client)
158 self.child_status = status.Status(client.get_history())
159 self.child_statistics = status.Statistics(client.stats_provider)
160 static_dir = resource_filename("allmydata.web", "static")
161 for filen in os.listdir(static_dir):
162 self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
164 def child_helper_status(self, ctx):
165 # the Helper isn't attached until after the Tub starts, so this child
166 # needs to created on each request
167 return status.HelperStatus(self.client.helper)
169 child_report_incident = IncidentReporter()
170 #child_server # let's reserve this for storage-server-over-HTTP
172 # FIXME: This code is duplicated in root.py and introweb.py.
173 def data_rendered_at(self, ctx, data):
174 return time.strftime(TIME_FORMAT, time.localtime())
175 def data_version(self, ctx, data):
176 return get_package_versions_string()
177 def data_import_path(self, ctx, data):
178 return str(allmydata)
179 def render_my_nodeid(self, ctx, data):
180 tubid_s = "TubID: "+self.client.get_long_tubid()
181 return T.td(title=tubid_s)[self.client.get_long_nodeid()]
182 def data_my_nickname(self, ctx, data):
183 return self.client.nickname
185 def render_services(self, ctx, data):
188 ss = self.client.getServiceNamed("storage")
189 stats = ss.get_stats()
190 if stats["storage_server.accepting_immutable_shares"]:
191 msg = "accepting new shares"
193 msg = "not accepting new shares (read-only)"
194 available = stats.get("storage_server.disk_avail")
195 if available is not None:
196 msg += ", %s available" % abbreviate_size(available)
197 ul[T.li[T.a(href="storage")["Storage Server"], ": ", msg]]
199 ul[T.li["Not running storage server"]]
201 if self.client.helper:
202 stats = self.client.helper.get_stats()
203 active_uploads = stats["chk_upload_helper.active_uploads"]
204 ul[T.li["Helper: %d active uploads" % (active_uploads,)]]
206 ul[T.li["Not running helper"]]
210 def data_introducer_furl_prefix(self, ctx, data):
211 ifurl = self.client.introducer_furl
212 # trim off the secret swissnum
213 (prefix, _, swissnum) = ifurl.rpartition("/")
216 if swissnum == "introducer":
219 return "%s/[censored]" % (prefix,)
221 def data_introducer_description(self, ctx, data):
222 if self.data_connected_to_introducer(ctx, data) == "no":
223 return "Introducer not connected"
226 def data_connected_to_introducer(self, ctx, data):
227 if self.client.connected_to_introducer():
231 def data_connected_to_introducer_alt(self, ctx, data):
232 return self._connectedalts[self.data_connected_to_introducer(ctx, data)]
234 def data_helper_furl_prefix(self, ctx, data):
236 uploader = self.client.getServiceNamed("uploader")
239 furl, connected = uploader.get_helper_info()
242 # trim off the secret swissnum
243 (prefix, _, swissnum) = furl.rpartition("/")
244 return "%s/[censored]" % (prefix,)
246 def data_helper_description(self, ctx, data):
247 if self.data_connected_to_helper(ctx, data) == "no":
248 return "Helper not connected"
251 def data_connected_to_helper(self, ctx, data):
253 uploader = self.client.getServiceNamed("uploader")
255 return "no" # we don't even have an Uploader
256 furl, connected = uploader.get_helper_info()
259 return "not-configured"
264 def data_connected_to_helper_alt(self, ctx, data):
265 return self._connectedalts[self.data_connected_to_helper(ctx, data)]
267 def data_known_storage_servers(self, ctx, data):
268 sb = self.client.get_storage_broker()
269 return len(sb.get_all_serverids())
271 def data_connected_storage_servers(self, ctx, data):
272 sb = self.client.get_storage_broker()
273 return len(sb.get_connected_servers())
275 def data_services(self, ctx, data):
276 sb = self.client.get_storage_broker()
277 return sorted(sb.get_known_servers(), key=lambda s: s.get_serverid())
279 def render_service_row(self, ctx, server):
280 nodeid = server.get_serverid()
282 ctx.fillSlots("peerid", server.get_longname())
283 ctx.fillSlots("nickname", server.get_nickname())
284 rhost = server.get_remote_host()
286 if nodeid == self.client.nodeid:
287 rhost_s = "(loopback)"
288 elif isinstance(rhost, address.IPv4Address):
289 rhost_s = "%s:%d" % (rhost.host, rhost.port)
294 since = server.get_last_connect_time()
298 since = server.get_last_loss_time()
299 announced = server.get_announcement_time()
300 announcement = server.get_announcement()
301 version = announcement["my-version"]
302 service_name = announcement["service-name"]
303 available_space = server.get_available_space()
304 if available_space is None:
305 available_space = "N/A"
307 available_space = abbreviate_size(available_space)
308 ctx.fillSlots("address", addr)
309 ctx.fillSlots("connected", connected)
310 ctx.fillSlots("connected_alt", self._connectedalts[connected])
311 ctx.fillSlots("connected-bool", bool(rhost))
312 ctx.fillSlots("since", time.strftime(TIME_FORMAT,
313 time.localtime(since)))
314 ctx.fillSlots("announced", time.strftime(TIME_FORMAT,
315 time.localtime(announced)))
316 ctx.fillSlots("version", version)
317 ctx.fillSlots("service_name", service_name)
318 ctx.fillSlots("available_space", available_space)
322 def render_download_form(self, ctx, data):
323 # this is a form where users can download files by URI
324 form = T.form(action="uri", method="get",
325 enctype="multipart/form-data")[
327 T.legend(class_="freeform-form-label")["Download a file"],
328 T.div["Tahoe-URI to download:"+SPACE,
329 T.input(type="text", name="uri")],
330 T.div["Filename to download as:"+SPACE,
331 T.input(type="text", name="filename")],
332 T.input(type="submit", value="Download!"),
336 def render_view_form(self, ctx, data):
337 # this is a form where users can download files by URI, or jump to a
339 form = T.form(action="uri", method="get",
340 enctype="multipart/form-data")[
342 T.legend(class_="freeform-form-label")["View a file or directory"],
343 "Tahoe-URI to view:"+SPACE,
344 T.input(type="text", name="uri"), SPACE*2,
345 T.input(type="submit", value="View!"),
349 def render_upload_form(self, ctx, data):
350 # This is a form where users can upload unlinked files.
351 # Users can choose immutable, SDMF, or MDMF from a radio button.
353 upload_chk = T.input(type='radio', name='format',
354 value='chk', id='upload-chk',
356 upload_sdmf = T.input(type='radio', name='format',
357 value='sdmf', id='upload-sdmf')
358 upload_mdmf = T.input(type='radio', name='format',
359 value='mdmf', id='upload-mdmf')
361 form = T.form(action="uri", method="post",
362 enctype="multipart/form-data")[
364 T.legend(class_="freeform-form-label")["Upload a file"],
365 T.div["Choose a file:"+SPACE,
366 T.input(type="file", name="file", class_="freeform-input-file")],
367 T.input(type="hidden", name="t", value="upload"),
368 T.div[upload_chk, T.label(for_="upload-chk") [" Immutable"], SPACE,
369 upload_sdmf, T.label(for_="upload-sdmf")[" SDMF"], SPACE,
370 upload_mdmf, T.label(for_="upload-mdmf")[" MDMF (experimental)"], SPACE*2,
371 T.input(type="submit", value="Upload!")],
375 def render_mkdir_form(self, ctx, data):
376 # This is a form where users can create new directories.
377 # Users can choose SDMF or MDMF from a radio button.
379 mkdir_sdmf = T.input(type='radio', name='format',
380 value='sdmf', id='mkdir-sdmf',
382 mkdir_mdmf = T.input(type='radio', name='format',
383 value='mdmf', id='mkdir-mdmf')
385 form = T.form(action="uri", method="post",
386 enctype="multipart/form-data")[
388 T.legend(class_="freeform-form-label")["Create a directory"],
389 mkdir_sdmf, T.label(for_='mkdir-sdmf')[" SDMF"], SPACE,
390 mkdir_mdmf, T.label(for_='mkdir-mdmf')[" MDMF (experimental)"], SPACE*2,
391 T.input(type="hidden", name="t", value="mkdir"),
392 T.input(type="hidden", name="redirect_to_result", value="true"),
393 T.input(type="submit", value="Create a directory"),
397 def render_incident_button(self, ctx, data):
398 # this button triggers a foolscap-logging "incident"
399 form = T.form(action="report_incident", method="post",
400 enctype="multipart/form-data")[
402 T.input(type="hidden", name="t", value="report-incident"),
403 "What went wrong?"+SPACE,
404 T.input(type="text", name="details"), SPACE,
405 T.input(type="submit", value=u"Save \u00BB"),