Skip to content

Commit 21cb657

Browse files
author
Richard Jones
committed
last-modified and if-modified-since header support for static files...
...(ie. style.css etc.) backported from HEAD
1 parent 71328dd commit 21cb657

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ are given with the most recent entry first.
44
2003-07-?? 0.5.9
55
- backported XSS message cleaning fix (sf bug 757128)
66
- only clean sessions once per hour (backport from trunk)
7+
- backported last-modified and if-modified-since header support for static
8+
files (ie. style.css etc.) from HEAD
79

810

911
2003-06-19 0.5.8

roundup/cgi/client.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# $Id: client.py,v 1.65.2.12 2003-06-24 05:02:45 richard Exp $
1+
# $Id: client.py,v 1.65.2.13 2003-06-24 05:19:10 richard Exp $
22

33
__doc__ = """
44
WWW request handler (also used in the stand-alone server).
55
"""
66

77
import os, os.path, cgi, StringIO, urlparse, re, traceback, mimetypes, urllib
8-
import binascii, Cookie, time, random
8+
import binascii, Cookie, time, random, rfc822, stat
99

1010
from roundup import roundupdb, date, hyperdb, password
1111
from roundup.i18n import _
@@ -24,6 +24,9 @@ class NotFound(ValueError):
2424
class Redirect(Exception):
2525
pass
2626

27+
class NotModified(Exception):
28+
pass
29+
2730
class SendFile(Exception):
2831
' Sent a file from the database '
2932

@@ -201,7 +204,12 @@ def inner_main(self):
201204
except SendFile, designator:
202205
self.serve_file(designator)
203206
except SendStaticFile, file:
204-
self.serve_static_file(str(file))
207+
try:
208+
self.serve_static_file(str(file))
209+
except NotModified:
210+
# send the 304 response
211+
self.request.send_response(304)
212+
self.request.end_headers()
205213
except Unauthorised, message:
206214
self.classname=None
207215
self.template=''
@@ -217,6 +225,9 @@ def inner_main(self):
217225
def determine_user(self):
218226
''' Determine who the user is
219227
'''
228+
# make sure the db is open
229+
self.opendb('admin')
230+
220231
# clean age sessions
221232
self.clean_sessions()
222233

@@ -384,11 +395,32 @@ def serve_file(self, designator, dre=re.compile(r'([^\d]+)(\d+)')):
384395
self.write(file.get(nodeid, 'content'))
385396

386397
def serve_static_file(self, file):
398+
ims = None
399+
# see if there's an if-modified-since...
400+
if hasattr(self.request, 'headers'):
401+
ims = self.request.headers.getheader('if-modified-since')
402+
elif self.env.has_key('HTTP_IF_MODIFIED_SINCE'):
403+
# cgi will put the header in the env var
404+
ims = self.env['HTTP_IF_MODIFIED_SINCE']
405+
filename = os.path.join(self.instance.config.TEMPLATES, file)
406+
lmt = os.stat(filename)[stat.ST_MTIME]
407+
if ims:
408+
ims = rfc822.parsedate(ims)[:6]
409+
lmtt = time.gmtime(lmt)[:6]
410+
if lmtt <= ims:
411+
raise NotModified
412+
387413
# we just want to serve up the file named
388-
mt = mimetypes.guess_type(str(file))[0]
414+
file = str(file)
415+
mt = mimetypes.guess_type(file)[0]
416+
if not mt:
417+
if file.endswith('.css'):
418+
mt = 'text/css'
419+
else:
420+
mt = 'text/plain'
389421
self.additional_headers['Content-Type'] = mt
390-
self.write(open(os.path.join(self.instance.config.TEMPLATES,
391-
file)).read())
422+
self.additional_headers['Last-Modifed'] = rfc822.formatdate(lmt)
423+
self.write(open(filename, 'rb').read())
392424

393425
def renderContext(self):
394426
''' Return a PageTemplate for the named page

0 commit comments

Comments
 (0)