| 1 | package Apache::HeavyCGI::IfModified; | ||
| 2 | use strict; | ||
| 3 | use base 'Class::Singleton'; | ||
| 4 | |||
| 5 | use vars qw($VERSION); | ||
| 6 | $VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; | ||
| 7 | |||
| 8 | use HTTP::Date (); | ||
| 9 | |||
| 10 | sub header { | ||
| 11 | my Apache::HeavyCGI::IfModified $self = shift; | ||
| 12 | my Apache::HeavyCGI $mgr = shift; | ||
| 13 | |||
| 14 | my $now = $mgr->time; | ||
| 15 | my $r = $mgr->{R}; | ||
| 16 | |||
| 17 | my $last_modified = $mgr->last_modified; | ||
| 18 | $r->header_out('Date', HTTP::Date::time2str($now)); | ||
| 19 | |||
| 20 | if (my $ifmodisi = $r->header_in('If-Modified-Since')) { | ||
| 21 | # warn "Got ifmodisi[$ifmodisi]"; | ||
| 22 | $ifmodisi =~ s/\;.*//; | ||
| 23 | my $ret; | ||
| 24 | if ($last_modified->http eq $ifmodisi) { | ||
| 25 | $ret = 304; | ||
| 26 | } else { | ||
| 27 | my $ifmodisi_unix = HTTP::Date::str2time($ifmodisi); | ||
| 28 | if (defined $ifmodisi_unix | ||
| 29 | && | ||
| 30 | $ifmodisi_unix < $now | ||
| 31 | && | ||
| 32 | $ifmodisi_unix >= $last_modified->unix | ||
| 33 | ) { | ||
| 34 | $ret = 304; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | return $mgr->{DONE} = $ret if $ret; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | =head1 NAME | ||
| 44 | |||
| 45 | Apache::HeavyCGI::IfModified - Within Apache::HeavyCGI return 304 | ||
| 46 | |||
| 47 | =head1 SYNOPSIS | ||
| 48 | |||
| 49 | require Apache::HeavyCGI::IfModified; | ||
| 50 | push @{$mgr->{HANDLER}}, | ||
| 51 | "Apache::HeavyCGI::IfModified"; # $mgr is an Apache::HeavyCGI object | ||
| 52 | |||
| 53 | =head1 DESCRIPTION | ||
| 54 | |||
| 55 | If-modified-since is tricky. We have pages with very differing | ||
| 56 | last modification. Some are modified NOW, some are old, most are | ||
| 57 | MADE now but would have been just the same many hours ago. | ||
| 58 | |||
| 59 | Because it's the recipe that is used for the composition of a page, it | ||
| 60 | may well be that a page that has never been generated before, | ||
| 61 | nonetheless has a Last-Modified date in the past. The Last-Modified | ||
| 62 | header acts as a weak validator for cache activities, and the older a | ||
| 63 | document appears to be, the longer the cache will store it for us by | ||
| 64 | default. When the cache revisits us after it has got a valid | ||
| 65 | Last-Modified header, it will use an If-Modified-Since header and if | ||
| 66 | we carefully determine our own Last-Modified time, we can spare a lot | ||
| 67 | of processing by returning a Not Modified response instead of working. | ||
| 68 | |||
| 69 | IfModified should be one of the last handlers in any Apache::HeavyCGI | ||
| 70 | environment, at least it must be processed after all the handlers that | ||
| 71 | might set the LAST_MODIFIED date. | ||
| 72 | |||
| 73 | =cut | ||
| 74 |