]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
webish: provide a valid Content-Length header on downloads
authorBrian Warner <warner@allmydata.com>
Tue, 3 Jul 2007 22:09:00 +0000 (15:09 -0700)
committerBrian Warner <warner@allmydata.com>
Tue, 3 Jul 2007 22:09:00 +0000 (15:09 -0700)
src/allmydata/download.py
src/allmydata/interfaces.py
src/allmydata/webish.py

index 8b963fbb655568482a5310f181ead69e30850a57..660d6e35424080fb632c5c2fc34ff1b62313a872 100644 (file)
@@ -25,13 +25,14 @@ class BadCrypttextHashValue(Exception):
     pass
 
 class Output:
-    def __init__(self, downloadable, key):
+    def __init__(self, downloadable, key, total_length):
         self.downloadable = downloadable
         self._decryptor = AES.new(key=key, mode=AES.MODE_CTR,
                                   counterstart="\x00"*16)
         self._crypttext_hasher = hashutil.crypttext_hasher()
         self._plaintext_hasher = hashutil.plaintext_hasher()
         self.length = 0
+        self.total_length = total_length
         self._segment_number = 0
         self._plaintext_hash_tree = None
         self._crypttext_hash_tree = None
@@ -71,7 +72,7 @@ class Output:
         # any memory usage beyond this.
         if not self._opened:
             self._opened = True
-            self.downloadable.open()
+            self.downloadable.open(self.total_length)
         self.downloadable.write(plaintext)
 
     def fail(self, why):
@@ -270,7 +271,7 @@ class FileDownloader:
         self._size = d['size']
         self._num_needed_shares = d['needed_shares']
 
-        self._output = Output(downloadable, d['key'])
+        self._output = Output(downloadable, d['key'], self._size)
 
         self.active_buckets = {} # k: shnum, v: bucket
         self._share_buckets = [] # list of (sharenum, bucket) tuples
@@ -585,7 +586,7 @@ class FileName:
     implements(IDownloadTarget)
     def __init__(self, filename):
         self._filename = filename
-    def open(self):
+    def open(self, size):
         self.f = open(self._filename, "wb")
         return self.f
     def write(self, data):
@@ -604,7 +605,7 @@ class Data:
     implements(IDownloadTarget)
     def __init__(self):
         self._data = []
-    def open(self):
+    def open(self, size):
         pass
     def write(self, data):
         self._data.append(data)
@@ -627,7 +628,7 @@ class FileHandle:
     implements(IDownloadTarget)
     def __init__(self, filehandle):
         self._filehandle = filehandle
-    def open(self):
+    def open(self, size):
         pass
     def write(self, data):
         self._filehandle.write(data)
index 6582f99463f60f25fe88684c8ca4185015b350c4..3ec401ec8448c95eddde9d6a90489c791fff51c9 100644 (file)
@@ -604,10 +604,13 @@ class IDecoder(Interface):
         """
 
 class IDownloadTarget(Interface):
-    def open():
+    def open(size):
         """Called before any calls to write() or close(). If an error
         occurs before any data is available, fail() may be called without
-        a previous call to open()."""
+        a previous call to open().
+
+        'size' is the length of the file being downloaded, in bytes."""
+
     def write(data):
         """Output some data to the target."""
     def close():
index 34d1942e4b0067fa87fb07c4dbbb93cdc591cd54..aaaaf3aa2d53de06aa8b947e3cfbcdab0897081f 100644 (file)
@@ -328,12 +328,12 @@ class WebDownloadTarget:
         self._content_encoding = content_encoding
         self._opened = False
 
-    def open(self):
+    def open(self, size):
         self._opened = True
         self._req.setHeader("content-type", self._content_type)
         if self._content_encoding:
-            self._req.setHeader('content-encoding', self._content_encoding)
-        # TODO: it would be nice to set the size header here
+            self._req.setHeader("content-encoding", self._content_encoding)
+        self._req.setHeader("content-length", str(size))
 
     def write(self, data):
         self._req.write(data)