bin/wdrender

filter: Statementstmt 60.6% 202 / 333 Branchbran 47.6% 60 / 126 Conditioncond 32.3% 23 / 71 MC/DCmcdc 4.6% 2 / 43 Subroutinesub 75.6% 28 / 37 Podpod n/a totaltotal 51.6% 315 / 610 CC 96 SCAR 50.4
CC
96
Cov
54%
main
289.8
request_mod_perl_postamble
90.0
html_colour
30.0
CRAP 154.4
« lib/WebDyne/Util.pm ↑ Summary blib/lib/WebDyne/Util.pm »
1 #!/usr/bin/env perl
2 #
3 # This file is part of WebDyne.
4 #
5 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 # Full license text is available at:
11 #
12 # <http://dev.perl.org/licenses/>
13 #
14 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Get in before module load
17 #
18 7 $ENV{'WEBDYNE_HTML_NEWLINE'}=0;
19 7 $ENV{'WEBDYNE_ERROR_TEXT'}=1;
20
21 }
22  
23  
24 #
25 # Compile and/or show compiled version of WebDyne HTML scripts
26 #
27 package main;
28  
29  
30 # Compiler pragma
31 #
32 7 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 # Use the base module for err()
39 #
40 7 ▶use WebDyne::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43 # Other external modules
44 #
45 7 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 7 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 7 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 7 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 7 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 7 ▶use FindBin qw($Script);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 #use URI::Escape;
53 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 7 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 7 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 7 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 7 ▶use Cwd qw(fastcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61  
62 # Error handling and environment
63 #
64 #use Carp;
65 #local $SIG{__DIE__}=\&Carp::confess;
66 #sub err { croak(@_) }
67  
68  
69 # Local customisation
70 #
71 7 local $Data::Dumper::Indent=1;
72 7 local $Data::Dumper::Sortkeys=1;
73  
74  
75 # Version Info, must be all one line for MakeMaker, CPAN.
76 #
77 7 $VERSION='3.029';
78  
79  
80 # Run main
81 #
82 7 ▶exit ${&main(\@ARGV) || die errdump()};
Condition50%1/2
&main(\@ARGV) || die(errdump())
l!l
60
MC/DC0%0/1
&main(\@ARGV) || die(errdump())
&main(\@ARGV)
Truth table50%1/2
A
A= &main(\@ARGV)
Aresult
11
00
83  
84 #===================================================================================================
85  
86  
87 7 ▶sub main {
Subroutine main: called CC=66 SCAR=56.7
88  
89  
90 # Get argv array ref
91 #
92 7 my $argv_ar=shift();
93  
94  
95 # Default options
96 #
97 my %opt=(
98  
99 handler => $ENV{'WebDyneHandler'} || 'WebDyne',
100 error => 'text',
101 header => 0,
102 tidy => 1,
103 colour => 1,
104 lineno => 1,
105 no_head_insert => 1,
106 theme => 'light',
107 #request => ['fake'],
108 method => 'GET',
109 root => fastcwd(),
110 keep_tmp => 0,
111 warn => 1,
112 apache_stdout => 0,
113 7 ▶ %{do(glob(sprintf('~/.%s.opt', basename(__FILE__)))) || {}}
Condition50%2/4
$ENV{'WebDyneHandler'} || 'WebDyne'
l!l
07
do (glob(sprintf('~/.%s.opt', basename(__FILE__)))) || {}
l!l
07
MC/DC0%0/1
$ENV{'WebDyneHandler'} || 'WebDyne'
$ENV{'WebDyneHandler'}
MC/DC0%0/1
do (glob(sprintf('~/.%s.opt', basename(__FILE__)))) || {}
do (glob(sprintf('~/.%s.opt', basename(__FILE__))))
Truth table50%1/2
A
A= $ENV{'WebDyneHandler'}
Aresult
11
00
Truth table50%1/2
A
A= do (glob(sprintf('~/.%s.opt', basename(__FILE__))))
Aresult
11
00
114  
115 );
116  
117  
118 # Get command line options
119 #
120 GetOptions(
121 \%opt,
122 my @opt=(
123 'all',
124 'colour|color!',
125 'compare',
126 'conf|config=s',
127 0 ▶ 'no_conf|no_config|no-conf|no-config' => sub { $opt{'no_conf'}=1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 7 ▶ 'delay|sleep=i',
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, my(@opt) = ("all", "colour|color!", "compare", "conf|config=s", "no_conf|no_config|no-conf|no-config", sub { $opt{"no_conf"} = 1; } , "delay|sleep=i", "fake", "get=s\@", "handler=s", "head", "head_insert|head-insert!", "header|headers!", "header_only|header-only|headers_only|headers-only|headersonly", "headers_in|header_in|headers-in|header-in=s\@", "htmx", "lineno!", "loop", "method=s", "mod_perl|mod-perl|apache", "apache_stdout|apache-stdout!", "dump_postamble|dump-postamble", "dump_opt|dump-opt|opt", "options=s\@", "outfile=s", "pagi", "param=s\@", "patch=s\@", "post=s\@", "psgi", "psgi_server|psgi-server", "put=s\@", "raw", "repeat|num|n=s", "request|r=s\@", "root|docroot|doc_root|doc-root|document_root|document-root=s", "sse", "silent|quiet!", "test", "theme=s", "tidy|pretty!", "keep_tmp|keep-tmp!", "help|?", "warn!", "man", "version")) or pod2usage(2) 7 0 7
129 # 'error=s', # Not applied
130 'fake',
131 'get=s@',
132 'handler=s',
133 'head',
134 'head_insert|head-insert!',
135 'header|headers!',
136 'header_only|header-only|headers_only|headers-only|headersonly',
137 'headers_in|header_in|headers-in|header-in=s@',
138 'htmx',
139 'lineno!',
140 'loop',
141 'method=s',
142 'mod_perl|mod-perl|apache',
143 'apache_stdout|apache-stdout!',
144 'dump_postamble|dump-postamble',
145 'dump_opt|dump-opt|opt',
146 'options=s@',
147 'outfile=s',
148 'pagi',
149 'param=s@',
150 'patch=s@',
151 'post=s@',
152 'psgi',
153 'psgi_server|psgi-server',
154 'put=s@',
155 'raw',
156 'repeat|num|n=s',
157 'request|r=s@',
158 'root|docroot|doc_root|doc-root|document_root|document-root=s',
159 'sse',
160 'silent|quiet!',
161 #'status=s', # Not applied
162 'test',
163 'theme=s',
164 'tidy|pretty!',
165 'keep_tmp|keep-tmp!',
166 'help|?',
167 'warn!',
168 'man',
169 'version'
170 ),
171 ) || pod2usage(2);
172 7 ▶ pod2usage(-verbose => 99, -sections => 'SYNOPSIS|OPTIONS', -exitval => 1) if $opt{'help'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 7 7
173 7 ▶ pod2usage(-verbose => 2) if $opt{'man'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"man"} 0 7 7
174 7 ▶ $opt{'version'} && do {
Branch: 1 branch
BranchTrueFalseTotal
$opt{"version"} and do { require WebDyne; print "$main::Script version: $main::VERSION\n"; print "WebDyne version: $WebDyne::VERSION ($WebDyne::VERSION_GIT_SHA)\n"; exit 0 } 1 6 7
175 1 require WebDyne;
176 1 print "$Script version: $VERSION\n";
177 1 print "WebDyne version: $WebDyne::VERSION ($WebDyne::VERSION_GIT_SHA)\n";
178 1 exit 0
179 };
180  
181  
182 # Convert negative flags
183 #
184 #map {$opt{"no_${_}"}=!($opt{$_})} qw(head_insert);
185 6 map {$opt{"no_${_}"}=!($opt{$_})} map { /^([^|!:=+]+)/ } grep {/\!$/} @opt;
186 6 ▶ $opt{'warn'}=0 if $opt{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"silent"} 1 5 6
187
188  
189 # Special config ?
190 #
191 6 ▶ if (my $conf_fn=$opt{'conf'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $conf_fn = $opt{'conf'}) { } 0 6 6
elsif ($opt{'no_conf'} or $opt{'raw'}) { } 0 6 6
192 0 $ENV{'WEBDYNE_CONF'}=$opt{'conf'};
193 }
194 ▶ elsif($opt{'no_conf'} || $opt{'raw'}) {
Condition33%1/3
$opt{"no_conf"} or $opt{"raw"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$opt{"no_conf"} or $opt{"raw"}
$opt{"no_conf"} $opt{"raw"}
Truth table33%1/3
A or B
A= $opt{"no_conf"}B= $opt{"raw"}
ABresult
1X1
011
000
195 0 $ENV{'WEBDYNE_CONF'}='.';
196 }
197  
198  
199 # Dump options for debugging
200 #
201 6 ▶ die Dumper(\%opt) if $opt{'dump_opt'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"dump_opt"} 0 6 6
202  
203
204 # Is dest file set ? If so open
205 #
206 6 my ($html, $html_fh);
207 6 ▶ if (my $dest_fn=$opt{'outfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dest_fn = $opt{"outfile"}) 1 5 6
208 1 ▶ $html_fh=IO::File->new($dest_fn, O_CREAT | O_TRUNC | O_WRONLY) ||
Condition50%1/2
'IO::File'->new($dest_fn, 577) || (return err("unable to open file $dest_fn for output, $!"))
l!l
10
MC/DC0%0/1
'IO::File'->new($dest_fn, 577) || (return err("unable to open file $dest_fn for output, $!"))
'IO::File'->new($dest_fn, 577)
Truth table50%1/2
A
A= 'IO::File'->new($dest_fn, 577)
Aresult
11
00
209 return err("unable to open file $dest_fn for output, $!");
210 }
211 #else {
212 # $html_fh=IO::String->new($html);
213 #}
214 #$opt{'select'}=$html_fh;
215  
216  
217 # Get srce file, add to options
218 #
219 6 ▶ my $srce_fn=$opt{'test'} ? $WEBDYNE_DEFAULT_TEST_FN : shift(@{$argv_ar});
Branch: 1 branch
BranchTrueFalseTotal
$opt{'test'} ? : 2 4 6
220 6 ▶ $srce_fn ||
Branch: 1 branch
BranchTrueFalseTotal
$srce_fn or pod2usage("$main::Script: no source file specified !") 6 0 6
221 pod2usage("$Script: no source file specified !");
222 0 ▶ (-f $srce_fn) || do { (-f File::Spec->catfile($opt{'root'}, $srce_fn)) if $opt{'mod_perl'} } ||
Branch: 2 branches
BranchTrueFalseTotal
-f $srce_fn or do { -f "File::Spec"->catfile($opt{"root"}, $srce_fn) if $opt{"mod_perl"} } or pod2usage("$main::Script: input file not found !") 6 0 6
if $opt{"mod_perl"} 0 0 0
Condition33%1/3
-f $srce_fn or do { -f "File::Spec"->catfile($opt{"root"}, $srce_fn) if $opt{"mod_perl"} }
l!l&&r!l&&!r
600
MC/DC0%0/2
-f $srce_fn or do { -f "File::Spec"->catfile($opt{"root"}, $srce_fn) if $opt{"mod_perl"} }
-f $srce_fn do { -f "File::Spec"->catfile($opt{"root"}, $srce_fn) if $opt{"mod_perl"} }
Truth table33%1/3
A or B
A= -f $srce_fnB= do { -f "File::Spec"->catfile($opt{"root"}, $srce_fn) if $opt{"mod_perl"} }
ABresult
1X1
011
000
223 pod2usage("$Script: input file not found !");
224  
225  
226 # Consolidate backend request handler options into one multiplexer option
227 #
228 6 my @request_type=qw(fake psgi psgi_server pagi mod_perl);
229 6 ▶ my $request_ar=ref($opt{'request'}) eq 'ARRAY' ? $opt{'request'} : [ grep { defined } $opt{'request'} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $opt{'request'} eq 'ARRAY' ? : 0 6 6
230 #my %request_type=map { $_=>1 } @{$opt{'request'}};
231 0 my %request_type=map { $_=>1 } @{$request_ar};
232 0 map { $request_type{$_}=1 } grep { $opt{$_} } @request_type;
233 6 ▶ unless ($request_type{'all'} || $opt{'all'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request_type{"all"} or $opt{"all"}) 6 0 6
Condition33%1/3
$request_type{"all"} or $opt{"all"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$request_type{"all"} or $opt{"all"}
$request_type{"all"} $opt{"all"}
Truth table33%1/3
A or B
A= $request_type{"all"}B= $opt{"all"}
ABresult
1X1
011
000
234 6 ▶ @request_type=grep {$request_type{$_} || $opt{$_}} @request_type;
Condition33%1/3
$request_type{$_} || $opt{$_}
l!l&&r!l&&!r
0030
MC/DC0%0/2
$request_type{$_} || $opt{$_}
$request_type{$_} $opt{$_}
Truth table33%1/3
A || B
A= $request_type{$_}B= $opt{$_}
ABresult
1X1
011
000
235 }
236 6 ▶ @request_type=('fake') unless @request_type;
Branch: 1 branch
BranchTrueFalseTotal
unless @request_type 6 0 6
237  
238  
239 # Santy check to make sure no invalid request types
240 #
241 6 delete @request_type{(@request_type, 'all')};
242 6 ▶ return err('invalid or missing request type \'%s\', must be one of fake,psgi,psgi_server,pagi,mod_perl,all', join(',', sort keys %request_type))
Branch: 1 branch
BranchTrueFalseTotal
if keys %request_type 0 6 6
243 if keys %request_type;
244
245
246 # Get request method wanted
247 #
248 6 ▶ my $method=lc((grep{exists $opt{$_}} qw(get post patch put options head))[0] || $opt{'method'});
Condition33%1/3
(grep {exists $opt{$_};} 'get', 'post', 'patch', 'put', 'options', 'head')[0] || $opt{'method'}
l!l&&r!l&&!r
060
MC/DC0%0/2
(grep {exists $opt{$_};} 'get', 'post', 'patch', 'put', 'options', 'head')[0] || $opt{'method'}
(grep {exists $opt{$_};} 'get', 'post', 'patch', 'put', 'options', 'head')[0] $opt{'method'}
Truth table33%1/3
A || B
A= (grep {exists $opt{$_};} 'get', 'post', 'patch', 'put', 'options', 'head')[0]B= $opt{'method'}
ABresult
1X1
011
000
249
250  
251 # Convert any headers into a HTTP::Header object
252 #
253 6 my @headers_in=map { split(/[:=]/, $_, 2) } @{$opt{'headers_in'}};
254 6 my $headers_in_or=HTTP::Headers->new(@headers_in);
255
256  
257 # Get any headers from environment vars
258 #
259 6 foreach my $env (qw(CONTENT_TYPE CONTENT_LENGTH), grep {/^HTTP_/} keys %ENV) {
260 12 my $value=$ENV{$env};
261 12 (my $header=$env)=~s/^HTTP_//;
262 12 $header=~s/\-/_/;
263 12 $headers_in_or->header(lc($header), $value);
264 }
265
266
267 # If htmx option add hx-request header
268 #
269 6 ▶ if ($opt{'htmx'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"htmx"}) 0 6 6
270 0 $headers_in_or->header('hx-request', 'true');
271 }
272 6 ▶ if ($opt{'sse'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"sse"}) 0 6 6
273 0 $headers_in_or->header('accept', 'text/event-stream');
274 }
275  
276  
277 # Pull out query or post params
278 #
279 6 my @query_string;
280 6 ▶ if (my $query_opt_ar=$opt{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $query_opt_ar = $opt{$method}) 0 6 6
281 0 my @query_opt=map { split(/[;&]/, $_) } @{$query_opt_ar};
282 0 foreach my $query_opt (@query_opt) {
283 0 my ($key, $value)=split(/[:=]/, $query_opt, 2);
284 0 push @query_string, $key, $value
285 }
286 };
287 6 $opt{'query_string'}=\@query_string;
288
289
290 # And parameters we want to supply to the handler for use in perl blocks ?
291 #
292 6 my %param;
293 6 ▶ if (my $param_opt_ar=$opt{'param'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param_opt_ar = $opt{"param"}) 2 4 6
294 2 my @param_opt=map { split(/[;&]/) } @{$param_opt_ar};
295 2 foreach my $param (@param_opt) {
296 2 my ($key, $value)=split(/[:=]/, $param, 2);
297 2 ▶ if (ref($param{$key}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $param{$key} eq 'ARRAY') { } 0 2 2
elsif (exists $param{$key}) { } 0 2 2
298 # Array already, push
299 0 push @{$param{$key}}, $value;
300 }
301 elsif (exists $param{$key}) {
302 # Duplicate, create array
303 0 $param{$key}=[$param{$key}, $value];
304 }
305 else {
306 # Vanilla
307 2 $param{$key}=$value;
308 }
309 }
310 };
311 6 $opt{'param'}=\%param;
312  
313  
314 # Construct the HTTP::Request object
315 #
316 6 my $req_or;
317 6 my $uri_or=URI->new($srce_fn);
318 6 ▶ if ($method eq 'get') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'get') { } 6 0 6
319 6 $uri_or->query_form(\@query_string);
320 6 $req_or=HTTP::Request->new('GET', $uri_or, $headers_in_or);
321 }
322 else {
323 #$req_or=HTTP::Request->new(uc($method), $uri_or, $headers_in_or, \@query_string);
324 0 my $method_uc=uc($method);
325 0 require HTTP::Request::Common;
326 0 $req_or=&{"HTTP::Request::Common::${method_uc}"}($uri_or, \@query_string, $headers_in_or->flatten );
327 }
328
329
330 # If we're running apache backend start server now
331 #
332 6 my $apache_runner;
333 6 ▶ if ($opt{'mod_perl'} || $opt{'all'} || (grep {/^(?:mod_perl|apache)$/} @request_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"mod_perl"} or $opt{"all"} or grep {/^(?:mod_perl|apache)$/;} @request_type) 0 6 6
Condition33%2/6
$opt{"mod_perl"} or $opt{"all"} or grep {/^(?:mod_perl|apache)$/;} @request_type
l!l&&r!l&&!r
006
$opt{"mod_perl"} or $opt{"all"}
l!l&&r!l&&!r
006
MC/DC0%0/3
$opt{"mod_perl"} or $opt{"all"} or grep {/^(?:mod_perl|apache)$/;} @request_type
$opt{"mod_perl"} $opt{"all"} grep {/^(?:mod_perl|apache)$/;} @request_type
Truth table25%1/4
A or B or C
A= $opt{"mod_perl"}B= $opt{"all"}C= grep {/^(?:mod_perl|apache)$/;} @request_type
ABCresult
1XX1
01X1
0011
0000
334 0 $apache_runner=&request_mod_perl_startup(\%opt);
335 }
336  
337  
338 # Run main routine to call appropriate handler
339 #
340 6 LOOP: while (1) {
341
342  
343 # Request type is PSGI, PAGI, Fake etc.
344 #
345 6 foreach my $request_type (@request_type) {
346
347
348 # If different request types (more than one) identify
349 #
350 6 ▶ if ((@request_type > 1) && !$opt{'silent'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@request_type > 1 and not $opt{"silent"}) 0 6 6
Condition33%1/3
@request_type > 1 and not $opt{"silent"}
!ll&&!rl&&r
600
MC/DC0%0/2
@request_type > 1 and not $opt{"silent"}
@request_type > 1 not $opt{"silent"}
Truth table33%1/3
A and not(B)
A= @request_type > 1B= not $opt{"silent"}
ABresult
0X0
100
111
351 0 print "Request type: $request_type\n";
352 }
353  
354
355 # Run as many repeats as nominated
356 #
357 6 ▶ for (1..($opt{'repeat'} || 1)) {
Condition50%1/2
$opt{'repeat'} || 1
l!l
06
MC/DC0%0/1
$opt{'repeat'} || 1
$opt{'repeat'}
Truth table50%1/2
A
A= $opt{'repeat'}
Aresult
11
00
358
359
360 # Send out to backend for render
361 #
362 6 ▶ my $res_or=&{sprintf('request_%s', $request_type)}($req_or, \%opt) ||
Condition50%1/2
&{sprintf 'request_%s', $request_type;}($req_or, \%opt) || (return err("no responsonse object returned on $request_type"))
l!l
60
MC/DC0%0/1
&{sprintf 'request_%s', $request_type;}($req_or, \%opt) || (return err("no responsonse object returned on $request_type"))
&{sprintf 'request_%s', $request_type;}($req_or, \%opt)
Truth table50%1/2
A
A= &{sprintf 'request_%s', $request_type;}($req_or, \%opt)
Aresult
11
00
363 return err("no responsonse object returned on $request_type");
364 6 my $html=$res_or->decoded_content;
365
366  
367 # If headers wanted strip them out
368 #
369 6 my $header;
370 6 ▶ if ($opt{'header'} || $opt{'header_only'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"header"} or $opt{"header_only"}) 2 4 6
Condition66%2/3
$opt{"header"} or $opt{"header_only"}
l!l&&r!l&&!r
204
MC/DC50%1/2
$opt{"header"} or $opt{"header_only"}
$opt{"header"} $opt{"header_only"}
Truth table66%2/3
A or B
A= $opt{"header"}B= $opt{"header_only"}
ABresult
1X1
011
000
371 2 $header=$res_or->status_line(). "\n";
372 2 $header.=$res_or->headers->as_string();
373 }
374 6 ▶ if ($opt{'header_only'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"header_only"}) 0 6 6
375 0 print $header, "\n";
376 0 next;
377 }
378
379  
380 # Don't colourise or format if --raw optoion given
381 #
382 6 ▶ unless ($opt{'raw'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"raw"}) 6 0 6
383  
384  
385 # Tidy ?
386 #
387 6 ▶ if ($opt{'tidy'} && ($res_or->content_type eq 'text/html') && !$opt{'htmx'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"tidy"} and $res_or->content_type eq "text/html" and not $opt{"htmx"}) 1 5 6
Condition66%4/6
$opt{"tidy"} and $res_or->content_type eq "text/html" and not $opt{"htmx"}
!ll&&!rl&&r
501
$opt{"tidy"} and $res_or->content_type eq "text/html"
!ll&&!rl&&r
501
MC/DC33%1/3
$opt{"tidy"} and $res_or->content_type eq "text/html" and not $opt{"htmx"}
$opt{"tidy"} $res_or->content_type eq "text/html" not $opt{"htmx"}
Truth table50%2/4
A and B and not(C)
A= $opt{"tidy"}B= $res_or->content_type eq "text/html"C= not $opt{"htmx"}
ABCresult
0XX0
10X0
1100
1111
388 1 $html=&html_tidy($html, \%opt);
389 }
390  
391  
392 # Send to syntax higlighter ?
393 #
394 6 ▶ unless ($opt{'outfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"outfile"}) 5 1 6
395 5 ▶ if ($opt{'colour'} && ($res_or->content_type eq 'text/html')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"colour"} and $res_or->content_type eq "text/html") 0 5 5
Condition33%1/3
$opt{"colour"} and $res_or->content_type eq "text/html"
!ll&&!rl&&r
500
MC/DC0%0/2
$opt{"colour"} and $res_or->content_type eq "text/html"
$opt{"colour"} $res_or->content_type eq "text/html"
Truth table33%1/3
A and B
A= $opt{"colour"}B= $res_or->content_type eq "text/html"
ABresult
0X0
100
111
396 0 $html=&html_colour($html, $header, \%opt);
397 0 undef $header;
398 }
399 }
400
401 }
402  
403 6 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 2 4 6
404 2 $html=join("\n", $header, $html);
405 }
406
407
408 # Defensive revert select in case very weird/bad error
409 #
410 6 ▶ select STDOUT if defined(fileno(STDOUT));
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno STDOUT 6 0 6
411
412
413 # Print it
414 #
415 6 ▶ $html_fh ? CORE::print $html_fh $html, $/ : CORE::print $html, $/;
Branch: 1 branch
BranchTrueFalseTotal
$html_fh ? : 1 5 6
416
417
418 6 ▶ if ($opt{'compare'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"compare"}) 0 6 6
419 0 ▶ unless ($html eq ($_{'html'} ||= $html)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($html eq ($_{"html"} ||= $html)) 0 0 0
Condition0%0/3
$_{"html"} ||= $html
l!l&&r!l&&!r
000
MC/DC0%0/2
$_{"html"} ||= $html
$_{"html"} $html
Truth table0%0/3
A ||= B
A= $_{"html"}B= $html
ABresult
1X1
011
000
420 0 ▶ eval { require Text::Diff } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Text::Diff } } or do { die sprintf("compare mismatch:\n\n$html\n\n%s", $_{"html"}) } 0 0 0
421 0 die(sprintf("compare mismatch:\n\n$html\n\n%s",$_{'html'}));
422 };
423 my $diff=Text::Diff::diff(
424 #\Data::Dumper->Dump([\$html], ['$ACTUAL']),
425 #\Data::Dumper->Dump([\$_{'html'}], ['$EXPECT']),
426 0 \$_{'html'},
427 \$html,
428 { STYLE => 'Unified', FILENAME_A=>'EXPECT', FILENAME_B=>'ACTUAL' }
429 );
430 0 local $SIG{'__DIE__'};
431 0 CORE::die("compare mismatch:\n$diff");
432 }
433 }
434
435
436 # If looking for leak
437 #
438 #my $gladiator_ar=Devel::Gladiator::walk_arena();
439  
440  
441 # Manual cleanup
442 #
443 #my $r;
444 #$r->DESTROY();
445
446
447 # Leak detection
448 #
449 #print("SV: ", scalar @{$gladiator_ar}, "\n") if $opt{'leak'};
450 #@{$gladiator_ar} = ();
451
452  
453 # Delay (presumably to test caching code)
454 #
455 6 ▶ if (my $delay=$opt{'delay'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delay = $opt{"delay"}) 0 6 6
456 0 sleep $delay
457 }
458  
459 }
460
461 # Reinit WebDyne for next run to clear cache etc
462 #
463 6 ▶ WebDyne->init() if defined(&WebDyne::init);
Branch: 1 branch
BranchTrueFalseTotal
if defined &WebDyne::init 6 0 6
464  
465 }
466
467
468 # Run again ?
469 #
470 6 ▶ last LOOP unless $opt{'loop'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"loop"} 6 0 6
471  
472 }
473  
474  
475 # Shutdown server if using Apache backend
476 #
477 6 ▶ if ($apache_runner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($apache_runner) 0 6 6
478 0 &apache_shutdown($apache_runner);
479 }
480
481  
482 # Done, return success
483 #
484 6 return \0;
485  
486 }
487  
488  
489  
490 0 ▶sub request_psgi {
Subroutine request_psgi: not called CC=3 SCAR=24.8
491  
492  
493 # Use PSGI mock handler. Get filename we want to render
494 #
495 0 my ($req_or, $opt_hr)=@_;
496  
497  
498 # Load modules we need
499 #
500 0 require WebDyne::PSGI;
501 0 require Plack::Test;
502  
503 # Create new instance of WebDyne PSGI handler
504 #
505 0 my $source_fn=File::Spec->rel2abs($req_or->uri->path());
506 my $app_cr=($_{'psgi_app_cr'} ||=
507 0 ▶ WebDyne::PSGI->new(root=>$opt_hr->{'root'}, no_head_insert=>$opt_hr->{'no_head_insert'}, filename=>$source_fn )->to_app());
Condition0%0/3
$_{"psgi_app_cr"} ||= "WebDyne::PSGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
l!l&&r!l&&!r
000
MC/DC0%0/2
$_{"psgi_app_cr"} ||= "WebDyne::PSGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
$_{"psgi_app_cr"} "WebDyne::PSGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
Truth table0%0/3
A ||= B
A= $_{"psgi_app_cr"}B= "WebDyne::PSGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
ABresult
1X1
011
000
508  
509  
510 # Now run. For some reason PSGI manipulates request object so clone first and restore
511 #
512 0 my $uri_or=$req_or->uri->clone();
513 0 ▶ my $test_or=($_{'psgi_test_or'} ||=
Condition0%0/3
$_{"psgi_test_or"} ||= "Plack::Test"->create($app_cr)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_{"psgi_test_or"} ||= "Plack::Test"->create($app_cr)
$_{"psgi_test_or"} "Plack::Test"->create($app_cr)
Truth table0%0/3
A ||= B
A= $_{"psgi_test_or"}B= "Plack::Test"->create($app_cr)
ABresult
1X1
011
000
514 Plack::Test->create($app_cr));
515 0 my $res_or=$test_or->request($req_or);
516 0 $req_or->uri($uri_or);
517
518
519 # Return results
520 #
521 0 return $res_or;
522
523 }
524  
525  
526 0 ▶sub request_psgi_server {
Subroutine request_psgi_server: not called CC=1 SCAR=0.6
527  
528 7 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
529 0 local $Plack::Test::Impl='Server';
530 0 return &request_psgi(@_);
531
532 }
533  
534  
535 0 ▶sub request_pagi {
Subroutine request_pagi: not called CC=3 SCAR=24.8
536  
537  
538 # Use PSGI mock handler. Get filename we want to render
539 #
540 0 my ($req_or, $opt_hr)=@_;
541
542  
543 # Load modules we need
544 #
545 0 require WebDyne::PAGI;
546 0 require PAGI::Test::Client;
547 #use Future::AsyncAwait;
548 #use Sub::Util qw(set_subname);
549  
550  
551 # Create new instance of WebDyne::PAGI handler
552 #
553 0 my $source_fn=File::Spec->rel2abs($req_or->uri->path());
554 my $app_cr=($_{'pagi_app_cr'} ||=
555 0 ▶ WebDyne::PAGI->new(root=>$opt_hr->{'root'}, no_head_insert=>$opt_hr->{'no_head_insert'}, filename=>$source_fn)->to_app());
Condition0%0/3
$_{"pagi_app_cr"} ||= "WebDyne::PAGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
l!l&&r!l&&!r
000
MC/DC0%0/2
$_{"pagi_app_cr"} ||= "WebDyne::PAGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
$_{"pagi_app_cr"} "WebDyne::PAGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
Truth table0%0/3
A ||= B
A= $_{"pagi_app_cr"}B= "WebDyne::PAGI"->new("root", $opt_hr->{"root"}, "no_head_insert", $opt_hr->{"no_head_insert"}, "filename", $source_fn)->to_app
ABresult
1X1
011
000
556 0 ▶ my $test_or=($_{'pagi_test_or'} ||=
Condition0%0/3
$_{"pagi_test_or"} ||= "PAGI::Test::Client"->new("app", $app_cr, "headers", {$req_or->headers->flatten})
l!l&&r!l&&!r
000
MC/DC0%0/2
$_{"pagi_test_or"} ||= "PAGI::Test::Client"->new("app", $app_cr, "headers", {$req_or->headers->flatten})
$_{"pagi_test_or"} "PAGI::Test::Client"->new("app", $app_cr, "headers", {$req_or->headers->flatten})
Truth table0%0/3
A ||= B
A= $_{"pagi_test_or"}B= "PAGI::Test::Client"->new("app", $app_cr, "headers", {$req_or->headers->flatten})
ABresult
1X1
011
000
557 PAGI::Test::Client->new(app => $app_cr, headers=>{$req_or->headers->flatten}));
558  
559  
560 # Extract method wanted and run
561 #
562 0 my $method=lc($req_or->method());
563 0 my $pagi_res_or=$test_or->$method($req_or->uri->path(), query=>{ @{ $opt_hr->{'query_string'}} });
564
565  
566 # Xlate to HTTP::Response object
567 #
568 my $res_or=HTTP::Response->new(
569 $pagi_res_or->status(),
570 HTTP::Status->status_message($pagi_res_or->status()),
571 0 HTTP::Headers->new(%{$pagi_res_or->headers()}),
572 $pagi_res_or->content()
573 );
574
575  
576 # Return
577 #
578 0 return $res_or;
579
580 }
581  
582  
583 0 ▶sub request_apache {
Subroutine request_apache: not called CC=1 SCAR=6.9
584  
585 0 return goto &request_mod_perl;
586
587 }
588  
589  
590 0 ▶sub request_mod_perl {
Subroutine request_mod_perl: not called CC=3 SCAR=24.8
591  
592  
593 # Spin up Apache and use that
594 #
595 0 my ($req_or, $opt_hr)=@_;
596
597
598 # Load modules we need
599 #
600 0 require Apache::TestRequest;
601  
602  
603 # Get response object
604 #
605 0 my $res_or;
606 0 my $method=uc($req_or->method());
607 0 ▶ if (grep { $method eq $_ } qw(GET PUT HEAD)) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$method eq $_;} 'GET', 'PUT', 'HEAD') { } 0 0 0
elsif ($method eq 'POST') { } 0 0 0
608 0 $res_or=&{"Apache::TestRequest::${method}"}($req_or->uri(), $req_or->headers->flatten());
609 }
610 elsif ($method eq 'POST') {
611 #$res_or=&{"Apache::TestRequest::${method}"}($req_or->uri, content => [$req_or->content], $req_or->headers->flatten());
612 0 $res_or=&{"Apache::TestRequest::${method}"}($req_or->uri(), $opt_hr->{'query_string'}, $req_or->headers->flatten());
613 }
614 else {
615 0 return err('method %s not supported for mod_perl/apache request type', $method);
616 }
617  
618  
619 # Done
620 #
621 0 return $res_or;
622
623 }
624  
625  
626 0 ▶sub request_mod_perl_startup {
Subroutine request_mod_perl_startup: not called CC=1 SCAR=6.9
627  
628  
629 # Startup Apache test instance
630 #
631 0 my $opt_hr=shift();
632  
633  
634 # Get postamble with WebDyne config
635 #
636 0 my $postamble=&request_mod_perl_postamble($opt_hr);
637  
638  
639 # Start Apache using Apache::Test.
640 #
641 return apache_startup({
642 port => 'select',
643 documentroot => $opt_hr->{'root'},
644 postamble => $postamble,
645 0 keep_tmp => $opt_hr->{'keep_tmp'},
646 });
647  
648 }
649  
650  
651 0 ▶sub request_mod_perl_postamble {
Subroutine request_mod_perl_postamble: not called CC=9 SCAR=45.0
652  
653  
654 # Create postamble suitable for running Apache and interpreting .psp files
655 #
656 0 my $opt_hr=shift();
657  
658  
659 # Need to work out if stderr writable. If not need to use alternate log file (prob running in container)
660 #
661 0 ▶ my $stderr_fn=$opt_hr->{'apache_stdout'} ? '/dev/stderr' : File::Spec->devnull();
Branch: 1 branch
BranchTrueFalseTotal
$opt_hr->{'apache_stdout'} ? : 0 0 0
662 0 ▶ my $stdout_fn=$opt_hr->{'apache_stdout'} ? '/dev/stdout' : File::Spec->devnull();
Branch: 1 branch
BranchTrueFalseTotal
$opt_hr->{'apache_stdout'} ? : 0 0 0
663 0 ▶ if (! open(my $fh, '>>', $stderr_fn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open my $fh, ">>", $stderr_fn) 0 0 0
664  
665 # Not writable, probably running under docker, revert to traditional
666 #
667 0 print STDERR "unable to write to stderr,stdout - reverting to log files\n";
668 0 $stderr_fn='logs/error.log';
669 0 $stdout_fn='logs/access.log';
670
671 }
672  
673  
674 # Get any additional library paths we want
675 #
676 0 my @perl_inc_dn=@{&perl_inc_dn()};
677  
678  
679 # Ok - proceed to build postamble
680 #
681 0 my @PerlSetEnv=map { sprintf('PerlSetEnv %s %s', $_, $ENV{$_}) }
682 0 ▶ grep { defined($ENV{$_}) && $ENV{$_} ne '' }
Condition0%0/3
defined $ENV{$_} && $ENV{$_} ne ''
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{$_} && $ENV{$_} ne ''
defined $ENV{$_} $ENV{$_} ne ''
Truth table0%0/3
A && B
A= defined $ENV{$_}B= $ENV{$_} ne ''
ABresult
0X0
100
111
683 0 grep { /^WEBDYNE_/ } keys %ENV;
684 0 ▶ push (@PerlSetEnv, "PerlSetEnv WEBDYNE_ERROR_TEXT 1") unless defined($ENV{'WEBDYNE_ERROR_TEXT'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"WEBDYNE_ERROR_TEXT"} 0 0 0
685 0 ▶ push (@PerlSetEnv, sprintf("PerlSetEnv WEBDYNE_REQUEST_NO_HEAD_INSERT %s", $opt_hr->{'no_head_insert'} ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$opt_hr->{'no_head_insert'} ? : 0 0 0
686 0 ▶ push (@PerlSetEnv, "PerlSetEnv WEBDYNE_REQUEST_FILENAME \"$WEBDYNE_DEFAULT_TEST_FN\"") if $opt_hr->{'test'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_hr->{"test"} 0 0 0
687 0 my $PerlSetEnv=join("\n", @PerlSetEnv);
688 0 my $PerlSwitches=join("\n", map { sprintf('PerlSwitches -I%s', $_) } @perl_inc_dn);
689 0 my $postamble= <<"END"
690 $PerlSwitches
691 $PerlSetEnv
692 PerlModule WebDyne
693 AddHandler modperl .psp
694 PerlResponseHandler WebDyne
695 <Location "/">
696 SetHandler modperl
697 PerlResponseHandler WebDyne
698 </Location>
699 ErrorLog $stderr_fn
700 LogLevel debug
701 LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined
702 CustomLog $stdout_fn combined
703 END
704 ;
705 0 ▶ exit print $postamble if $opt_hr->{'dump_postamble'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_hr->{"dump_postamble"} 0 0 0
706 0 return $postamble;
707  
708 }
709  
710  
711 6 ▶sub request_fake {
Subroutine request_fake: called CC=11 SCAR=25.9
712  
713  
714 # Use fake request handler
715 #
716 6 my ($req_or, $opt_hr)=@_;
717  
718  
719 # Open file hanldle to receive HTML
720 #
721 6 my $html;
722 6 my $html_fh=IO::String->new($html);
723
724
725 # Setup environment to emulate CGI param if needed
726 #
727  
728 my %env=(
729 6 ▶ QUERY_STRING => sub { $req_or->uri->query },
Subroutine __ANON__: called CC=1 SCAR=0.0
730 6 ▶ REQUEST_METHOD => sub { $req_or->method },
Subroutine __ANON__: called CC=1 SCAR=0.0
731 6 ▶ CONTENT_TYPE => sub { $req_or->content_type },
Subroutine __ANON__: called CC=1 SCAR=0.0
732 6 ▶ CONTENT_LENGTH => sub { $req_or->content_length }
Subroutine __ANON__: called CC=1 SCAR=0.0
733 6 );
734 6 local %ENV = %ENV;
735 6 ▶ while (my($key, $cr)=each %env) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %env) 24 6 30
736 24 ▶ if (defined(my $value=$cr->($key))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = &$cr($key))) 12 12 24
737 12 $ENV{$key}=$value;
738 }
739 }
740
741
742 # Setup stdin if there is a content length
743 #
744 6 my $io_fh;
745 6 ▶ if (defined($ENV{'CONTENT_LENGTH'}) && ($ENV{'CONTENT_LENGTH'} > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"CONTENT_LENGTH"} and $ENV{"CONTENT_LENGTH"} > 0) 0 6 6
Condition33%1/3
defined $ENV{"CONTENT_LENGTH"} and $ENV{"CONTENT_LENGTH"} > 0
!ll&&!rl&&r
600
MC/DC0%0/2
defined $ENV{"CONTENT_LENGTH"} and $ENV{"CONTENT_LENGTH"} > 0
defined $ENV{"CONTENT_LENGTH"} $ENV{"CONTENT_LENGTH"} > 0
Truth table33%1/3
A and B
A= defined $ENV{"CONTENT_LENGTH"}B= $ENV{"CONTENT_LENGTH"} > 0
ABresult
0X0
100
111
746 0 $io_fh=IO::String->new($req_or->content());
747 }
748 6 ▶ local *STDIN=$io_fh if $io_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $io_fh 0 6 6
749
750
751 # Get new request handler
752 #
753 6 require WebDyne::Request::Fake;
754 my $r=WebDyne::Request::Fake->new(
755  
756 filename => $req_or->uri->path(),
757 select => $html_fh,
758 input => $io_fh,
759 6 ▶ no_head_insert => $opt_hr->{'no_head_insert'},
Condition50%1/2
'WebDyne::Request::Fake'->new('filename', $req_or->uri->path, 'select', $html_fh, 'input', $io_fh, 'no_head_insert', $opt_hr->{'no_head_insert'}) || (return err())
l!l
60
MC/DC0%0/1
'WebDyne::Request::Fake'->new('filename', $req_or->uri->path, 'select', $html_fh, 'input', $io_fh, 'no_head_insert', $opt_hr->{'no_head_insert'}) || (return err())
'WebDyne::Request::Fake'->new('filename', $req_or->uri->path, 'select', $html_fh, 'input', $io_fh, 'no_head_insert', $opt_hr->{'no_head_insert'})
Truth table50%1/2
A
A= 'WebDyne::Request::Fake'->new('filename', $req_or->uri->path, 'select', $html_fh, 'input', $io_fh, 'no_head_insert', $opt_hr->{'no_head_insert'})
Aresult
11
00
760  
761 ) || return err();
762
763  
764 # Set headers
765 #
766 6 $r->headers_in($req_or->headers->flatten);
767  
768  
769 # Get handler
770 #
771 6 my $handler=$opt_hr->{'handler'};
772  
773  
774 # Load up whichever handler we are using
775 #
776 6 (my $handler_pm = $handler) =~ s{::}{/}g;
777 6 $handler_pm.='.pm';
778 6 ▶ eval {require $handler_pm} ||
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $handler_pm } } or return err("$main::Script: unable to load handler $handler, $@") 6 0 6
779 return err("$Script: unable to load handler $handler, $@");
780  
781  
782 # Set text errors only
783 #
784 #$WebDyne::Err::WEBDYNE_ERROR_TEXT=1 if ($opt{'error'} eq 'text');
785  
786  
787 # Set header, warning output
788 #
789 #$r->notes('noheader', $opt{'header'} ? 0 : 1);
790 #$r->notes('nowarn', $opt{'warn'} ? 0 : 1);
791  
792  
793 # Run it and display results, or any error generated
794 #
795 #defined(my $status=$handler->handler(grep {$_} $r, $opt_hr)) || return err();
796 6 ▶ defined(my $status=$handler->handler(grep {$_} $r, $opt_hr->{'param'})) || return err();
Branch: 1 branch
BranchTrueFalseTotal
defined(my $status = $handler->handler(grep({$_;} $r, $opt_hr->{"param"}))) or return err() 6 0 6
797
798
799 # Close file handle
800 #
801 6 $html_fh->close();
802
803  
804 # HTTP::Response requires bytes rather than Perl character strings.
805 #
806 6 ▶ if (utf8::is_utf8($html)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($html)) 0 6 6
807 0 utf8::encode($html);
808 0 my $headers_or=$r->headers_out();
809 0 delete $headers_or->{$_} foreach grep {lc($_) eq 'content-length'} keys %{$headers_or};
810 0 $headers_or->header('Content-Length' => length($html));
811 }
812  
813  
814 # Construct HTTP::Response and return it
815 #
816 my $res_or=HTTP::Response->new(
817 $status,
818 HTTP::Status->status_message($status),
819 6 HTTP::Headers->new(%{$r->headers_out()}),
820 $html
821 );
822
823  
824 # Return response obkect
825 #
826 6 return $res_or;
827
828 }
829  
830  
831 0 ▶sub html_colour {
Subroutine html_colour: not called CC=5 SCAR=34.0
832  
833  
834 # Colouris HTML
835 #
836 0 my ($html, $header, $opt_hr)=@_;
837  
838  
839 # Try to load colour module
840 #
841 0 eval { require Syntax::Highlight::Engine::Kate } ||
842 0 ▶ return do { warn("install Syntax::Highlight::Engine::Kate to colourise output.\n") if $opt_hr->{'warn'}; $html };
Branch: 2 branches
BranchTrueFalseTotal
eval { do { require Syntax::Highlight::Engine::Kate } } or return do { warn "install Syntax::Highlight::Engine::Kate to colourise output.\n" if $opt_hr->{"warn"}; $html } 0 0 0
if $opt_hr->{"warn"} 0 0 0
843  
844
845 # Var to hold output
846 #
847 0 my $html_colour;
848  
849  
850 # Parse optional theme argument
851 #
852 0 my $theme = $opt_hr->{'theme'};
853  
854  
855 # Theme definitions
856 #
857 0 my %themes = (
858 dark => {
859 Alert => [ color('bold red'), color('reset') ],
860 Error => [ color('bold red'), color('reset') ],
861 Warning => [ color('red'), color('reset') ],
862  
863 Comment => [ color('bright_black'), color('reset') ],
864 Documentation => [ color('bright_black'), color('reset') ],
865  
866 Keyword => [ color('bold yellow'), color('reset') ],
867 ControlFlow => [ color('bold yellow'), color('reset') ],
868 Operator => [ color('blue'), color('reset') ],
869  
870 DataType => [ color('cyan'), color('reset') ],
871 DecVal => [ color('cyan'), color('reset') ],
872 Float => [ color('cyan'), color('reset') ],
873 BaseN => [ color('cyan'), color('reset') ],
874 Constant => [ color('cyan'), color('reset') ],
875  
876 String => [ color('green'), color('reset') ],
877 Char => [ color('green'), color('reset') ],
878 SpecialString => [ color('green'), color('reset') ],
879  
880 Function => [ color('magenta'), color('reset') ],
881 Variable => [ color('white'), color('reset') ],
882  
883 Others => [ color('blue'), color('reset') ],
884 Normal => [ '', '' ],
885 BString => [ '', '' ],
886 Reserved => [ '', '' ],
887  
888 LineNum => color('bright_black'),
889 },
890  
891 light => {
892 Alert => [ color('red'), color('reset') ],
893 Error => [ color('red'), color('reset') ],
894 Warning => [ color('magenta'), color('reset') ],
895  
896 Comment => [ color('bright_black'), color('reset') ],
897 Documentation => [ color('bright_black'), color('reset') ],
898  
899 Keyword => [ color('bold blue'), color('reset') ],
900 ControlFlow => [ color('bold blue'), color('reset') ],
901 Operator => [ color('cyan'), color('reset') ],
902  
903 DataType => [ color('magenta'), color('reset') ],
904 DecVal => [ color('magenta'), color('reset') ],
905 Float => [ color('magenta'), color('reset') ],
906 BaseN => [ color('magenta'), color('reset') ],
907 Constant => [ color('magenta'), color('reset') ],
908  
909 String => [ color('bold green'), color('reset') ],
910 Char => [ color('bold green'), color('reset') ],
911 SpecialString => [ color('bold green'), color('reset') ],
912  
913 Function => [ color('bold red'), color('reset') ],
914 Variable => [ color('black'), color('reset') ],
915  
916 Others => [ color('cyan'), color('reset') ],
917 Normal => [ '', '' ],
918 BString => [ '', '' ],
919 Reserved => [ '', '' ],
920  
921 LineNum => color('bright_black'),
922 }
923 );
924 0 my $colors = $themes{$theme};
925  
926  
927 # Set up Kate highlighter
928 #
929 my $hl = Syntax::Highlight::Engine::Kate->new(
930 language => 'HTML',
931 substitutions => {
932 0 map { $_ => $colors->{$_}[0] } keys %{ $themes{$theme} }
933 },
934 format_table => {
935 0 map { $_ => $colors->{$_} } keys %{ $themes{$theme} }
936 },
937 );
938  
939 # Highlight
940 #
941 0 my $html_highlighted = $hl->highlightText($html);
942  
943  
944 # Print with line numbers if wanted
945 #
946 0 ▶ if ($opt_hr->{'lineno'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_hr->{'lineno'}) { } 0 0 0
947 0 my @lines = split /\n/, (join("\n", grep {$_} $header, $html_highlighted));
948 0 my $width = length(scalar @lines);
949 0 for my $i (0..$#lines) {
950 0 my $num = sprintf("%*d", $width, $i + 1);
951 0 $html_colour.=join('', $colors->{LineNum}, "$num | ", color('reset'), "$lines[$i]\n");
952 }
953 }
954 else {
955
956 # Or just raw
957 #
958 0 $html_colour=$html_highlighted
959 }
960
961
962 # Done
963 #
964 0 return $html_colour;
965  
966 }
967  
968  
969 1 ▶sub html_tidy {
Subroutine html_tidy: called CC=3 SCAR=12.3
970  
971 1 my ($html, $opt_hr)=@_;
972 1 eval { require HTML::Tidy5 } ||
973 1 ▶ return do { warn("install system libtidy-devel package and HTML::Tidy5 to tidy output.\n") if $opt_hr->{'warn'}; $html };
Branch: 2 branches
BranchTrueFalseTotal
eval { do { require HTML::Tidy5 } } or return do { warn "install system libtidy-devel package and HTML::Tidy5 to tidy output.\n" if $opt_hr->{"warn"}; $html } 0 1 1
if $opt_hr->{"warn"} 0 1 1
974 0 $html=HTML::Tidy5->new($WEBDYNE_HTML_TIDY_CONFIG_HR)->clean($html);
975 0 return $html;
976  
977 }
978  
979  
980 __END__
« lib/WebDyne/Util.pm ↑ Summary blib/lib/WebDyne/Util.pm »