changeset 1469:79d8956de3f5

implemented last-modified and if-modified-since support
author Richard Jones <richard@users.sourceforge.net>
date Wed, 26 Feb 2003 04:51:41 +0000
parents f57759a5ee1a
children 9ccd69fbe33e
files roundup/cgi/client.py roundup/scripts/roundup_server.py
diffstat 2 files changed, 29 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/cgi/client.py	Wed Feb 26 04:08:04 2003 +0000
+++ b/roundup/cgi/client.py	Wed Feb 26 04:51:41 2003 +0000
@@ -1,4 +1,4 @@
-# $Id: client.py,v 1.98 2003-02-26 04:08:04 richard Exp $
+# $Id: client.py,v 1.99 2003-02-26 04:51:41 richard Exp $
 
 __doc__ = """
 WWW request handler (also used in the stand-alone server).
@@ -6,6 +6,7 @@
 
 import os, os.path, cgi, StringIO, urlparse, re, traceback, mimetypes, urllib
 import binascii, Cookie, time, random, MimeWriter, smtplib, socket, quopri
+import stat, rfc822
 
 from roundup import roundupdb, date, hyperdb, password
 from roundup.i18n import _
@@ -22,6 +23,8 @@
        pass
 class  Redirect(HTTPException):
        pass
+class  NotModified(HTTPException):
+       pass
 
 # XXX actually _use_ FormError
 class FormError(ValueError):
@@ -235,7 +238,12 @@
         except SendFile, designator:
             self.serve_file(designator)
         except SendStaticFile, file:
-            self.serve_static_file(str(file))
+            try:
+                self.serve_static_file(str(file))
+            except NotModified:
+                # send the 304 response
+                self.request.send_response(304)
+                self.request.end_headers()
         except Unauthorised, message:
             self.classname = None
             self.template = ''
@@ -422,11 +430,26 @@
         self.write(file.get(nodeid, 'content'))
 
     def serve_static_file(self, file):
+        # see if there's an if-modified-since...
+        ims = self.request.headers.getheader('if-modified-since')
+        # cgi will put the header in the env var
+        if not ims and self.env.has_key('HTTP_IF_MODIFIED_SINCE'):
+            ims = self.env['HTTP_IF_MODIFIED_SINCE']
+        filename = os.path.join(self.instance.config.TEMPLATES, file)
+        lmt = os.stat(filename)[stat.ST_MTIME]
+        if ims:
+            ims = rfc822.parsedate(ims)[:6]
+            lmtt = time.gmtime(lmt)[:6]
+            if lmtt <= ims:
+                raise NotModified
+
         # we just want to serve up the file named
         mt = mimetypes.guess_type(str(file))[0]
+        if not mt:
+            mt = 'text/plain'
         self.additional_headers['Content-Type'] = mt
-        self.write(open(os.path.join(self.instance.config.TEMPLATES,
-            file)).read())
+        self.additional_headers['Last-Modifed'] = rfc822.formatdate(lmt)
+        self.write(open(filename).read())
 
     def renderContext(self):
         ''' Return a PageTemplate for the named page
--- a/roundup/scripts/roundup_server.py	Wed Feb 26 04:08:04 2003 +0000
+++ b/roundup/scripts/roundup_server.py	Wed Feb 26 04:51:41 2003 +0000
@@ -16,7 +16,7 @@
 # 
 """ HTTP Server that serves roundup.
 
-$Id: roundup_server.py,v 1.18 2003-02-06 05:43:49 richard Exp $
+$Id: roundup_server.py,v 1.19 2003-02-26 04:51:41 richard Exp $
 """
 
 # python version check
@@ -86,9 +86,9 @@
                 self.wfile.write(cgitb.breaker())
                 self.wfile.write(cgitb.html())
             except:
-                self.wfile.write("<pre>")
                 s = StringIO.StringIO()
                 traceback.print_exc(None, s)
+                self.wfile.write("<pre>")
                 self.wfile.write(cgi.escape(s.getvalue()))
                 self.wfile.write("</pre>\n")
         sys.stdin = save_stdin

Roundup Issue Tracker: http://roundup-tracker.org/