Code

implemented last-modified and if-modified-since support
authorrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Wed, 26 Feb 2003 04:51:41 +0000 (04:51 +0000)
committerrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Wed, 26 Feb 2003 04:51:41 +0000 (04:51 +0000)
git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1546 57a73879-2fb5-44c3-a270-3262357dd7e2

roundup/cgi/client.py
roundup/scripts/roundup_server.py

index 10848f0808401e582e5d5814490bff6e235922d7..174e4e8a95d9a6298111ca3abc5753c0145a78c3 100644 (file)
@@ -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 @@ WWW request handler (also used in the stand-alone server).
 
 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 @@ class  NotFound(HTTPException):
        pass
 class  Redirect(HTTPException):
        pass
+class  NotModified(HTTPException):
+       pass
 
 # XXX actually _use_ FormError
 class FormError(ValueError):
@@ -235,7 +238,12 @@ class Client:
         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 @@ class Client:
         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
index afe9d5edead6b4c96705337df4dee7a33af33a70..8c850488522c5ebf862bb7c373f9735d1fc45ed8 100644 (file)
@@ -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 @@ class RoundupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
                 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