--- util.py.orig 2003-05-28 12:07:35.000000000 -0500 +++ util.py.expires 2003-05-28 12:37:29.000000000 -0500 @@ -21,6 +21,8 @@ import os, mimetypes, urllib from quixote import errors, html from cStringIO import StringIO +from rfc822 import formatdate, parsedate +import time def xmlrpc (request, func): """xmlrpc(request:Request, func:callable) : string @@ -66,7 +68,7 @@ """ def __init__(self, path, follow_symlinks=0, - mime_type=None, encoding=None): + mime_type=None, encoding=None, cache_time=3600): """StaticFile(path:string, follow_symlinks:bool) Initialize instance with the absolute path to the file. If @@ -90,8 +92,19 @@ strict=0) self.mime_type = mime_type or guess_mime or 'text/plain' self.encoding = encoding or guess_enc or None + self.cache_time = cache_time def __call__(self, request): + last_mod = os.stat(self.path).st_mtime + # check for an If-Modified-Since header, and try to honour it. + ims = request.get_header('if-modified-since') + if ims: + ims_value = parsedate(ims) + last_mod_tuple = time.gmtime(last_mod) + if last_mod_tuple[:6] <= ims_value[:6]: + request.response.set_status(304, 'Not Modified') + return '' + # Set the Content-Type for the response and return the file's contents. request.response.set_content_type(self.mime_type) if self.encoding: @@ -99,6 +112,16 @@ fsfile = open(self.path, 'rb') contents = fsfile.read() fsfile.close() + + # set the last-modified header (in GMT) + last_modified = formatdate(last_mod) + request.response.set_header('Last-Modified', last_modified) + + # set the cache value (so the response object can set + # the "Expires:" header correctly) + if self.cache_time != None: + request.response.cache = self.cache_time + return contents @@ -110,7 +133,7 @@ _q_exports = [] - def __init__(self, path, use_cache=0, list_directory=0, follow_symlinks=0): + def __init__(self, path, use_cache=0, list_directory=0, follow_symlinks=0, cache_time=3600): """StaticDirectory(path:string, use_cache:bool, list_directory:bool, follow_symlinks:bool) @@ -129,6 +152,7 @@ self.cache = {} self.list_directory = list_directory self.follow_symlinks = follow_symlinks + self.cache_time = cache_time def _q_index(self, request): """ @@ -182,7 +206,7 @@ item = StaticDirectory(item_filepath, self.use_cache, self.list_directory, self.follow_symlinks) elif os.path.isfile(item_filepath): - item = StaticFile(item_filepath, self.follow_symlinks) + item = StaticFile(item_filepath, self.follow_symlinks, cache_time=self.cache_time) else: raise errors.TraversalError if self.use_cache: