From 933ffbd886b31d1af5dc0cf22bd4c58ec7343d95 Mon Sep 17 00:00:00 2001 From: richard Date: Wed, 26 Feb 2003 04:51:41 +0000 Subject: [PATCH] implemented last-modified and if-modified-since support git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1546 57a73879-2fb5-44c3-a270-3262357dd7e2 --- roundup/cgi/client.py | 31 +++++++++++++++++++++++++++---- roundup/scripts/roundup_server.py | 4 ++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/roundup/cgi/client.py b/roundup/cgi/client.py index 10848f0..174e4e8 100644 --- a/roundup/cgi/client.py +++ b/roundup/cgi/client.py @@ -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 diff --git a/roundup/scripts/roundup_server.py b/roundup/scripts/roundup_server.py index afe9d5e..8c85048 100644 --- a/roundup/scripts/roundup_server.py +++ b/roundup/scripts/roundup_server.py @@ -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("
")
                 s = StringIO.StringIO()
                 traceback.print_exc(None, s)
+                self.wfile.write("
")
                 self.wfile.write(cgi.escape(s.getvalue()))
                 self.wfile.write("
\n") sys.stdin = save_stdin -- 2.39.5