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.util.time_format import format_time
14 from allmydata.interfaces import IFileNode
15 from allmydata.web import filenode, directory, unlinked, status, operations
16 from allmydata.web import storage
17 from allmydata.web.common import abbreviate_size, getxmlfile, WebError, \
18 get_arg, RenderMixin, get_format, get_mutable_type
21 class URIHandler(RenderMixin, rend.Page):
22 # I live at /uri . There are several operations defined on /uri itself,
23 # mostly involved with creation of unlinked files and directories.
25 def __init__(self, client):
26 rend.Page.__init__(self, client)
29 def render_GET(self, ctx):
31 uri = get_arg(req, "uri", None)
33 raise WebError("GET /uri requires uri=")
34 there = url.URL.fromContext(ctx)
35 there = there.clear("uri")
36 # I thought about escaping the childcap that we attach to the URL
37 # here, but it seems that nevow does that for us.
38 there = there.child(uri)
41 def render_PUT(self, ctx):
43 # either "PUT /uri" to create an unlinked file, or
44 # "PUT /uri?t=mkdir" to create an unlinked directory
45 t = get_arg(req, "t", "").strip()
47 file_format = get_format(req, "CHK")
48 mutable_type = get_mutable_type(file_format)
49 if mutable_type is not None:
50 return unlinked.PUTUnlinkedSSK(req, self.client, mutable_type)
52 return unlinked.PUTUnlinkedCHK(req, self.client)
54 return unlinked.PUTUnlinkedCreateDirectory(req, self.client)
55 errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
57 raise WebError(errmsg, http.BAD_REQUEST)
59 def render_POST(self, ctx):
60 # "POST /uri?t=upload&file=newfile" to upload an
61 # unlinked file or "POST /uri?t=mkdir" to create a
64 t = get_arg(req, "t", "").strip()
65 if t in ("", "upload"):
66 file_format = get_format(req)
67 mutable_type = get_mutable_type(file_format)
68 if mutable_type is not None:
69 return unlinked.POSTUnlinkedSSK(req, self.client, mutable_type)
71 return unlinked.POSTUnlinkedCHK(req, self.client)
73 return unlinked.POSTUnlinkedCreateDirectory(req, self.client)
74 elif t == "mkdir-with-children":
75 return unlinked.POSTUnlinkedCreateDirectoryWithChildren(req,
77 elif t == "mkdir-immutable":
78 return unlinked.POSTUnlinkedCreateImmutableDirectory(req,
80 errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
82 raise WebError(errmsg, http.BAD_REQUEST)
84 def childFactory(self, ctx, name):
85 # 'name' is expected to be a URI
87 node = self.client.create_node_from_uri(name)
88 return directory.make_handler_for(node, self.client)
89 except (TypeError, AssertionError):
90 raise WebError("'%s' is not a valid file- or directory- cap"
93 class FileHandler(rend.Page):
94 # I handle /file/$FILECAP[/IGNORED] , which provides a URL from which a
95 # file can be downloaded correctly by tools like "wget".
97 def __init__(self, client):
98 rend.Page.__init__(self, client)
101 def childFactory(self, ctx, name):
103 if req.method not in ("GET", "HEAD"):
104 raise WebError("/file can only be used with GET or HEAD")
105 # 'name' must be a file URI
107 node = self.client.create_node_from_uri(name)
108 except (TypeError, AssertionError):
109 # I think this can no longer be reached
110 raise WebError("'%s' is not a valid file- or directory- cap"
112 if not IFileNode.providedBy(node):
113 raise WebError("'%s' is not a file-cap" % name)
114 return filenode.FileNodeDownloadHandler(self.client, node)
116 def renderHTTP(self, ctx):
117 raise WebError("/file must be followed by a file-cap and a name",
120 class IncidentReporter(RenderMixin, rend.Page):
121 def render_POST(self, ctx):
123 log.msg(format="User reports incident through web page: %(details)s",
124 details=get_arg(req, "details", ""),
125 level=log.WEIRD, umid="LkD9Pw")
126 req.setHeader("content-type", "text/plain")
127 return "An incident report has been saved to logs/incidents/ in the node directory."
131 class Root(rend.Page):
134 docFactory = getxmlfile("welcome.xhtml")
137 "not-configured": "Not Configured",
139 "no": "Disconnected",
142 def __init__(self, client, clock=None):
143 rend.Page.__init__(self, client)
145 # If set, clock is a twisted.internet.task.Clock that the tests
146 # use to test ophandle expiration.
147 self.child_operations = operations.OphandleTable(clock)
149 s = client.getServiceNamed("storage")
152 self.child_storage = storage.StorageStatus(s, self.client.nickname)
154 self.child_uri = URIHandler(client)
155 self.child_cap = URIHandler(client)
157 self.child_file = FileHandler(client)
158 self.child_named = FileHandler(client)
159 self.child_status = status.Status(client.get_history())
160 self.child_statistics = status.Statistics(client.stats_provider)
161 static_dir = resource_filename("allmydata.web", "static")
162 for filen in os.listdir(static_dir):
163 self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
165 def child_helper_status(self, ctx):
166 # the Helper isn't attached until after the Tub starts, so this child
167 # needs to created on each request
168 return status.HelperStatus(self.client.helper)
170 child_report_incident = IncidentReporter()
171 #child_server # let's reserve this for storage-server-over-HTTP
173 # FIXME: This code is duplicated in root.py and introweb.py.
174 def data_rendered_at(self, ctx, data):
175 return format_time(time.localtime())
176 def data_version(self, ctx, data):
177 return get_package_versions_string()
178 def data_import_path(self, ctx, data):
179 return str(allmydata)
180 def render_my_nodeid(self, ctx, data):
181 tubid_s = "TubID: "+self.client.get_long_tubid()
182 return T.td(title=tubid_s)[self.client.get_long_nodeid()]
183 def data_my_nickname(self, ctx, data):
184 return self.client.nickname
186 def render_services(self, ctx, data):
189 ss = self.client.getServiceNamed("storage")
190 stats = ss.get_stats()
191 if stats["storage_server.accepting_immutable_shares"]:
192 msg = "accepting new shares"
194 msg = "not accepting new shares (read-only)"
195 available = stats.get("storage_server.disk_avail")
196 if available is not None:
197 msg += ", %s available" % abbreviate_size(available)
198 ul[T.li[T.a(href="storage")["Storage Server"], ": ", msg]]
200 ul[T.li["Not running storage server"]]
202 if self.client.helper:
203 stats = self.client.helper.get_stats()
204 active_uploads = stats["chk_upload_helper.active_uploads"]
205 ul[T.li["Helper: %d active uploads" % (active_uploads,)]]
207 ul[T.li["Not running helper"]]
211 def data_introducer_furl_prefix(self, ctx, data):
212 ifurl = self.client.introducer_furl
213 # trim off the secret swissnum
214 (prefix, _, swissnum) = ifurl.rpartition("/")
217 if swissnum == "introducer":
220 return "%s/[censored]" % (prefix,)
222 def data_introducer_description(self, ctx, data):
223 if self.data_connected_to_introducer(ctx, data) == "no":
224 return "Introducer not connected"
227 def data_connected_to_introducer(self, ctx, data):
228 if self.client.connected_to_introducer():
232 def data_connected_to_introducer_alt(self, ctx, data):
233 return self._connectedalts[self.data_connected_to_introducer(ctx, data)]
235 def data_helper_furl_prefix(self, ctx, data):
237 uploader = self.client.getServiceNamed("uploader")
240 furl, connected = uploader.get_helper_info()
243 # trim off the secret swissnum
244 (prefix, _, swissnum) = furl.rpartition("/")
245 return "%s/[censored]" % (prefix,)
247 def data_helper_description(self, ctx, data):
248 if self.data_connected_to_helper(ctx, data) == "no":
249 return "Helper not connected"
252 def data_connected_to_helper(self, ctx, data):
254 uploader = self.client.getServiceNamed("uploader")
256 return "no" # we don't even have an Uploader
257 furl, connected = uploader.get_helper_info()
260 return "not-configured"
265 def data_connected_to_helper_alt(self, ctx, data):
266 return self._connectedalts[self.data_connected_to_helper(ctx, data)]
268 def data_known_storage_servers(self, ctx, data):
269 sb = self.client.get_storage_broker()
270 return len(sb.get_all_serverids())
272 def data_connected_storage_servers(self, ctx, data):
273 sb = self.client.get_storage_broker()
274 return len(sb.get_connected_servers())
276 def data_services(self, ctx, data):
277 sb = self.client.get_storage_broker()
278 return sorted(sb.get_known_servers(), key=lambda s: s.get_serverid())
280 def render_service_row(self, ctx, server):
281 nodeid = server.get_serverid()
283 ctx.fillSlots("peerid", server.get_longname())
284 ctx.fillSlots("nickname", server.get_nickname())
285 rhost = server.get_remote_host()
287 if nodeid == self.client.nodeid:
288 rhost_s = "(loopback)"
289 elif isinstance(rhost, address.IPv4Address):
290 rhost_s = "%s:%d" % (rhost.host, rhost.port)
295 since = server.get_last_connect_time()
299 since = server.get_last_loss_time()
300 announced = server.get_announcement_time()
301 announcement = server.get_announcement()
302 version = announcement["my-version"]
303 service_name = announcement["service-name"]
304 available_space = server.get_available_space()
305 if available_space is None:
306 available_space = "N/A"
308 available_space = abbreviate_size(available_space)
309 ctx.fillSlots("address", addr)
310 ctx.fillSlots("connected", connected)
311 ctx.fillSlots("connected_alt", self._connectedalts[connected])
312 ctx.fillSlots("connected-bool", bool(rhost))
313 ctx.fillSlots("since", format_time(time.localtime(since)))
314 ctx.fillSlots("announced", format_time(time.localtime(announced)))
315 ctx.fillSlots("version", version)
316 ctx.fillSlots("service_name", service_name)
317 ctx.fillSlots("available_space", available_space)
321 def render_download_form(self, ctx, data):
322 # this is a form where users can download files by URI
323 form = T.form(action="uri", method="get",
324 enctype="multipart/form-data")[
326 T.legend(class_="freeform-form-label")["Download a file"],
327 T.div["Tahoe-URI to download:"+SPACE,
328 T.input(type="text", name="uri")],
329 T.div["Filename to download as:"+SPACE,
330 T.input(type="text", name="filename")],
331 T.input(type="submit", value="Download!"),
335 def render_view_form(self, ctx, data):
336 # this is a form where users can download files by URI, or jump to a
338 form = T.form(action="uri", method="get",
339 enctype="multipart/form-data")[
341 T.legend(class_="freeform-form-label")["View a file or directory"],
342 "Tahoe-URI to view:"+SPACE,
343 T.input(type="text", name="uri"), SPACE*2,
344 T.input(type="submit", value="View!"),
348 def render_upload_form(self, ctx, data):
349 # This is a form where users can upload unlinked files.
350 # Users can choose immutable, SDMF, or MDMF from a radio button.
352 upload_chk = T.input(type='radio', name='format',
353 value='chk', id='upload-chk',
355 upload_sdmf = T.input(type='radio', name='format',
356 value='sdmf', id='upload-sdmf')
357 upload_mdmf = T.input(type='radio', name='format',
358 value='mdmf', id='upload-mdmf')
360 form = T.form(action="uri", method="post",
361 enctype="multipart/form-data")[
363 T.legend(class_="freeform-form-label")["Upload a file"],
364 T.div["Choose a file:"+SPACE,
365 T.input(type="file", name="file", class_="freeform-input-file")],
366 T.input(type="hidden", name="t", value="upload"),
367 T.div[upload_chk, T.label(for_="upload-chk") [" Immutable"], SPACE,
368 upload_sdmf, T.label(for_="upload-sdmf")[" SDMF"], SPACE,
369 upload_mdmf, T.label(for_="upload-mdmf")[" MDMF (experimental)"], SPACE*2,
370 T.input(type="submit", value="Upload!")],
374 def render_mkdir_form(self, ctx, data):
375 # This is a form where users can create new directories.
376 # Users can choose SDMF or MDMF from a radio button.
378 mkdir_sdmf = T.input(type='radio', name='format',
379 value='sdmf', id='mkdir-sdmf',
381 mkdir_mdmf = T.input(type='radio', name='format',
382 value='mdmf', id='mkdir-mdmf')
384 form = T.form(action="uri", method="post",
385 enctype="multipart/form-data")[
387 T.legend(class_="freeform-form-label")["Create a directory"],
388 mkdir_sdmf, T.label(for_='mkdir-sdmf')[" SDMF"], SPACE,
389 mkdir_mdmf, T.label(for_='mkdir-mdmf')[" MDMF (experimental)"], SPACE*2,
390 T.input(type="hidden", name="t", value="mkdir"),
391 T.input(type="hidden", name="redirect_to_result", value="true"),
392 T.input(type="submit", value="Create a directory"),
396 def render_incident_button(self, ctx, data):
397 # this button triggers a foolscap-logging "incident"
398 form = T.form(action="report_incident", method="post",
399 enctype="multipart/form-data")[
401 T.input(type="hidden", name="t", value="report-incident"),
402 "What went wrong?"+SPACE,
403 T.input(type="text", name="details"), SPACE,
404 T.input(type="submit", value=u"Save \u00BB"),