blib/lib/FCGI/Buffer.pm

filter: Statementstmt 59.9% 535 / 893 Branchbran 48.1% 279 / 580 Conditioncond 49.2% 138 / 280 MC/DCmcdc 29.8% 45 / 151 Subroutinesub 96.5% 28 / 29 Podpod 100% 5 / 5 totaltotal 53.1% 1030 / 1938 CC 392 SCAR 74.1
CC
392
Cov
54%
DESTROY
3063.1
_save_to
1495.5
_generate_key
193.6
CRAP 1649.4
↑ Summary
1 package FCGI::Buffer;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # FIXME: save_to treats ?arg1=a&arg2=b and ?arg2=b&arg1=a as different
7 # FIXME: save_to treats /cgi-bin/foo.fcgi and /cgi-bin2/foo.fcgi as the same
8  
9 11 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use CGI::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 Readonly my $DEFAULT_GENERATE_304 => 1;
20 11 Readonly my $DEFAULT_GENERATE_LAST_MODIFIED => 1;
21 11 Readonly my $DEFAULT_COMPRESS_CONTENT => 1;
22 11 Readonly my $DEFAULT_OPTIMISE_CONTENT => 0;
23 11 Readonly my $DEFAULT_LINT_CONTENT => 0;
24 11 Readonly my $MIN_GZIP_LEN => 32;
25  
26 =head1 NAME
27  
28 FCGI::Buffer - Verify, Cache and Optimise FCGI Output
29  
30 =head1 VERSION
31  
32 Version 0.22
33  
34 =cut
35  
36 11 our $VERSION = '0.22';
37  
38 =head1 SYNOPSIS
39  
40 FCGI::Buffer verifies the HTML that you produce by passing it through
41 C<HTML::Lint>.
42  
43 FCGI::Buffer optimises FCGI programs by reducing, filtering and compressing
44 output to speed up the transmission and by nearly seamlessly making use of
45 client and server caches.
46  
47 To make use of client caches, that is to say to reduce needless calls
48 to your server asking for the same data:
49  
50 use FCGI;
51 use FCGI::Buffer;
52 # ...
53 my $request = FCGI::Request();
54 while($request->FCGI::Accept() >= 0) {
55 my $buffer = FCGI::Buffer->new();
56 $buffer->init(
57 optimise_content => 1,
58 lint_content => 0,
59 );
60 # ...
61 }
62  
63 To also make use of server caches, that is to say,
64 to save regenerating output when different clients ask you for the same data,
65 you will need to create a cache.
66 But that's simple:
67  
68 use FCGI;
69 use CHI;
70 use FCGI::Buffer;
71  
72 # ...
73 my $request = FCGI::Request();
74 while($request->FCGI::Accept() >= 0) {
75 my $buffer = FCGI::Buffer->new();
76 $buffer->init(
77 optimise_content => 1,
78 lint_content => 0,
79 cache => CHI->new(driver => 'File')
80 );
81 if($buffer->is_cached()) {
82 # Nothing has changed - use the version in the cache
83 $request->Finish();
84 next;
85 # ...
86 }
87 }
88  
89 To temporarily prevent the use of server-side caches, for example, whilst
90 debugging before publishing a code change, set the NO_CACHE environment variable
91 to any non-zero value.
92 This will also stop ETag from being added to the header.
93 If you get errors about Wide characters in print,
94 you've forgotten to emit pure HTML on non-ASCII characters.
95 See L<HTML::Entities>.
96 As a hack work around you could also remove accents and the like by using
97 L<Text::Unidecode>,
98 which works well but isn't really what you want.
99  
100 =head1 SUBROUTINES/METHODS
101  
102 =cut
103  
104 =head2 new
105  
106 Create an FCGI::Buffer object.
107 Do one of these for each FCGI::Accept.
108  
109 =cut
110  
111 # FIXME: Call init() on any arguments that are given
112 sub new
113 48 ▶{
Subroutine new: called CC=9 SCAR=22.5
Pod documented
114 48 my $proto = shift;
115 48 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
0471
MC/DC50%1/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
116  
117 # Use FCGI::Buffer->new(), not FCGI::Buffer::new()
118 48 ▶ if(!defined($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 1 47 48
119 1 carp(__PACKAGE__, ': use ->new() not ::new() to instantiate');
120 1 return;
121 }
122  
123 # Handle hash or hashref arguments
124 47 my %args;
125 47 ▶ if((@_ == 1) && (ref $_[0] eq 'HASH')) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 1 46 47
elsif (@_ % 2 == 0) { } 46 0 46
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
4601
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
126 1 %args = %{$_[0]};
127 } elsif((@_ % 2) == 0) {
128 46 %args = @_;
129 } else {
130 0 carp(__PACKAGE__, ': Invalid arguments passed to new()');
131 0 return;
132 }
133  
134 # Initialize buffer and object properties
135 47 ▶ my $buf = IO::String->new() or croak(__PACKAGE__, ': Failed to initialize IO::String');
Branch: 1 branch
BranchTrueFalseTotal
my $buf = "IO::String"->new or croak(__PACKAGE__, ": Failed to initialize IO::String") 47 0 47
136 47 my $rc = {
137 buf => $buf,
138 old_buf => select($buf),
139 generate_304 => $DEFAULT_GENERATE_304,
140 generate_last_modified => $DEFAULT_GENERATE_LAST_MODIFIED,
141 compress_content => $DEFAULT_COMPRESS_CONTENT,
142 optimise_content => $DEFAULT_OPTIMISE_CONTENT,
143 lint_content => $DEFAULT_LINT_CONTENT,
144 %args,
145 };
146 # $rc->{o} = ();
147  
148 # Set generate_etag based on server protocol
149 47 ▶ $rc->{generate_etag} = ($ENV{'SERVER_PROTOCOL'} || '') =~ /^(HTTP\/1\.1|HTTP\/2\.0)$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'SERVER_PROTOCOL'} || '') =~ m[^(HTTP/1\.1|HTTP/2\.0)$] ? : 28 19 47
Condition100%2/2
$ENV{'SERVER_PROTOCOL'} || ''
l!l
2918
MC/DC100%1/1
$ENV{'SERVER_PROTOCOL'} || ''
$ENV{'SERVER_PROTOCOL'}
Truth table100%2/2
A
A= $ENV{'SERVER_PROTOCOL'}
Aresult
11
00
150  
151 # Return the blessed object
152 47 return bless $rc, $class;
153 }
154  
155 sub DESTROY
156 46 ▶{
Subroutine DESTROY: called CC=203 SCAR=80.3
157 46 ▶ if(defined($^V) && ($^V ge 'v5.14.0')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $^V and $^V ge "v5.14.0") 46 0 46
Condition33%1/3
defined $^V and $^V ge "v5.14.0"
!ll&&!rl&&r
0046
MC/DC0%0/2
defined $^V and $^V ge "v5.14.0"
defined $^V $^V ge "v5.14.0"
Truth table33%1/3
A and B
A= defined $^VB= $^V ge "v5.14.0"
ABresult
0X0
100
111
158 46 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; # >= 5.14.0 only
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 46 46
159 }
160 46 my $self = shift;
161  
162 46 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 6 40 46
163 6 $self->{'logger'}->trace('>In ' . __PACKAGE__ . ' DESTROY');
164 }
165 46 select($self->{old_buf});
166 46 ▶ if((!defined($self->{buf})) || (!defined($self->{buf}->getpos()))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"buf"} and defined $self->{"buf"}->getpos) 0 46 46
Condition33%1/3
defined $self->{"buf"} and defined $self->{"buf"}->getpos
!ll&&!rl&&r
0046
MC/DC0%0/2
defined $self->{"buf"} and defined $self->{"buf"}->getpos
defined $self->{"buf"} defined $self->{"buf"}->getpos
Truth table33%1/3
A and B
A= defined $self->{"buf"}B= defined $self->{"buf"}->getpos
ABresult
0X0
100
111
167 # Unlikely
168 0 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
169 0 $self->{'logger'}->info('Nothing to send');
170 }
171 0 return;
172 }
173 46 my $pos = $self->{buf}->getpos();
174 46 $self->{buf}->setpos(0);
175 46 my $buf;
176 46 read($self->{buf}, $buf, $pos);
177 46 my $headers;
178 46 ($headers, $self->{body}) = split /\r?\n\r?\n/, $buf, 2;
179  
180 46 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 6 40 46
181 6 ▶ if($ENV{'HTTP_IF_NONE_MATCH'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_NONE_MATCH"}) 0 6 6
182 0 $self->{logger}->debug("HTTP_IF_NONE_MATCH: $ENV{HTTP_IF_NONE_MATCH}");
183 }
184 6 ▶ if($ENV{'HTTP_IF_MODIFIED_SINCE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_MODIFIED_SINCE"}) 3 3 6
185 3 $self->{logger}->debug("HTTP_IF_MODIFIED_SINCE: $ENV{HTTP_IF_MODIFIED_SINCE}");
186 }
187 6 $self->{logger}->debug("Generate_etag = $self->{generate_etag}, ",
188 "Generate_304 = $self->{generate_304}, ",
189 "Generate_last_modified = $self->{generate_last_modified}");
190 }
191  
192 46 ▶ unless($headers || $self->is_cached()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers or $self->is_cached) 3 43 46
Condition100%3/3
$headers or $self->is_cached
l!l&&r!l&&!r
3943
MC/DC100%2/2
$headers or $self->is_cached
$headers $self->is_cached
Truth table100%3/3
A or B
A= $headersB= $self->is_cached
ABresult
1X1
011
000
193 3 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 3 3
194 0 $self->{'logger'}->debug('There was no output');
195 }
196 3 ▶ if(!defined($headers)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $headers) 3 0 3
197 3 require HTTP::Status;
198 3 HTTP::Status->import();
199  
200 3 ▶ if(!defined($self->{'status'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"status"}) 3 0 3
201 3 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'info'}) { } 0 3 3
202 0 $self->{'status'} = $self->{'info'}->status();
203 } else {
204 3 $self->{'status'} = 200;
205 }
206 }
207 print 'Status: ', $self->{status}, ' ',
208 3 HTTP::Status::status_message($self->{status}),
209 "\n\n";
210 }
211 3 return;
212 }
213 43 ▶ if($ENV{'REQUEST_METHOD'} && ($ENV{'REQUEST_METHOD'} eq 'HEAD')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'REQUEST_METHOD'} and $ENV{'REQUEST_METHOD'} eq 'HEAD') { } 1 42 43
Condition100%3/3
$ENV{"REQUEST_METHOD"} and $ENV{"REQUEST_METHOD"} eq "HEAD"
!ll&&!rl&&r
11311
MC/DC100%2/2
$ENV{"REQUEST_METHOD"} and $ENV{"REQUEST_METHOD"} eq "HEAD"
$ENV{"REQUEST_METHOD"} $ENV{"REQUEST_METHOD"} eq "HEAD"
Truth table100%3/3
A and B
A= $ENV{"REQUEST_METHOD"}B= $ENV{"REQUEST_METHOD"} eq "HEAD"
ABresult
0X0
100
111
214 1 $self->{send_body} = 0;
215 } else {
216 42 $self->{send_body} = 1;
217 }
218  
219 43 ▶ if($headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers) 39 4 43
220 39 $self->_set_content_type($headers);
221 }
222  
223 43 ▶ if(defined($self->{body}) && ($self->{body} eq '')) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'body'} and $self->{'body'} eq '') { } 3 40 43
elsif (defined $self->{'content_type'}) { } 33 7 40
Condition100%3/3
defined $self->{"body"} and $self->{"body"} eq ""
!ll&&!rl&&r
4363
MC/DC100%2/2
defined $self->{"body"} and $self->{"body"} eq ""
defined $self->{"body"} $self->{"body"} eq ""
Truth table100%3/3
A and B
A= defined $self->{"body"}B= $self->{"body"} eq ""
ABresult
0X0
100
111
224 # E.g. if header of Location is given with no body, for
225 # redirection
226 3 delete $self->{body};
227 3 ▶ if($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 0 3 3
228 # Don't try to retrieve it below from the cache
229 0 $self->{send_body} = 0;
230 }
231 } elsif(defined($self->{content_type})) {
232 33 my @content_type = @{$self->{content_type}};
233 33 ▶ if(defined($content_type[0]) && (lc($content_type[0]) eq 'text') && (lc($content_type[1]) =~ /^html/) && defined($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_type[0] and lc $content_type[0] eq "text" and lc($content_type[1]) =~ /^html/ and defined $self->{"body"}) 33 0 33
Condition33%3/9
defined $content_type[0] and lc $content_type[0] eq "text" and lc($content_type[1]) =~ /^html/ and defined $self->{"body"}
!ll&&!rl&&r
0033
defined $content_type[0] and lc $content_type[0] eq "text" and lc($content_type[1]) =~ /^html/
!ll&&!rl&&r
0033
defined $content_type[0] and lc $content_type[0] eq "text"
!ll&&!rl&&r
0033
MC/DC0%0/4
defined $content_type[0] and lc $content_type[0] eq "text" and lc($content_type[1]) =~ /^html/ and defined $self->{"body"}
defined $content_type[0] lc $content_type[0] eq "text" lc($content_type[1]) =~ /^html/ defined $self->{"body"}
Truth table20%1/5
A and B and C and D
A= defined $content_type[0]B= lc $content_type[0] eq "text"C= lc($content_type[1]) =~ /^html/D= defined $self->{"body"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
234 33 ▶ if($self->{optimise_content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"optimise_content"}) 25 8 33
235 # require HTML::Clean;
236 25 require HTML::Packer; # Overkill using HTML::Clean and HTML::Packer...
237  
238 25 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 25 25
239 0 $self->{'logger'}->trace('Packer');
240 }
241  
242 25 my $oldlength = length($self->{body});
243 25 my $newlength;
244  
245 25 ▶ if($self->{optimise_content} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'optimise_content'} == 1) { } 24 1 25
246 24 $self->_optimise_content();
247 } else {
248 1 while(1) {
249 3 $self->_optimise_content();
250 3 $newlength = length($self->{body});
251 3 ▶ last if ($newlength >= $oldlength);
Branch: 1 branch
BranchTrueFalseTotal
if $newlength >= $oldlength 1 2 3
252 2 $oldlength = $newlength;
253 }
254 }
255  
256 # If we're on http://www.example.com and have a link
257 # to http://www.example.com/foo/bar.htm, change the
258 # link to /foo/bar.htm - there's no need to include
259 # the site name in the link
260 25 ▶ unless(defined($self->{info})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"info"}) 22 3 25
261 22 ▶ if($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cache'}) { } 0 22 22
262 0 $self->{info} = CGI::Info->new({ cache => $self->{cache} });
263 } else {
264 22 $self->{info} = CGI::Info->new();
265 }
266 }
267  
268 25 my $href = $self->{info}->host_name();
269 25 my $protocol = $self->{info}->protocol();
270  
271 25 ▶ unless($protocol) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($protocol) 10 15 25
272 10 $protocol = 'http';
273 }
274  
275 25 $self->{body} =~ s/<a\s+?href="$protocol:\/\/$href"/<a href="\/"/gim;
276 25 $self->{body} =~ s/<a\s+?href="$protocol:\/\/$href/<a href="/gim;
277 25 $self->{body} =~ s/<a\s+?href="$protocol:\/\//<a href="\/\//gim;
278  
279 # If we're in "/cgi-bin/foo.cgi?arg1=a" replace
280 # "/cgi-bin/foo.cgi?arg2=b" with "?arg2=b"
281  
282 25 ▶ if(my $script_name = $ENV{'SCRIPT_NAME'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $script_name = $ENV{"SCRIPT_NAME"}) 18 7 25
283 18 ▶ if($script_name =~ /^\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_name =~ m[^/]) 18 0 18
284 18 $self->{body} =~ s/<a\s+?href="$script_name(\?.+)?"/<a href="$1"/gim;
285 }
286 }
287  
288 # TODO use URI->path_segments to change links in
289 # /aa/bb/cc/dd.htm which point to /aa/bb/ff.htm to
290 # ../ff.htm
291  
292 # TODO: <img border=0 src=...>
293 25 $self->{body} =~ s/<img\s+?src="$protocol:\/\/$href"/<img src="\/"/gim;
294 25 $self->{body} =~ s/<img\s+?src="$protocol:\/\/$href/<img src="/gim;
295  
296 # Don't use HTML::Clean because of RT402
297 # my $h = new HTML::Clean(\$self->{body});
298 # # $h->compat();
299 # $h->strip();
300 # my $ref = $h->data();
301  
302 # Don't always do javascript 'best' since it's confused
303 # by the common <!-- HIDE technique.
304 25 my $options = {
305 remove_comments => 1,
306 remove_newlines => 0,
307 do_stylesheet => 'minify'
308 };
309 25 ▶ if($self->{optimise_content} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"optimise_content"} >= 2) 1 24 25
310 1 $options->{do_javascript} = 'best';
311 1 $self->{body} =~ s/(<script.*?>)\s*<!--/$1/gi;
312 1 $self->{body} =~ s/\/\/-->\s*<\/script>/<\/script>/gi;
313 1 $self->{body} =~ s/(<script.*?>)\s+/$1/gi;
314 }
315 25 $self->{body} = HTML::Packer->init()->minify(\$self->{body}, $options);
316 25 ▶ if($self->{optimise_content} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"optimise_content"} >= 2) 1 24 25
317 # Change document.write("a"); document.write("b")
318 # into document.write("a"+"b");
319 1 while(1) {
320 1 $self->{body} =~ s/<script\s*?type\s*?=\s*?"text\/javascript"\s*?>(.*?)document\.write\((.+?)\);\s*?document\.write\((.+?)\)/<script type="text\/JavaScript">${1}document.write($2+$3)/igs;
321 1 $newlength = length($self->{body});
322 1 ▶ last if ($newlength >= $oldlength);
Branch: 1 branch
BranchTrueFalseTotal
if $newlength >= $oldlength 1 0 1
323 0 $oldlength = $newlength;
324 }
325 }
326 }
327 33 ▶ if($self->{lint_content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lint_content"}) 3 30 33
328 3 require HTML::Lint;
329 3 HTML::Lint->import;
330  
331 3 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 3 3
332 0 $self->{'logger'}->trace('Lint');
333 }
334 3 my $lint = HTML::Lint->new();
335 3 $lint->parse($self->{body});
336 3 $lint->eof();
337  
338 3 ▶ if($lint->errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lint->errors) 1 2 3
339 1 $headers = 'Status: 500 Internal Server Error';
340 1 @{$self->{o}} = ('Content-type: text/plain');
341 1 $self->{body} = '';
342 1 foreach my $error ($lint->errors) {
343 3 my $errtext = $error->where() . ': ' . $error->errtext() . "\n";
344 3 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'logger'}) { } 0 3 3
345 0 $self->{logger}->warn($errtext);
346 } else {
347 3 warn($errtext);
348 }
349 3 $self->{body} .= $errtext;
350 }
351 }
352 }
353 }
354 }
355  
356 43 ▶ if(defined($headers) && ($headers =~ /^Status: (\d+)/m)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $headers and $headers =~ /^Status: (\d+)/m) { } 2 41 43
elsif (defined $self->{'info'}) { } 29 12 41
Condition100%3/3
defined $headers and $headers =~ /^Status: (\d+)/m
!ll&&!rl&&r
4372
MC/DC100%2/2
defined $headers and $headers =~ /^Status: (\d+)/m
defined $headers $headers =~ /^Status: (\d+)/m
Truth table100%3/3
A and B
A= defined $headersB= $headers =~ /^Status: (\d+)/m
ABresult
0X0
100
111
357 2 $self->{status} = $1;
358 } elsif(defined($self->{info})) {
359 29 $self->{status} = $self->{info}->status();
360 } else {
361 12 $self->{status} = 200;
362 }
363  
364 43 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 6 37 43
365 6 $self->{'logger'}->debug("Initial status = $self->{status}");
366 }
367  
368 # Generate the eTag before compressing, since the compressed data
369 # includes the mtime field which changes thus causing a different
370 # Etag to be generated
371 43 ▶ if($ENV{'SERVER_PROTOCOL'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_etag"} and defined $self->{"body"}) 14 29 43
Condition100%12/12
$ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_etag"} and defined $self->{"body"}
!ll&&!rl&&r
25414
$ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_etag"}
!ll&&!rl&&r
17818
$ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"
!ll&&!rl&&r
16126
$ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" or $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"
l!l&&r!l&&!r
2421
MC/DC100%5/5
$ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_etag"} and defined $self->{"body"}
$ENV{"SERVER_PROTOCOL"} $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" $self->{"generate_etag"} defined $self->{"body"}
Truth table87%7/8
A and B or C and D and E
A= $ENV{"SERVER_PROTOCOL"}B= $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1"C= $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"D= $self->{"generate_etag"}E= defined $self->{"body"}
ABCDEresult
0XXXX0
100XX0
11X0X0
1010X0
11X100
101100
11X111
101111
372 (($ENV{'SERVER_PROTOCOL'} eq 'HTTP/1.1') || ($ENV{'SERVER_PROTOCOL'} eq 'HTTP/2.0')) &&
373 $self->{generate_etag} && defined($self->{body})) {
374 14 $self->_generate_etag();
375  
376 14 ▶ if($ENV{'HTTP_IF_NONE_MATCH'} && $self->{generate_304} && ($self->{status} == 200)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_NONE_MATCH"} and $self->{"generate_304"} and $self->{"status"} == 200) 3 11 14
Condition83%5/6
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"generate_304"} and $self->{"status"} == 200
!ll&&!rl&&r
1103
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"generate_304"}
!ll&&!rl&&r
923
MC/DC66%2/3
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"generate_304"} and $self->{"status"} == 200
$ENV{"HTTP_IF_NONE_MATCH"} $self->{"generate_304"} $self->{"status"} == 200
Truth table75%3/4
A and B and C
A= $ENV{"HTTP_IF_NONE_MATCH"}B= $self->{"generate_304"}C= $self->{"status"} == 200
ABCresult
0XX0
10X0
1100
1111
377 3 $self->_check_if_none_match();
378 }
379 }
380  
381 43 my $dbh;
382 43 ▶ if(my $save_to = $self->{save_to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $save_to = $self->{"save_to"}) 0 43 43
383 0 my $sqlite_file = File::Spec->catfile($save_to->{directory}, 'fcgi.buffer.sql');
384 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
385 0 $self->{logger}->debug("save_to sqlite file: $sqlite_file");
386 }
387 0 ▶ if(!-r $sqlite_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -r $sqlite_file) { } 0 0 0
388 0 ▶ if(!-d $save_to->{directory}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $save_to->{"directory"}) 0 0 0
389 0 mkdir $save_to->{directory};
390 }
391 0 $dbh = DBI->connect("dbi:SQLite:dbname=$sqlite_file", undef, undef);
392 0 ▶ if($self->{save_to}->{create_table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_to"}{"create_table"}) 0 0 0
393 0 $dbh->prepare('CREATE TABLE fcgi_buffer(key char PRIMARY KEY, language char, browser_type char, path char UNIQUE NOT NULL, uri char NOT NULL, creation timestamp NOT NULL)')->execute();
394 }
395 } else {
396 0 $dbh = DBI->connect("dbi:SQLite:dbname=$sqlite_file", undef, undef);
397 }
398 }
399  
400 43 my $encoding = $self->_should_gzip();
401 43 my $unzipped_body = $self->{body};
402  
403 43 ▶ if(defined($unzipped_body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $unzipped_body) 36 7 43
404 36 ▶ my $range = $ENV{'Range'} ? $ENV{'Range'} : $ENV{'HTTP_RANGE'};
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'Range'} ? : 0 36 36
405  
406 36 ▶ if($range && !$self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range and not $self->{"cache"}) 3 33 36
Condition66%2/3
$range and not $self->{"cache"}
!ll&&!rl&&r
3303
MC/DC50%1/2
$range and not $self->{"cache"}
$range not $self->{"cache"}
Truth table66%2/3
A and not(B)
A= $rangeB= not $self->{"cache"}
ABresult
0X0
100
111
407 # TODO: Partials
408 3 ▶ if($range =~ /^bytes=(\d*)-(\d*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range =~ /^bytes=(\d*)-(\d*)/) 3 0 3
409 3 ▶ if($1 && $2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 and $2) { } 1 2 3
elsif ($1) { } 1 1 2
elsif ($2) { } 1 0 1
Condition100%3/3
$1 and $2
!ll&&!rl&&r
111
MC/DC100%2/2
$1 and $2
$1 $2
Truth table100%3/3
A and B
A= $1B= $2
ABresult
0X0
100
111
410 1 $self->{body} = substr($self->{body}, $1, $2-$1);
411 } elsif($1) {
412 1 $self->{body} = substr($self->{body}, $1);
413 } elsif($2) {
414 1 $self->{body} = substr($self->{body}, 0, $2);
415 }
416 3 $unzipped_body = $self->{body};
417 3 $self->{'status'} = 206;
418 3 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 3 0 3
419 3 $self->{'info'}->status(206);
420 }
421 }
422 }
423 36 $self->_compress({ encoding => $encoding });
424 }
425  
426 43 ▶ if($self->{cache}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'cache'}) { } 7 36 43
elsif ($self->{'info'}) { } 26 10 36
427 7 require Storable;
428  
429 7 ▶ if($self->{'cache_age'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_age"}) 0 7 7
430 # Legacy
431 0 $self->{'cache_duration'} = $self->{'cache_age'};
432 }
433  
434 7 my $cache_hash;
435 7 my $key = $self->_generate_key();
436  
437 # Cache unzipped version
438 7 ▶ if(!defined($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'body'}) { } 4 3 7
439 4 ▶ if($self->{send_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"send_body"}) 4 0 4
440 4 $self->{cobject} = $self->{cache}->get_object($key);
441 4 ▶ if(defined($self->{cobject})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'cobject'}) { } 4 0 4
elsif ($self->{'logger'}) { } 0 0 0
442 4 $cache_hash = Storable::thaw($self->{cobject}->value());
443 4 $headers = $cache_hash->{'headers'};
444 4 $self->_set_content_type($headers);
445 4 @{$self->{o}} = ("X-FCGI-Buffer-$VERSION: Hit");
446 4 ▶ if($self->{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'info'}) { } 4 0 4
447 4 my $host_name = $self->{info}->host_name();
448 4 push @{$self->{o}}, "X-Cache: HIT from $host_name", "X-Cache-Lookup: HIT from $host_name";
449 } else {
450 0 push @{$self->{o}}, 'X-Cache: HIT', 'X-Cache-Lookup: HIT';
451 }
452 } elsif($self->{logger}) {
453 0 $self->{logger}->warn(ref($self), ": Error retrieving data for key $key");
454 } else {
455 0 carp(ref($self), ": error retrieving data for key $key");
456 }
457 }
458  
459 # Nothing has been output yet, so we can check if it's
460 # OK to send 304 if possible
461 4 ▶ if($self->{send_body} && $ENV{'SERVER_PROTOCOL'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_304"} and $self->{"status"} == 200) 3 1 4
Condition60%9/15
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_304"} and $self->{"status"} == 200
!ll&&!rl&&r
103
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_304"}
!ll&&!rl&&r
103
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"
!ll&&!rl&&r
103
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"}
!ll&&!rl&&r
013
$ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" or $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"
l!l&&r!l&&!r
300
MC/DC16%1/6
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"generate_304"} and $self->{"status"} == 200
$self->{"send_body"} $ENV{"SERVER_PROTOCOL"} $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" $self->{"generate_304"} $self->{"status"} == 200
Truth table22%2/9
A and B and C or D and E and F
A= $self->{"send_body"}B= $ENV{"SERVER_PROTOCOL"}C= $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1"D= $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"E= $self->{"generate_304"}F= $self->{"status"} == 200
ABCDEFresult
0XXXXX0
10XXXX0
1100XX0
111X0X0
11010X0
111X100
1101100
111X111
1101111
462 (($ENV{'SERVER_PROTOCOL'} eq 'HTTP/1.1') || ($ENV{'SERVER_PROTOCOL'} eq 'HTTP/2.0')) &&
463 $self->{generate_304} && ($self->{status} == 200)) {
464 3 ▶ if($ENV{'HTTP_IF_MODIFIED_SINCE'} && $self->{'cobject'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"cobject"}) 3 0 3
Condition33%1/3
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"cobject"}
!ll&&!rl&&r
003
MC/DC0%0/2
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"cobject"}
$ENV{"HTTP_IF_MODIFIED_SINCE"} $self->{"cobject"}
Truth table33%1/3
A and B
A= $ENV{"HTTP_IF_MODIFIED_SINCE"}B= $self->{"cobject"}
ABresult
0X0
100
111
465 $self->_check_modified_since({
466 since => $ENV{'HTTP_IF_MODIFIED_SINCE'},
467 modified => $self->{cobject}->created_at()
468 3 });
469 }
470 }
471 4 ▶ if($self->{send_body} && ($self->{status} == 200) && defined($cache_hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"send_body"} and $self->{"status"} == 200 and defined $cache_hash) 2 2 4
Condition66%4/6
$self->{"send_body"} and $self->{"status"} == 200 and defined $cache_hash
!ll&&!rl&&r
202
$self->{"send_body"} and $self->{"status"} == 200
!ll&&!rl&&r
202
MC/DC33%1/3
$self->{"send_body"} and $self->{"status"} == 200 and defined $cache_hash
$self->{"send_body"} $self->{"status"} == 200 defined $cache_hash
Truth table50%2/4
A and B and C
A= $self->{"send_body"}B= $self->{"status"} == 200C= defined $cache_hash
ABCresult
0XX0
10X0
1100
1111
472 2 $self->{body} = $cache_hash->{'body'};
473 2 ▶ if($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) 0 2 2
474 0 my $changes = $self->_save_to($self->{body}, $dbh);
475 0 ▶ if($changes && (my $ttl = $self->{save_to}->{ttl})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes and my $ttl = $self->{"save_to"}{"ttl"}) 0 0 0
Condition0%0/3
$changes and my $ttl = $self->{"save_to"}{"ttl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$changes and my $ttl = $self->{"save_to"}{"ttl"}
$changes my $ttl = $self->{"save_to"}{"ttl"}
Truth table0%0/3
A and B
A= $changesB= my $ttl = $self->{"save_to"}{"ttl"}
ABresult
0X0
100
111
476 0 push @{$self->{o}}, 'Expires: ' . HTTP::Date::time2str(time + $ttl);
477 }
478 }
479 2 ▶ if(!defined($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"body"}) 0 2 2
480 # Panic
481 0 $headers = 'Status: 500 Internal Server Error';
482 0 @{$self->{o}} = ('Content-type: text/plain');
483 0 $self->{body} = "Can't retrieve body for key $key, cache_hash contains:\n";
484 0 foreach my $k (keys %{$cache_hash}) {
485 0 $self->{body} .= "\t$k\n";
486 }
487  
488 0 ▶ if($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) 0 0 0
489 0 my $query = "SELECT DISTINCT path FROM fcgi_buffer WHERE key = '$key'";
490 0 my $sth = $dbh->prepare($query);
491 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
492 0 $self->{logger}->debug($query);
493 }
494 0 ▶ if($sth->execute() && (my $href = $sth->fetchrow_hashref())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->execute and my $href = $sth->fetchrow_hashref) 0 0 0
Condition0%0/3
$sth->execute and my $href = $sth->fetchrow_hashref
!ll&&!rl&&r
000
MC/DC0%0/2
$sth->execute and my $href = $sth->fetchrow_hashref
$sth->execute my $href = $sth->fetchrow_hashref
Truth table0%0/3
A and B
A= $sth->executeB= my $href = $sth->fetchrow_hashref
ABresult
0X0
100
111
495 0 ▶ if(my $path = $href->{'path'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $href->{"path"}) 0 0 0
496 0 unlink($path);
497 }
498 }
499 0 $query = "DELETE FROM fcgi_buffer WHERE key = '$key'";
500 0 $dbh->do($query);
501 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
502 0 $self->{logger}->debug($query);
503 }
504 }
505  
506 0 $self->{cache}->remove($key);
507  
508 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'logger'}) { } 0 0 0
509 0 $self->{logger}->error("Can't retrieve body for key $key");
510 0 $self->{logger}->warn($self->{body});
511 } else {
512 0 carp("Can't retrieve body for key $key");
513 0 warn($self->{body});
514 }
515 0 $self->{send_body} = 0;
516 0 $self->{status} = 500;
517 0 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 0 0 0
518 0 $self->{'info'}->status(500);
519 }
520 }
521 }
522 4 ▶ if($self->{send_body} && $ENV{'SERVER_PROTOCOL'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"status"} == 200) 1 3 4
Condition66%8/12
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"status"} == 200
!ll&&!rl&&r
301
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"
!ll&&!rl&&r
301
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"}
!ll&&!rl&&r
211
$ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" or $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"
l!l&&r!l&&!r
100
MC/DC40%2/5
$self->{"send_body"} and $ENV{"SERVER_PROTOCOL"} and $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" || $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" and $self->{"status"} == 200
$self->{"send_body"} $ENV{"SERVER_PROTOCOL"} $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1" $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0" $self->{"status"} == 200
Truth table42%3/7
A and B and C or D and E
A= $self->{"send_body"}B= $ENV{"SERVER_PROTOCOL"}C= $ENV{"SERVER_PROTOCOL"} eq "HTTP/1.1"D= $ENV{"SERVER_PROTOCOL"} eq "HTTP/2.0"E= $self->{"status"} == 200
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
523 (($ENV{'SERVER_PROTOCOL'} eq 'HTTP/1.1') || ($ENV{'SERVER_PROTOCOL'} eq 'HTTP/2.0')) &&
524 ($self->{status} == 200)) {
525 1 ▶ if($ENV{'HTTP_IF_NONE_MATCH'} && $self->{generate_etag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_NONE_MATCH"} and $self->{"generate_etag"}) 0 1 1
Condition33%1/3
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"generate_etag"}
!ll&&!rl&&r
100
MC/DC0%0/2
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"generate_etag"}
$ENV{"HTTP_IF_NONE_MATCH"} $self->{"generate_etag"}
Truth table33%1/3
A and B
A= $ENV{"HTTP_IF_NONE_MATCH"}B= $self->{"generate_etag"}
ABresult
0X0
100
111
526 0 ▶ if(!defined($self->{etag})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"etag"}) 0 0 0
527 0 $self->_generate_etag();
528 }
529 0 $self->_check_if_none_match();
530 }
531 }
532 4 ▶ if($self->{status} == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"status"} == 200) 2 2 4
533 2 $encoding = $self->_should_gzip();
534 2 ▶ if($self->{send_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"send_body"}) 2 0 2
535 2 ▶ if($self->{generate_etag} && !defined($self->{etag}) && ((!defined($headers)) || ($headers !~ /^ETag: /m))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"generate_etag"} and not defined $self->{"etag"} and not defined $headers && $headers =~ /^ETag: /m) 1 1 2
Condition55%5/9
$self->{"generate_etag"} and not defined $self->{"etag"} and not defined $headers && $headers =~ /^ETag: /m
!ll&&!rl&&r
101
$self->{"generate_etag"} and not defined $self->{"etag"}
!ll&&!rl&&r
101
defined $headers && $headers =~ /^ETag: /m
!ll&&!rl&&r
010
MC/DC25%1/4
$self->{"generate_etag"} and not defined $self->{"etag"} and not defined $headers && $headers =~ /^ETag: /m
$self->{"generate_etag"} not defined $self->{"etag"} defined $headers $headers =~ /^ETag: /m
Truth table40%2/5
A and not(B) and not(C && D)
A= $self->{"generate_etag"}B= not defined $self->{"etag"}C= defined $headersD= $headers =~ /^ETag: /m
ABCDresult
0XXX0
10XX0
11110
110X1
11101
536 1 $self->_generate_etag();
537 }
538 2 $self->_compress({ encoding => $encoding });
539 }
540 }
541 4 my $cannot_304 = !$self->{generate_304};
542 4 ▶ unless($self->{etag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"etag"}) 3 1 4
543 3 ▶ if(defined($headers) && ($headers =~ /^ETag: "([a-z0-9]{32})"/m)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers and $headers =~ /^ETag: "([a-z0-9]{32})"/m) { } 0 3 3
Condition33%1/3
defined $headers and $headers =~ /^ETag: "([a-z0-9]{32})"/m
!ll&&!rl&&r
030
MC/DC0%0/2
defined $headers and $headers =~ /^ETag: "([a-z0-9]{32})"/m
defined $headers $headers =~ /^ETag: "([a-z0-9]{32})"/m
Truth table33%1/3
A and B
A= defined $headersB= $headers =~ /^ETag: "([a-z0-9]{32})"/m
ABresult
0X0
100
111
544 0 $self->{etag} = $1;
545 } else {
546 3 $self->{etag} = $cache_hash->{'etag'};
547 }
548 }
549 4 ▶ if($ENV{'HTTP_IF_NONE_MATCH'} && $self->{send_body} && ($self->{status} != 304) && $self->{generate_304}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_NONE_MATCH"} and $self->{"send_body"} and $self->{"status"} != 304 and $self->{"generate_304"}) 0 4 4
Condition33%3/9
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"send_body"} and $self->{"status"} != 304 and $self->{"generate_304"}
!ll&&!rl&&r
400
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"send_body"} and $self->{"status"} != 304
!ll&&!rl&&r
400
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"send_body"}
!ll&&!rl&&r
400
MC/DC0%0/4
$ENV{"HTTP_IF_NONE_MATCH"} and $self->{"send_body"} and $self->{"status"} != 304 and $self->{"generate_304"}
$ENV{"HTTP_IF_NONE_MATCH"} $self->{"send_body"} $self->{"status"} != 304 $self->{"generate_304"}
Truth table20%1/5
A and B and C and D
A= $ENV{"HTTP_IF_NONE_MATCH"}B= $self->{"send_body"}C= $self->{"status"} != 304D= $self->{"generate_304"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
550 0 ▶ if(!$self->_check_if_none_match()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_if_none_match) 0 0 0
551 0 $cannot_304 = 1;
552 }
553 }
554 4 ▶ if($self->{cobject}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cobject"}) 4 0 4
555 4 ▶ if($ENV{'HTTP_IF_MODIFIED_SINCE'} && ($self->{status} != 304) && !$cannot_304) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304 and not $cannot_304) 1 3 4
Condition83%5/6
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304 and not $cannot_304
!ll&&!rl&&r
301
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304
!ll&&!rl&&r
121
MC/DC66%2/3
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304 and not $cannot_304
$ENV{"HTTP_IF_MODIFIED_SINCE"} $self->{"status"} != 304 not $cannot_304
Truth table75%3/4
A and B and not(C)
A= $ENV{"HTTP_IF_MODIFIED_SINCE"}B= $self->{"status"} != 304C= not $cannot_304
ABCresult
0XX0
10X0
1100
1111
556 $self->_check_modified_since({
557 since => $ENV{'HTTP_IF_MODIFIED_SINCE'},
558 modified => $self->{cobject}->created_at()
559 1 });
560 }
561 4 ▶ if(($self->{status} == 200) && $self->{generate_last_modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"status"} == 200 and $self->{"generate_last_modified"}) 2 2 4
Condition66%2/3
$self->{"status"} == 200 and $self->{"generate_last_modified"}
!ll&&!rl&&r
202
MC/DC50%1/2
$self->{"status"} == 200 and $self->{"generate_last_modified"}
$self->{"status"} == 200 $self->{"generate_last_modified"}
Truth table66%2/3
A and B
A= $self->{"status"} == 200B= $self->{"generate_last_modified"}
ABresult
0X0
100
111
562 2 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 2 0 2
563 2 $self->{logger}->debug('Set Last-Modified to ', HTTP::Date::time2str($self->{cobject}->created_at()));
564 }
565 2 push @{$self->{o}}, 'Last-Modified: ' . HTTP::Date::time2str($self->{cobject}->created_at());
566 }
567 }
568 } else {
569 # Not in the server-side cache
570 3 ▶ if($self->{status} == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"status"} == 200) 2 1 3
571 2 my $changes = $self->_save_to($unzipped_body, $dbh);
572  
573 2 ▶ unless($self->{cache_duration}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cache_duration"}) 2 0 2
574 # It would be great if CHI::set()
575 # allowed the time to be 'lru' for least
576 # recently used.
577 2 $self->{cache_duration} = '10 minutes';
578 }
579 2 $cache_hash->{'body'} = $unzipped_body;
580 2 ▶ if($changes && $encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes and $encoding) 0 2 2
Condition33%1/3
$changes and $encoding
!ll&&!rl&&r
200
MC/DC0%0/2
$changes and $encoding
$changes $encoding
Truth table33%1/3
A and B
A= $changesB= $encoding
ABresult
0X0
100
111
581 0 $self->_compress({ encoding => $encoding });
582 }
583 2 ▶ if($self->{o} && scalar(@{$self->{o}})) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'o'} and scalar @{$self->{'o'};}) { } 0 2 2
elsif (defined $headers and length $headers) { } 2 0 2
Condition50%1/2
$self->{"o"} and scalar @{$self->{"o"};}
!ll
20
MC/DC0%0/1
$self->{"o"} and scalar @{$self->{"o"};}
$self->{"o"}
Truth table50%1/2
A
A= $self->{"o"}
Aresult
00
11
584 # Remember, we're storing the UNzipped
585 # version in the cache
586 0 my $c;
587 0 ▶ if(defined($headers) && length($headers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers and length $headers) { } 0 0 0
Condition0%0/3
defined $headers and length $headers
!ll&&!rl&&r
000
MC/DC0%0/2
defined $headers and length $headers
defined $headers length $headers
Truth table0%0/3
A and B
A= defined $headersB= length $headers
ABresult
0X0
100
111
588 0 $c = "$headers\r\n" . join("\r\n", @{$self->{o}});
589 } else {
590 0 $c = join("\r\n", @{$self->{o}});
591 }
592 0 $c =~ s/^Content-Encoding: .+$//mg;
593 0 $c =~ s/^Vary: Accept-Encoding.*\r?$//mg;
594 0 $c =~ s/\n+/\n/gs;
595 0 ▶ if(length($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $c) 0 0 0
596 0 $cache_hash->{'headers'} = $c;
597 }
598 ▶ } elsif(defined($headers) && length($headers)) {
Condition33%1/3
defined $headers and length $headers
!ll&&!rl&&r
002
MC/DC0%0/2
defined $headers and length $headers
defined $headers length $headers
Truth table33%1/3
A and B
A= defined $headersB= length $headers
ABresult
0X0
100
111
599 2 $headers =~ s/^Content-Encoding: .+$//mg;
600 2 $headers =~ s/^Vary: Accept-Encoding.*\r?$//mg;
601 2 $headers =~ s/\n+/\n/gs;
602 2 ▶ if(length($headers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $headers) 2 0 2
603 2 $cache_hash->{'headers'} = $headers;
604 }
605 }
606 2 ▶ if($self->{generate_etag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"generate_etag"}) 2 0 2
607 2 ▶ if(!defined($self->{etag})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"etag"}) 0 2 2
608 0 $self->_generate_etag();
609 }
610 2 $cache_hash->{'etag'} = $self->{etag};
611 }
612 # TODO: Support the Expires header
613 # if($headers !~ /^Expires: /m))) {
614 # }
615 2 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 1 1 2
616 1 $self->{logger}->debug("Store $key in the cache, age = ", $self->{cache_duration}, ' ', length($cache_hash->{'body'}), ' bytes');
617 }
618 2 $self->{cache}->set($key, Storable::freeze($cache_hash), $self->{cache_duration});
619  
620 # Create a static page with the information and link to that in the output
621 # HTML
622 2 ▶ if($dbh && $self->{info} && $self->{save_to} && (my $request_uri = $ENV{'REQUEST_URI'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and $self->{"info"} and $self->{"save_to"} and my $request_uri = $ENV{"REQUEST_URI"}) 0 2 2
Condition33%3/9
$dbh and $self->{"info"} and $self->{"save_to"} and my $request_uri = $ENV{"REQUEST_URI"}
!ll&&!rl&&r
200
$dbh and $self->{"info"} and $self->{"save_to"}
!ll&&!rl&&r
200
$dbh and $self->{"info"}
!ll&&!rl&&r
200
MC/DC0%0/4
$dbh and $self->{"info"} and $self->{"save_to"} and my $request_uri = $ENV{"REQUEST_URI"}
$dbh $self->{"info"} $self->{"save_to"} my $request_uri = $ENV{"REQUEST_URI"}
Truth table20%1/5
A and B and C and D
A= $dbhB= $self->{"info"}C= $self->{"save_to"}D= my $request_uri = $ENV{"REQUEST_URI"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
623 0 my $query = "SELECT DISTINCT creation FROM fcgi_buffer WHERE key = ?";
624 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
625 0 $self->{logger}->debug("$query: $key");
626 }
627 0 my $sth = $dbh->prepare($query);
628 0 $sth->execute($key);
629 0 ▶ if(my $href = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $href = $sth->fetchrow_hashref) { } 0 0 0
630 0 ▶ if(my $ttl = $self->{save_to}->{ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ttl = $self->{"save_to"}{"ttl"}) 0 0 0
631 0 push @{$self->{o}}, 'Expires: ' .
632 0 HTTP::Date::time2str($href->{'creation'} + $ttl);
633 }
634 } else {
635 0 my $dir = $self->{save_to}->{directory};
636 0 my $browser_type = $self->{info}->browser_type();
637 0 my $language;
638 0 ▶ if($self->{'lingua'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'lingua'}) { } 0 0 0
639 0 $language = $self->{lingua}->language();
640 0 ▶ if($language =~ /([\w\s]+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language =~ /([\w\s]+)/i) 0 0 0
641 0 $language = $1; # Untaint
642 }
643 } else {
644 0 $language = 'default';
645 }
646 0 my $bdir = File::Spec->catfile($dir, $browser_type);
647 0 ▶ if($bdir =~ /^([\/\\])(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bdir =~ m(^([/\\])(.+)$)) 0 0 0
648 0 $bdir = "$1$2"; # Untaint
649 }
650 0 my $ldir = File::Spec->catfile($bdir, $language);
651 0 my $sdir = File::Spec->catfile($ldir, $self->{info}->script_name());
652 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
653 0 $self->{logger}->debug("Create paths to $sdir");
654 }
655 0 File::Path::make_path($sdir);
656 0 my $file = $self->{'info'}->as_string();
657 0 $file =~ tr/\//_/;
658 0 $file =~ s/\s//g;
659 0 my $path = File::Spec->catfile($sdir, "$file.html");
660 0 ▶ if($path =~ /^(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /^(.+)$/) 0 0 0
661 0 $path = $1; # Untaint
662 0 $path =~ tr/[\|;]/_/;
663 }
664 0 ▶ if(open(my $fout, '>', $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fout, '>', $path) { } 0 0 0
665 0 my $u = $request_uri;
666 0 $u =~ s/\?/\\?/g;
667 0 $u =~ s/\)/\\)/g;
668 0 my $copy = $unzipped_body;
669 0 my $changes = ($copy =~ s/<a\s+href="$u"/<a href="$path"/gi);
670  
671 # handle <a href="?arg3=4">Call self with different args</a>
672 0 my $script_name = $ENV{'SCRIPT_NAME'};
673 0 $copy =~ s/<a\s+href="(\?.+?)"/<a href="$script_name$1"/gi;
674  
675 # Avoid Wide character
676 0 ▶ unless($self->{_encode_loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_encode_loaded"}) 0 0 0
677 0 require Encode;
678 0 $self->{_encode_loaded} = 1;
679 }
680 0 print $fout Encode::encode_utf8($copy);
681 0 close $fout;
682 # Do INSERT OR REPLACE in case another program has
683 # got in first,
684 0 $query = "INSERT OR REPLACE INTO fcgi_buffer(key, language, browser_type, path, uri, creation) VALUES('$key', '$language', '$browser_type', '$path', '$request_uri', strftime('\%s','now'))";
685 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
686 0 $self->{logger}->debug($query);
687 }
688 0 $dbh->prepare($query)->execute();
689  
690 0 ▶ if($changes && (my $ttl = $self->{save_to}->{ttl})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes and my $ttl = $self->{"save_to"}{"ttl"}) 0 0 0
Condition0%0/3
$changes and my $ttl = $self->{"save_to"}{"ttl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$changes and my $ttl = $self->{"save_to"}{"ttl"}
$changes my $ttl = $self->{"save_to"}{"ttl"}
Truth table0%0/3
A and B
A= $changesB= my $ttl = $self->{"save_to"}{"ttl"}
ABresult
0X0
100
111
691 0 push @{$self->{o}}, 'Expires: ' . HTTP::Date::time2str(time + $ttl);
692 }
693 } else {
694 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
695 0 $self->{logger}->warn("Can't create $path");
696 }
697 }
698 }
699 }
700 2 ▶ if($self->{generate_last_modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"generate_last_modified"}) 2 0 2
701 2 $self->{cobject} = $self->{cache}->get_object($key);
702 2 ▶ if(defined($self->{cobject})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cobject'}) { } 2 0 2
703 2 push @{$self->{o}}, 'Last-Modified: ' . HTTP::Date::time2str($self->{cobject}->created_at());
704 } else {
705 0 push @{$self->{o}}, 'Last-Modified: ' . HTTP::Date::time2str(time);
706 }
707 }
708 }
709 3 ▶ if($self->{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'info'}) { } 0 3 3
710 0 my $host_name = $self->{info}->host_name();
711 0 ▶ if(defined($self->{x_cache})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'x_cache'}) { } 0 0 0
712 0 push @{$self->{o}}, 'X-Cache: ' . $self->{x_cache} . " from $host_name";
713 } else {
714 0 push @{$self->{o}}, "X-Cache: MISS from $host_name";
715 }
716 0 push @{$self->{o}}, "X-Cache-Lookup: MISS from $host_name";
717 } else {
718 3 ▶ if(defined($self->{x_cache})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'x_cache'}) { } 3 0 3
719 3 push @{$self->{o}}, 'X-Cache: ' . $self->{x_cache};
720 } else {
721 0 push @{$self->{o}}, 'X-Cache: MISS';
722 }
723 3 push @{$self->{o}}, 'X-Cache-Lookup: MISS';
724 }
725 3 push @{$self->{o}}, "X-FCGI-Buffer-$VERSION: Miss";
726 }
727 # We don't need it any more, so give Perl a chance to
728 # tidy it up seeing as we're in the destructor
729 7 delete $self->{cache};
730 } elsif($self->{info}) {
731 26 my $host_name = $self->{info}->host_name();
732 26 push @{$self->{o}}, ("X-Cache: MISS from $host_name", "X-Cache-Lookup: MISS from $host_name");
733 26 ▶ if($self->{generate_last_modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"generate_last_modified"}) 26 0 26
734 26 ▶ if(my $age = $self->_my_age()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $age = $self->_my_age) 8 18 26
735 8 push @{$self->{o}}, 'Last-Modified: ' . HTTP::Date::time2str($age);
736 }
737 }
738 26 ▶ if($ENV{'HTTP_IF_MODIFIED_SINCE'} && ($self->{status} != 304) && $self->{generate_304}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304 and $self->{"generate_304"}) 3 23 26
Condition66%4/6
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304 and $self->{"generate_304"}
!ll&&!rl&&r
2303
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304
!ll&&!rl&&r
2303
MC/DC33%1/3
$ENV{"HTTP_IF_MODIFIED_SINCE"} and $self->{"status"} != 304 and $self->{"generate_304"}
$ENV{"HTTP_IF_MODIFIED_SINCE"} $self->{"status"} != 304 $self->{"generate_304"}
Truth table50%2/4
A and B and C
A= $ENV{"HTTP_IF_MODIFIED_SINCE"}B= $self->{"status"} != 304C= $self->{"generate_304"}
ABCresult
0XX0
10X0
1100
1111
739 $self->_check_modified_since({
740 3 since => $ENV{'HTTP_IF_MODIFIED_SINCE'},
741 modified => $self->_my_age()
742 });
743 }
744 26 ▶ if($self->_save_to($unzipped_body, $dbh) && $encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_save_to($unzipped_body, $dbh) and $encoding) 0 26 26
Condition33%1/3
$self->_save_to($unzipped_body, $dbh) and $encoding
!ll&&!rl&&r
2600
MC/DC0%0/2
$self->_save_to($unzipped_body, $dbh) and $encoding
$self->_save_to($unzipped_body, $dbh) $encoding
Truth table33%1/3
A and B
A= $self->_save_to($unzipped_body, $dbh)B= $encoding
ABresult
0X0
100
111
745 0 $self->_compress({ encoding => $encoding });
746 }
747 } else {
748 10 push @{$self->{o}}, ('X-Cache: MISS', 'X-Cache-Lookup: MISS');
749 }
750  
751 # if($self->{generate_etag} && ((!defined($headers)) || ($headers !~ /^ETag: /m))) {
752 # if((!defined($self->{etag})) &&
753 # (($self->{status} == 200) || $self->{status} == 304) &&
754 # $self->{body} && (!$ENV{'NO_CACHE'}) &&
755 # !$self->is_cached()) {
756 # unless($self->{_encode_loaded}) {
757 # require Encode;
758 # $self->{_encode_loaded} = 1;
759 # }
760 # $self->{etag} = '"' . Digest::MD5->new->add(Encode::encode_utf8($unzipped_body))->hexdigest() . '"';
761 # }
762 # if(defined($self->{etag})) {
763 # push @{$self->{o}}, "ETag: $self->{etag}";
764 # if($self->{logger}) {
765 # $self->{logger}->debug("Set ETag to $self->{etag}");
766 # }
767 # } else {
768 # open(my $fout, '>>', '/tmp/FCGI-bug');
769 # print $fout "BUG: ETag not generated, status $self->{status}:\n",
770 # "$headers\n",
771 # 'x' x 40, "\n",
772 # # defined($self->{body}) ? $self->{body} : "body is empty\n",
773 # defined($unzipped_body) ? "$unzipped_body\n" : "body is empty\n",
774 # 'x' x 40,
775 # "\n";
776 # print $fout "ENV:\n";
777 # while(my ($key, $value) = each %ENV) {
778 # print $fout "$key = $value\n";
779 # }
780 # close $fout;
781 # $self->{logger}->warn("BUG: ETag not generated, status $self->{status}");
782 # }
783 # }
784 43 ▶ if($self->{generate_etag} && ((!defined($headers)) || ($headers !~ /^ETag: /m))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"generate_etag"} and not defined $headers && $headers =~ /^ETag: /m) 18 25 43
Condition50%3/6
$self->{"generate_etag"} and not defined $headers && $headers =~ /^ETag: /m
!ll&&!rl&&r
25018
defined $headers && $headers =~ /^ETag: /m
!ll&&!rl&&r
0180
MC/DC33%1/3
$self->{"generate_etag"} and not defined $headers && $headers =~ /^ETag: /m
$self->{"generate_etag"} defined $headers $headers =~ /^ETag: /m
Truth table50%2/4
A and not(B && C)
A= $self->{"generate_etag"}B= defined $headersC= $headers =~ /^ETag: /m
ABCresult
0XX0
1110
10X1
1101
785 18 ▶ if(defined($self->{etag})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'etag'}) { } 17 1 18
elsif ($self->{'logger'} and $self->{'status'} == 200 || $self->{'status'} == 304 and $self->{'body'} and not $ENV{'NO_CACHE'} and not $self->is_cached) { } 0 2 2
786 17 push @{$self->{o}}, "ETag: $self->{etag}";
787 17 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 4 13 17
788 4 $self->{logger}->debug("Set ETag to $self->{etag}");
789 }
790 ▶ } elsif($self->{logger} && (($self->{status} == 200) || $self->{status} == 304) && $self->{body} && (!$ENV{'NO_CACHE'}) && !$self->is_cached()) {
Condition26%4/15
$self->{"logger"} and $self->{"status"} == 200 || $self->{"status"} == 304 and $self->{"body"} and not $ENV{"NO_CACHE"} and not $self->is_cached
!ll&&!rl&&r
100
$self->{"logger"} and $self->{"status"} == 200 || $self->{"status"} == 304 and $self->{"body"} and not $ENV{"NO_CACHE"}
!ll&&!rl&&r
100
$self->{"logger"} and $self->{"status"} == 200 || $self->{"status"} == 304 and $self->{"body"}
!ll&&!rl&&r
100
$self->{"logger"} and $self->{"status"} == 200 || $self->{"status"} == 304
!ll&&!rl&&r
100
$self->{"status"} == 200 or $self->{"status"} == 304
l!l&&r!l&&!r
000
MC/DC0%0/6
$self->{"logger"} and $self->{"status"} == 200 || $self->{"status"} == 304 and $self->{"body"} and not $ENV{"NO_CACHE"} and not $self->is_cached
$self->{"logger"} $self->{"status"} == 200 $self->{"status"} == 304 $self->{"body"} not $ENV{"NO_CACHE"} not $self->is_cached
Truth table10%1/10
A and B or C and D and not(E) and not(F)
A= $self->{"logger"}B= $self->{"status"} == 200C= $self->{"status"} == 304D= $self->{"body"}E= not $ENV{"NO_CACHE"}F= not $self->is_cached
ABCDEFresult
0XXXXX0
100XXX0
11X0XX0
1010XX0
11X10X0
10110X0
11X1100
1011100
11X1111
1011111
791 # $self->{logger}->warn("BUG: ETag not generated, status $self->{status}");
792 # open(my $fout, '>>', '/tmp/FCGI-bug');
793 # print $fout "BUG: ETag not generated, status $self->{status}:\n",
794 # $headers,
795 # 'x' x 40,
796 # defined($self->{body}) ? $self->{body} : "body is empty\n",
797 # 'x' x 40,
798 # "\n";
799 # print $fout "ENV:\n";
800 # while(my ($key, $value) = each %ENV) {
801 # print $fout "$key = $value\n;
802 # }
803 # print $fout 'x' x 40, "\n";
804 # close $fout;
805 # $self->{logger}->warn("BUG: ETag not generated, status $self->{status}");
806 }
807 }
808  
809 43 my $body_length;
810 43 ▶ if(defined($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'body'}) { } 38 5 43
811 38 ▶ if(utf8::is_utf8($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($self->{"body"})) 1 37 38
812 1 utf8::encode($self->{body});
813 }
814 38 $body_length = length($self->{body});
815 } else {
816 5 $body_length = 0;
817 }
818  
819 43 ▶ if(defined($headers) && length($headers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers and length $headers) { } 42 1 43
Condition66%2/3
defined $headers and length $headers
!ll&&!rl&&r
0142
MC/DC50%1/2
defined $headers and length $headers
defined $headers length $headers
Truth table66%2/3
A and B
A= defined $headersB= length $headers
ABresult
0X0
100
111
820 # Put the original headers first, then those generated within
821 # FCGI::Buffer
822 42 unshift @{$self->{o}}, split(/\r\n/, $headers);
823 42 ▶ if($self->{body} && $self->{send_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"body"} and $self->{"send_body"}) 34 8 42
Condition100%3/3
$self->{"body"} and $self->{"send_body"}
!ll&&!rl&&r
5334
MC/DC100%2/2
$self->{"body"} and $self->{"send_body"}
$self->{"body"} $self->{"send_body"}
Truth table100%3/3
A and B
A= $self->{"body"}B= $self->{"send_body"}
ABresult
0X0
100
111
824 34 ▶ unless(grep(/^Content-Length: \d/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Content-Length: \d/, @{$$self{"o"};}) 34 0 34
825 34 push @{$self->{o}}, "Content-Length: $body_length";
826 }
827 }
828 42 ▶ unless(grep(/^Status: \d/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Status: \d/, @{$$self{"o"};}) 35 7 42
829 35 require HTTP::Status;
830 35 HTTP::Status->import();
831  
832 35 push @{$self->{o}}, 'Status: ' . $self->{status} . ' ' . HTTP::Status::status_message($self->{status});
833 35 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 26 9 35
834 26 $self->{'info'}->status($self->{'status'});
835 }
836 }
837 } else {
838 1 push @{$self->{o}}, "X-FCGI-Buffer-$VERSION: No headers";
839 }
840  
841 43 ▶ if($body_length && $self->{send_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_length and $self->{"send_body"}) 34 9 43
Condition100%3/3
$body_length and $self->{"send_body"}
!ll&&!rl&&r
6334
MC/DC100%2/2
$body_length and $self->{"send_body"}
$body_length $self->{"send_body"}
Truth table100%3/3
A and B
A= $body_lengthB= $self->{"send_body"}
ABresult
0X0
100
111
842 34 push @{$self->{o}}, ('', $self->{body});
843 }
844  
845 # XXXXXXXXXXXXXXXXXXXXXXX
846 43 ▶ if(0) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { scalar @{$self->{"o"};} }) 43 0 43
847 # This code helps to debug Wide character prints
848 my $wideCharWarningsIssued = 0;
849 my $widemess;
850 0 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
851 0 $wideCharWarningsIssued += "@_" =~ /Wide character in .../;
852 0 $widemess = "@_";
853 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
854 0 $self->{logger}->fatal($widemess);
855 0 my $i = 1;
856 0 $self->{logger}->trace('Stack Trace');
857 0 ▶ while((my @call_details = (caller($i++)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@call_details) = &CORE::GLOBAL::caller($i++)) 0 0 0
858 0 $self->{logger}->trace($call_details[1], ':', $call_details[2], ' in function ', $call_details[3]);
859 }
860 }
861 0 CORE::warn(@_); # call the builtin warn as usual
862 };
863  
864 if(scalar @{$self->{o}}) {
865 print join("\r\n", @{$self->{o}});
866 if($wideCharWarningsIssued) {
867 my $mess = join("\r\n", @{$self->{o}});
868 $mess =~ /[^\x00-\xFF]/;
869 open(my $fout, '>>', '/tmp/NJH');
870 print $fout "$widemess:\n",
871 $mess,
872 'x' x 40,
873 "\n";
874 close $fout;
875 }
876 }
877 0 } elsif(scalar @{$self->{o}}) {
878 43 print join("\r\n", @{$self->{o}});
879 }
880 # XXXXXXXXXXXXXXXXXXXXXXX
881  
882 43 ▶ if((!$self->{send_body}) || !defined($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"send_body"} && defined $self->{"body"}) 8 35 43
Condition100%3/3
$self->{'send_body'} && defined $self->{'body'}
!ll&&!rl&&r
5335
MC/DC100%2/2
$self->{'send_body'} && defined $self->{'body'}
$self->{'send_body'} defined $self->{'body'}
Truth table100%3/3
A && B
A= $self->{'send_body'}B= defined $self->{'body'}
ABresult
0X0
100
111
883 8 print "\r\n\r\n";
884 }
885 }
886  
887 15 ▶sub _generate_etag {
Subroutine _generate_etag: called CC=5 SCAR=16.4
888 15 my $self = shift;
889  
890 15 ▶ return if defined($self->{'etag'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"etag"} 0 15 15
891 15 ▶ return unless defined($self->{'body'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"body"} 0 15 15
892  
893 15 ▶ if(!defined($self->{_encode_loaded})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_encode_loaded"}) 15 0 15
894 # encode to avoid "Wide character in subroutine entry"
895 15 require Encode;
896 15 $self->{_encode_loaded} = 1;
897 }
898 15 $self->{etag} = '"' . Digest::MD5->new->add(Encode::encode_utf8($self->{body}))->hexdigest() . '"';
899 15 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 2 13 15
900 2 $self->{'logger'}->debug('Etag = ', $self->{'etag'});
901 }
902 }
903  
904 7 ▶sub _check_modified_since {
Subroutine _check_modified_since: called CC=14 SCAR=27.8
905 7 my $self = shift;
906  
907 7 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 4 3 7
908 4 $self->{logger}->trace('In _check_modified_since');
909 }
910  
911 7 ▶ if(!$self->{generate_304}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"generate_304"}) 0 7 7
912 0 return;
913 }
914 7 my $params = shift;
915  
916 7 ▶ if(!defined($$params{since})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"since"}) 0 7 7
917 0 return;
918 }
919 7 my $s = HTTP::Date::str2time($$params{since});
920 7 ▶ if(!defined($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s) 1 6 7
921 1 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 1 1
922 0 $self->{logger}->info("$$params{since} is not a valid date");
923 }
924 1 return;
925 }
926  
927 6 my $age = $self->_my_age();
928 6 ▶ if(!defined($age)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $age) 2 4 6
929 2 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 2 2
930 0 $self->{logger}->info("Can't determine my age");
931 }
932 2 return;
933 }
934 4 ▶ if($age > $s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age > $s) 2 2 4
935 2 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 2 0 2
936 2 $self->{logger}->debug('_check_modified_since: script has been modified');
937 }
938 # Script has been updated so it may produce different output
939 2 return;
940 }
941  
942 2 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 2 0 2
943 2 $self->{logger}->debug("_check_modified_since: Compare $$params{modified} with $s");
944 }
945 2 ▶ if($$params{modified} <= $s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"modified"} <= $s) 2 0 2
946 2 push @{$self->{o}}, 'Status: 304 Not Modified';
947 2 $self->{status} = 304;
948 2 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 2 0 2
949 2 $self->{'info'}->status(304);
950 }
951 2 $self->{send_body} = 0;
952 2 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 2 0 2
953 2 $self->{logger}->debug('Set status to 304');
954 }
955 }
956 }
957  
958 # Reduce output, e.g. remove superfluous white-space.
959 27 ▶sub _optimise_content {
Subroutine _optimise_content: called CC=1 SCAR=0.0
960 27 my $self = shift;
961  
962 # FIXME: regex bad, HTML parser good
963 # Regexp::List - wow!
964 27 $self->{body} =~ s/(((\s+|\r)\n|\n(\s+|\+)))/\n/g;
965 # $self->{body} =~ s/\r\n/\n/gs;
966 # $self->{body} =~ s/\s+\n/\n/gs;
967 # $self->{body} =~ s/\n+/\n/gs;
968 # $self->{body} =~ s/\n\s+|\s+\n/\n/g;
969 27 $self->{body} =~ s/\<\/div\>\s+\<div/\<\/div\>\<div/gis;
970 # $self->{body} =~ s/\<\/p\>\s\<\/div/\<\/p\>\<\/div/gis;
971 # $self->{body} =~ s/\<div\>\s+/\<div\>/gis; # Remove spaces after <div>
972 27 $self->{body} =~ s/(<div>\s+|\s+<div>)/<div>/gis;
973 27 $self->{body} =~ s/\s+<\/div\>/\<\/div\>/gis; # Remove spaces before </div>
974 27 $self->{body} =~ s/\s+\<p\>|\<p\>\s+/\<p\>/im; # TODO <p class=
975 27 $self->{body} =~ s/\s+\<\/p\>|\<\/p\>\s+/\<\/p\>/gis;
976 27 $self->{body} =~ s/<html>\s+<head>/<html><head>/is;
977 27 $self->{body} =~ s/\s*<\/head>\s+<body>\s*/<\/head><body>/is;
978 27 $self->{body} =~ s/<html>\s+<body>/<html><body>/is;
979 27 $self->{body} =~ s/<body>\s+/<body>/is;
980 27 $self->{body} =~ s/\s+\<\/html/\<\/html/is;
981 27 $self->{body} =~ s/\s+\<\/body/\<\/body/is;
982 27 $self->{body} =~ s/\s(\<.+?\>\s\<.+?\>)/$1/;
983 # $self->{body} =~ s/(\<.+?\>\s\<.+?\>)\s/$1/g;
984 27 $self->{body} =~ s/\<p\>\s/\<p\>/gi;
985 27 $self->{body} =~ s/\<\/p\>\s\<p\>/\<\/p\>\<p\>/gi;
986 27 $self->{body} =~ s/\<\/tr\>\s\<tr\>/\<\/tr\>\<tr\>/gi;
987 27 $self->{body} =~ s/\<\/td\>\s\<\/tr\>/\<\/td\>\<\/tr\>/gi;
988 27 $self->{body} =~ s/\<\/td\>\s*\<td\>/\<\/td\>\<td\>/gis;
989 27 $self->{body} =~ s/\<\/tr\>\s\<\/table\>/\<\/tr\>\<\/table\>/gi;
990 27 $self->{body} =~ s/\<br\s?\/?\>\s?\<p\>/\<p\>/gi;
991 27 $self->{body} =~ s/\<br\>\s+/\<br\>/gi;
992 27 $self->{body} =~ s/\s+\<br/\<br/gi;
993 27 $self->{body} =~ s/\<br\s?\/\>\s/\<br \/\>/gi;
994 27 $self->{body} =~ s/[ \t]+/ /gs; # Remove duplicate space, don't use \s+ it breaks JavaScript
995 27 $self->{body} =~ s/\s\<p\>/\<p\>/gi;
996 27 $self->{body} =~ s/\s\<script/\<script/gi;
997 27 $self->{body} =~ s/(<script>\s|\s<script>)/<script>/gis;
998 27 $self->{body} =~ s/(<\/script>\s|\s<\/script>)/<\/script>/gis;
999 27 $self->{body} =~ s/\<td\>\s/\<td\>/gi;
1000 27 $self->{body} =~ s/\s+\<a\shref="(.+?)"\>\s?/ <a href="$1">/gis;
1001 27 $self->{body} =~ s/\s?<a\shref=\s"(.+?)"\>/ <a href="$1">/gis;
1002 27 $self->{body} =~ s/\s+<\/a\>\s+/<\/a> /gis;
1003 27 $self->{body} =~ s/(\s?<hr>\s+|\s+<hr>\s?)/<hr>/gis;
1004 # $self->{body} =~ s/\s<hr>/<hr>/gis;
1005 # $self->{body} =~ s/<hr>\s/<hr>/gis;
1006 27 $self->{body} =~ s/<\/li>\s+<li>/<\/li><li>/gis;
1007 27 $self->{body} =~ s/<\/li>\s+<\/ul>/<\/li><\/ul>/gis;
1008 27 $self->{body} =~ s/<ul>\s+<li>/<ul><li>/gis;
1009 27 $self->{body} =~ s/\s+<\/li>/<\/li>/gis;
1010 27 $self->{body} =~ s/\<\/option\>\s+\<option/\<\/option\>\<option/gis;
1011 27 $self->{body} =~ s/<title>\s*(.+?)\s*<\/title>/<title>$1<\/title>/is;
1012 27 $self->{body} =~ s/<\/center>\s+<center>/ /gis;
1013 }
1014  
1015 # Create a key for the cache
1016 24 ▶sub _generate_key {
Subroutine _generate_key: called CC=15 SCAR=52.7
1017 24 my $self = shift;
1018 24 ▶ if($self->{cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_key"}) 24 0 24
1019 24 return $self->{cache_key};
1020 }
1021 0 ▶ unless(defined($self->{info})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"info"}) 0 0 0
1022 0 $self->{info} = CGI::Info->new({ cache => $self->{cache} });
1023 }
1024  
1025 0 my $key = __PACKAGE__ . '::' . $self->{info}->browser_type() . '::' . $self->{info}->domain_name() . '::' . $self->{info}->script_name() . '::' . $self->{info}->as_string();
1026  
1027 0 ▶ if($self->{lingua}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lingua"}) 0 0 0
1028 0 $key .= '::' . $self->{lingua}->language();
1029 }
1030 0 ▶ if($ENV{'HTTP_COOKIE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_COOKIE"}) 0 0 0
1031 # Different states of the client are stored in different caches
1032 # Don't put different Google Analytics in different caches, and anyway they
1033 # would be wrong
1034 0 foreach my $cookie(split(/;/, $ENV{'HTTP_COOKIE'})) {
1035 0 ▶ unless($cookie =~ /^__utm[abcz]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cookie =~ /^__utm[abcz]/) 0 0 0
1036 0 $key .= "::$cookie";
1037 }
1038 }
1039 }
1040  
1041 # Honour the Vary headers
1042 0 my $headers = $self->{'headers'};
1043 0 ▶ if($headers && ($headers =~ /^Vary: .*$/m)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers and $headers =~ /^Vary: .*$/m) 0 0 0
Condition0%0/3
$headers and $headers =~ /^Vary: .*$/m
!ll&&!rl&&r
000
MC/DC0%0/2
$headers and $headers =~ /^Vary: .*$/m
$headers $headers =~ /^Vary: .*$/m
Truth table0%0/3
A and B
A= $headersB= $headers =~ /^Vary: .*$/m
ABresult
0X0
100
111
1044 0 ▶ if(defined($self->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logger"}) 0 0 0
1045 0 $self->{logger}->debug('Found Vary header');
1046 }
1047 0 foreach my $h1(split(/\r?\n/, $headers)) {
1048 0 my ($h1_name, $h1_value) = split /\:\s*/, $h1, 2;
1049 0 ▶ if(lc($h1_name) eq 'vary') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h1_name eq "vary") 0 0 0
1050 0 foreach my $h2(split(/\r?\n/, $headers)) {
1051 0 my ($h2_name, $h2_value) = split /\:\s*/, $h2, 2;
1052 0 ▶ if($h2_name eq $h1_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h2_name eq $h1_value) 0 0 0
1053 0 $key .= "::$h2_value";
1054 0 last;
1055 }
1056 }
1057 }
1058 }
1059 }
1060 0 $key =~ s/\//::/g;
1061 0 $key =~ s/::::/::/g;
1062 0 $key =~ s/::$//;
1063 0 ▶ if(defined($self->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logger"}) 0 0 0
1064 0 $self->{logger}->trace("Returning $key");
1065 }
1066 0 $self->{cache_key} = $key;
1067 0 return $key;
1068 }
1069  
1070 =head2 init
1071  
1072 Set various options and override default values.
1073  
1074 # Put this toward the top of your program before you do anything
1075 # By default, generate_tag, generate_304 and compress_content are ON,
1076 # optimise_content and lint_content are OFF. Set optimise_content to 2 to
1077 # do aggressive JavaScript optimisations which may fail.
1078 use FCGI::Buffer;
1079  
1080 my $buffer = FCGI::Buffer->new()->init({
1081 generate_etag => 1, # make good use of client's cache
1082 generate_last_modified => 1, # more use of client's cache
1083 compress_content => 1, # if gzip the output
1084 optimise_content => 0, # optimise your program's HTML, CSS and JavaScript
1085 cache => CHI->new(driver => 'File'), # cache requests
1086 cache_key => 'string', # key for the cache
1087 cache_duration => '10 minutes', # how long to store responses in the cache
1088 logger => $self->{logger},
1089 lint_content => 0, # Pass through HTML::Lint
1090 generate_304 => 1, # When appropriate, generate 304: Not modified
1091 save_to => { directory => '/var/www/htdocs/save_to', ttl => 600, create_table => 1 },
1092 info => CGI::Info->new(),
1093 lingua => CGI::Lingua->new(),
1094 });
1095  
1096 If no cache_key is given, one will be generated which may not be unique.
1097 The cache_key should be a unique value dependent upon the values set by the
1098 browser.
1099  
1100 The cache object will be an object that understands get_object(),
1101 set(), remove() and created_at() messages, such as an L<CHI> object. It is
1102 used as a server-side cache to reduce the need to rerun database accesses.
1103  
1104 Items stay in the server-side cache by default for 10 minutes.
1105 This can be overridden by the cache_control HTTP header in the request, and
1106 the default can be changed by the cache_duration argument to init().
1107  
1108 Save_to is feature which stores output of dynamic pages to your
1109 htdocs tree and replaces future links that point to that page with static links
1110 to avoid going through CGI at all.
1111 Ttl is set to the number of seconds that the static pages are deemed to
1112 be live for, the default is 10 minutes.
1113 If set to 0, the page is live forever.
1114 To enable save_to, a info and lingua arguments must also be given.
1115 It works best when cache is also given.
1116 Only use where output is guaranteed to be the same with a given set of arguments
1117 (the same criteria for enabling generate_304).
1118 You can turn it off on a case by case basis thus:
1119  
1120 my $params = CGI::Info->new()->params();
1121 if($params->{'send_private_email'}) {
1122 $buffer->init('save_to' => undef);
1123 }
1124  
1125 Info is an optional argument to give information about the FCGI environment, e.g.
1126 a L<CGI::Info> object.
1127  
1128 Logger will be an object that understands debug() such as an L<Log::Log4perl>
1129 object.
1130  
1131 To generate a last_modified header, you must give a cache object.
1132  
1133 Init allows a reference of the options to be passed. So both of these work:
1134 use FCGI::Buffer;
1135 #...
1136 my $buffer = FCGI::Buffer->new();
1137 $b->init(generate_etag => 1);
1138 $b->init({ generate_etag => 1, info => CGI::Info->new() });
1139  
1140 Generally speaking, passing by reference is better since it copies less on to
1141 the stack.
1142  
1143 If you give a cache to init() then later give cache => undef,
1144 the server-side cache is no longer used.
1145 This is useful when you find an error condition when creating your HTML
1146 and decide that you no longer wish to store the output in the cache.
1147  
1148 =cut
1149  
1150 41 ▶sub init {
Subroutine init: called CC=35 SCAR=46.2
Pod documented
1151 41 my $self = shift;
1152 41 ▶ my %params = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 29 12 41
1153  
1154 # Safe options - can be called at any time
1155 41 ▶ if(defined($params{generate_etag})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"generate_etag"}) 9 32 41
1156 9 $self->{generate_etag} = $params{generate_etag};
1157 }
1158 41 ▶ if(defined($params{generate_last_modified})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"generate_last_modified"}) 0 41 41
1159 0 $self->{generate_last_modified} = $params{generate_last_modified};
1160 }
1161 41 ▶ if(defined($params{compress_content})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"compress_content"}) 1 40 41
1162 1 $self->{compress_content} = $params{compress_content};
1163 }
1164 41 ▶ if(defined($params{optimise_content})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"optimise_content"}) 31 10 41
1165 31 $self->{optimise_content} = $params{optimise_content};
1166 }
1167 41 ▶ if(defined($params{lint_content})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"lint_content"}) 4 37 41
1168 4 $self->{lint_content} = $params{lint_content};
1169 }
1170 41 ▶ if(defined($params{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"logger"}) 6 35 41
1171 6 $self->{logger} = $params{logger};
1172 }
1173 41 ▶ if(defined($params{lingua})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"lingua"}) 0 41 41
1174 0 $self->{lingua} = $params{lingua};
1175 }
1176  
1177 41 ▶ if(defined($params{save_to}) && $self->can_cache()) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params{'save_to'} and $self->can_cache) { } 2 39 41
elsif (exists $params{'save_to'} and not defined $params{'save_to'}) { } 0 78 78
Condition66%2/3
defined $params{"save_to"} and $self->can_cache
!ll&&!rl&&r
3902
MC/DC50%1/2
defined $params{"save_to"} and $self->can_cache
defined $params{"save_to"} $self->can_cache
Truth table66%2/3
A and B
A= defined $params{"save_to"}B= $self->can_cache
ABresult
0X0
100
111
1178 2 ▶ if(my $dir = $params{'save_to'}->{'directory'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = $params{"save_to"}{"directory"}) 2 0 2
1179 2 ▶ if(! -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 1 1 2
1180 1 mkdir $dir;
1181 1 ▶ if(! -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 1 0 1
1182 1 Carp::carp("$dir isn't a directory");
1183 1 return;
1184 }
1185 }
1186 1 ▶ if(! -w $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $dir) 1 0 1
1187 1 Carp::carp("$dir isn't writeable");
1188 1 return;
1189 }
1190 }
1191 0 $self->{save_to} = $params{save_to};
1192 0 ▶ if(!exists($params{save_to}->{'ttl'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"save_to"}{"ttl"}) 0 0 0
1193 0 $self->{save_to}->{'ttl'} = 600;
1194 }
1195 ▶ } elsif(exists($params{'save_to'}) && !defined($params{'save_to'})) {
Condition33%1/3
exists $params{"save_to"} and not defined $params{"save_to"}
!ll&&!rl&&r
3900
MC/DC0%0/2
exists $params{"save_to"} and not defined $params{"save_to"}
exists $params{"save_to"} not defined $params{"save_to"}
Truth table33%1/3
A and not(B)
A= exists $params{"save_to"}B= not defined $params{"save_to"}
ABresult
0X0
100
111
1196 0 delete $self->{'save_to'};
1197 }
1198 39 ▶ if(defined($params{generate_304})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"generate_304"}) 3 36 39
1199 3 $self->{generate_304} = $params{generate_304};
1200 }
1201 39 ▶ if(defined($params{info}) && (!defined($self->{info}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"info"} and not defined $self->{"info"}) 4 35 39
Condition66%2/3
defined $params{"info"} and not defined $self->{"info"}
!ll&&!rl&&r
3504
MC/DC50%1/2
defined $params{"info"} and not defined $self->{"info"}
defined $params{"info"} not defined $self->{"info"}
Truth table66%2/3
A and not(B)
A= defined $params{"info"}B= not defined $self->{"info"}
ABresult
0X0
100
111
1202 4 $self->{info} = $params{info};
1203 }
1204  
1205 # Unsafe options - must be called before output has been started
1206 39 my $pos = $self->{buf}->getpos;
1207 39 ▶ if($pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 0) 1 38 39
1208 1 ▶ if(defined($self->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'logger'}) { } 0 1 1
1209 0 my @call_details = caller(0);
1210 0 $self->{logger}->warn("Too late to call init, $pos characters have been printed, caller line $call_details[2] of $call_details[1]");
1211 } else {
1212 # Must do Carp::carp instead of carp for Test::Carp
1213 1 Carp::carp "Too late to call init, $pos characters have been printed";
1214 }
1215 }
1216 39 ▶ if(exists($params{cache}) && $self->can_cache()) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"cache"} and $self->can_cache) 8 31 39
Condition66%2/3
exists $params{"cache"} and $self->can_cache
!ll&&!rl&&r
3108
MC/DC50%1/2
exists $params{"cache"} and $self->can_cache
exists $params{"cache"} $self->can_cache
Truth table66%2/3
A and B
A= exists $params{"cache"}B= $self->can_cache
ABresult
0X0
100
111
1217 8 ▶ if(defined($ENV{'HTTP_CACHE_CONTROL'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HTTP_CACHE_CONTROL"}) 0 8 8
1218 0 my $control = $ENV{'HTTP_CACHE_CONTROL'};
1219 0 ▶ if(defined($self->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logger"}) 0 0 0
1220 0 $self->{logger}->debug("cache_control = $control");
1221 }
1222 0 ▶ if($control =~ /^max-age\s*=\s*(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($control =~ /^max-age\s*=\s*(\d+)$/) 0 0 0
1223 # There is an argument not to do this
1224 # since one client will affect others
1225 0 $self->{cache_duration} = "$1 seconds";
1226 0 ▶ if(defined($self->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logger"}) 0 0 0
1227 0 $self->{logger}->debug("cache_duration = $self->{cache_duration}");
1228 }
1229 }
1230 }
1231 8 ▶ $self->{cache_duration} ||= $params{cache_duration};
Condition33%1/3
$self->{"cache_duration"} ||= $params{"cache_duration"}
l!l&&r!l&&!r
008
MC/DC0%0/2
$self->{"cache_duration"} ||= $params{"cache_duration"}
$self->{"cache_duration"} $params{"cache_duration"}
Truth table33%1/3
A ||= B
A= $self->{"cache_duration"}B= $params{"cache_duration"}
ABresult
1X1
011
000
1232 8 ▶ if((!defined($params{cache})) && defined($self->{cache})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $params{'cache'} and defined $self->{'cache'}) { } 0 8 8
Condition33%1/3
not defined $params{"cache"} and defined $self->{"cache"}
!ll&&!rl&&r
800
MC/DC0%0/2
not defined $params{"cache"} and defined $self->{"cache"}
not defined $params{"cache"} defined $self->{"cache"}
Truth table33%1/3
not(A) and B
A= not defined $params{"cache"}B= defined $self->{"cache"}
ABresult
0X0
100
111
1233 0 ▶ if(defined($self->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logger"}) 0 0 0
1234 0 ▶ if($self->{cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cache_key'}) { } 0 0 0
1235 0 $self->{logger}->debug('disabling cache ', $self->{cache_key});
1236 } else {
1237 0 $self->{logger}->debug('disabling cache');
1238 }
1239 }
1240 0 delete $self->{cache};
1241 } else {
1242 8 $self->{cache} = $params{cache};
1243 }
1244 8 ▶ if(defined($params{cache_key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"cache_key"}) 8 0 8
1245 8 $self->{cache_key} = $params{cache_key};
1246 }
1247 }
1248  
1249 39 return $self;
1250 }
1251  
1252 10 ▶sub import {
Subroutine import: called CC=2 SCAR=8.1
1253 # my $class = shift;
1254 10 shift;
1255  
1256 10 ▶ return unless scalar(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 10 0 10
1257  
1258 0 init(@_);
1259 }
1260  
1261 =head2 set_options
1262  
1263 Synonym for init, kept for historical reasons.
1264  
1265 =cut
1266  
1267 11 ▶sub set_options {
Subroutine set_options: called CC=2 SCAR=6.9
Pod documented
1268 11 my $self = shift;
1269 11 ▶ my %params = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 8 3 11
1270  
1271 11 $self->init(\%params);
1272 }
1273  
1274 =head2 can_cache
1275  
1276 Returns true if the server is allowed to store the results locally.
1277 This is the value of X-Cache in the returned header.
1278  
1279 =cut
1280  
1281 25 ▶sub can_cache {
Subroutine can_cache: called CC=10 SCAR=37.1
Pod documented
1282 25 my $self = shift;
1283  
1284 25 ▶ if(defined($self->{x_cache})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"x_cache"}) 10 15 25
1285 10 return ($self->{x_cache} eq 'HIT');
1286 }
1287 15 ▶ if(defined($ENV{'NO_CACHE'}) || defined($ENV{'NO_STORE'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"NO_CACHE"} or defined $ENV{"NO_STORE"}) 0 15 15
Condition33%1/3
defined $ENV{"NO_CACHE"} or defined $ENV{"NO_STORE"}
l!l&&r!l&&!r
0015
MC/DC0%0/2
defined $ENV{"NO_CACHE"} or defined $ENV{"NO_STORE"}
defined $ENV{"NO_CACHE"} defined $ENV{"NO_STORE"}
Truth table33%1/3
A or B
A= defined $ENV{"NO_CACHE"}B= defined $ENV{"NO_STORE"}
ABresult
1X1
011
000
1288 0 $self->{x_cache} = 'MISS';
1289 0 return 0;
1290 }
1291 15 ▶ if(defined($ENV{'HTTP_CACHE_CONTROL'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HTTP_CACHE_CONTROL"}) 0 15 15
1292 0 my $control = $ENV{'HTTP_CACHE_CONTROL'};
1293 0 ▶ if(defined($self->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logger"}) 0 0 0
1294 0 $self->{logger}->debug("cache_control = $control");
1295 }
1296 # TODO: check Authorization header not present
1297 0 ▶ if(($control eq 'no-store') ||
Branch: 1 branch
BranchTrueFalseTotal
if ($control eq "no-store" or $control eq "no-cache" or $control eq "max-age=0" or $control eq "private") 0 0 0
Condition0%0/9
$control eq "no-store" or $control eq "no-cache" or $control eq "max-age=0" or $control eq "private"
l!l&&r!l&&!r
000
$control eq "no-store" or $control eq "no-cache" or $control eq "max-age=0"
l!l&&r!l&&!r
000
$control eq "no-store" or $control eq "no-cache"
l!l&&r!l&&!r
000
MC/DC0%0/4
$control eq "no-store" or $control eq "no-cache" or $control eq "max-age=0" or $control eq "private"
$control eq "no-store" $control eq "no-cache" $control eq "max-age=0" $control eq "private"
Truth table0%0/5
A or B or C or D
A= $control eq "no-store"B= $control eq "no-cache"C= $control eq "max-age=0"D= $control eq "private"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1298 ($control eq 'no-cache') ||
1299 ($control eq 'max-age=0') ||
1300 ($control eq 'private')) {
1301 0 $self->{x_cache} = 'MISS';
1302 0 return 0;
1303 }
1304 }
1305 15 $self->{x_cache} = 'HIT';
1306 15 return 1;
1307 }
1308  
1309 =head2 is_cached
1310  
1311 Returns true if the output is cached. If it is then it means that all of the
1312 expensive routines in the FCGI script can be by-passed because we already have
1313 the result stored in the cache.
1314  
1315 # Put this toward the top of your program before you do anything
1316  
1317 # Example key generation - use whatever you want as something
1318 # unique for this call, so that subsequent calls with the same
1319 # values match something in the cache
1320 use CGI::Info;
1321 use CGI::Lingua;
1322 use FCGI::Buffer;
1323  
1324 my $i = CGI::Info->new();
1325 my $l = CGI::Lingua->new(supported => ['en']);
1326  
1327 # To use server-side caching you must give the cache argument, however
1328 # the cache_key argument is optional - if you don't give one then one will
1329 # be generated for you
1330 my $buffer = FCGI::Buffer->new();
1331 if($buffer->can_cache()) {
1332 $buffer->init(
1333 cache => CHI->new(driver => 'File'),
1334 cache_key => $i->domain_name() . '/' . $i->script_name() . '/' . $i->as_string() . '/' . $l->language()
1335 );
1336 if($buffer->is_cached()) {
1337 # Output will be retrieved from the cache and sent automatically
1338 exit;
1339 }
1340 }
1341 # Not in the cache, so now do our expensive computing to generate the
1342 # results
1343 print "Content-type: text/html\n";
1344 # ...
1345  
1346 =cut
1347  
1348 35 ▶sub is_cached {
Subroutine is_cached: called CC=14 SCAR=35.6
Pod documented
1349 35 my $self = shift;
1350  
1351 35 ▶ unless($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cache"}) 18 17 35
1352 18 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 18 18
1353 0 $self->{logger}->debug("is_cached: cache hasn't been enabled");
1354 0 my $i = 0;
1355 0 ▶ while((my @call_details = (caller($i++)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@call_details) = &CORE::GLOBAL::caller($i++)) 0 0 0
1356 0 $self->{logger}->debug($call_details[1], ':', $call_details[2], ' calling function ', $call_details[3]);
1357 }
1358 }
1359 18 return 0;
1360 }
1361  
1362 17 my $key = $self->_generate_key();
1363  
1364 17 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 14 3 17
1365 14 $self->{logger}->debug("is_cached: looking for key = $key");
1366 }
1367 17 $self->{cobject} = $self->{cache}->get_object($key);
1368 17 ▶ unless($self->{cobject}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cobject"}) 6 11 17
1369 6 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 3 3 6
1370 3 $self->{logger}->debug('not found in cache');
1371 }
1372 6 return 0;
1373 }
1374 11 ▶ unless($self->{cobject}->value($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cobject"}->value($key)) 0 11 11
1375 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1376 0 $self->{logger}->warn('is_cached: object is in the cache but not the data');
1377 }
1378 0 delete $self->{cobject};
1379 0 return 0;
1380 }
1381  
1382 # If the script has changed, don't use the cache since we may produce
1383 # different output
1384 11 my $age = $self->_my_age();
1385 11 ▶ unless(defined($age)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $age) 0 11 11
1386 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1387 0 $self->{logger}->debug("Can't determine script's age");
1388 }
1389 # Can't determine the age. Play it safe an assume we're not
1390 # cached
1391 0 delete $self->{cobject};
1392 0 return 0;
1393 }
1394 11 ▶ if($age > $self->{cobject}->created_at()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age > $self->{"cobject"}->created_at) 0 11 11
1395 # Script has been updated so it may produce different output
1396 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1397 0 $self->{logger}->debug('Script has been updated');
1398 }
1399 0 delete $self->{cobject};
1400 # Nothing will be in date and all new searches would miss
1401 # anyway, so may as well clear it all
1402 # FIXME: RT104471
1403 # $self->{cache}->clear();
1404 0 return 0;
1405 }
1406 11 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 11 0 11
1407 11 $self->{logger}->debug('Script is in the cache');
1408 }
1409 11 return 1;
1410 }
1411  
1412 # Determine the last modification time of the script and cache it for subsequent calls
1413 46 ▶sub _my_age {
Subroutine _my_age: called CC=7 SCAR=21.3
1414 46 my $self = shift;
1415  
1416 46 ▶ if($self->{script_mtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"script_mtime"}) 11 35 46
1417 11 return $self->{script_mtime};
1418 }
1419 35 ▶ unless(defined($self->{info})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"info"}) 4 31 35
1420 4 ▶ if($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cache'}) { } 4 0 4
1421 $self->{info} = CGI::Info->new({ cache => $self->{cache} })
1422 4 ▶ or croak 'Failed to create CGI::Info object with cache';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"info"} = "CGI::Info"->new({"cache", $self->{"cache"}}) or croak("Failed to create CGI::Info object with cache") 4 0 4
1423 } else {
1424 0 ▶ $self->{info} = CGI::Info->new()
Branch: 1 branch
BranchTrueFalseTotal
$self->{"info"} = "CGI::Info"->new or croak("Failed to create CGI::Info object") 0 0 0
1425 or croak 'Failed to create CGI::Info object';
1426 }
1427 }
1428  
1429 35 my $path = $self->{info}->script_path();
1430 35 ▶ unless(defined($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 35 35
1431 0 croak 'Failed to retrieve script path';
1432 0 return;
1433 }
1434  
1435 35 my @statb = stat($path);
1436 35 $self->{'script_mtime'} = $statb[9]; # Set script_mtime to the modification time of the script
1437 35 return $self->{script_mtime};
1438 }
1439  
1440 sub _should_gzip
1441 45 ▶{
Subroutine _should_gzip: called CC=12 SCAR=26.0
1442 45 my $self = shift;
1443  
1444 45 ▶ if($self->{compress_content} && ($ENV{'HTTP_ACCEPT_ENCODING'} || $ENV{'HTTP_TE'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"compress_content"} and $ENV{"HTTP_ACCEPT_ENCODING"} || $ENV{"HTTP_TE"}) 2 43 45
Condition100%6/6
$self->{"compress_content"} and $ENV{"HTTP_ACCEPT_ENCODING"} || $ENV{"HTTP_TE"}
!ll&&!rl&&r
1422
$ENV{"HTTP_ACCEPT_ENCODING"} or $ENV{"HTTP_TE"}
l!l&&r!l&&!r
1142
MC/DC100%3/3
$self->{"compress_content"} and $ENV{"HTTP_ACCEPT_ENCODING"} || $ENV{"HTTP_TE"}
$self->{"compress_content"} $ENV{"HTTP_ACCEPT_ENCODING"} $ENV{"HTTP_TE"}
Truth table100%4/4
A and B or C
A= $self->{"compress_content"}B= $ENV{"HTTP_ACCEPT_ENCODING"}C= $ENV{"HTTP_TE"}
ABCresult
0XX0
1000
11X1
1011
1445 2 ▶ if(defined($self->{content_type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"content_type"}) 2 0 2
1446 2 my @content_type = @{$self->{content_type}};
1447 2 ▶ if($content_type[0] ne 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type[0] ne "text") 0 2 2
1448 0 return '';
1449 }
1450 }
1451 2 ▶ my $accept = lc($ENV{'HTTP_ACCEPT_ENCODING'} ? $ENV{'HTTP_ACCEPT_ENCODING'} : $ENV{'HTTP_TE'});
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'HTTP_ACCEPT_ENCODING'} ? : 1 1 2
1452 2 foreach my $method(split(/,\s?/, $accept)) {
1453 2 ▶ if(($method eq 'gzip') || ($method eq 'x-gzip') || ($method eq 'br') || ($method eq 'zstd')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "gzip" or $method eq "x-gzip" or $method eq "br" or $method eq "zstd") 2 0 2
Condition55%5/9
$method eq "gzip" or $method eq "x-gzip" or $method eq "br" or $method eq "zstd"
l!l&&r!l&&!r
200
$method eq "gzip" or $method eq "x-gzip" or $method eq "br"
l!l&&r!l&&!r
110
$method eq "gzip" or $method eq "x-gzip"
l!l&&r!l&&!r
101
MC/DC0%0/4
$method eq "gzip" or $method eq "x-gzip" or $method eq "br" or $method eq "zstd"
$method eq "gzip" $method eq "x-gzip" $method eq "br" $method eq "zstd"
Truth table40%2/5
A or B or C or D
A= $method eq "gzip"B= $method eq "x-gzip"C= $method eq "br"D= $method eq "zstd"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1454 2 return $method;
1455 }
1456 }
1457 }
1458  
1459 43 return '';
1460 }
1461  
1462 sub _set_content_type
1463 43 ▶{
Subroutine _set_content_type: called CC=3 SCAR=11.0
1464 43 my $self = shift;
1465 43 my $headers = shift;
1466  
1467 43 foreach my $header (split(/\r?\n/, $headers)) {
1468 42 my ($header_name, $header_value) = split /\:\s*/, $header, 2;
1469 42 ▶ if(lc($header_name) eq 'content-type') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header_name eq "content-type") 38 4 42
1470 38 my @content_type = split /\//, $header_value, 2;
1471 38 $self->{content_type} = \@content_type;
1472 38 return;
1473 }
1474 }
1475 }
1476  
1477 sub _compress()
1478 38 ▶{
Subroutine _compress: called CC=23 SCAR=47.4
1479 38 my $self = shift;
1480 38 ▶ my %params = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 38 0 38
1481  
1482 38 ▶ return unless(defined($self->{'body'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"body"} 0 38 38
1483 38 my $encoding = $params{encoding};
1484  
1485 38 ▶ if((length($encoding) == 0) || (length($self->{body}) < $MIN_GZIP_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $encoding == 0 or length $self->{"body"} < $MIN_GZIP_LEN) 36 2 38
Condition66%2/3
length $encoding == 0 or length $self->{"body"} < $MIN_GZIP_LEN
l!l&&r!l&&!r
3602
MC/DC50%1/2
length $encoding == 0 or length $self->{"body"} < $MIN_GZIP_LEN
length $encoding == 0 length $self->{"body"} < $MIN_GZIP_LEN
Truth table66%2/3
A or B
A= length $encoding == 0B= length $self->{"body"} < $MIN_GZIP_LEN
ABresult
1X1
011
000
1486 36 return;
1487 }
1488  
1489 2 ▶ if($encoding eq 'gzip') {
Branch: 3 branches
BranchTrueFalseTotal
if ($encoding eq 'gzip') { } 1 1 2
elsif ($encoding eq 'zstd') { } 0 1 1
elsif ($encoding eq 'br') { } 1 0 1
1490 1 require Compress::Zlib;
1491 1 Compress::Zlib->import;
1492  
1493 # Avoid 'Wide character in memGzip'
1494 1 ▶ unless($self->{_encode_loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_encode_loaded"}) 1 0 1
1495 1 require Encode;
1496 1 $self->{_encode_loaded} = 1;
1497 }
1498 1 my $nbody = Compress::Zlib::memGzip(\Encode::encode_utf8($self->{body}));
1499 1 ▶ if(length($nbody) < length($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nbody < length $self->{"body"}) 0 1 1
1500 0 $self->{body} = $nbody;
1501 0 ▶ unless(grep(/^Content-Encoding: gzip/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Content-Encoding: gzip/, @{$$self{"o"};}) 0 0 0
1502 0 push @{$self->{o}}, 'Content-Encoding: gzip';
1503 }
1504 0 ▶ unless(grep(/^Vary: Accept-Encoding/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Vary: Accept-Encoding/, @{$$self{"o"};}) 0 0 0
1505 0 push @{$self->{o}}, 'Vary: Accept-Encoding';
1506 }
1507 }
1508 } elsif($encoding eq 'zstd') {
1509 # Facebook
1510 0 ▶ if(eval { require Compress::Zstd; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Compress::Zstd; 1 } }) { } 0 0 0
1511 0 my $compressed_body = Compress::Zstd::compress(\Encode::encode_utf8($self->{'body'}));
1512 0 ▶ if(length($compressed_body) < length($self->{'body'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $compressed_body < length $self->{"body"}) 0 0 0
1513 0 $self->{'body'} = $compressed_body;
1514 0 ▶ unless(grep(/^Content-Encoding: zstd/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Content-Encoding: zstd/, @{$$self{"o"};}) 0 0 0
1515 0 push @{$self->{o}}, 'Content-Encoding: zstd';
1516 }
1517 0 ▶ unless(grep(/^Vary: Accept-Encoding/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Vary: Accept-Encoding/, @{$$self{"o"};}) 0 0 0
1518 0 push @{$self->{o}}, 'Vary: Accept-Encoding';
1519 }
1520 }
1521 } else {
1522 0 $self->{status} = 406;
1523 0 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 0 0 0
1524 0 $self->{'info'}->status(406);
1525 }
1526 }
1527 } elsif($encoding eq 'br') {
1528 1 ▶ if(eval { require IO::Compress::Brotli }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require IO::Compress::Brotli } }) { } 1 0 1
1529 1 IO::Compress::Brotli->import();
1530  
1531 1 ▶ unless($self->{_encode_loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_encode_loaded"}) 0 1 1
1532 0 require Encode;
1533 0 $self->{_encode_loaded} = 1;
1534 }
1535 1 my $nbody = IO::Compress::Brotli::bro(Encode::encode_utf8($self->{body}));
1536 1 ▶ if(length($nbody) < length($self->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nbody < length $self->{"body"}) 1 0 1
1537 1 $self->{body} = $nbody;
1538 1 ▶ unless(grep(/^Content-Encoding: br/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Content-Encoding: br/, @{$$self{"o"};}) 1 0 1
1539 1 push @{$self->{o}}, 'Content-Encoding: br';
1540 }
1541 1 ▶ unless(grep(/^Vary: Accept-Encoding/, @{$self->{o}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Vary: Accept-Encoding/, @{$$self{"o"};}) 1 0 1
1542 1 push @{$self->{o}}, 'Vary: Accept-Encoding';
1543 }
1544 }
1545 } else {
1546 0 $self->{status} = 406;
1547 0 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 0 0 0
1548 0 $self->{'info'}->status(406);
1549 }
1550 }
1551 }
1552 }
1553  
1554 3 ▶sub _check_if_none_match {
Subroutine _check_if_none_match: called CC=11 SCAR=38.2
1555 3 my $self = shift;
1556  
1557 3 ▶ if($ENV{'HTTP_IF_NONE_MATCH'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_NONE_MATCH"}) 3 0 3
1558 3 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 3 3
1559 0 $self->{logger}->debug("Compare $ENV{HTTP_IF_NONE_MATCH} with $self->{etag}");
1560 }
1561 3 ▶ if($ENV{'HTTP_IF_NONE_MATCH'} eq $self->{etag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_IF_NONE_MATCH"} eq $self->{"etag"}) 3 0 3
1562 3 push @{$self->{o}}, 'Status: 304 Not Modified';
1563 3 $self->{send_body} = 0;
1564 3 $self->{status} = 304;
1565 3 ▶ if($self->{'info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 0 3 3
1566 0 $self->{'info'}->status(304);
1567 }
1568 3 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 3 3
1569 0 $self->{logger}->debug('Set status to 304');
1570 }
1571 3 return 1;
1572 }
1573 }
1574 0 ▶ if($self->{cache} && $self->{logger} && $self->{logger}->is_debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} and $self->{"logger"} and $self->{"logger"}->is_debug) 0 0 0
Condition0%0/6
$self->{"cache"} and $self->{"logger"} and $self->{"logger"}->is_debug
!ll&&!rl&&r
000
$self->{"cache"} and $self->{"logger"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"cache"} and $self->{"logger"} and $self->{"logger"}->is_debug
$self->{"cache"} $self->{"logger"} $self->{"logger"}->is_debug
Truth table0%0/4
A and B and C
A= $self->{"cache"}B= $self->{"logger"}C= $self->{"logger"}->is_debug
ABCresult
0XX0
10X0
1100
1111
1575 0 my $cached_copy = $self->{cache}->get($self->_generate_key());
1576  
1577 0 ▶ if($cached_copy && $self->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_copy and $self->{'body'}) { } 0 0 0
Condition0%0/3
$cached_copy and $self->{"body"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cached_copy and $self->{"body"}
$cached_copy $self->{"body"}
Truth table0%0/3
A and B
A= $cached_copyB= $self->{"body"}
ABresult
0X0
100
111
1578 0 require Text::Diff;
1579 0 Text::Diff->import();
1580  
1581 0 $cached_copy = Storable::thaw($cached_copy)->{body};
1582 0 my $diffs = diff(\$self->{body}, \$cached_copy);
1583 0 $self->{logger}->debug('diffs: ', $diffs);
1584 } else {
1585 0 $self->{logger}->debug('Nothing to compare');
1586 }
1587 }
1588 0 return 0;
1589 }
1590  
1591 # replace dynamic links with static links
1592 28 ▶sub _save_to {
Subroutine _save_to: called CC=40 SCAR=73.1
1593 28 my ($self, $unzipped_body, $dbh) = @_;
1594  
1595 28 ▶ return 0 unless($dbh && $self->{info} && (my $request_uri = $ENV{'REQUEST_URI'}));
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh and $self->{"info"} and my $request_uri = $ENV{"REQUEST_URI"} 28 0 28
Condition33%2/6
$dbh and $self->{"info"} and my $request_uri = $ENV{"REQUEST_URI"}
!ll&&!rl&&r
2800
$dbh and $self->{"info"}
!ll&&!rl&&r
2800
MC/DC0%0/3
$dbh and $self->{"info"} and my $request_uri = $ENV{"REQUEST_URI"}
$dbh $self->{"info"} my $request_uri = $ENV{"REQUEST_URI"}
Truth table25%1/4
A and B and C
A= $dbhB= $self->{"info"}C= my $request_uri = $ENV{"REQUEST_URI"}
ABCresult
0XX0
10X0
1100
1111
1596 0 ▶ return 0 if(!defined($unzipped_body));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unzipped_body 0 0 0
1597  
1598 0 my $query;
1599 0 my $copy = $unzipped_body;
1600 0 my $changes = 0;
1601 0 my $creation;
1602 my %seen_links;
1603 0 ▶ while($unzipped_body =~ /<a\shref="(.+?)"/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unzipped_body =~ /<a\shref="(.+?)"/sig) 0 0 0
1604 0 my $link = $1;
1605 0 ▶ next if($seen_links{$link}); # Already updated in the copy
Branch: 1 branch
BranchTrueFalseTotal
if $seen_links{$link} 0 0 0
1606 0 $seen_links{$link} = 1;
1607 0 $link =~ tr/[\|;]/_/;
1608  
1609 0 my $search_uri = $link;
1610 0 ▶ if($search_uri =~ /^\?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_uri =~ /^\?/) { } 0 0 0
1611 # CGI script has links to itself
1612 # $search_uri = "${request_uri}${link}";
1613 0 my $r = $request_uri;
1614 0 $r =~ s/\?.*$//;
1615 0 $search_uri = "${r}$link";
1616 } else {
1617 0 ▶ next if($link =~ /^https?:\/\//); # FIXME: skips full URLs to ourself
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ m[^https?://] 0 0 0
1618 # Though optimise_content fixes that
1619 0 ▶ next if($link =~ /.html?$/);
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ /.html?$/ 0 0 0
1620 0 ▶ next if($link =~ /.jpg$/);
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ /.jpg$/ 0 0 0
1621 0 ▶ next if($link =~ /.gif$/);
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ /.gif$/ 0 0 0
1622 }
1623 0 ▶ if($self->{save_to}->{ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'save_to'}{'ttl'}) { } 0 0 0
1624 0 $query = "SELECT DISTINCT path, creation FROM fcgi_buffer WHERE uri = ? AND language = ? AND browser_type = ? AND creation >= strftime('\%s','now') - " . $self->{save_to}->{ttl};
1625 } else {
1626 0 $query = "SELECT DISTINCT path, creation FROM fcgi_buffer WHERE uri = ? AND language = ? AND browser_type = ?";
1627 }
1628 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1629 0 $self->{logger}->debug("$query: $search_uri, ", $self->{lingua}->language(), ', ', $self->{info}->browser_type());
1630 }
1631 0 ▶ if(defined(my $sth = $dbh->prepare($query))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $sth = $dbh->prepare($query))) { } 0 0 0
elsif ($self->{'logger'}) { } 0 0 0
1632 0 $sth->execute($search_uri, $self->{lingua}->language(), $self->{info}->browser_type());
1633 0 ▶ if(my $href = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $href = $sth->fetchrow_hashref) 0 0 0
1634 0 ▶ if(my $path = $href->{'path'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $href->{"path"}) 0 0 0
1635 0 ▶ if(-r $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $path) { } 0 0 0
1636 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1637 0 $self->{logger}->debug("Changing links from $link to $path");
1638 }
1639 0 $link =~ s/\?/\\?/g;
1640 0 my $rootdir = $self->{info}->rootdir();
1641 0 $path = substr($path, length($rootdir));
1642 0 $changes += ($copy =~ s/<a\s+href="$link">/<a href="$path">/gis);
1643 # Find the first link that will expire and use that
1644 0 ▶ if((!defined($creation)) || ($href->{'creation'} < $creation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $creation or $href->{"creation"} < $creation) 0 0 0
Condition0%0/3
not defined $creation or $href->{"creation"} < $creation
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $creation or $href->{"creation"} < $creation
not defined $creation $href->{"creation"} < $creation
Truth table0%0/3
not(A) or B
A= not defined $creationB= $href->{"creation"} < $creation
ABresult
1X1
011
000
1645 0 $creation = $href->{'creation'};
1646 }
1647 } else {
1648 0 $query = "DELETE FROM fcgi_buffer WHERE path = ?";
1649 0 $dbh->prepare($query)->execute($path);
1650 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1651 0 $self->{logger}->warn("Remove entry for non-existant file $path");
1652 }
1653 }
1654 }
1655 }
1656 } elsif($self->{logger}) {
1657 0 $self->{logger}->warn("failed to prepare '$query'");
1658 }
1659 }
1660 0 my $expiration = 0;
1661 0 ▶ if(defined($creation) && (my $ttl = $self->{save_to}->{ttl})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $creation and my $ttl = $self->{"save_to"}{"ttl"}) 0 0 0
Condition0%0/3
defined $creation and my $ttl = $self->{"save_to"}{"ttl"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $creation and my $ttl = $self->{"save_to"}{"ttl"}
defined $creation my $ttl = $self->{"save_to"}{"ttl"}
Truth table0%0/3
A and B
A= defined $creationB= my $ttl = $self->{"save_to"}{"ttl"}
ABresult
0X0
100
111
1662 0 $expiration = $creation + $ttl;
1663 }
1664 0 ▶ if($changes && (($expiration == 0) || ($expiration >= time))) {
Branch: 2 branches
BranchTrueFalseTotal
if ($changes and $expiration == 0 || $expiration >= time) { } 0 0 0
elsif ($expiration and $expiration < time) { } 0 0 0
Condition0%0/6
$changes and $expiration == 0 || $expiration >= time
!ll&&!rl&&r
000
$expiration == 0 or $expiration >= time
l!l&&r!l&&!r
000
MC/DC0%0/3
$changes and $expiration == 0 || $expiration >= time
$changes $expiration == 0 $expiration >= time
Truth table0%0/4
A and B or C
A= $changesB= $expiration == 0C= $expiration >= time
ABCresult
0XX0
1000
11X1
1011
1665 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1666 # $self->{logger}->debug("$changes links now point to static pages");
1667 0 ▶ if($changes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes == 1) { } 0 0 0
1668 0 ▶ if($self->{'save_to'}->{'ttl'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'save_to'}{'ttl'}) { } 0 0 0
1669 0 $self->{logger}->info('1 link now points to a static page for ', $expiration - time, 's');
1670 } else {
1671 0 $self->{logger}->info('1 link now points to a static page');
1672 }
1673 } else {
1674 0 $self->{logger}->info("$changes links now point to static pages");
1675 }
1676 }
1677 0 $unzipped_body = $copy;
1678 0 $self->{'body'} = $unzipped_body;
1679 0 ▶ if(my $ttl = $self->{save_to}->{ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ttl = $self->{"save_to"}{"ttl"}) 0 0 0
1680 0 push @{$self->{o}}, 'Expires: ' . HTTP::Date::time2str($creation + $ttl);
1681 }
1682 ▶ } elsif($expiration && ($expiration < time)) {
Condition0%0/3
$expiration and $expiration < time
!ll&&!rl&&r
000
MC/DC0%0/2
$expiration and $expiration < time
$expiration $expiration < time
Truth table0%0/3
A and B
A= $expirationB= $expiration < time
ABresult
0X0
100
111
1683 # Delete the save_to files
1684 0 ▶ if($self->{save_to}->{ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'save_to'}{'ttl'}) { } 0 0 0
1685 0 $query = "SELECT path FROM fcgi_buffer WHERE creation < strftime('\%s','now') - " . $self->{save_to}->{ttl};
1686 } else {
1687 0 $query = 'SELECT path FROM fcgi_buffer'; # Hmm, I suspect this is overkill
1688 }
1689 0 my $sth = $dbh->prepare($query);
1690 0 $sth->execute();
1691 0 ▶ while(my $href = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $href = $sth->fetchrow_hashref) 0 0 0
1692 0 ▶ if(my $path = $href->{'path'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $href->{"path"}) 0 0 0
1693 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1694 0 $self->{logger}->debug("unlink $path");
1695 }
1696 0 unlink $path;
1697 }
1698 }
1699 0 ▶ if($self->{save_to}->{ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'save_to'}{'ttl'}) { } 0 0 0
1700 0 $query = "DELETE FROM fcgi_buffer WHERE creation < strftime('\%s','now') - " . $self->{save_to}->{ttl};
1701 } else {
1702 0 $query = 'DELETE FROM fcgi_buffer'; # Hmm, I suspect this is overkill
1703 }
1704 0 ▶ if($self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logger"}) 0 0 0
1705 0 $self->{logger}->debug($query);
1706 }
1707 0 $dbh->prepare($query)->execute();
1708 # } else {
1709 # Old code
1710 # if($self->{save_to}->{ttl}) {
1711 # $query = "SELECT DISTINCT path, creation FROM fcgi_buffer WHERE key = '$key' AND creation >= strftime('\%s','now') - " . $self->{save_to}->{ttl};
1712 # } else {
1713 # $query = "SELECT DISTINCT path, creation FROM fcgi_buffer WHERE key = '$key'";
1714 # }
1715 # my $sth = $dbh->prepare($query);
1716 # $sth->execute();
1717 # my $href = $sth->fetchrow_hashref();
1718 # if(my $path = $href->{'path'}) {
1719 # # FIXME: don't do this if we've passed the TTL, and if we are clean
1720 # # up the database and remove the static page
1721 # $request_uri =~ s/\?/\\?/g;
1722 # if(($unzipped_body =~ s/<a href="$request_uri"/<a href="$path"/gi) > 0) {
1723 # $self->{'body'} = $unzipped_body;
1724 # if(my $ttl = $self->{save_to}->{ttl}) {
1725 # push @{$self->{o}}, 'Expires: ' . HTTP::Date::time2str($href->{creation} + $ttl);
1726 # }
1727 # }
1728 # }
1729 }
1730 0 return $changes;
1731 }
1732  
1733 =head1 AUTHOR
1734  
1735 Nigel Horne, C<< <njh at nigelhorne.com> >>
1736  
1737 =head1 BUGS
1738  
1739 FCGI::Buffer should be safe even in scripts that produce lots of different
1740 output, e.g. e-commerce situations.
1741 On such pages, however, I strongly urge setting generate_304 to 0 and
1742 sending the HTTP header "Cache-Control: no-cache".
1743  
1744 When using L<Template>, ensure that you don't use it to output to STDOUT,
1745 instead,
1746 you will need to capture into a variable and print that.
1747 For example:
1748  
1749 my $output;
1750 $template->process($input, $vars, \$output) || ($output = $template->error());
1751 print $output;
1752  
1753 Can produce buggy JavaScript if you use the <!-- HIDING technique.
1754 This is a bug in L<JavaScript::Packer>, not FCGI::Buffer.
1755  
1756 Mod_deflate can confuse this when compressing output.
1757 Ensure that deflation is off for .pl files:
1758  
1759 SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|pl)$ no-gzip dont-vary
1760  
1761 If you request compressed output then uncompressed output (or vice
1762 versa) on input that produces the same output, the status will be 304.
1763 The letter of the spec says that's wrong, so I'm noting it here, but
1764 in practice, you should not see this happen or have any difficulties
1765 because of it.
1766  
1767 FCGI::Buffer has not been tested against FastCGI.
1768  
1769 I advise adding FCGI::Buffer as the last use statement so that it is
1770 cleared up first. In particular, it should be loaded after
1771 L<Log::Log4perl>, if you're using that, so that any messages it
1772 produces are printed after the HTTP headers have been sent by
1773 FCGI::Buffer;
1774  
1775 Save_to doesn't understand links in JavaScript, which means that if you use self-calling
1776 CGIs loaded as a static page,
1777 they may point to the wrong place.
1778 The workaround is to avoid self-calling CGIs in JavaScript
1779  
1780 Please report any bugs or feature requests to C<bug-fcgi-buffer at rt.cpan.org>
1781 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FCGI-Buffer>.
1782 I will be notified, and then you'll automatically be notified of the progress on
1783 your bug as I make changes.
1784  
1785 The lint operation only works on HTML4, because of a restriction in L<HTML::Lint>.
1786  
1787 =head1 SEE ALSO
1788  
1789 L<CGI::Buffer>,
1790 L<HTML::Packer>,
1791 L<HTML::Lint>
1792  
1793 =head1 SUPPORT
1794  
1795 This module is provided as-is without any warranty.
1796  
1797 You can find documentation for this module with the perldoc command.
1798  
1799 perldoc FCGI::Buffer
1800  
1801 You can also look for information at:
1802  
1803 =over 4
1804  
1805 =item * RT: CPAN's request tracker
1806  
1807 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FCGI-Buffer>
1808  
1809 =item * Search CPAN
1810  
1811 L<http://search.cpan.org/dist/FCGI-Buffer/>
1812  
1813 =back
1814  
1815 =head1 ACKNOWLEDGEMENTS
1816  
1817 The inspiration and code for some of this is cgi_buffer by Mark
1818 Nottingham: L<https://www.mnot.net/blog/2003/04/24/etags>.
1819  
1820 =head1 LICENSE AND COPYRIGHT
1821  
1822 The licence for cgi_buffer is:
1823  
1824 "(c) 2000 Copyright Mark Nottingham <mnot@pobox.com>
1825  
1826 This software may be freely distributed, modified and used,
1827 provided that this copyright notice remain intact.
1828  
1829 This software is provided 'as is' without warranty of any kind."
1830  
1831 The rest of the program is Copyright 2015-2026 Nigel Horne,
1832 and is released under the following licence: GPL2
1833  
1834 =cut
1835  
1836 11 1; # End of FCGI::Buffer
↑ Summary