common: html page headers on plp errors
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 27 Apr 2017 01:36:28 +0000 (03:36 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 29 May 2017 17:09:01 +0000 (19:09 +0200)
Ensure generic Html if called before page setup.

common.inc.plp

index d7e2b7936e2674f2b13c8abf507319d9ead36f4c..02821bbd4d284977df0d022d271f14dd834b766e 100644 (file)
@@ -27,13 +27,18 @@ BEGIN {
        $PLP::ERROR = sub {
                my ($text, $html) = @_;
                warn $text;
+               unless ($PLP::sentheaders and $PLP::sentheaders->[0] !~ m{/PLP\.pm\z}) {
+                       Html({nocache => 1});
+                       say '<h1>Page unavailable</h1>';
+               }
                Alert("<strong>Fatal error</strong>: $html.");
        };
+
+       # user request
+       our $Dev = $ENV{HTTP_HOST} =~ /\bdev\./;
+       our ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
 }
 
-# user request
-our $Dev = $ENV{HTTP_HOST} =~ /\bdev\./;
-our ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
 our $Request = decode_utf8($ENV{PATH_INFO} =~ s{^/}{}r);
 
 our $style;
@@ -89,13 +94,15 @@ sub checkmodified {
 sub Html {
        my ($meta) = @_;
 
-       # announce and check data modification
-       checkmodified(
-               $ENV{SCRIPT_FILENAME},
-               (grep { /\bShiar_/ } values %INC),
-               $meta->{data} ? @{ $meta->{data} } : (),
-       );
-       $header{'Cache-Control'} = sprintf 'max-age: ', 24*60*60;
+       unless ($meta->{nocache}) {
+               # announce and check data modification
+               checkmodified(
+                       $ENV{SCRIPT_FILENAME},
+                       (grep { /\bShiar_/ } values %INC),
+                       $meta->{data} ? @{ $meta->{data} } : (),
+               );
+               $header{'Cache-Control'} = sprintf 'max-age: ', 24*60*60;
+       }
 
        # default fallbacks
        $meta->{stylesheet} ||= [qw'light dark circus mono red terse'];
@@ -106,7 +113,8 @@ sub Html {
                for grep {$_} $meta->{raw}, $meta->{description}, $meta->{keywords};
 
        # document headers before output
-       $header{content_type} = "text/html; charset=$meta->{charset}";
+       $header{content_type} = "text/html; charset=$meta->{charset}"
+               unless $PLP::sentheaders;
        unshift @{ $meta->{raw} }, stylesheet($meta->{stylesheet});
 
        # optional amends