webish: add /file links, change directory page to use them. This fixes filenames...
authorBrian Warner <warner@allmydata.com>
Wed, 14 May 2008 21:32:21 +0000 (14:32 -0700)
committerBrian Warner <warner@allmydata.com>
Wed, 14 May 2008 21:32:21 +0000 (14:32 -0700)
src/allmydata/test/test_web.py
src/allmydata/webish.py

index ca20669292359da63807cfde095a784c5b72ddbb..2bb05809d6c8fa86eca09468bfb1f61b5e0bdb18 100644 (file)
@@ -464,6 +464,16 @@ class Web(WebMixin, unittest.TestCase):
         d.addCallback(self.failUnlessIsBarDotTxt)
         return d
 
+    def test_GET_FILEURL_named(self):
+        base = "/file/%s" % urllib.quote(self._bar_txt_uri)
+        d = self.GET(base + "/@@name=/blah.txt")
+        d.addCallback(self.failUnlessIsBarDotTxt)
+        d.addCallback(lambda res: self.GET(base + "/blah.txt"))
+        d.addCallback(self.failUnlessIsBarDotTxt)
+        d.addCallback(lambda res: self.GET(base + "/ignore/lots/blah.txt"))
+        d.addCallback(self.failUnlessIsBarDotTxt)
+        return d
+
     def test_GET_FILEURL_save(self):
         d = self.GET(self.public_url + "/foo/bar.txt?save=bar.txt")
         # TODO: look at the headers, expect a Content-Disposition: attachment
index 92ce7ea11a90ddc7e23c35ef35ac5532f9606d39..b1c3c48ae94661c157fa95b621c4e2c5bf384f51 100644 (file)
@@ -252,25 +252,17 @@ class Directory(rend.Page):
                 times.append("m: " + mtime)
         ctx.fillSlots("times", times)
 
-
-        # build the base of the uri_link link url
-        uri_link = "/uri/" + urllib.quote(target.get_uri())
-
         assert (IFileNode.providedBy(target)
                 or IDirectoryNode.providedBy(target)
                 or IMutableFileNode.providedBy(target)), target
 
-        if IMutableFileNode.providedBy(target):
-            # file
-
-            # add the filename to the uri_link url
-            uri_link += '?%s' % (urllib.urlencode({'filename': name}),)
+        quoted_uri = urllib.quote(target.get_uri())
 
+        if IMutableFileNode.providedBy(target):
             # to prevent javascript in displayed .html files from stealing a
             # secret directory URI from the URL, send the browser to a URI-based
             # page that doesn't know about the directory at all
-            #dlurl = urllib.quote(name)
-            dlurl = uri_link
+            dlurl = "/file/%s/@@named=/%s" % (quoted_uri, urllib.quote(name))
 
             ctx.fillSlots("filename",
                           T.a(href=dlurl)[html.escape(name)])
@@ -278,20 +270,11 @@ class Directory(rend.Page):
 
             ctx.fillSlots("size", "?")
 
-            text_plain_link = uri_link + "?filename=foo.txt"
-            text_plain_tag = T.a(href=text_plain_link)["text/plain"]
+            text_plain_url = "/file/%s/@@named=/foo.txt" % quoted_uri
+            text_plain_tag = T.a(href=text_plain_url)["text/plain"]
 
         elif IFileNode.providedBy(target):
-            # file
-
-            # add the filename to the uri_link url
-            uri_link += '?%s' % (urllib.urlencode({'filename': name}),)
-
-            # to prevent javascript in displayed .html files from stealing a
-            # secret directory URI from the URL, send the browser to a URI-based
-            # page that doesn't know about the directory at all
-            #dlurl = urllib.quote(name)
-            dlurl = uri_link
+            dlurl = "/file/%s/@@named=/%s" % (quoted_uri, urllib.quote(name))
 
             ctx.fillSlots("filename",
                           T.a(href=dlurl)[html.escape(name)])
@@ -299,11 +282,13 @@ class Directory(rend.Page):
 
             ctx.fillSlots("size", target.get_size())
 
-            text_plain_link = uri_link + "?filename=foo.txt"
-            text_plain_tag = T.a(href=text_plain_link)["text/plain"]
+            text_plain_url = "/file/%s/@@named=/foo.txt" % quoted_uri
+            text_plain_tag = T.a(href=text_plain_url)["text/plain"]
+
 
         elif IDirectoryNode.providedBy(target):
             # directory
+            uri_link = "/uri/" + urllib.quote(target.get_uri())
             ctx.fillSlots("filename",
                           T.a(href=uri_link)[html.escape(name)])
             if target.is_readonly():
@@ -491,6 +476,7 @@ class WebDownloadTarget:
         pass
 
 class FileDownloader(resource.Resource):
+    isLeaf = True
     def __init__(self, filenode, name):
         assert (IFileNode.providedBy(filenode)
                 or IMutableFileNode.providedBy(filenode))
@@ -1391,7 +1377,18 @@ class Root(rend.Page):
         client = IClient(ctx)
         req = inevow.IRequest(ctx)
 
-        if not segments or segments[0] != "uri":
+        if not segments:
+            return rend.Page.locateChild(self, ctx, segments)
+
+        if segments[0] == "file":
+            if len(segments) < 2:
+                return rend.Page.locateChild(self, ctx, segments)
+            filecap = segments[1]
+            node = client.create_node_from_uri(filecap)
+            name = segments[-1]
+            return FileDownloader(node, name), ()
+
+        if segments[0] != "uri":
             return rend.Page.locateChild(self, ctx, segments)
 
         segments = list(segments)