bin/webdyne.apache

filter: Statementstmt 83.4% 126 / 151 Branchbran 65.3% 34 / 52 Conditioncond 50.0% 16 / 32 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 91.3% 21 / 23 Podpod n/a totaltotal 73.5% 203 / 276 CC 39 SCAR 26.7
CC
39
Cov
75%
request_mod_perl_postamble
25.5
main
23.2
BEGIN
12.2
CRAP 14.4
« blib/lib/WebDyne/Template.pm ↑ Summary blib/lib/WebDyne/Constant.pm »
1 #!/usr/bin/env perl
2  
3 #
4 # This file is part of WebDyne.
5 #
6 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
7 #
8 # This is free software; you can redistribute it and/or modify it under
9 # the same terms as the Perl 5 programming language system itself.
10 #
11 # Full license text is available at:
12 #
13 # <http://dev.perl.org/licenses/>
14 #
15  
16  
17 # Pragma
18 #
19 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 # External modules
26 #
27 11 ▶use Cwd qw(fastcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 11 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 11 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 11 ▶use POSIX qw(pause);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 # Local customisation
39 #
40 11 local $Data::Dumper::Indent=1;
41 11 local $Data::Dumper::Sortkeys=1;
42  
43  
44 # WebDyne modules
45 #
46 11 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 11 ▶use WebDyne::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49  
50 # Version information
51 #
52 11 $VERSION='3.029';
53  
54  
55 # Check for supporting modules
56 #
57 11 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=25.0
58 11 ▶ eval ("require Apache2::Build; 1") || do {
Branch: 1 branch
BranchTrueFalseTotal
eval "require Apache2::Build; 1" or do { if ($@ =~ /Module::CoreList/i) { print STDERR "Apache2::Build found but needs rebuild/reinstall of Module::CoreList. Please install/upgrade Module::CoreList\n"; exit 1; } } 11 0 11
59 0 ▶ if ($@=~/Module::CoreList/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Module::CoreList/i) 0 0 0
60 0 print STDERR ("Apache2::Build found but needs rebuild/reinstall of Module::CoreList. Please install/upgrade Module::CoreList\n");
61 0 exit 1;
62 }
63 };
64 11 my @missing;
65 11 for my $module (qw(Apache2::Build Apache::Test Module::CoreList)) {
66 33 ▶ eval "require $module; 1" or push @missing, $module;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $module; 1" or push @missing, $module 33 0 33
67 }
68 11 ▶ if (@missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 0 11 11
69 0 printf STDERR ("Please install missing CPAN modules (you may need mod_perl-devel packages also): %s \n", join(', ', @missing));
70 0 exit 1;
71 }
72  
73 }
74  
75  
76 # Run main
77 #
78 11 ▶exit ${&main(\@ARGV) || die errdump()};
Condition0%0/2
&main(\@ARGV) || die(errdump())
l!l
00
MC/DC0%0/1
&main(\@ARGV) || die(errdump())
&main(\@ARGV)
Truth table0%0/2
A
A= &main(\@ARGV)
Aresult
11
00
79  
80  
81 #==================================================================================================
82  
83  
84 11 ▶sub main {
Subroutine main: called CC=18 SCAR=31.5
85  
86  
87 # Get argv array ref
88 #
89 11 my $argv_ar=shift();
90  
91  
92 # Defaults
93 #
94 my %opt=(
95 index => defined($ENV{'DOCUMENT_DEFAULT'}) ? $ENV{'DOCUMENT_DEFAULT'} : 0,
96 keep_tmp => 0,
97 port => ($^O eq 'darwin') ? 5001 : 5000,
98 11 ▶ %{do(glob(sprintf('~/.%s.opt', basename(__FILE__)))) || {}}
Branch: 2 branches
BranchTrueFalseTotal
defined $ENV{'DOCUMENT_DEFAULT'} ? : 2 9 11
$^O eq 'darwin' ? : 0 11 11
Condition100%2/2
do (glob(sprintf('~/.%s.opt', basename(__FILE__)))) || {}
l!l
38
MC/DC100%1/1
do (glob(sprintf('~/.%s.opt', basename(__FILE__)))) || {}
do (glob(sprintf('~/.%s.opt', basename(__FILE__))))
Truth table100%2/2
A
A= do (glob(sprintf('~/.%s.opt', basename(__FILE__))))
Aresult
11
00
99 );
100 11 ▶ if (delete $opt{'no_index'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $opt{"no_index"}) 2 9 11
101 2 $opt{'index'}=0;
102 }
103  
104  
105 # Process
106 #
107 11 Getopt::Long::Configure('pass_through');
108 11 @{$argv_ar}=grep {
109 27 ▶ if ($_ eq '--no-index') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq '--no-index') { } 3 24 27
elsif (/^--index=(.*)$/) { } 0 24 24
110 3 $opt{'index'}=0;
111 3 0;
112 }
113 elsif (/^--index=(.*)$/) {
114 0 $opt{'index'}=$1;
115 0 0;
116 }
117 else {
118 24 1;
119 }
120 11 } @{$argv_ar};
121 GetOptions(
122 \%opt,
123 my @opt=(
124 3 ▶ 'index!' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
125 3 my ($name, $value)=@_;
126 3 ▶ $opt{'index'}=$value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 3 0 3
127 },
128 0 ▶ 'no_index' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 $opt{'index'}=0
130 },
131 11 ▶ 'view_source|view-source!',
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, my(@opt) = ("index!", sub { my($name, $value) = @_; $opt{"index"} = $value ? 1 : 0; } , "no_index", sub { $opt{"index"} = 0; } , "view_source|view-source!", "root|docroot|doc_root|doc-root|document_root|document-root=s", "port=s", "keep_tmp|keep-tmp!", "test!", "dump_postamble|dump-postamble", "dump_opt|dump-opt|opt", "argv=s", "help|?")) or return err("bad options") 11 0 11
132 'root|docroot|doc_root|doc-root|document_root|document-root=s',
133 'port=s',
134 'keep_tmp|keep-tmp!',
135 'test!',
136 'dump_postamble|dump-postamble',
137 'dump_opt|dump-opt|opt',
138 'argv=s',
139 'help|?'
140 )
141 ) || return err('bad options');
142 11 ▶ map {$opt{"no_${_}"} = $opt{$_} ? 0 : 1} map { /^([^|!:=+]+)/ } grep {!ref($_) && /\!$/} @opt;
Branch: 1 branch
BranchTrueFalseTotal
$opt{$_} ? : 8 36 44
Condition100%3/3
!ref($_) && /\!$/
!ll&&!rl&&r
227744
MC/DC100%2/2
!ref($_) && /\!$/
!ref($_) /\!$/
Truth table100%3/3
not(A) && B
A= !ref($_)B= /\!$/
ABresult
0X0
100
111
143  
144  
145 # Last argument is root directory
146 #
147 11 ▶ if (@{$argv_ar} && $argv_ar->[-1] !~ /^--?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$argv_ar;} and not $argv_ar->[-1] =~ /^--?/) 3 8 11
Condition66%2/3
@{$argv_ar;} and not $argv_ar->[-1] =~ /^--?/
!ll&&!rl&&r
803
MC/DC50%1/2
@{$argv_ar;} and not $argv_ar->[-1] =~ /^--?/
@{$argv_ar;} not $argv_ar->[-1] =~ /^--?/
Truth table66%2/3
A and not(B)
A= @{$argv_ar;}B= not $argv_ar->[-1] =~ /^--?/
ABresult
0X0
100
111
148 3 $opt{'root'} = pop @{$argv_ar};
149 }
150 11 ▶ $opt{'root'} ||=($ENV{'DOCUMENT_ROOT'} || fastcwd());
Condition66%4/6
$opt{"root"} ||= $ENV{"DOCUMENT_ROOT"} || fastcwd()
l!l&&r!l&&!r
380
$ENV{'DOCUMENT_ROOT'} || fastcwd()
l!l&&r!l&&!r
530
MC/DC0%0/3
$opt{"root"} ||= $ENV{"DOCUMENT_ROOT"} || fastcwd()
$opt{"root"} $ENV{'DOCUMENT_ROOT'} fastcwd()
Truth table0%0/4
A ||= B || C
A= $opt{"root"}B= $ENV{'DOCUMENT_ROOT'}C= fastcwd()
ABCresult
1XX1
01X1
0011
0000
151  
152  
153 # File or dir ? If file Apache won't like as DOCUMENT_ROOT so need to set
154 # as Perl environment var in httpd.conf (later) and handle differently
155 #
156 11 ▶ if (-f ($opt{'root'}=File::Spec->rel2abs($opt{'root'}))) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f ($opt{'root'} = 'File::Spec'->rel2abs($opt{'root'}))) { } 2 9 11
elsif ($opt{'test'}) { } 0 9 9
157 2 $opt{'root_pn'}=$opt{'root'};
158 2 $opt{'root'}=dirname($opt{'root'});
159 }
160 elsif($opt{'test'}) {
161 0 $opt{'root_pn'}=$WEBDYNE_DEFAULT_TEST_FN;
162 }
163  
164 # Dump options for debugging
165 #
166 11 &view_source_apply(\%opt);
167 11 ▶ die Dumper(\%opt) if $opt{'dump_opt'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"dump_opt"} 9 2 11
168
169
170 # Startup server
171 #
172 2 printf("Starting server on localhost (127.0.0.1), port: %s\n", $opt{'port'});
173 2 ▶ my $runner=request_mod_perl_startup(\%opt) || return err();
Condition0%0/2
request_mod_perl_startup(\%opt) || (return err())
l!l
00
MC/DC0%0/1
request_mod_perl_startup(\%opt) || (return err())
request_mod_perl_startup(\%opt)
Truth table0%0/2
A
A= request_mod_perl_startup(\%opt)
Aresult
11
00
174  
175  
176 # Shutdown on signal
177 #
178 0 ▶ my $shutdown_cr=sub { apache_shutdown($runner); exit 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 0 $SIG{'INT'}=$shutdown_cr;
180 0 $SIG{'TERM'}=$shutdown_cr;
181  
182  
183 # Block forever
184 #
185 0 while (1) {
186 0 pause();
187 }
188  
189 }
190  
191  
192 11 ▶sub view_source_apply {
Subroutine view_source_apply: called CC=3 SCAR=11.0
193  
194  
195 # Source viewing is an extra opt-in on top of built-in index handling.
196 #
197 11 my $opt_hr=shift();
198 11 ▶ if ($opt_hr->{'index'} && $opt_hr->{'view_source'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_hr->{'index'} and $opt_hr->{'view_source'}) { } 2 9 11
Condition100%3/3
$opt_hr->{"index"} and $opt_hr->{"view_source"}
!ll&&!rl&&r
632
MC/DC100%2/2
$opt_hr->{"index"} and $opt_hr->{"view_source"}
$opt_hr->{"index"} $opt_hr->{"view_source"}
Truth table100%3/3
A and B
A= $opt_hr->{"index"}B= $opt_hr->{"view_source"}
ABresult
0X0
100
111
199 2 $ENV{'WEBDYNE_INDEX_SOURCE_ENABLE'}=1;
200 2 $WebDyne::WEBDYNE_INDEX_SOURCE_ENABLE=1;
201 2 $WebDyne::Constant::Constant{'WEBDYNE_INDEX_SOURCE_ENABLE'}=1;
202 }
203 else {
204 9 $opt_hr->{'view_source'}=0;
205 }
206 11 return \undef;
207  
208 }
209  
210  
211 2 ▶sub request_mod_perl_startup {
Subroutine request_mod_perl_startup: called CC=1 SCAR=0.0
212  
213  
214 # Startup Apache test instance
215 #
216 2 my $opt_hr=shift();
217  
218  
219 # Get postamble with WebDyne config
220 #
221 2 my $postamble=request_mod_perl_postamble($opt_hr->{'root'}, $opt_hr);
222  
223  
224 # Start Apache using Apache::Test.
225 #
226 return apache_startup({
227 port => $opt_hr->{'port'},
228 documentroot => $opt_hr->{'root'},
229 postamble => $postamble,
230 2 keep_tmp => $opt_hr->{'keep_tmp'},
231 cache_dn_env => 1,
232 });
233  
234 }
235  
236  
237 2 ▶sub request_mod_perl_postamble {
Subroutine request_mod_perl_postamble: called CC=14 SCAR=32.4
238  
239  
240 # Get all WEBDYNE_CONF env vars and setup config file
241 #
242 2 my ($document_root, $opt_hr)=@_;
243 2 $document_root=File::Spec->rel2abs($document_root);
244  
245  
246 # Need to work out if stderr writable. If not need to use alternate log file (prob running in container)
247 #
248 2 my $stderr_fn='/dev/stderr';
249 2 my $stdout_fn='/dev/stdout';
250 2 ▶ if (! open(my $fh, '>>', $stderr_fn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open my $fh, ">>", $stderr_fn) 0 2 2
251  
252 # Not writable, probably running under docker, revert to traditional
253 #
254 0 print STDERR "unable to write to stderr,stdout - reverting to log files\n";
255 0 $stderr_fn='logs/error.log';
256 0 $stdout_fn='logs/access.log';
257 }
258  
259  
260
261 # Kept for reference, but disabled for now. WebDyne::Util::apache_startup()
262 # already sets APACHE_TEST_ULIMIT_SET before Apache::TestRunPerl is loaded,
263 # and this shell probe emits noisy stderr on restricted CPAN smoke hosts
264 # where raising the core-file ulimit is not permitted.
265 #
266 # if (my $out = qx{sh -c 'ulimit -c unlimited 2>&1'}) {
267 # my $rc = $? >> 8;
268 # if ($rc != 0) {
269 # print STDERR "unable alter ulimit, stubbing Apache::TestRun::ulimit\n";
270 # no warnings qw(once redefine);
271 # *Apache::TestRun::set_ulimit=sub {};
272 # }
273 # }
274
275
276 # Running on Mac ? Need to load mod_rewrite manually.
277 #
278 2 my $module_load_rewrite='';
279 2 ▶ if ($^O eq 'darwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "darwin") 0 2 2
280 0 ▶ my $apxs = $ENV{APXS} || $ENV{APACHE_TEST_APXS} || 'apxs';
Condition0%0/5
$ENV{'APXS'} || $ENV{'APACHE_TEST_APXS'} || 'apxs'
l!l
00
$ENV{"APXS"} or $ENV{"APACHE_TEST_APXS"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'APXS'} || $ENV{'APACHE_TEST_APXS'} || 'apxs'
$ENV{"APXS"} $ENV{"APACHE_TEST_APXS"}
Truth table0%0/3
A or B
A= $ENV{"APXS"}B= $ENV{"APACHE_TEST_APXS"}
ABresult
1X1
011
000
281 0 my $libexecdir = qx{"$apxs" -q 'libexecdir' 2>/dev/null};
282 0 ▶ die "Unable to query 'libexecdir' using $apxs\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 0 0
283 0 chomp $libexecdir;
284 0 $module_load_rewrite= <<"END"
285 <IfModule !rewrite_module>
286 LoadModule rewrite_module $libexecdir/mod_rewrite.so
287 </IfModule>
288 END
289 ;
290 }
291
292
293 # Get any additional library paths we want
294 #
295 2 my @perl_inc_dn=@{&perl_inc_dn()};
296  
297  
298 # Ok - proceed to build postamble
299 #
300 2 my $index_postamble='';
301 2 ▶ if (my $document_default=$opt_hr->{'index'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $document_default = $opt_hr->{"index"}) 1 1 2
302 1 ▶ if ($document_default eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($document_default eq 1) 1 0 1
303 1 ▶ $document_default=File::Spec->rel2abs($WEBDYNE_DEFAULT_INDEX_FN || File::Spec->catfile($document_root, 'index.psp'));
Condition33%1/3
$main::WEBDYNE_DEFAULT_INDEX_FN || 'File::Spec'->catfile($document_root, 'index.psp')
l!l&&r!l&&!r
100
MC/DC0%0/2
$main::WEBDYNE_DEFAULT_INDEX_FN || 'File::Spec'->catfile($document_root, 'index.psp')
$main::WEBDYNE_DEFAULT_INDEX_FN 'File::Spec'->catfile($document_root, 'index.psp')
Truth table33%1/3
A || B
A= $main::WEBDYNE_DEFAULT_INDEX_FNB= 'File::Spec'->catfile($document_root, 'index.psp')
ABresult
1X1
011
000
304 }
305 1 ▶ if (File::Spec->file_name_is_absolute($document_default)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($document_default)) { } 1 0 1
306 1 my $default_index_fn=File::Spec->rel2abs($document_default);
307 1 my $default_index_dn=dirname($default_index_fn);
308 1 $index_postamble= <<"END";
309 PerlSetEnv DOCUMENT_DEFAULT $default_index_fn
310 DirectoryIndex index.psp
311 Alias /index.psp $default_index_fn
312 AliasMatch ^/(.+)/index\\.psp\$ $default_index_fn
313  
314 <Directory "$default_index_dn">
315 Require all granted
316 </Directory>
317 RewriteEngine On
318 RewriteCond %{REQUEST_URI} !^/index\\.psp\$
319 RewriteCond %{REQUEST_FILENAME} -d
320 RewriteRule ^ /index.psp [L,PT]
321 END
322 }
323 else {
324 0 $index_postamble= <<"END";
325 PerlSetEnv DOCUMENT_DEFAULT $document_default
326 DirectoryIndex $document_default
327 END
328 }
329 }
330 1 my @PerlSetEnv=map { sprintf('PerlSetEnv %s %s', $_, $ENV{$_}) }
331 1 ▶ grep { defined($ENV{$_}) && $ENV{$_} ne '' }
Condition33%1/3
defined $ENV{$_} && $ENV{$_} ne ''
!ll&&!rl&&r
001
MC/DC0%0/2
defined $ENV{$_} && $ENV{$_} ne ''
defined $ENV{$_} $ENV{$_} ne ''
Truth table33%1/3
A && B
A= defined $ENV{$_}B= $ENV{$_} ne ''
ABresult
0X0
100
111
332 2 grep { /^WEBDYNE_/ } keys %ENV;
333 2 ▶ push (@PerlSetEnv, sprintf('PerlSetEnv DOCUMENT_ROOT %s', $opt_hr->{'root_pn'})) if $opt_hr->{'root_pn'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_hr->{"root_pn"} 1 1 2
334 2 ▶ push (@PerlSetEnv, "PerlSetEnv WEBDYNE_ERROR_TEXT 1") unless defined($ENV{'WEBDYNE_ERROR_TEXT'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"WEBDYNE_ERROR_TEXT"} 2 0 2
335 2 my $PerlSetEnv=join("\n", @PerlSetEnv);
336 2 my $PerlSwitches=join("\n", map { sprintf('PerlSwitches -I%s', $_) } @perl_inc_dn);
337 2 my $postamble= <<"END"
338 $module_load_rewrite
339 $PerlSetEnv
340 $PerlSwitches
341 PerlModule WebDyne
342 AddHandler modperl .psp
343 PerlResponseHandler WebDyne
344 $index_postamble
345 #PerlOptions +GlobalRequest
346  
347 ErrorLog $stderr_fn
348 LogLevel debug
349 CustomLog $stdout_fn combined
350  
351 <Directory "$document_root">
352 Require all granted
353 </Directory>
354 END
355 ;
356 2 ▶ exit print $postamble if $opt_hr->{'dump_postamble'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_hr->{"dump_postamble"} 0 2 2
357 2 return $postamble;
358 }
359  
360  
361 __END__
« blib/lib/WebDyne/Template.pm ↑ Summary blib/lib/WebDyne/Constant.pm »