changeset 32:0edb831061f5

test not modified response
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 05 Mar 2012 14:06:00 -0800
parents f00fcf6d9f1d
children 86b519dd8467
files fileserver/web.py
diffstat 1 files changed, 24 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/fileserver/web.py	Mon Mar 05 13:49:07 2012 -0800
+++ b/fileserver/web.py	Mon Mar 05 14:06:00 2012 -0800
@@ -19,24 +19,42 @@
 ### classes for iterating over files
 
 class FileIterable(object):
-    def __init__(self, filename):
+    def __init__(self, filename, start=None, stop=None):
         self.filename = filename
+        self.start = start
+        self.stop = stop
     def __iter__(self):
-        return FileIterator(self.filename)
+        return FileIterator(self.filename, self.start, self.stop)
 class FileIterator(object):
-    def __init__(self, filename, chunk_size=4096):
+    def __init__(self, filename, start, stop, chunk_size=4096):
         self.filename = filename
         self.chunk_size = chunk_size
         self.fileobj = open(self.filename, 'rb')
+        if start:
+            self.fileobj.seek(start)
+        if stop is not None:
+            self.length = stop - start
+        else:
+            self.length = None
     def __iter__(self):
         return self
     def next(self):
+        if self.length is not None and self.length <= 0:
+             raise StopIteration
         chunk = self.fileobj.read(self.chunk_size)
         if not chunk:
             raise StopIteration
+        if self.length is not None:
+            self.length -= len(chunk)
+            if self.length < 0:
+                # Chop off the extra:
+                chunk = chunk[:self.length]
         return chunk
     __next__ = next # py3 compat
 
+
+### attributes for serving static files
+
 def get_mimetype(filename):
     type, encoding = mimetypes.guess_type(filename)
      # We'll ignore encoding, even though we shouldn't really
@@ -65,6 +83,9 @@
         res = file_response(self.filename)
         return res(environ, start_response)
 
+
+### class for serving directory indices
+
 class DirectoryServer(object):
 
     def __init__(self, directory, sort=True):