From: Mischa POSLAWSKY Date: Mon, 12 Apr 2010 17:45:00 +0000 (+0000) Subject: export and check last-modified header X-Git-Tag: v1.3~17 X-Git-Url: http://git.shiar.nl/sheet.git/commitdiff_plain/4b28462f6dccd2437a3313b02a2ec678a4bf674b?ds=sidebyside export and check last-modified header Avoid resending unaltered pages. --- diff --git a/common.inc.plp b/common.inc.plp index b3a9a70..d51e3e6 100644 --- a/common.inc.plp +++ b/common.inc.plp @@ -6,6 +6,8 @@ no warnings 'qw'; # you know what you doing no warnings 'uninitialized'; # save some useless checks for more legible code use open IO => ':utf8'; +use File::stat 'stat'; +use HTTP::Date; use Shiar_Sheet::KeySigns qw(%sign); # dependant on $get{ascii} our $style; @@ -36,9 +38,33 @@ sub stylesheet { ) } @_; } +sub checkmodified { + my $lastmod; + for (@_) { + my $mod = stat $_ or next; + $mod = $mod->mtime or next; + $lastmod = $mod if $mod gt $lastmod; + } + + for ($ENV{HTTP_IF_MODIFIED_SINCE} || ()) { + next if str2time($_) < $lastmod; + $header{status} = '304 Same old'; + exit; + } + + $header{'Last-Modified'} = time2str($lastmod); +} + sub Html { my ($meta) = @_; + # announce and check data modification + checkmodified( + $ENV{SCRIPT_FILENAME}, + (grep { /^Shiar_/ } values %INC), + $meta->{data} ? @{ $meta->{data} } : (), + ); + # default fallbacks $meta->{stylesheet} ||= [qw'light dark circus mono red terse']; $meta->{charset} ||= 'utf-8';