def test_parse_replace_arg(self): self.failUnlessReallyEqual(common.parse_replace_arg(b"true"), True) self.failUnlessReallyEqual(common.parse_replace_arg(b"false"), False) self.failUnlessReallyEqual(common.parse_replace_arg(b"only-files"), ONLY_FILES) self.failUnlessRaises(common.WebError, common.parse_replace_arg, b"only_fles")
def _POST_set_children(self, req): replace = parse_replace_arg(get_arg(req, "replace", "true")) req.content.seek(0) body = req.content.read() try: children = json.loads(body) except ValueError, le: le.args = tuple(le.args + (body,)) # TODO test handling of bad JSON raise
def _POST_set_children(self, req): replace = parse_replace_arg(get_arg(req, "replace", "true")) req.content.seek(0) body = req.content.read() try: children = simplejson.loads(body) except ValueError, le: le.args = tuple(le.args + (body,)) # TODO test handling of bad JSON raise
def _POST_relink(self, req): charset = get_arg(req, "_charset", "utf-8") replace = parse_replace_arg(get_arg(req, "replace", "true")) from_name = get_arg(req, "from_name") if from_name is not None: from_name = from_name.strip() from_name = from_name.decode(charset) assert isinstance(from_name, unicode) else: raise WebError("from_name= is required") to_name = get_arg(req, "to_name") if to_name is not None: to_name = to_name.strip() to_name = to_name.decode(charset) assert isinstance(to_name, unicode) else: to_name = from_name # Disallow slashes in both from_name and to_name, that would only # cause confusion. if "/" in from_name: raise WebError("from_name= may not contain a slash", http.BAD_REQUEST) if "/" in to_name: raise WebError("to_name= may not contain a slash", http.BAD_REQUEST) to_dir = get_arg(req, "to_dir") if to_dir is not None and to_dir != self.node.get_write_uri(): to_dir = to_dir.strip() to_dir = to_dir.decode(charset) assert isinstance(to_dir, unicode) to_path = to_dir.split(u"/") to_root = self.client.nodemaker.create_from_cap(to_str(to_path[0])) if not IDirectoryNode.providedBy(to_root): raise WebError("to_dir is not a directory", http.BAD_REQUEST) d = to_root.get_child_at_path(to_path[1:]) else: d = defer.succeed(self.node) def _got_new_parent(new_parent): if not IDirectoryNode.providedBy(new_parent): raise WebError("to_dir is not a directory", http.BAD_REQUEST) return self.node.move_child_to(from_name, new_parent, to_name, replace) d.addCallback(_got_new_parent) d.addCallback(lambda res: "thing moved") return d
def render_PUT(self, ctx): req = IRequest(ctx) t = get_arg(req, "t", "").strip() replace = parse_replace_arg(get_arg(req, "replace", "true")) assert self.parentnode and self.name if req.getHeader("content-range"): raise WebError("Content-Range in PUT not yet supported", http.NOT_IMPLEMENTED) if not t: return self.replace_me_with_a_child(req, self.client, replace) if t == "uri": return self.replace_me_with_a_childcap(req, self.client, replace) raise WebError("PUT to a file: bad t=%s" % t)
def render_PUT(self, req): t = get_arg(req, "t", "").strip() replace = parse_replace_arg(get_arg(req, "replace", "true")) assert self.parentnode and self.name if req.getHeader("content-range"): raise WebError("Content-Range in PUT not yet supported", http.NOT_IMPLEMENTED) if not t: return self.replace_me_with_a_child(req, self.client, replace) if t == "uri": return self.replace_me_with_a_childcap(req, self.client, replace) raise WebError("PUT to a file: bad t=%s" % t)
def _POST_uri(self, req): childcap = get_arg(req, "uri") if not childcap: raise WebError("set-uri requires a uri") name = get_arg(req, "name") if not name: raise WebError("set-uri requires a name") charset = get_arg(req, "_charset", "utf-8") name = name.decode(charset) replace = parse_replace_arg(get_arg(req, "replace", "true")) # We mustn't pass childcap for the readcap argument because we don't # know whether it is a read cap. Passing a read cap as the writecap # argument will work (it ends up calling NodeMaker.create_from_cap, # which derives a readcap if necessary and possible). d = self.node.set_uri(name, childcap, None, overwrite=replace) d.addCallback(lambda res: childcap) return d
def render_PUT(self, ctx): req = IRequest(ctx) t = get_arg(req, "t", "").strip() replace = parse_replace_arg(get_arg(req, "replace", "true")) if t == "mkdir": # our job was done by the traversal/create-intermediate-directory # process that got us here. return text_plain(self.node.get_uri(), ctx) # TODO: urlencode if t == "uri": if not replace: # they're trying to set_uri and that name is already occupied # (by us). raise ExistingChildError() d = self.replace_me_with_a_childcap(req, self.client, replace) # TODO: results return d raise WebError("PUT to a directory")
def render_PUT(self, ctx): req = IRequest(ctx) t = get_arg(req, "t", "").strip() replace = parse_replace_arg(get_arg(req, "replace", "true")) offset = parse_offset_arg(get_arg(req, "offset", None)) if not t: if not replace: # this is the early trap: if someone else modifies the # directory while we're uploading, the add_file(overwrite=) # call in replace_me_with_a_child will do the late trap. raise ExistingChildError() if self.node.is_mutable(): # Are we a readonly filenode? We shouldn't allow callers # to try to replace us if we are. if self.node.is_readonly(): raise WebError("PUT to a mutable file: replace or update" " requested with read-only cap") if offset is None: return self.replace_my_contents(req) if offset >= 0: return self.update_my_contents(req, offset) raise WebError("PUT to a mutable file: Invalid offset") else: if offset is not None: raise WebError("PUT to a file: append operation invoked " "on an immutable cap") assert self.parentnode and self.name return self.replace_me_with_a_child(req, self.client, replace) if t == "uri": if not replace: raise ExistingChildError() assert self.parentnode and self.name return self.replace_me_with_a_childcap(req, self.client, replace) raise WebError("PUT to a file: bad t=%s" % t)
def render_PUT(self, ctx): req = IRequest(ctx) t = get_arg(req, "t", "").strip() replace = parse_replace_arg(get_arg(req, "replace", "true")) if not t: if self.node.is_mutable(): return self.replace_my_contents(req) if not replace: # this is the early trap: if someone else modifies the # directory while we're uploading, the add_file(overwrite=) # call in replace_me_with_a_child will do the late trap. raise ExistingChildError() assert self.parentnode and self.name return self.replace_me_with_a_child(req, self.client, replace) if t == "uri": if not replace: raise ExistingChildError() assert self.parentnode and self.name return self.replace_me_with_a_childcap(req, self.client, replace) raise WebError("PUT to a file: bad t=%s" % t)
def _POST_set_children(self, req): replace = parse_replace_arg(get_arg(req, "replace", "true")) req.content.seek(0) body = req.content.read() try: children = json.loads(body) except ValueError as le: le.args = tuple(le.args + (body, )) # TODO test handling of bad JSON raise cs = {} for name, (file_or_dir, mddict) in children.iteritems(): name = unicode(name) # json returns str *or* unicode writecap = mddict.get('rw_uri') if writecap is not None: writecap = str(writecap) readcap = mddict.get('ro_uri') if readcap is not None: readcap = str(readcap) cs[name] = (writecap, readcap, mddict.get('metadata')) d = self.node.set_children(cs, replace) d.addCallback(lambda res: "Okay so I did it.") # TODO: results return d
def _POST_set_children(self, req): replace = parse_replace_arg(get_arg(req, "replace", "true")) req.content.seek(0) body = req.content.read() try: children = json.loads(body) except ValueError as le: le.args = tuple(le.args + (body,)) # TODO test handling of bad JSON raise cs = {} for name, (file_or_dir, mddict) in children.iteritems(): name = unicode(name) # json returns str *or* unicode writecap = mddict.get('rw_uri') if writecap is not None: writecap = str(writecap) readcap = mddict.get('ro_uri') if readcap is not None: readcap = str(readcap) cs[name] = (writecap, readcap, mddict.get('metadata')) d = self.node.set_children(cs, replace) d.addCallback(lambda res: "Okay so I did it.") # TODO: results return d
def test_parse_replace_arg(self): self.failUnlessReallyEqual(common.parse_replace_arg("true"), True) self.failUnlessReallyEqual(common.parse_replace_arg("false"), False) self.failUnlessReallyEqual(common.parse_replace_arg("only-files"), "only-files") self.failUnlessRaises(common.WebError, common.parse_replace_arg, "only_fles")