]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - docs/webapi.txt
3cc00592714f94c1c766e4de2c9bc0f488cc42ea
[tahoe-lafs/tahoe-lafs.git] / docs / webapi.txt
1 This document has six sections:
2
3 1.  the basic API for how to programmatically control your tahoe node
4 2.  convenience methods
5 3.  safety and security issues
6 4.  features for controlling your tahoe node from a standard web browser
7 5.  debugging and testing features
8 6.  XML-RPC (coming soon)
9
10
11 1. the basic API for how to programmatically control your tahoe node
12
13 a. connecting to the tahoe node
14
15 Writing "8123" into $NODEDIR/webport causes the node to run a webserver on
16 port 8123. Writing "tcp:8123:interface=127.0.0.1" into $NODEDIR/webport does
17 the same but binds to the loopback interface, ensuring that only the programs
18 on the local host can connect. Using
19 "ssl:8123:privateKey=mykey.pem:certKey=cert.pem" would run an SSL server. See
20 twisted.application.strports for more details.
21
22 This webport can be set when the node is created by passing a --webport
23 option to the 'tahoe create-client' command. By default, the node listens on
24 port 8123, on the loopback (127.0.0.1) interface.
25
26 b. file names
27
28 The node provides some small number of "virtual drives". In the 0.5 release,
29 this number is two: the first is the global shared vdrive, the second is the
30 private non-shared vdrive. We will call the global one "global", and we will
31 refer to the second one by "$PRIVATE_VDRIVE_URI", to show that to use it you
32 have to insert the specific URI for that private vdrive.
33
34 For the purpose of this document, let us assume that the vdrives currently
35 contain the following directories and files:
36
37 global/
38 global/Documents/
39 global/Documents/notes.txt
40
41 $PRIVATE_VDRIVE_URI/
42 $PRIVATE_VDRIVE_URI/Pictures/
43 $PRIVATE_VDRIVE_URI/Pictures/tractors.jpg
44 $PRIVATE_VDRIVE_URI/Pictures/family/
45 $PRIVATE_VDRIVE_URI/Pictures/family/bobby.jpg
46
47 Within the webserver, there is a tree of resources. The top-level "vdrive"
48 resource gives access to files and directories in all of the user's virtual
49 drives. For example, the URL that corresponds to notes.txt would be:
50
51 http://localhost:8123/vdrive/global/Documents/notes.txt
52
53 and the URL for tractors.jpg would be:
54
55 http://localhost:8123/uri/$PRIVATE_VDRIVE_URI/Pictures/tractors.jpg
56
57 In addition, each directory has a corresponding URL. The Pictures URL is:
58
59 http://localhost:8123/uri/$PRIVATE_VDRIVE_URI/Pictures
60
61 c. URIs
62
63 From the "URIs" chapter in architecture.txt, recall that each file and
64 directory has a unique "URI". This is a string which provides a secure
65 reference to the file or directory: if you know the URI, you can retrieve
66 (and possibly modify) the object. If you don't know the URI, you cannot
67 access the object.
68
69 A separate top-level namespace ("uri/" instead of "vdrive/") is used to
70 access to files and directories directly by URI, rather than by going through
71 the pathnames in the vdrive.
72
73 For example, this identifies a file or directory:
74
75 http://localhost:8123/uri/$URI
76
77 And this identifies a file or directory named "tractors.jpg" in a
78 subdirectory "Pictures" of the identified directory:
79
80 http://localhost:8123/uri/$URI/Pictures/tractors.jpg
81
82 In the following examples, "$URL" is a shorthand for a URL like the ones
83 above, either with "vdrive/" and a vdrive name as the top level and a
84 sequence of slash-separated pathnames following, or with "uri/" as the top
85 level, followed by a URI, optionally followed by a sequence of
86 slash-separated pathnames.
87
88
89 Now, what can we do with these URLs? By varying the HTTP method
90 (GET/PUT/POST/DELETE) and by appending a type-indicating query argument, we
91 control what we want to do with the data and how it should be presented.
92
93 d. examining files or directories
94
95   GET $URL?t=json
96
97   out: json metadata
98
99   This returns machine-parseable information about the indicated file or
100   directory in the HTTP response body. The JSON always contains a list, and
101   the first element of the list is always a flag that indicates whether the
102   referenced object is a file or a directory.
103
104   If it is a file, then the information includes file size and URI, like
105   this:
106
107    [ 'filenode', { 'ro_uri': file_uri,
108                    'size': bytes } ]
109
110   If it is a directory, then it includes information about the children of
111   this directory, as a mapping from child name to a set of metadata about the
112   child (the same data that would appear in a corresponding GET?t=json of the
113   child itself). Like this:
114
115    [ 'dirnode', { 'rw_uri': read_write_uri,
116                   'ro_uri': read_only_uri,
117                   'children': children } ]
118
119   In the above example, 'children' is a dictionary in which the keys are
120   child names and the values depend upon whether the child is a file or a
121   directory:
122
123    'foo.txt': [ 'filenode', { 'ro_uri': uri, 'size': bytes } ]
124    'subdir':  [ 'dirnode', { 'rw_uri': rwuri, 'ro_uri': rouri } ]
125
126   note that the value is the same as the JSON representation of the child
127   object (except that directories do not recurse -- the "children" entry of
128   the child is omitted).
129
130   Then the rw_uri field will be present in the information about a directory
131   if and only if you have read-write access to that directory,
132
133 e. downloading a file
134
135   GET $URL
136
137   out: file contents or dir metadata
138   options:
139         save=<boolean> - If true add header "Content-Disposition: attachment"
140
141   If the indicated object is a file, then this simply retrieves the contents
142   of the file. The file's contents are provided in the body of the HTTP
143   response.
144
145   If the indicated object a directory, then this returns an HTML page,
146   intended to be displayed to a human by a web browser, which contains HREF
147   links to all files and directories reachable from this directory. These
148   HREF links do not have a t= argument, meaning that a human who follows them
149   will get pages also meant for a human. It also contains forms to upload new
150   files, and to delete files and directories. These forms use POST methods to
151   do their job.
152
153   You can add the "save=true" argument, which adds a 'Content-Disposition:
154   attachment' header to prompt most web browsers to save the file to disk
155   rather than attempting to display it.
156
157   A filename (from which a MIME type can be derived, for use in the
158   Content-Type header) can be specified using a 'filename=' query argument.
159   This is especially useful if the $URL does not end with the name of the
160   file (e.g. if it ends with the URI of the file instead). This filename is
161   also the one used if the 'save=true' argument is set. For example:
162
163    GET http://localhost:8123/uri/$TRACTORS_URI?filename=tractors.jpg
164
165 f. uploading a file
166
167   PUT http://localhost:8123/uri
168
169    in: file contents
170    out: file write cap
171
172    Upload a file, using the data from the HTTP request body, and returning
173    the resulting URI as the HTTP response body. This does not make the file
174    visible from the virtual drive -- to do that, see section 1.h. below, or
175    the convenience method in section 2.a..
176
177   POST http://localhost:8123/uri?t=upload
178
179    This action also uploads a file without attaching it to a virtual drive
180    directory, but can be used from an HTML form. The response is the file
181    read cap.
182
183   POST http://localhost:8123/uri?t=upload&mutable=true
184
185    This action also uploads a file without attaching it to a virtual drive
186    directory, but creates a mutable file (SSK) instead of an immutable one.
187    The response contains the new URI that was created.
188
189   PUT http://localhost:8123/uri?mutable=true
190
191    This second form also accepts data from the HTTP request body, but creates
192    a mutable file (SSK) instead of an immutable one (CHK). The response
193    contains the new URI that was created.
194
195
196 g. creating a new directory
197
198   PUT http://localhost:8123/uri?t=mkdir
199
200   in: (nothing)
201   out: directory write cap
202
203   Create a new empty directory and return its URI as the HTTP response body.
204   This does not make the newly created directory visible from the virtual
205   drive, but you can use section 1.h. to attach it, or the convenience method
206   in section 2.XXX.
207
208 h. attaching a file or directory as the child of an extant directory
209
210   PUT $URL?t=uri
211
212   in: child cap
213   out: the same child cap
214   options:
215         replace=<boolean> - If true, overwrite existing contents.
216
217   This attaches a child (either a file or a directory) to the given directory
218   $URL is required to indicate a directory as the second-to-last element and
219   the desired filename as the last element, for example:
220
221    PUT http://localhost:8123/uri/$URI_OF_SOME_DIR/Pictures/tractors.jpg
222    PUT http://localhost:8123/uri/$URI_OF_SOME_DIR/tractors.jpg
223    PUT http://localhost:8123/uri/$PRIVATE_VDRIVE_URI/Pictures/tractors.jpg
224
225   (Note that a URI_OF_SOME_DIR and a PRIVATE_VDRIVE_URI are each just
226   separate URIs, and there is nothing special about the latter except that it
227   is useful to put all of the user's top-level files and directories into one
228   place, so we choose to use that particular directory to be the user's main
229   directory.)
230
231   The URI of the child is provided in the body of the HTTP request,
232   and this same URI is returned in the response body.
233
234   There is an optional "?replace=" param whose value can be "true", "t", "1",
235   "false", "f", or "0" (case-insensitive), and which defaults to "true". If
236   the indicated directory already contains the given child name, then if
237   replace is true then the value of that name is changed to be the new URI.
238   If replace is false then an HTTP 409 "Conflict" error is returned.
239
240   This can be used to attach a shared directory (a directory that other
241   people can read or write) to the vdrive. Intermediate directories, if any,
242   are created on-demand.
243
244 i. removing a name from a directory
245
246   DELETE $URL
247
248   This removes the given name from the given directory. $URL is required to
249   indicate a directory as the second-to-last element and the name to remove
250   from that directory as the last element, just as in section 1.g..
251
252   Note that this does not actually delete the resource that the name points
253   to from the tahoe grid -- it only removes this name in this directory. If
254   there are other names in this directory or in other directories that point
255   to the resource, then it will remain accessible through those paths. Even
256   if all names pointing to this resource are removed from their parent
257   directories, then if someone is in possession of the URI of this resource
258   they can continue to access the resource through the URI. Only if a person
259   is not in possession of the URI, and they do not have access to any
260   directories which contain names pointing to this resource, are they
261   prevented from accessing the resource. (This behavior is very similar to
262   the way hardlinks and anonymous files work in traditional unix
263   filesystems).
264
265 2. convenience methods
266
267 a. uploading a file and attaching it to the vdrive
268
269   PUT $URI
270
271   in: file contents
272   out: file write cap
273
274   statuses:
275   200 - File updated.  [FIXME: Is this true yet?]
276   201 - File created.  [FIXME: Is this true yet?]
277
278   Upload a file and link it into the the vdrive at the location specified by
279   $URI. The last item in the $URI must be a filename, and the second-to-last
280   item must identify a directory.
281
282   It will create intermediate directories as necessary. The file's contents
283   are taken from the body of the HTTP request. For convenience, the HTTP
284   response contains the URI that results from uploading the file, although
285   the client is not obligated to do anything with the URI. According to the
286   HTTP/1.1 specification (rfc2616), this should return a 200 (OK) code when
287   modifying an existing file, and a 201 (Created) code when creating a new
288   file. (TODO: as of 0.5, the web server only returns 200, never 201).
289
290   To use this, run 'curl -T localfile http://localhost:8123/vdrive/global/newfile'
291
292 3. safety and security issues -- names vs. URIs
293
294 The vdrive provides a mutable filesystem, but the ways that the filesystem
295 can change are limited. The only thing that can change is that the mapping
296 from child names to child objects that each directory contains can be changed
297 by adding a new child name pointing to an object, removing an existing child
298 name, or changing an existing child name to point to a different object.
299
300 Obviously if you query tahoe for information about the filesystem and then
301 act upon the filesystem (such as by getting a listing of the contents of a
302 directory and then adding a file to the directory), then the filesystem might
303 have been changed after you queried it and before you acted upon it.
304 However, if you use the URI instead of the pathname of an object when you act
305 upon the object, then the only change that can happen is when the object is a
306 directory then the set of child names it has might be different. If, on the
307 other hand, you act upon the object using its pathname, then a different
308 object might be in that place, which can result in more kinds of surprises.
309
310 For example, suppose you are writing code which recursively downloads the
311 contents of a directory. The first thing your code does is fetch the listing
312 of the contents of the directory. For each child that it fetched, if that
313 child is a file then it downloads the file, and if that child is a directory
314 then it recurses into that directory. Now, if the download and the recurse
315 actions are performed using the child's name, then the results might be
316 wrong, because for example a child name that pointed to a sub-directory when
317 you listed the directory might have been changed to point to a file (in which
318 case your attempt to recurse into it would result in an error and the file
319 would be skipped), or a child name that pointed to a file when you listed the
320 directory might now point to a sub-directory (in which case your attempt to
321 download the child would result in a file containing HTML text describing the
322 sub-directory!).
323
324 If your recursive algorithm uses the uri of the child instead of the name of
325 the child, then those kinds of mistakes just can't happen. Note that both the
326 child's name and the child's URI are included in the results of listing the
327 parent directory, so it isn't any harder to use the URI for this purpose.
328
329 In general, use names if you want "whatever object (whether file or
330 directory) is found by following this name (or sequence of names) when my
331 request reaches the server". Use URIs if you want "this particular object".
332
333 4. features for controlling your tahoe node from a standard web browser
334
335 a. uri redirect
336
337   GET http://localhost:8123/uri?uri=$URI
338
339   This causes a redirect to /uri/$URI, and retains any additional query
340   arguments (like filename= or save=). This is for the convenience of web
341   forms which allow the user to paste in a URI (obtained through some
342   out-of-band channel, like IM or email).
343
344   Note that this form merely redirects to the specific file or directory
345   indicated by the URI: unlike the GET /uri/$URI form, you cannot traverse to
346   children by appending additional path segments to the URL.
347
348 b. web page offering rename
349
350   GET $URL?t=rename-form&name=$CHILDNAME
351
352   This provides a useful facility to browser-based user interfaces. It
353   returns a page containing a form targetting the "POST $URL t=rename"
354   functionality described below, with the provided $CHILDNAME present in the
355   'from_name' field of that form. I.e. this presents a form offering to
356   rename $CHILDNAME, requesting the new name, and submitting POST rename.
357
358 c. POST forms
359
360   POST $URL
361   t=upload
362   name=childname  (optional)
363   file=newfile
364
365   This instructs the node to upload a file into the given directory. We need
366   this because forms are the only way for a web browser to upload a file
367   (browsers do not know how to do PUT or DELETE). The file's contents and the
368   new child name will be included in the form's arguments. This can only be
369   used to upload a single file at a time. To avoid confusion, name= is not
370   allowed to contain a slash (a 400 Bad Request error will result). The
371   response is the file read-cap (URI) of the resulting file.
372
373
374   POST $URL
375   t=upload
376   name=childname  (optional)
377   mutable="true"
378   file=newfile
379
380   This instructs the node to upload a file into the given directory, using a
381   mutable file (SSK) rather than the usual immutable file (CHK). As a result,
382   further operations to the same $URL will not cause the identity of the file
383   to change. The response is the file write-cap (URI) of the resulting
384   mutable file.
385
386
387   POST $URL
388   t=overwrite
389   file=newfile
390
391   This is used to replace the existing (mutable) file's contents with new
392   ones. It may only be used when $URL refers to a mutable file, as created by
393   POST $URL?t=upload&mutable=true, or PUT /uri?t=mutable . The name
394   associated with the uploaded file is ignored. TODO: rethink this, it's kind
395   of weird.
396
397
398   POST $URL
399   t=mkdir
400   name=childname
401
402   This instructs the node to create a new empty directory. The name of the
403   new child directory will be included in the form's arguments.
404
405
406   POST $URL
407   t=uri
408   name=childname
409   uri=newuri
410
411   This instructs the node to attach a child that is referenced by URI (just
412   like the PUT $URL?t=uri method). The name and URI of the new child
413   will be included in the form's arguments.
414
415
416   POST $URL
417   t=delete
418   name=childname
419
420   This instructs the node to delete a file from the given directory. The name
421   of the child to be deleted will be included in the form's arguments.
422
423
424   POST $URL
425   t=rename
426   from_name=oldchildname
427   to_name=newchildname
428
429   This instructs the node to rename a child within the given directory. The
430   child specified by 'from_name' is removed, and reattached as a child named
431   for 'to_name'. This is unconditional and will replace any child already
432   present under 'to_name', akin to 'mv -f' in unix parlance.
433
434
435   POST $URL
436   t=check
437
438   This triggers the FileChecker to determine the current "health" of the
439   given file, by counting how many shares are available. The results will be
440   displayed on the directory page containing this file.
441
442
443 5. debugging and testing features
444
445 GET $URL?t=download&localfile=$LOCALPATH
446 GET $URL?t=download&localdir=$LOCALPATH
447
448   The localfile= form instructs the node to download the given file and write
449   it into the local filesystem at $LOCALPATH. The localdir= form instructs
450   the node to recursively download everything from the given directory and
451   below into the local filesystem. To avoid surprises, the localfile= form
452   will signal an error if $URL actually refers to a directory, likewise if
453   localdir= is used with a $URL that refers to a file.
454
455   This request will only be accepted from an HTTP client connection
456   originating at 127.0.0.1 . This request is most useful when the client node
457   and the HTTP client are operated by the same user. $LOCALPATH should be an
458   absolute pathname.
459
460   This form is only implemented for testing purposes, because of a trivially
461   easy attack: any web server that the local browser visits could serve an
462   IMG tag that causes the local node to modify the local filesystem.
463   Therefore this form is only enabled if you create a file named
464   'webport_allow_localfile' in the node's base directory.
465
466 PUT $NEWURL?t=upload&localfile=$LOCALPATH
467 PUT $NEWURL?t=upload&localdir=$LOCALPATH
468
469   This uploads a file or directory from the node's local filesystem to the
470   vdrive. As with "GET $URL?t=download&localfile=$LOCALPATH", this request
471   will only be accepted from an HTTP connection originating from 127.0.0.1 .
472
473   The localfile= form expects that $LOCALPATH will point to a file on the
474   node's local filesystem, and causes the node to upload that one file into
475   the vdrive at the given location. Any parent directories will be created in
476   the vdrive as necessary.
477
478   The localdir= form expects that $LOCALPATH will point to a directory on the
479   node's local filesystem, and it causes the node to perform a recursive
480   upload of the directory into the vdrive at the given location, creating
481   parent directories as necessary. When the operation is complete, the
482   directory referenced by $NEWURL will contain all of the files and
483   directories that were present in $LOCALPATH, so this is equivalent to the
484   unix commands:
485
486    mkdir -p $NEWURL; cp -r $LOCALPATH/* $NEWURL/
487
488   Note that the "curl" utility can be used to provoke this sort of recursive
489   upload, since the -T option will make it use an HTTP 'PUT':
490
491    curl -T /dev/null 'http://localhost:8123/vdrive/global/newdir?t=upload&localdir=/home/user/directory-to-upload'
492
493   This form is only implemented for testing purposes, because any attacker's
494   web server that a local browser visits could serve an IMG tag that causes
495   the local node to modify the local filesystem. Therefore this form is only
496   enabled if you create a file named 'webport_allow_localfile' in the node's
497   base directory.
498
499 GET $URL?t=manifest
500
501   Return an HTML-formatted manifest of the given directory, for debugging.
502
503 6. XMLRPC (coming soon)
504
505   http://localhost:8123/xmlrpc
506
507   This resource provides an XMLRPC server on which all of the previous
508   operations can be expressed as function calls taking a "pathname" argument.
509   This is provided for applications that want to think of everything in terms
510   of XMLRPC.
511
512    listdir(vdrivename, path) -> dict of (childname -> (stuff))
513    put(vdrivename, path, contents) -> URI
514    get(vdrivename, path) -> contents
515    mkdir(vdrivename, path) -> URI
516    put_localfile(vdrivename, path, localfilename) -> URI
517    get_localfile(vdrivename, path, localfilename)
518    put_localdir(vdrivename, path, localdirname)   # recursive
519    get_localdir(vdrivename, path, localdirname)   # recursive
520    put_uri(vdrivename, path, URI)
521
522    etc..
523
524