From 24760d11ac95c7e902b890534b6961e0b00f407f Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 30 Nov 2023 18:55:13 +0100 Subject: [PATCH] args function + default headers --- lib/perl/NB/CGI.pm | 47 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/perl/NB/CGI.pm b/lib/perl/NB/CGI.pm index ee872146..55b5e77f 100644 --- a/lib/perl/NB/CGI.pm +++ b/lib/perl/NB/CGI.pm @@ -188,10 +188,55 @@ sub header_status { return ($ENV{SERVER_PROTOCOL}||'HTTP/1.1')." $status\r\n"; } +sub args2hash { + my @p = @_; + my %hash; + #warn join('|',@p); + + for (my $i=0;$i<@p-1;$i++) + { + #next if index($p[$i], '-') == -1; + next if $p[$i] !~ /^-(.*)$/; + $hash{$1} = $p[$i+1]; + #warn $1."=".$hash{$1}; + } + + return wantarray ? %hash : \%hash; +} + +sub argsAdd { + my $p = shift; + my $key = shift; $key = "-$key"; + my $val = shift; + + # Delete existing + for (my $i=0;$i<@$p-1;$i++) + { + next if $p->[$i] ne $key; + splice(@$p,$i,2); + } + + # Add new one + #push(@$p,$key,$val); + unshift(@$p,$key,$val); + return &args2hash(@$p); +} + sub header { return "" if $NO_HEADERS; my($self,@p) = &CGI::self_or_default(@_); - # NB 27.11.23 my $status = '200 OK'; + my %p = &args2hash(@p); + #&argsAdd(\@p,'expires',-1) unless $p{expires}; + # Cache-Control: private, no-cache, no-store, must-revalidate, post-check=0, pre-check=0 + if ($p{expires}||0 <0) + { + %p = &argsAdd(\@p,'Cache-Control','private, no-cache, no-store, must-revalidate, post-check=0, pre-check=0'); + %p = &argsAdd(\@p,'Pragma','no-cache'); + } + #warn join('|',@p); + #warn join('|',keys(%p)); + #die join('|',keys(%p)); + #die $p{expires}; my $status = ''; if (!&CGI::nph()) -- 2.47.3