comparison roundup/cgi/client.py @ 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 77942e0a12fe
comparison
equal deleted inserted replaced
1468:f57759a5ee1a 1469:79d8956de3f5
1 # $Id: client.py,v 1.98 2003-02-26 04:08:04 richard Exp $ 1 # $Id: client.py,v 1.99 2003-02-26 04:51:41 richard Exp $
2 2
3 __doc__ = """ 3 __doc__ = """
4 WWW request handler (also used in the stand-alone server). 4 WWW request handler (also used in the stand-alone server).
5 """ 5 """
6 6
7 import os, os.path, cgi, StringIO, urlparse, re, traceback, mimetypes, urllib 7 import os, os.path, cgi, StringIO, urlparse, re, traceback, mimetypes, urllib
8 import binascii, Cookie, time, random, MimeWriter, smtplib, socket, quopri 8 import binascii, Cookie, time, random, MimeWriter, smtplib, socket, quopri
9 import stat, rfc822
9 10
10 from roundup import roundupdb, date, hyperdb, password 11 from roundup import roundupdb, date, hyperdb, password
11 from roundup.i18n import _ 12 from roundup.i18n import _
12 from roundup.cgi.templating import Templates, HTMLRequest, NoTemplate 13 from roundup.cgi.templating import Templates, HTMLRequest, NoTemplate
13 from roundup.cgi import cgitb 14 from roundup.cgi import cgitb
19 class Unauthorised(HTTPException): 20 class Unauthorised(HTTPException):
20 pass 21 pass
21 class NotFound(HTTPException): 22 class NotFound(HTTPException):
22 pass 23 pass
23 class Redirect(HTTPException): 24 class Redirect(HTTPException):
25 pass
26 class NotModified(HTTPException):
24 pass 27 pass
25 28
26 # XXX actually _use_ FormError 29 # XXX actually _use_ FormError
27 class FormError(ValueError): 30 class FormError(ValueError):
28 ''' An "expected" exception occurred during form parsing. 31 ''' An "expected" exception occurred during form parsing.
233 self.response_code = 302 236 self.response_code = 302
234 self.write('Redirecting to <a href="%s">%s</a>'%(url, url)) 237 self.write('Redirecting to <a href="%s">%s</a>'%(url, url))
235 except SendFile, designator: 238 except SendFile, designator:
236 self.serve_file(designator) 239 self.serve_file(designator)
237 except SendStaticFile, file: 240 except SendStaticFile, file:
238 self.serve_static_file(str(file)) 241 try:
242 self.serve_static_file(str(file))
243 except NotModified:
244 # send the 304 response
245 self.request.send_response(304)
246 self.request.end_headers()
239 except Unauthorised, message: 247 except Unauthorised, message:
240 self.classname = None 248 self.classname = None
241 self.template = '' 249 self.template = ''
242 self.error_message.append(message) 250 self.error_message.append(message)
243 self.write(self.renderContext()) 251 self.write(self.renderContext())
420 file = self.db.file 428 file = self.db.file
421 self.additional_headers['Content-Type'] = file.get(nodeid, 'type') 429 self.additional_headers['Content-Type'] = file.get(nodeid, 'type')
422 self.write(file.get(nodeid, 'content')) 430 self.write(file.get(nodeid, 'content'))
423 431
424 def serve_static_file(self, file): 432 def serve_static_file(self, file):
433 # see if there's an if-modified-since...
434 ims = self.request.headers.getheader('if-modified-since')
435 # cgi will put the header in the env var
436 if not ims and self.env.has_key('HTTP_IF_MODIFIED_SINCE'):
437 ims = self.env['HTTP_IF_MODIFIED_SINCE']
438 filename = os.path.join(self.instance.config.TEMPLATES, file)
439 lmt = os.stat(filename)[stat.ST_MTIME]
440 if ims:
441 ims = rfc822.parsedate(ims)[:6]
442 lmtt = time.gmtime(lmt)[:6]
443 if lmtt <= ims:
444 raise NotModified
445
425 # we just want to serve up the file named 446 # we just want to serve up the file named
426 mt = mimetypes.guess_type(str(file))[0] 447 mt = mimetypes.guess_type(str(file))[0]
448 if not mt:
449 mt = 'text/plain'
427 self.additional_headers['Content-Type'] = mt 450 self.additional_headers['Content-Type'] = mt
428 self.write(open(os.path.join(self.instance.config.TEMPLATES, 451 self.additional_headers['Last-Modifed'] = rfc822.formatdate(lmt)
429 file)).read()) 452 self.write(open(filename).read())
430 453
431 def renderContext(self): 454 def renderContext(self):
432 ''' Return a PageTemplate for the named page 455 ''' Return a PageTemplate for the named page
433 ''' 456 '''
434 name = self.classname 457 name = self.classname

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