]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/web/common.py
Change web-API to support t=relink instead of t=move (+ docs and tests). fixes #1732
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / common.py
index 8a0a9cb5a7c98c707d0680d09e3b7842c63bd512..c3b94d2a36957c238a1355a5332096231f968e8e 100644 (file)
@@ -6,13 +6,15 @@ from zope.interface import Interface
 from nevow import loaders, appserver
 from nevow.inevow import IRequest
 from nevow.util import resource_filename
+from allmydata import blacklist
 from allmydata.interfaces import ExistingChildError, NoSuchChildError, \
      FileTooLargeError, NotEnoughSharesError, NoSharesError, \
      EmptyPathnameComponentError, MustBeDeepImmutableError, \
-     MustBeReadonlyError, MustNotBeUnknownRWError
+     MustBeReadonlyError, MustNotBeUnknownRWError, SDMF_VERSION, MDMF_VERSION
 from allmydata.mutable.common import UnrecoverableFileError
 from allmydata.util import abbreviate
-from allmydata.util.encodingutil import to_str
+from allmydata.util.encodingutil import to_str, quote_output
+
 
 class IOpHandleTable(Interface):
     pass
@@ -22,14 +24,60 @@ def getxmlfile(name):
 
 def boolean_of_arg(arg):
     # TODO: ""
-    assert arg.lower() in ("true", "t", "1", "false", "f", "0", "on", "off")
+    if arg.lower() not in ("true", "t", "1", "false", "f", "0", "on", "off"):
+        raise WebError("invalid boolean argument: %r" % (arg,), http.BAD_REQUEST)
     return arg.lower() in ("true", "t", "1", "on")
 
 def parse_replace_arg(replace):
     if replace.lower() == "only-files":
         return replace
-    else:
+    try:
         return boolean_of_arg(replace)
+    except WebError:
+        raise WebError("invalid replace= argument: %r" % (replace,), http.BAD_REQUEST)
+
+
+def get_format(req, default="CHK"):
+    arg = get_arg(req, "format", None)
+    if not arg:
+        if boolean_of_arg(get_arg(req, "mutable", "false")):
+            return "SDMF"
+        return default
+    if arg.upper() == "CHK":
+        return "CHK"
+    elif arg.upper() == "SDMF":
+        return "SDMF"
+    elif arg.upper() == "MDMF":
+        return "MDMF"
+    else:
+        raise WebError("Unknown format: %s, I know CHK, SDMF, MDMF" % arg,
+                       http.BAD_REQUEST)
+
+def get_mutable_type(file_format): # accepts result of get_format()
+    if file_format == "SDMF":
+        return SDMF_VERSION
+    elif file_format == "MDMF":
+        return MDMF_VERSION
+    else:
+        # this is also used to identify which formats are mutable. Use
+        #  if get_mutable_type(file_format) is not None:
+        #      do_mutable()
+        #  else:
+        #      do_immutable()
+        return None
+
+
+def parse_offset_arg(offset):
+    # XXX: This will raise a ValueError when invoked on something that
+    # is not an integer. Is that okay? Or do we want a better error
+    # message? Since this call is going to be used by programmers and
+    # their tools rather than users (through the wui), it is not
+    # inconsistent to return that, I guess.
+    if offset is not None:
+        offset = int(offset)
+
+    return offset
+
 
 def get_root(ctx_or_req):
     req = IRequest(ctx_or_req)
@@ -90,6 +138,19 @@ def abbreviate_time(data):
         return "%.1fms" % (1000*s)
     return "%.0fus" % (1000000*s)
 
+def compute_rate(bytes, seconds):
+    if bytes is None:
+      return None
+
+    if seconds is None or seconds == 0:
+      return None
+
+    # negative values don't make sense here
+    assert bytes > -1
+    assert seconds > 0
+
+    return 1.0 * bytes / seconds
+
 def abbreviate_rate(data):
     # 21.8kBps, 554.4kBps 4.37MBps
     if data is None:
@@ -153,8 +214,8 @@ def humanize_failure(f):
         return ("There was already a child by that name, and you asked me "
                 "to not replace it.", http.CONFLICT)
     if f.check(NoSuchChildError):
-        name = f.value.args[0]
-        return ("No such child: %s" % name.encode("utf-8"), http.NOT_FOUND)
+        quoted_name = quote_output(f.value.args[0], encoding="utf-8", quotemarks=False)
+        return ("No such child: %s" % quoted_name, http.NOT_FOUND)
     if f.check(NotEnoughSharesError):
         t = ("NotEnoughSharesError: This indicates that some "
              "servers were unavailable, or that shares have been "
@@ -181,42 +242,45 @@ def humanize_failure(f):
              "this object to learn more.")
         return (t, http.GONE)
     if f.check(MustNotBeUnknownRWError):
-        name = f.value.args[1]
+        quoted_name = quote_output(f.value.args[1], encoding="utf-8")
         immutable = f.value.args[2]
         if immutable:
             t = ("MustNotBeUnknownRWError: an operation to add a child named "
-                 "'%s' to a directory was given an unknown cap in a write slot.\n"
+                 "%s to a directory was given an unknown cap in a write slot.\n"
                  "If the cap is actually an immutable readcap, then using a "
                  "webapi server that supports a later version of Tahoe may help.\n\n"
                  "If you are using the webapi directly, then specifying an immutable "
                  "readcap in the read slot (ro_uri) of the JSON PROPDICT, and "
                  "omitting the write slot (rw_uri), would also work in this "
-                 "case.") % name.encode("utf-8")
+                 "case.") % quoted_name
         else:
             t = ("MustNotBeUnknownRWError: an operation to add a child named "
-                 "'%s' to a directory was given an unknown cap in a write slot.\n"
+                 "%s to a directory was given an unknown cap in a write slot.\n"
                  "Using a webapi server that supports a later version of Tahoe "
                  "may help.\n\n"
                  "If you are using the webapi directly, specifying a readcap in "
                  "the read slot (ro_uri) of the JSON PROPDICT, as well as a "
                  "writecap in the write slot if desired, would also work in this "
-                 "case.") % name.encode("utf-8")
+                 "case.") % quoted_name
         return (t, http.BAD_REQUEST)
     if f.check(MustBeDeepImmutableError):
-        name = f.value.args[1]
+        quoted_name = quote_output(f.value.args[1], encoding="utf-8")
         t = ("MustBeDeepImmutableError: a cap passed to this operation for "
-             "the child named '%s', needed to be immutable but was not. Either "
+             "the child named %s, needed to be immutable but was not. Either "
              "the cap is being added to an immutable directory, or it was "
              "originally retrieved from an immutable directory as an unknown "
-             "cap." % name.encode("utf-8"))
+             "cap.") % quoted_name
         return (t, http.BAD_REQUEST)
     if f.check(MustBeReadonlyError):
-        name = f.value.args[1]
+        quoted_name = quote_output(f.value.args[1], encoding="utf-8")
         t = ("MustBeReadonlyError: a cap passed to this operation for "
              "the child named '%s', needed to be read-only but was not. "
              "The cap is being passed in a read slot (ro_uri), or was retrieved "
-             "from a read slot as an unknown cap." % name.encode("utf-8"))
+             "from a read slot as an unknown cap.") % quoted_name
         return (t, http.BAD_REQUEST)
+    if f.check(blacklist.FileProhibited):
+        t = "Access Prohibited: %s" % quote_output(f.value.reason, encoding="utf-8", quotemarks=False)
+        return (t, http.FORBIDDEN)
     if f.check(WebError):
         return (f.value.text, f.value.code)
     if f.check(FileTooLargeError):