X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=www%2Fanalysis.pl;h=46795a98632ef39b0b072e4b864233aaea1a5b8c;hp=b7e99b6639d951401654b58f282acaf8d4d292f3;hb=a42a7b25408acf92a72a4c04f4b577e5c8b38195;hpb=799c4b5cbf8c8aac97c3dbb8c36a1f06aa5e1220 diff --git a/www/analysis.pl b/www/analysis.pl index b7e99b6..46795a9 100755 --- a/www/analysis.pl +++ b/www/analysis.pl @@ -1,13 +1,12 @@ #! /usr/bin/perl use CGI; use POSIX; -use Date::Manip; use Linux::Inotify2; use AnyEvent; use strict; use warnings; -my $json_filename = "/srv/analysis.sesse.net/www/analysis.json"; +our $json_filename = "/srv/analysis.sesse.net/www/analysis.json"; my $cv = AnyEvent->condvar; my $updated = 0; @@ -26,11 +25,11 @@ my $wait = AnyEvent->timer ( cb => sub { $cv->send; }, ); +# Yes, this is reinventing If-Modified-Since, but browsers are so incredibly +# unpredictable on this, so blargh. my $ims = 0; -if (exists($ENV{'HTTP_IF_MODIFIED_SINCE'})) { - my $date = Date::Manip::Date->new; - $date->parse($ENV{'HTTP_IF_MODIFIED_SINCE'}); - $ims = $date->printf("%s"); +if (defined($cgi->param('ims')) && $cgi->param('ims') ne '') { + $ims = $cgi->param('ims'); } my $time = (stat($json_filename))[9]; @@ -40,20 +39,11 @@ if ($time > $ims) { exit; } -# If not, wait, then send. Apache will deal with the 304-ing. -if (defined($cgi->param('first')) && $cgi->param('first') != 1) { - $cv->recv; -} +# If not, wait, then send. +$cv->recv; output(); sub output { - my $time = (stat($json_filename))[9]; - my $lm_str = POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time)); - - print CGI->header(-type=>'text/json', - -last_modified=>$lm_str, - -access_control_allow_origin=>'http://analysis.sesse.net', - -expires=>'now'); open my $fh, "<", $json_filename or die "$json_filename: $!"; my $data; @@ -61,5 +51,13 @@ sub output { local $/ = undef; $data = <$fh>; } + my $time = (stat($fh))[9]; + close $fh; + + print CGI->header(-type=>'text/json', + -x_remoteglot_last_modified=>$time, + -access_control_allow_origin=>'http://analysis.sesse.net', + -access_control_expose_headers=>'X-Remoteglot-Last-Modified', + -expires=>'now'); print $data; }