Baz has asked for the wisdom of the Perl Monks concerning the following question:
The first time I run this from my browser, the image displays fine, but if I hit reload (on IE6 or NS) I get Internal Server Error. If I hit back then forward, the image reappers - its loaded from the cache as there is no download pause.#!/usr/bin/perl $lifetime = 2*60 ; # image expiry time # load the HTTP date routines $libloc = "/usr/local/lib/libwww-perl-0.40" ; unshift(@INC, $libloc); require "timelocal.pl" ; require "wwwdates.pl" ; $path = $ENV{'PATH_INFO'} ; $query = $ENV{'QUERY_STRING'}; $pragma = $ENV{'HTTP_PRAGMA'} ; $pragma =~ tr/A-Z/a-z/ ; $imsd = $ENV{'HTTP_IF_MODIFIED_SINCE'} ; $ims = $imsd ; $ims =~ s/;.*// ; # for Netscape $ims = &wwwdates'get_gmtime($ims) ; $file = "./shut.jpg" ; # find the modification time of the cached image and work out # its age #if ($ims && $modtime<=$ims) { # print "Content-type: text/plain\n\nError opening file\n"; # print "Status: 304 Not Modified\n\n" ; # exit ; #} # set expiry time. Agents will automatically load a new image # if their cached version is older than this. $expires = time + $lifetime ; # Convert expiry and last-modified times to RFC1123 time $gexpires = &wwwdates'wtime($expires,'GMT'); $glastmod = &wwwdates'wtime($modtime ,'GMT'); open (JPG,$file) ; $jlen = read (JPG,$doc,50000) ; if (!$jlen) { print "Content-type: text/plain\n\nError opening file\n"; exit ; } # Generate the complete HTTP response for a successful GET print "Content-type: image/jpeg\n"; print "Last-Modified: $glastmod\n"; print "Expires: $gexpires\n"; print "Content-Length: $jlen\n"; print"\n"; print "$doc";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Trouble Caching an Image
by merlyn (Sage) on Sep 04, 2002 at 23:06 UTC | |
|
Re: Trouble Caching an Image
by dws (Chancellor) on Sep 04, 2002 at 21:42 UTC | |
by Baz (Friar) on Sep 04, 2002 at 22:05 UTC | |
|
Re: Trouble Caching an Image
by perrin (Chancellor) on Sep 04, 2002 at 21:07 UTC | |
by Baz (Friar) on Sep 04, 2002 at 21:20 UTC | |
by perrin (Chancellor) on Sep 04, 2002 at 21:39 UTC |