[Zope] Browser Sticky Fingers

Tony McDonald tony.mcdonald@ncl.ac.uk
Tue, 19 Oct 1999 17:24:18 +0100


At 5:45 pm +0200 19/10/99, Peter Sabaini wrote:
>possibly the src= of the <img> points to a cgi program that returns "Expires:
>(something soon)" headers so it wont get cached.
>
>in perl pseudocode (sorry i'm not that accustomed to python yet) you'd write
>something like this:
>
># this from thimble smith, tim@desert.net
># read image data
>$img = <wherever you get the image data from, gif assumed>
># print expires with date=now
>print "Expires: ", scalar(localtime), "\r\n";
># or whatever content, then insert gif data
>print "Content-type: image/gif\r\n\n$img"

Neat!
It looks like the place to change this is in the index_html method of 
OFS/Image.py

You *could* add an Expires header to the group of

         RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
         RESPONSE.setHeader('Content-Type', self.content_type)

lines near the end of this method, but I think that will mean that 
*all* images from a Zope system will be expired. A better solution 
would be if you could send an Expires header to the Image object.

One thing I do is have an external method called 'img' at the root of 
my Zope structure and call images so <img src="img?src=tone.gif">. It 
consists of this;

def img(self, src, RESPONSE):
         import os
         try:
                 img_dir = self.images_dir
         except AttributeError, NameError:
                 img_dir = '/home/nmedfac/guide_images'

         typemap={'.gif' : 'image/gif', '.jpg' : 'image/jpeg'}

         img_name=os.path.join(img_dir, src)

         img_ext = img_name[-4:]
         img_type = typemap[img_ext]    

         # Open file, using 'rb' to open it in binary mode!
         img_file=open(img_name, 'rb')
         img_data=img_file.read()
         img_file.close()

         # Set the content-type of the response
         RESPONSE.setHeader('content-type', img_type)
         return img_data

and there's nothing to stop you adding the Expires header before the 
Content-type header. Note that this means your images must live in a 
real live directory and *not* in the ZODB. Note also that I've got a 
property called 'images_dir' that is the directory where the images 
live. That way the same code works for however many subsites you've 
got.

hth
tone.
------
Dr Tony McDonald,  FMCC, Networked Learning Environments Project 
http://nle.ncl.ac.uk/
The Medical School, Newcastle University Tel: +44 191 222 5888
Fingerprint: 3450 876D FA41 B926 D3DD  F8C3 F2D0 C3B9 8B38 18A2