[Zope] HTTP caching headers in Zope: mini-Howto

Itamar Shtull-Trauring itamars@ibm.net
Sun, 16 Apr 2000 18:43:04 +0300


 Add HTTP caching headers to your pages
========================================

Let's assume your DTML docs always generate the same content (unless you've
changed their DTML code).  No stock quotes, no time of day.  


1. Add the following code to the beginning of your standard_html_header:

	<dtml-unless dynamicpage>
	<dtml-call "httpHandler(this(), REQUEST, RESPONSE)">
	</dtml-unless>

The unless is there so that pages that DO have dynamic content, such as
stock quotes, can skip the http caching by doing <dtml-call
"REQUEST.set('dynamicpage', 1)"> before the standard_html_header.



2. Create the following PythonMethod called httpHandler in the root of your
site:

<params>self, obj, REQUEST, RESPONSE</params>

def rfc1123_date(dt):
	return dt.toZone('GMT').rfc822()

# this is our initial assumption - the last time that the DTML was modified
# is the last time its output was modified:
mod_time = obj.bobobase_modification_time()

header=REQUEST.get_header('If-Modified-Since', None)
if header is not None:
    header=_.string.split(header, ';')[0]
    mod_since=int(_.DateTime(header).timeTime())

    if int(mod_time) <= mod_since:
        RESPONSE.setStatus(304)
        RESPONSE.write('')
        raise "NotModified", ''

# if we havn't sent a 304, send the content with appropiate headers
RESPONSE.setHeader('Last-Modified', rfc1123_date(mod_time))
# this page should expire in 15 minutes
RESPONSE.setHeader('Expires',
rfc1123_date(_.DateTime(_.DateTime().timeTime() + 900)))