comparison fileserver/web.py @ 32:0edb831061f5

test not modified response
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 05 Mar 2012 14:06:00 -0800
parents 395c6744bcd9
children
comparison
equal deleted inserted replaced
31:f00fcf6d9f1d 32:0edb831061f5
17 __all__ = ['get_mimetype', 'file_response', 'FileApp', 'DirectoryServer', 'main'] 17 __all__ = ['get_mimetype', 'file_response', 'FileApp', 'DirectoryServer', 'main']
18 18
19 ### classes for iterating over files 19 ### classes for iterating over files
20 20
21 class FileIterable(object): 21 class FileIterable(object):
22 def __init__(self, filename): 22 def __init__(self, filename, start=None, stop=None):
23 self.filename = filename 23 self.filename = filename
24 self.start = start
25 self.stop = stop
24 def __iter__(self): 26 def __iter__(self):
25 return FileIterator(self.filename) 27 return FileIterator(self.filename, self.start, self.stop)
26 class FileIterator(object): 28 class FileIterator(object):
27 def __init__(self, filename, chunk_size=4096): 29 def __init__(self, filename, start, stop, chunk_size=4096):
28 self.filename = filename 30 self.filename = filename
29 self.chunk_size = chunk_size 31 self.chunk_size = chunk_size
30 self.fileobj = open(self.filename, 'rb') 32 self.fileobj = open(self.filename, 'rb')
33 if start:
34 self.fileobj.seek(start)
35 if stop is not None:
36 self.length = stop - start
37 else:
38 self.length = None
31 def __iter__(self): 39 def __iter__(self):
32 return self 40 return self
33 def next(self): 41 def next(self):
42 if self.length is not None and self.length <= 0:
43 raise StopIteration
34 chunk = self.fileobj.read(self.chunk_size) 44 chunk = self.fileobj.read(self.chunk_size)
35 if not chunk: 45 if not chunk:
36 raise StopIteration 46 raise StopIteration
47 if self.length is not None:
48 self.length -= len(chunk)
49 if self.length < 0:
50 # Chop off the extra:
51 chunk = chunk[:self.length]
37 return chunk 52 return chunk
38 __next__ = next # py3 compat 53 __next__ = next # py3 compat
54
55
56 ### attributes for serving static files
39 57
40 def get_mimetype(filename): 58 def get_mimetype(filename):
41 type, encoding = mimetypes.guess_type(filename) 59 type, encoding = mimetypes.guess_type(filename)
42 # We'll ignore encoding, even though we shouldn't really 60 # We'll ignore encoding, even though we shouldn't really
43 return type or 'application/octet-stream' 61 return type or 'application/octet-stream'
62 self.filename = filename 80 self.filename = filename
63 81
64 def __call__(self, environ, start_response): 82 def __call__(self, environ, start_response):
65 res = file_response(self.filename) 83 res = file_response(self.filename)
66 return res(environ, start_response) 84 return res(environ, start_response)
85
86
87 ### class for serving directory indices
67 88
68 class DirectoryServer(object): 89 class DirectoryServer(object):
69 90
70 def __init__(self, directory, sort=True): 91 def __init__(self, directory, sort=True):
71 assert os.path.exists(directory), "'%s' does not exist" % directory 92 assert os.path.exists(directory), "'%s' does not exist" % directory