From: Brian Warner Date: Tue, 3 Jul 2007 22:09:00 +0000 (-0700) Subject: webish: provide a valid Content-Length header on downloads X-Git-Url: https://git.rkrishnan.org/pf/content/en/service/FOOURL?a=commitdiff_plain;h=c4a8db3eb25de932d4d70fb5288246fbe3b4d636;p=tahoe-lafs%2Ftahoe-lafs.git webish: provide a valid Content-Length header on downloads --- diff --git a/src/allmydata/download.py b/src/allmydata/download.py index 8b963fbb..660d6e35 100644 --- a/src/allmydata/download.py +++ b/src/allmydata/download.py @@ -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) diff --git a/src/allmydata/interfaces.py b/src/allmydata/interfaces.py index 6582f994..3ec401ec 100644 --- a/src/allmydata/interfaces.py +++ b/src/allmydata/interfaces.py @@ -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(): diff --git a/src/allmydata/webish.py b/src/allmydata/webish.py index 34d1942e..aaaaf3aa 100644 --- a/src/allmydata/webish.py +++ b/src/allmydata/webish.py @@ -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)