SSI/URI.pm

filter: Statementstmt 56.9% 274 / 481 Branchbran 37.3% 106 / 284 Conditioncond 24.0% 26 / 108 MC/DCmcdc 4.6% 3 / 65 Subroutinesub 77.2% 34 / 44 Podpod 91.6% 22 / 24 totaltotal 46.2% 465 / 1006 CC 183 SCAR 42.7
CC
183
Cov
47%
lookup_uri
380.0
document_uri
139.3
make
110.0
CRAP 71.2
« SSI/File/Type.pm ↑ Summary SSI/SharedMem.pm »
1 ##----------------------------------------------------------------------------
2 ## Apache2 Server Side Include Parser - ~/lib/Apache2/SSI/URI.pm
3 ## Version v0.1.3
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2020/12/18
7 ## Modified 2024/09/04
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Apache2::SSI::URI;
14 BEGIN
15 9 ▶{
Subroutine BEGIN: called CC=2 SCAR=7.0
16 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶ use parent qw( Apache2::SSI::Common );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶ use vars qw( $VERSION $DEBUG $DIR_SEP );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶ use Apache2::SSI::Finfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶ use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶ use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 require constant;
25 9 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶ use constant URI_CLASS => 'URI';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶ use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶ if( $ENV{MOD_PERL} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOD_PERL"}) 0 9 9
29 {
30 0 require Apache2::RequestRec;
31 0 require Apache2::RequestUtil;
32 0 require Apache2::SubRequest;
33 0 require Apache2::Access;
34 0 require Apache2::Const;
35 0 Apache2::Const->import( compile => qw( :common :http OK DECLINED ) );
36 }
37 9 our( $DEBUG );
38 9 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
39 23 ▶ q{""} => sub { $_[0]->document_uri->as_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 bool => sub () { 1 },
41 9 fallback => 1,
42 9 );
43 9 our $VERSION = 'v0.1.3';
44 9 our $DIR_SEP = $Apache2::SSI::Common::DIR_SEP;
45 };
46  
47 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # document_root = /home/joe/www
51 # base_uri = /my/uri/file.html/some/path/info?q=something&l=ja_JP
52 # base_uri is the current reference document
53 # document_uri = ./about.html
54 # document_uri is the uri which is the purpose of this object. It will be made absolute and its dots flattened
55 # Example: ../about.html?q=hello would become /my/about.html?q=hello
56 sub init
57 65 ▶{
Subroutine init: called CC=8 SCAR=21.9
Pod documented
58 65 my $self = shift( @_ );
59 65 $self->{apache_request} = '';
60 65 ▶ $self->{base_uri} = '/' unless( length( $self->{base_uri} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"base_uri"} 65 0 65
61 # By default
62 65 $self->{code} = 200;
63 65 $self->{document_path} = '';
64 65 $self->{document_root} = '';
65 # Reference document for the main request
66 65 $self->{document_uri} = '';
67 65 $self->{filepath} = '';
68 65 $self->{finfo} = '';
69 65 $self->{_init_params_order} = [qw( apache_request document_root base_uri document_uri document_path filepath )];
70 65 $self->{_init_strict_use_sub} = 1;
71 65 ▶ $self->SUPER::init( @_ ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return 65 0 65
72 65 $self->{_env} = {};
73 65 $self->{_path_info_processed} = 0;
74 65 $self->{_uri_reset} = 0;
75 65 ▶ $self->{document_root} ||= $self->env( 'DOCUMENT_ROOT' );
Condition33%1/3
$self->{"document_root"} ||= $self->env("DOCUMENT_ROOT")
l!l&&r!l&&!r
6500
MC/DC0%0/2
$self->{"document_root"} ||= $self->env("DOCUMENT_ROOT")
$self->{"document_root"} $self->env("DOCUMENT_ROOT")
Truth table33%1/3
A ||= B
A= $self->{"document_root"}B= $self->env("DOCUMENT_ROOT")
ABresult
1X1
011
000
76 65 ▶ $self->{base_uri} ||= $self->env( 'DOCUMENT_URI' );
Condition33%1/3
$self->{"base_uri"} ||= $self->env("DOCUMENT_URI")
l!l&&r!l&&!r
6500
MC/DC0%0/2
$self->{"base_uri"} ||= $self->env("DOCUMENT_URI")
$self->{"base_uri"} $self->env("DOCUMENT_URI")
Truth table33%1/3
A ||= B
A= $self->{"base_uri"}B= $self->env("DOCUMENT_URI")
ABresult
1X1
011
000
77 65 ▶ return( $self->error( "No document root was provided." ) ) if( !length( $self->{document_root} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"document_root"} 0 65 65
78 65 ▶ return( $self->error( "No base uri was provided." ) ) if( !length( $self->{base_uri} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"base_uri"} 0 65 65
79 65 ▶ return( $self->error( "No document uri was provided." ) ) if( !length( $self->{document_uri} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"document_uri"} 0 65 65
80 # Small correction if necessary. If the base uri is a directory, it needs to have a trailing "/", so URI knows this is a directory and not a file.
81 # URI->new( "./file.pl" )->abs( "/ssi/plop" ) becomes "/ssi/file.pl" whereas it should be /ssi/plop/file.pl
82 # $self->{base_uri} .= '/' if( length( $self->{base_uri} ) && -d( "$self->{document_root}$self->{base_uri}" ) && substr( $self->{base_uri}, -1, 1 ) ne '/' );
83 65 return( $self );
84 }
85  
86 1157 ▶sub apache_request { return( shift->_set_get_object_without_init( 'apache_request', 'Apache2::RequestRec', @_ ) ); }
Subroutine apache_request: called CC=1 SCAR=0.0
Pod documented
87  
88 sub base_dir
89 0 ▶{
Subroutine base_dir: not called CC=9 SCAR=45.0
Pod undocumented
90 0 my $self = shift( @_ );
91 0 ▶ return( $self->{base_dir} ) if( length( $self->{base_dir} ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"base_dir"} 0 0 0
92 # Just in case
93 0 ▶ return( $self->root ) if( !length( $self->{base_uri} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"base_uri"} 0 0 0
94 0 my $base = $self->base_uri;
95 0 ▶ return( $self->error( "No base uri defined." ) ) if( !length( $base ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $base 0 0 0
96 0 my $path = $base->document_path;
97 0 my @segments = split( '/', $path, -1 );
98 0 pop( @segments );
99 0 ▶ return( $base ) if( !scalar( @segments ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @segments 0 0 0
100 0 my $r = $self->apache_request;
101 0 my $dir_path = join( '/', @segments );
102
103 0 my $hash = {};
104 0 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
105 {
106 0 my $rr = $self->lookup_uri( $dir_path );
107 0 ▶ if( !defined( $rr ) )
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rr) { } 0 0 0
elsif ($rr->status != &Apache2::Const::HTTP_OK) { } 0 0 0
elsif ($rr->finfo->filetype == &APR::Const::FILETYPE_NOFILE) { } 0 0 0
108 {
109 0 return;
110 }
111 elsif( $rr->status != &Apache2::Const::HTTP_OK )
112 {
113 0 return( $self->error( "Could not look up base directory \"$dir_path\". Returned code is: ", $rr->status ) );
114 }
115 elsif( $rr->finfo->filetype == &APR::Const::FILETYPE_NOFILE )
116 {
117 0 return( $self->error( "Could not find base directory \"$dir_path\"." ) );
118 }
119 # Remove trailing slash
120 0 my $u = $self->_trim_trailing_slash( $rr->uri );
121
122 0 $hash =
123 {
124 apache_request => $self->apache_request,
125 base_dir => $self->root,
126 base_uri => $self->root,
127 document_path => "$u",
128 document_root => $rr->document_root,
129 document_uri => "$u",
130 filename => $rr->filename,
131 path_info => $rr->path_info,
132 query_string => scalar( $rr->args ),
133 _path_info_processed => 1,
134 };
135 }
136 else
137 {
138 0 $hash =
139 {
140 base_dir => $self->root,
141 base_uri => $self->root,
142 document_path => $dir_path,
143 document_root => $self->document_root,
144 document_uri => $dir_path,
145 filename => $self->document_root . $dir_path,
146 path_info => '',
147 query_string => '',
148 _path_info_processed => 1,
149 };
150 }
151 0 $self->{base_dir} = bless( $hash => ref( $self ) );
152 0 return( $self->{base_dir} );
153 }
154  
155 sub base_uri
156 15 ▶{
Subroutine base_uri: called CC=14 SCAR=33.6
Pod documented
157 15 my $self = shift( @_ );
158 15 my $new;
159 15 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 7 8 15
elsif (not ref $self->{'base_uri'}) { } 1 7 8
160 {
161 7 $new = shift( @_ );
162 }
163 elsif( !ref( $self->{base_uri} ) )
164 {
165 1 $new = $self->{base_uri};
166 }
167
168 15 ▶ unless( length( $new ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length $new) 7 8 15
169 {
170 7 return( $self->{base_uri} );
171 }
172
173 8 my $r = $self->apache_request;
174 # We create an URI object, so we can get the path only
175 8 my $u = $self->new_uri( $new );
176 8 my $path = $u->path;
177 8 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 8 8
178 {
179 0 my $rr = $self->lookup_uri( $path );
180 0 ▶ if( !defined( $rr ) )
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rr) { } 0 0 0
elsif ($rr->status != &Apache2::Const::HTTP_OK) { } 0 0 0
elsif ($rr->finfo->filetype == &APR::Const::FILETYPE_NOFILE) { } 0 0 0
181 {
182 0 return;
183 }
184 elsif( $rr->status != &Apache2::Const::HTTP_OK )
185 {
186 0 my $hdrs = $rr->headers_out;
187 0 return( $self->error( "Could not look up uri \"$path\". Returned code is: ", $rr->status ) );
188 }
189 elsif( $rr->finfo->filetype == &APR::Const::FILETYPE_NOFILE )
190 {
191 0 return( $self->error( "Could not find uri \"$path\" (originally $u)." ) );
192 }
193
194 # Remove trailing slash
195 0 my $u2 = $self->_trim_trailing_slash( $rr->unparsed_uri );
196
197 0 my $hash =
198 {
199 apache_request => $r,
200 base_dir => $self->root,
201 base_uri => $self->root,
202 document_path => substr( $u2->path, 0, length( $u2->path ) - length( $rr->path_info ) ),
203 document_root => $self->document_root,
204 document_uri => $u2,
205 filename => $rr->filename,
206 path_info => $rr->path_info,
207 query_string => scalar( $rr->args ),
208 _path_info_processed => 1,
209 };
210 0 ▶ if( $rr->finfo->filetype == &APR::Const::FILETYPE_DIR )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->finfo->filetype == &APR::Const::FILETYPE_DIR) 0 0 0
211 {
212 0 $self->{base_dir} = bless( $hash => ref( $self ) );
213 }
214 0 $self->{base_uri} = bless( $hash => ref( $self ) );
215 }
216 else
217 {
218 # We need to ensure the base uri is free of any path info or query string !
219 8 my $ref = $self->_find_path_info( $u->path );
220 8 ▶ if( !defined( $ref ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ref) { } 0 8 8
elsif ($ref->{'code'} != 200) { } 0 8 8
221 {
222 0 return( $self->error( "Unable to resolve \"$u\"." ) );
223 }
224 elsif( $ref->{code} != 200 )
225 {
226 0 $self->error( "Failed to resolve \"$u\". Resulting code is '$ref->{code}'." );
227 }
228 my $hash =
229 {
230 base_dir => $self->root,
231 base_uri => $self->root,
232 document_path => $ref->{path},
233 document_root => $self->document_root,
234 filename => $ref->{filepath},
235 path_info => $ref->{path_info},
236 query_string => $ref->{query_string},
237 8 _path_info_processed => 1,
238 };
239 8 ▶ my $tmp = $self->new_uri( $ref->{path_info} ? join( '', $ref->{path}, $ref->{path_info} ) : $ref->{path} );
Branch: 1 branch
BranchTrueFalseTotal
$ref->{'path_info'} ? : 1 7 8
240 8 ▶ $tmp->query( $ref->{query_string} ) if( $ref->{query_string} );
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"query_string"} 0 8 8
241 8 $hash->{document_uri} = $tmp;
242 8 ▶ $self->{base_dir} = bless( $hash => ref( $self ) ) if( -d( $ref->{path} ) );
Branch: 1 branch
BranchTrueFalseTotal
if -d $ref->{"path"} 1 7 8
243 8 $self->{base_uri} = bless( $hash => ref( $self ) );
244 }
245 8 return( $self->{base_uri} );
246 }
247  
248 sub clone
249 0 ▶{
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
250 0 my $self = shift( @_ );
251 0 my $new = {};
252 0 my @fields = grep( !/^(apache_request|finfo)$/, keys( %$self ) );
253 0 @$new{ @fields } = @$self{ @fields };
254 0 $new->{apache_request} = $self->{apache_request};
255 0 my $env = {};
256 0 %$env = %{$self->{_env}};
257 0 $new->{_env} = $env;
258 0 ▶ return( bless( $new => ( ref( $self ) || $self ) ) );
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
259 }
260  
261 sub code
262 113 ▶{
Subroutine code: called CC=4 SCAR=15.9
Pod documented
263 113 my $self = shift( @_ );
264 113 my $r = $self->apache_request;
265 113 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 113 113
266 {
267 0 ▶ $r->status( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
268 0 return( $r->status );
269 }
270 else
271 {
272 113 ▶ $self->{code} = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 74 39 113
273 113 return( int( $self->{code} ) );
274 }
275 }
276  
277 0 ▶sub document_dir { return( shift->document_directory( @_ ) ); }
Subroutine document_dir: not called CC=1 SCAR=6.9
Pod undocumented
278  
279 sub document_directory
280 0 ▶{
Subroutine document_directory: not called CC=5 SCAR=34.0
Pod documented
281 0 my $self = shift( @_ );
282 0 ▶ my $doc_path = $self->document_path || return( $self->error( "No document path set." ) );
Condition0%0/2
$self->document_path || (return $self->error('No document path set.'))
l!l
00
MC/DC0%0/1
$self->document_path || (return $self->error('No document path set.'))
$self->document_path
Truth table0%0/2
A
A= $self->document_path
Aresult
11
00
283 0 ▶ my $doc_root = $self->document_root || return( $self->error( "No document root set." ) );
Condition0%0/2
$self->document_root || (return $self->error('No document root set.'))
l!l
00
MC/DC0%0/1
$self->document_root || (return $self->error('No document root set.'))
$self->document_root
Truth table0%0/2
A
A= $self->document_root
Aresult
11
00
284 0 ▶ return( $self->make( document_uri => $doc_path ) ) if( -e( "${doc_root}${doc_path}" ) && -d( _ ) );
Branch: 1 branch
BranchTrueFalseTotal
if -e "$doc_root$doc_path" and -d _ 0 0 0
Condition0%0/3
-e "$doc_root$doc_path" and -d _
!ll&&!rl&&r
000
MC/DC0%0/2
-e "$doc_root$doc_path" and -d _
-e "$doc_root$doc_path" -d _
Truth table0%0/3
A and B
A= -e "$doc_root$doc_path"B= -d _
ABresult
0X0
100
111
285 0 my $parent = $self->parent;
286 0 return( $parent );
287 }
288  
289 0 ▶sub document_filename { return( shift->filename( @_ ) ); }
Subroutine document_filename: not called CC=1 SCAR=6.9
Pod documented
290  
291 sub document_path
292 74 ▶{
Subroutine document_path: called CC=10 SCAR=36.2
Pod documented
293 74 my $self = shift( @_ );
294 74 my $class = ref( $self );
295 74 ▶ my $caller = (caller(1))[3] // '';
Condition50%1/2
(caller 1)[3] // ""
l!l
740
MC/DC0%0/1
(caller 1)[3] // ""
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
296 # my $caller = substr( $sub, rindex( $sub, ':' ) + 1 );
297 74 my $r = $self->apache_request;
298 74 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 74 74
299 {
300 0 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif (not length $self->{'document_path'}) { } 0 0 0
301 {
302 0 my $uri = shift( @_ );
303 0 ▶ $r = $r->is_initial_req ? $r : $r->main;
Branch: 1 branch
BranchTrueFalseTotal
$r->is_initial_req ? : 0 0 0
304 0 my $rr = $self->lookup_uri( $uri );
305 0 ▶ if( !defined( $rr ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rr) 0 0 0
306 {
307 0 return;
308 }
309 0 my $u = APR::URI->parse( $rr->pool, $r->uri );
310 # Remove trailing slash
311 0 my $u2 = $self->_trim_trailing_slash( $u->rpath );
312 0 $self->{document_path} = $u2;
313 0 ▶ $self->{_uri_reset} = 'document_path' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 0 0
314 }
315 elsif( !length( $self->{document_path} ) )
316 {
317 0 my $u = APR::URI->parse( $r->pool, $r->uri );
318 0 $self->{document_path} = $self->new_uri( $u->rpath );
319 }
320 }
321 else
322 {
323 74 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 74 0 74
324 {
325 74 my $uri = shift( @_ );
326 74 $self->{document_path} = $self->new_uri( $self->collapse_dots( $uri ) );
327 74 ▶ $self->{_uri_reset} = 'document_path' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 74 74
328 }
329 }
330 74 return( $self->{document_path} );
331 }
332  
333 sub document_root
334 311 ▶{
Subroutine document_root: called CC=7 SCAR=21.0
Pod documented
335 311 my $self = shift( @_ );
336 311 my $r = $self->apache_request;
337 311 my $new;
338 311 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 65 246 311
339 {
340 65 $new = shift( @_ );
341 # unless( substr( $new, 0, 1 ) eq '/' )
342 65 ▶ unless( File::Spec->file_name_is_absolute( $new ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($new)) 1 64 65
343 {
344 1 $new = URI::file->new_abs( $new )->file( $^O );
345 }
346 }
347
348 311 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 311 311
349 {
350 0 ▶ $r->document_root( $new ) if( defined( $new ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $new 0 0 0
351 0 $r->subprocess_env( DOCUMENT_ROOT => $r->document_root );
352 0 return( $r->document_root );
353 }
354 else
355 {
356 311 ▶ if( defined( $new ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new) 65 246 311
357 {
358 65 $self->{document_root} = $new;
359 65 $self->_set_env( DOCUMENT_ROOT => $self->{document_root} );
360 }
361 311 ▶ return( $self->{document_root} || $self->env( 'DOCUMENT_ROOT' ) );
Condition33%1/3
$self->{'document_root'} || $self->env('DOCUMENT_ROOT')
l!l&&r!l&&!r
31100
MC/DC0%0/2
$self->{'document_root'} || $self->env('DOCUMENT_ROOT')
$self->{'document_root'} $self->env('DOCUMENT_ROOT')
Truth table33%1/3
A || B
A= $self->{'document_root'}B= $self->env('DOCUMENT_ROOT')
ABresult
1X1
011
000
362 }
363 }
364  
365 sub document_uri
366 94 ▶{
Subroutine document_uri: called CC=26 SCAR=49.4
Pod documented
367 94 my $self = shift( @_ );
368 94 my $r = $self->apache_request;
369 94 my $new = '';
370 94 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 65 29 94
371 {
372 65 $new = shift( @_ );
373 65 local $URI::ABS_REMOTE_LEADING_DOTS = 1;
374 65 ▶ unless( substr( "$new", 0, 1 ) eq '/' )
Branch: 1 branch
BranchTrueFalseTotal
unless (substr("$new", 0, 1) eq "/") 8 57 65
375 {
376 8 my $base_uri = $self->base_uri;
377 8 $new = URI->new( $new )->abs( $base_uri->{document_path} );
378 }
379 }
380
381 # return( $self->error( "Document URI needs to be an absolute URL path. Value provided was '$new'." ) ) if( length( $new ) && substr( $new, 0, 1 ) ne '/' );
382
383 94 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 94 94
384 {
385 # We do a lookup unless we are already in a sub request, and we do not want to end up in an infinite loop
386 # $r = $r->is_initial_req ? $r : $r->main;
387 0 ▶ if( length( "$new" ) )
Branch: 3 branches
BranchTrueFalseTotal
if (length "$new") { } 0 0 0
elsif ($self->{'_uri_reset'}) { } 0 0 0
elsif (not length $self->{'document_uri'}) { } 0 0 0
388 {
389 0 ▶ $r = $r->is_initial_req ? $r : $r->main;
Branch: 1 branch
BranchTrueFalseTotal
$r->is_initial_req ? : 0 0 0
390 0 my $rr = $self->lookup_uri( "$new" );
391 0 ▶ if( !defined( $rr ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rr) 0 0 0
392 {
393 0 return;
394 }
395 0 $self->apache_request( $rr );
396 # Remove trailing slash
397 0 my $u = $self->_trim_trailing_slash( $rr->unparsed_uri );
398 0 $self->{document_uri} = $u;
399 0 $self->_set_env( DOCUMENT_URI => $self->{document_uri} );
400 0 $self->_set_env( REQUEST_URI => $self->{document_uri} );
401 0 ▶ $self->_set_env( QUERY_STRING => scalar( $rr->args ) ) if( scalar( $rr->args ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar $rr->args 0 0 0
402 0 ▶ $self->_set_env( PATH_INFO => $rr->path_info ) if( $rr->path_info );
Branch: 1 branch
BranchTrueFalseTotal
if $rr->path_info 0 0 0
403 }
404 elsif( $self->{_uri_reset} )
405 {
406 0 ▶ my $u = URI->new( $r->uri . ( $r->path_info // '' ) );
Condition0%0/2
$r->path_info // ""
l!l
00
MC/DC0%0/1
$r->path_info // ""
$r->path_info
Truth table0%0/2
A
A= $r->path_info
Aresult
11
00
407 0 ▶ $u->query( scalar( $r->args ) ) if( length( scalar( $r->args ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if length scalar $r->args 0 0 0
408 # Cannot change the value of $r->unparsed_uri
409 0 $r->uri( "$u" );
410 0 $self->{document_uri} = $u;
411 0 $self->{_uri_reset} = 0;
412 }
413 elsif( !length( $self->{document_uri} ) )
414 {
415 0 $self->{document_uri} = $self->new_uri( $r->unparsed_uri );
416 0 $self->_set_env( DOCUMENT_URI => $self->{document_uri} );
417 0 $self->_set_env( REQUEST_URI => $self->{document_uri} );
418 0 ▶ $self->_set_env( QUERY_STRING => scalar( $r->args ) ) if( scalar( $r->args ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar $r->args 0 0 0
419 0 ▶ $self->_set_env( PATH_INFO => $r->path_info ) if( $r->path_info );
Branch: 1 branch
BranchTrueFalseTotal
if $r->path_info 0 0 0
420 }
421 0 return( $self->{document_uri} );
422 }
423 else
424 {
425 94 ▶ if( length( "$new" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length "$new") 65 29 94
426 {
427 65 $self->{_path_info_processed} = 0;
428 }
429 94 ▶ return( '' ) if( !length( $self->{document_uri} ) && $self->{_path_info_processed} );
Branch: 1 branch
BranchTrueFalseTotal
if not length $self->{"document_uri"} and $self->{"_path_info_processed"} 0 94 94
Condition66%2/3
not length $self->{"document_uri"} and $self->{"_path_info_processed"}
!ll&&!rl&&r
29650
MC/DC0%0/2
not length $self->{"document_uri"} and $self->{"_path_info_processed"}
not length $self->{"document_uri"} $self->{"_path_info_processed"}
Truth table66%2/3
not(A) and B
A= not length $self->{"document_uri"}B= $self->{"_path_info_processed"}
ABresult
0X0
100
111
430 94 ▶ my $v = $new || $self->{document_uri};
Condition66%2/3
$new || $self->{'document_uri'}
l!l&&r!l&&!r
65290
MC/DC0%0/2
$new || $self->{'document_uri'}
$new $self->{'document_uri'}
Truth table66%2/3
A || B
A= $newB= $self->{'document_uri'}
ABresult
1X1
011
000
431 94 ▶ if( !$self->{_path_info_processed} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_path_info_processed"}) 74 20 94
432 {
433 74 $self->{_path_info_processed}++;
434 74 my $res;
435 74 ▶ if( defined( $res = $self->_find_path_info( $v ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($res = $self->_find_path_info($v))) { } 74 0 74
436 {
437 74 $self->{document_uri} = URI->new( $v );
438 74 $self->document_path( $res->{path} );
439 74 $self->filename( $res->{filepath} );
440 74 ▶ $self->path_info( $res->{path_info} ) if( length( $res->{path_info} ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $res->{"path_info"} 3 71 74
441 74 ▶ $self->query_string( $res->{query_string} ) if( length( $res->{query_string} ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $res->{"query_string"} 18 56 74
442 74 $self->_set_env( DOCUMENT_URI => $self->{document_uri} );
443 74 $self->_set_env( REQUEST_URI => $self->{document_uri} );
444 74 $self->code( $res->{code} );
445 }
446 else
447 {
448 }
449 }
450
451 94 ▶ if( $self->{_uri_reset} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_uri_reset"}) 0 94 94
452 {
453 0 $self->{_uri_reset} = 0;
454 0 ▶ my $u = URI->new( $self->document_path . ( $self->path_info // '' ) );
Condition0%0/2
$self->path_info // ""
l!l
00
MC/DC0%0/1
$self->path_info // ""
$self->path_info
Truth table0%0/2
A
A= $self->path_info
Aresult
11
00
455 0 ▶ $u->query( $self->query_string ) if( $self->query_string );
Branch: 1 branch
BranchTrueFalseTotal
if $self->query_string 0 0 0
456 0 $self->{document_uri} = $u;
457 }
458 94 return( $self->{document_uri} );
459 }
460 }
461  
462 sub env
463 74 ▶{
Subroutine env: called CC=13 SCAR=34.0
Pod documented
464 74 my $self = shift( @_ );
465 # The user wants the entire hash reference
466 74 ▶ unless( @_ )
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 74 74
467 {
468 0 my $r = $self->apache_request;
469 0 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
470 {
471 # $r = $r->is_initial_req ? $r : $r->main;
472 0 return( $r->subprocess_env )
473 }
474 else
475 {
476 0 ▶ unless( scalar( keys( %{$self->{_env}} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$self{"_env"};}) 0 0 0
477 {
478 0 $self->{_env} = {%ENV};
479 }
480 0 return( $self->{_env} );
481 }
482 }
483 74 my $name = shift( @_ );
484 74 ▶ return( $self->error( "No environment variable name was provided." ) ) if( !length( $name ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 74 74
485 74 my $opts = {};
486 9 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
487 74 ▶ $opts = pop( @_ ) if( scalar( @_ ) && Scalar::Util::reftype( $_[-1] ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ and builtin::reftype($_[-1]) eq "HASH" 0 74 74
Condition33%1/3
scalar @_ and builtin::reftype($_[-1]) eq "HASH"
!ll&&!rl&&r
0740
MC/DC0%0/2
scalar @_ and builtin::reftype($_[-1]) eq "HASH"
scalar @_ builtin::reftype($_[-1]) eq "HASH"
Truth table33%1/3
A and B
A= scalar @_B= builtin::reftype($_[-1]) eq "HASH"
ABresult
0X0
100
111
488 # return( $self->error( "Environment variable value provided is a reference data (", overload::StrVal( $val ), ")." ) ) if( ref( $val ) && ( !overload::Overloaded( $val ) || ( overload::Overloaded( $val ) && !overload::Method( $val, '""' ) ) ) );
489 74 ▶ my $r = $opts->{apache_request} || $self->apache_request;
Condition33%1/3
$opts->{'apache_request'} || $self->apache_request
l!l&&r!l&&!r
0074
MC/DC0%0/2
$opts->{'apache_request'} || $self->apache_request
$opts->{'apache_request'} $self->apache_request
Truth table33%1/3
A || B
A= $opts->{'apache_request'}B= $self->apache_request
ABresult
1X1
011
000
490 74 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 74 74
491 {
492 # $r = $r->is_initial_req ? $r : $r->main;
493 0 ▶ $r->subprocess_env( $name => shift( @_ ) ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
494 0 my $v = $r->subprocess_env( $name );
495 0 return( $v );
496 }
497 else
498 {
499 74 my $env = {};
500 74 ▶ unless( scalar( keys( %{$self->{_env}} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$self{"_env"};}) 9 65 74
501 {
502 # Make a copy of the environment variables
503 9 $self->{_env} = {%ENV};
504 }
505 74 $env = $self->{_env};
506 74 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 74 0 74
507 {
508 74 $env->{ $name } = shift( @_ );
509 74 my $meth = lc( $name );
510 74 ▶ if( $self->can( $meth ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($meth)) 0 74 74
511 {
512 0 $self->$meth( $env->{ $name } );
513 }
514 }
515 74 return( $env->{ $name } );
516 }
517 }
518  
519 # This is set by document_uri
520 sub filename
521 135 ▶{
Subroutine filename: called CC=16 SCAR=39.0
Pod documented
522 135 my $self = shift( @_ );
523 135 my $class = ref( $self );
524 135 ▶ my $caller = (caller(1))[3] // '';
Condition50%1/2
(caller 1)[3] // ""
l!l
1350
MC/DC0%0/1
(caller 1)[3] // ""
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
525 # my $caller = substr( $sub, rindex( $sub, ':' ) + 1 );
526 135 my $r = $self->apache_request;
527 135 my $newfile;
528 135 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 74 61 135
529 {
530 74 $newfile = shift( @_ );
531 74 ▶ return( $self->error( "New file provided, but it was an empty string." ) ) if( !defined( $newfile ) || !length( $newfile ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newfile and length $newfile 0 74 74
Condition33%1/3
defined $newfile and length $newfile
!ll&&!rl&&r
0074
MC/DC0%0/2
defined $newfile and length $newfile
defined $newfile length $newfile
Truth table33%1/3
A and B
A= defined $newfileB= length $newfile
ABresult
0X0
100
111
532 }
533
534 135 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 135 135
535 {
536 0 ▶ if( defined( $newfile ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $newfile) { } 0 0 0
elsif (not length $self->{'filename'}) { } 0 0 0
537 {
538 0 ▶ $r = $r->is_initial_req ? $r : $r->main;
Branch: 1 branch
BranchTrueFalseTotal
$r->is_initial_req ? : 0 0 0
539 0 my $rr = $r->lookup_file( $newfile );
540 0 ▶ if( $rr->status == &Apache2::Const::HTTP_OK )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->status == &Apache2::Const::HTTP_OK) { } 0 0 0
541 {
542 0 $newfile = $rr->filename;
543 }
544 else
545 {
546 0 $r->filename( $self->collapse_dots( $newfile, { separator => $DIR_SEP }) );
547 # <https://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#toc_C_filename_>
548 0 $r->finfo( APR::Finfo::stat( $newfile, &APR::Const::FINFO_NORM, $r->pool ) );
549 0 $self->finfo( $newfile );
550 }
551 0 $r->subprocess_env( SCRIPT_FILENAME => $newfile );
552 # Force to create new Apache2::SSI::URI object
553 0 $self->{filename} = $newfile;
554 0 ▶ $self->{_uri_reset} = 'filename' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 0 0
555 }
556 elsif( !length( $self->{filename} ) )
557 {
558 0 $self->{filename} = $r->filename;
559 }
560 }
561 else
562 {
563 135 ▶ if( defined( $newfile ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newfile) 74 61 135
564 {
565 74 my $try = Cwd::realpath( $newfile );
566 # Cwd::realpath would convert
567 # Z:\perl\Apache2-SSI\t\htdocs\ssi\include.cgi
568 # into
569 # Z:/perl/Apache2-SSI/t/htdocs/ssi/include.cgi
570 # amazingly enough, so to make sure this keeps working on windows related platform, we need to call URI::file
571 74 ▶ $newfile = URI::file->new( $try )->file( $^O ) if( defined( $try ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $try 74 0 74
572 74 ▶ unless( File::Spec->file_name_is_absolute( $newfile ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($newfile)) 0 74 74
573 {
574 0 $newfile = URI::file->new_abs( $newfile )->file( $^O );
575 }
576 74 $self->env( SCRIPT_FILENAME => $newfile );
577 74 $self->finfo( $newfile );
578 # Force to create new Apache2::SSI::URI object
579 # Either a URI object or an URI::file object
580 74 $self->{filename} = $self->collapse_dots( $newfile, { separator => $DIR_SEP })->file( $^O );
581 # Pass the file as new argument to URI::file which will create an object based on the value of the current OS
582 # and transform it into a path à la linux, which is same as web, which is what we want
583 # All this is unnecessary for linux type system or those who use / as directory separator,
584 # but for windows type systems this is necessary
585 74 ▶ if( CORE::index( $self->{filename}, $self->document_root ) != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{'filename'}, $self->document_root) != -1) { } 74 0 74
586 {
587 74 $self->{document_path} = $self->new_uri( URI::file->new( substr( $self->{filename}, length( $self->document_root ) ) )->file( 'linux' ) );
588 }
589 else
590 {
591 0 $self->{document_path} = $self->new_uri( URI::file->new( $self->{filename} )->file( 'linux' ) );
592 }
593 74 ▶ $self->{_uri_reset} = 'filename' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 74 74
594 }
595 }
596 135 return( $self->{filename} );
597 }
598  
599 # Alias
600 26 ▶sub filepath { return( shift->filename( @_ ) ); }
Subroutine filepath: called CC=1 SCAR=0.0
Pod documented
601  
602 sub finfo
603 77 ▶{
Subroutine finfo: called CC=9 SCAR=25.1
Pod documented
604 77 my $self = shift( @_ );
605 77 my $r = $self->apache_request;
606 77 my $newfile;
607 77 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 74 3 77
elsif (not $self->{'finfo'}) { } 0 3 3
608 {
609 74 $newfile = shift( @_ );
610 74 ▶ return( $self->error( "New file path specified but is an empty string." ) ) if( !defined( $newfile ) || !length( $newfile ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newfile and length $newfile 0 74 74
Condition33%1/3
defined $newfile and length $newfile
!ll&&!rl&&r
0074
MC/DC0%0/2
defined $newfile and length $newfile
defined $newfile length $newfile
Truth table33%1/3
A and B
A= defined $newfileB= length $newfile
ABresult
0X0
100
111
611 }
612 elsif( !$self->{finfo} )
613 {
614 0 $newfile = $self->filename;
615 0 ▶ return( $self->error( "No file path set. This should not happen." ) ) if( !$newfile );
Branch: 1 branch
BranchTrueFalseTotal
unless $newfile 0 0 0
616 }
617
618 77 ▶ if( defined( $newfile ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newfile) 74 3 77
619 {
620 74 ▶ $self->{finfo} = Apache2::SSI::Finfo->new( $newfile, ( $r ? ( apache_request => $r ) : () ), debug => $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 74 74
621 74 ▶ return( $self->pass_error( Apache2::SSI::Finfo->error ) ) if( !$self->{finfo} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"finfo"} 0 74 74
622 }
623 77 return( $self->{finfo} );
624 }
625  
626 sub lookup_uri
627 0 ▶{
Subroutine lookup_uri: not called CC=19 SCAR=59.4
Pod documented
628 0 my $self = shift( @_ );
629 0 my $uri = '';
630 0 ▶ $uri = shift( @_ ) if( @_ && !ref( $_[0] ) && ( scalar( @_ ) % 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ and not ref $_[0] and scalar(@_) % 2 0 0 0
Condition0%0/6
@_ and not ref $_[0] and scalar(@_) % 2
!ll&&!rl&&r
000
@_ and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ and not ref $_[0] and scalar(@_) % 2
@_ not ref $_[0] scalar(@_) % 2
Truth table0%0/4
A and not(B) and C
A= @_B= not ref $_[0]C= scalar(@_) % 2
ABCresult
0XX0
10X0
1100
1111
631 0 my $opts = $self->_get_args_as_hash( @_ );
632 0 ▶ $uri = $opts->{uri} if( !length( $uri ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $uri 0 0 0
633 0 ▶ return( $self->error( "No uri provided." ) ) if( !length( $uri ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $uri 0 0 0
634 0 ▶ my $r = $opts->{apache_request} || $self->apache_request;
Condition0%0/3
$opts->{'apache_request'} || $self->apache_request
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'apache_request'} || $self->apache_request
$opts->{'apache_request'} $self->apache_request
Truth table0%0/3
A || B
A= $opts->{'apache_request'}B= $self->apache_request
ABresult
1X1
011
000
635 0 ▶ my $max_redirects = $opts->{max_redirect} || 10;
Condition0%0/2
$opts->{'max_redirect'} || 10
l!l
00
MC/DC0%0/1
$opts->{'max_redirect'} || 10
$opts->{'max_redirect'}
Truth table0%0/2
A
A= $opts->{'max_redirect'}
Aresult
11
00
636 0 my $c = 0;
637 0 my $rr = $r->lookup_uri( $uri );
638 0 ▶ while( ++$c <= $max_redirects &&
Branch: 1 branch
BranchTrueFalseTotal
if (++$c <= $max_redirects and $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY || $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY) 0 0 0
Condition0%0/6
++$c <= $max_redirects and $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY || $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
!ll&&!rl&&r
000
$rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY or $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
l!l&&r!l&&!r
000
MC/DC0%0/3
++$c <= $max_redirects and $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY || $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
++$c <= $max_redirects $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
Truth table0%0/4
A and B or C
A= ++$c <= $max_redirectsB= $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLYC= $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
ABCresult
0XX0
1000
11X1
1011
639 ( $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY ||
640 $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY ) )
641 {
642 0 my $next_r = $rr->next;
643 0 ▶ if( !defined( $next_r ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next_r) { } 0 0 0
644 {
645 0 last;
646 }
647 else
648 {
649 0 $rr = $next_r;
650 }
651 }
652 0 ▶ if( defined( $rr ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rr and $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY || $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY) 0 0 0
Condition0%0/6
defined $rr and $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY || $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
!ll&&!rl&&r
000
$rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY or $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $rr and $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY || $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
defined $rr $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
Truth table0%0/4
A and B or C
A= defined $rrB= $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLYC= $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY
ABCresult
0XX0
1000
11X1
1011
653 ( $rr->status == &Apache2::Const::HTTP_MOVED_PERMANENTLY ||
654 $rr->status == &Apache2::Const::HTTP_MOVED_TEMPORARILY ) )
655 {
656 0 my $hdrs = $rr->headers_out;
657 # Weird, should not happen, but just in case
658 0 ▶ if( !exists( $hdrs->{Location} ) || !length( $hdrs->{Location} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hdrs->{"Location"} and length $hdrs->{"Location"}) 0 0 0
Condition0%0/3
exists $hdrs->{"Location"} and length $hdrs->{"Location"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $hdrs->{"Location"} and length $hdrs->{"Location"}
exists $hdrs->{"Location"} length $hdrs->{"Location"}
Truth table0%0/3
A and B
A= exists $hdrs->{"Location"}B= length $hdrs->{"Location"}
ABresult
0X0
100
111
659 {
660 0 return( $rr );
661 }
662
663 0 local $@;
664 # try-catch
665 my $rv = eval
666 0 {
667 # No, we cannot use $rr->uri. This would give us the initial requested uri, not the redirected uri
668 0 my $u = URI->new( $hdrs->{Location} );
669 0 $uri = $u->path;
670 0 ▶ if( ++$self->{_lookup_looping} > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{'_lookup_looping'} > 1) { } 0 0 0
671 {
672 0 return( $rr );
673 }
674 else
675 {
676 0 delete( $self->{_lookup_looping} );
677 0 my $new_r = $self->lookup_uri( $uri );
678 0 return( $new_r );
679 }
680 };
681 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
682 {
683 0 $self->error( "An error occurred while creating URI object for \"$hdrs->{Location}\": $@" );
684 0 return( $rr );
685 }
686 0 return( $rv );
687 }
688 0 return( $rr );
689 }
690  
691 sub make
692 0 ▶{
Subroutine make: not called CC=10 SCAR=47.0
Pod documented
693 0 my $self = shift( @_ );
694 0 ▶ return( $self->error( "Must be called with an existing object and not as ", __PACKAGE__, "->make()" ) ) if( !Scalar::Util::blessed( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 0 0
695 0 my $p = $self->_get_args_as_hash( @_ );
696 0 my $r = $self->apache_request;
697 0 my $d = $self->document_root;
698 0 my $b = $self->base_uri;
699 0 my $f = $self->document_uri;
700 0 ▶ $p->{apache_request} = $r if( !$p->{apache_request} && $r );
Branch: 1 branch
BranchTrueFalseTotal
if not $p->{"apache_request"} and $r 0 0 0
Condition0%0/3
not $p->{"apache_request"} and $r
!ll&&!rl&&r
000
MC/DC0%0/2
not $p->{"apache_request"} and $r
not $p->{"apache_request"} $r
Truth table0%0/3
not(A) and B
A= not $p->{"apache_request"}B= $r
ABresult
0X0
100
111
701 0 ▶ $p->{document_root} = "$d" if( !$p->{document_root} && length( $d ) );
Branch: 1 branch
BranchTrueFalseTotal
if not $p->{"document_root"} and length $d 0 0 0
Condition0%0/3
not $p->{"document_root"} and length $d
!ll&&!rl&&r
000
MC/DC0%0/2
not $p->{"document_root"} and length $d
not $p->{"document_root"} length $d
Truth table0%0/3
not(A) and B
A= not $p->{"document_root"}B= length $d
ABresult
0X0
100
111
702 0 ▶ $p->{base_uri} = "$b" if( !$p->{base_uri} && length( $b ) );
Branch: 1 branch
BranchTrueFalseTotal
if not $p->{"base_uri"} and length $b 0 0 0
Condition0%0/3
not $p->{"base_uri"} and length $b
!ll&&!rl&&r
000
MC/DC0%0/2
not $p->{"base_uri"} and length $b
not $p->{"base_uri"} length $b
Truth table0%0/3
not(A) and B
A= not $p->{"base_uri"}B= length $b
ABresult
0X0
100
111
703 0 ▶ $p->{document_uri} = "$f" if( !$p->{document_uri} );
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"document_uri"} 0 0 0
704 0 ▶ $p->{debug} = $self->debug if( !length( $p->{debug} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $p->{"debug"} 0 0 0
705 0 return( $self->new( $p ) );
706 }
707  
708 sub new_uri
709 172 ▶{
Subroutine new_uri: called CC=2 SCAR=7.1
Pod documented
710 172 my $self = shift( @_ );
711 172 my $class = URI_CLASS;
712 172 my $uri = shift( @_ );
713 172 local $@;
714 # try-catch
715 my $rv = eval
716 172 {
717 172 return( $class->new( $uri ) );
718 };
719 172 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 172 172
720 {
721 0 return( $self->error( "Unable to instantiate an URI object with \"$uri\": $@" ) );
722 }
723 172 return( $rv );
724 }
725  
726 sub parent
727 0 ▶{
Subroutine parent: not called CC=2 SCAR=17.9
Pod documented
728 0 my $self = shift( @_ );
729 0 my $path = $self->document_path;
730 0 my $r = $self->apache_request;
731 # I deliberately did not do split( '/', $path, -1 ) so that if there is a trailing '/', it will not be counted
732 0 my @segments = $self->document_path->path_segments;
733 0 pop( @segments );
734 0 ▶ return( $self ) if( !scalar( @segments ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @segments 0 0 0
735 0 return( $self->make( document_uri => join( '/', @segments ) ) );
736 }
737  
738 sub path_info
739 5 ▶{
Subroutine path_info: called CC=7 SCAR=24.2
Pod documented
740 5 my $self = shift( @_ );
741 5 my $class = ref( $self );
742 5 ▶ my $caller = (caller(1))[3] // '';
Condition50%1/2
(caller 1)[3] // ""
l!l
50
MC/DC0%0/1
(caller 1)[3] // ""
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
743 # my $caller = substr( $sub, rindex( $sub, ':' ) + 1 );
744 5 my $r = $self->apache_request;
745 5 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 5 5
746 {
747 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
748 {
749 0 $r->path_info( shift( @_ ) );
750 0 $self->_set_env( PATH_INFO => $r->path_info );
751 0 ▶ $self->{_uri_reset} = 'path_info' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 0 0
752 }
753 0 return( $r->path_info );
754 }
755 else
756 {
757 5 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
758 {
759 3 $self->{path_info} = shift( @_ );
760 3 $self->_set_env( PATH_INFO => $self->{path_info} );
761 3 ▶ $self->{_uri_reset} = 'path_info' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 3 3
762 }
763 5 return( $self->{path_info} );
764 }
765 }
766  
767 sub query_string
768 24 ▶{
Subroutine query_string: called CC=7 SCAR=24.8
Pod documented
769 24 my $self = shift( @_ );
770 24 my $class = ref( $self );
771 24 ▶ my $caller = (caller(1))[3] // '';
Condition50%1/2
(caller 1)[3] // ""
l!l
240
MC/DC0%0/1
(caller 1)[3] // ""
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
772 # my $caller = substr( $sub, rindex( $sub, ':' ) + 1 );
773 24 my $r = $self->apache_request;
774 24 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 24 24
775 {
776 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
777 {
778 0 my $qs = shift( @_ );
779 0 $r->args( $qs );
780 0 $self->_set_env( QUERY_STRING => $qs );
781 0 ▶ $self->{_uri_reset} = 'query_string' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 0 0
782 }
783 0 return( $r->args );
784 }
785 else
786 {
787 24 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 18 6 24
788 {
789 18 $self->{query_string} = shift( @_ );
790 18 $self->_set_env( QUERY_STRING => $self->{query_string} );
791 18 ▶ $self->{_uri_reset} = 'query_string' unless( $caller eq "${class}\::document_uri" );
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "${class}::document_uri" 0 18 18
792 }
793 24 return( $self->{query_string} );
794 }
795 }
796  
797 sub root
798 16 ▶{
Subroutine root: called CC=3 SCAR=11.0
Pod documented
799 16 my $self = shift( @_ );
800 16 ▶ return( $self->{root} ) if( $self->{root} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"root"} 8 8 16
801 8 my $hash =
802 {
803 code => 200,
804 document_uri => $self->new_uri( '/' ),
805 document_root => $self->document_root,
806 debug => $self->debug,
807 path_info => '',
808 query_string => '',
809 _path_info_processed => 1,
810 };
811 8 $hash->{document_path} = $hash->{document_uri};
812 8 ▶ $hash->{apache_request} = $self->apache_request if( $self->apache_request );
Branch: 1 branch
BranchTrueFalseTotal
if $self->apache_request 0 8 8
813 8 my $root = bless( $hash => ref( $self ) );
814 # Scalar::Util::weaken( $copy );
815 8 $root->{base_dir} = $root;
816 8 $root->{base_uri} = $root;
817 8 $self->{root} = $root;
818 8 return( $root );
819 }
820  
821 # shortcut
822 0 ▶sub uri { return( shift->document_uri( @_ ) ); }
Subroutine uri: not called CC=1 SCAR=6.9
Pod documented
823  
824 # Path info works as a path added to a document uri, such as:
825 # /my/doc.html/path/info
826 # But we need to distinguish with missing document hierarchy inside a directory, such as:
827 # /my/folder/missing_doc.html/path/info
828 # otherwise we would be treating /missing_doc.html/path/info as a path info
829 sub _find_path_info
830 82 ▶{
Subroutine _find_path_info: called CC=19 SCAR=31.9
831 82 my $self = shift( @_ );
832 82 my( $uri_path, $doc_root ) = @_;
833 82 ▶ $doc_root //= $self->document_root;
Condition33%1/3
$doc_root //= $self->document_root
l!l&&r!l&&!r
0820
MC/DC0%0/2
$doc_root //= $self->document_root
$doc_root $self->document_root
Truth table33%1/3
A //= B
A= $doc_rootB= $self->document_root
ABresult
1X1
011
000
834 82 my $qs = '';
835 82 my $sep = $DIR_SEP;
836 82 ▶ $sep = '/' if( !length( $sep ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $sep 0 82 82
837 82 ▶ if( Scalar::Util::blessed( $uri_path ) && $uri_path->isa( 'URI::file' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri_path) and $uri_path->isa("URI::file")) 0 82 82
Condition66%2/3
builtin::blessed($uri_path) and $uri_path->isa("URI::file")
!ll&&!rl&&r
65170
MC/DC0%0/2
builtin::blessed($uri_path) and $uri_path->isa("URI::file")
builtin::blessed($uri_path) $uri_path->isa("URI::file")
Truth table66%2/3
A and B
A= builtin::blessed($uri_path)B= $uri_path->isa("URI::file")
ABresult
0X0
100
111
838 {
839 0 $uri_path = $uri_path->file;
840 }
841 82 my $u = $self->collapse_dots( $uri_path );
842 82 $qs = $u->query;
843 82 $uri_path = $u->path;
844 # Pass the OS to ensure we get ./ss/include.cgi becomes .\ssi\include.cgi
845 82 my $path = URI::file->new( $uri_path )->file( $^O );
846 82 ▶ $doc_root = $doc_root->file( $^O ) if( Scalar::Util::blessed( $doc_root ) && $doc_root->isa( 'URI::file' ) );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($doc_root) and $doc_root->isa("URI::file") 0 82 82
Condition33%1/3
builtin::blessed($doc_root) and $doc_root->isa("URI::file")
!ll&&!rl&&r
8200
MC/DC0%0/2
builtin::blessed($doc_root) and $doc_root->isa("URI::file")
builtin::blessed($doc_root) $doc_root->isa("URI::file")
Truth table33%1/3
A and B
A= builtin::blessed($doc_root)B= $doc_root->isa("URI::file")
ABresult
0X0
100
111
847 82 ▶ $doc_root = substr( $doc_root, 0, length( $doc_root ) - length( $sep ) ) if( substr( $doc_root, -length( $sep ), length( $sep ) ) eq $sep );
Branch: 1 branch
BranchTrueFalseTotal
if substr($doc_root, -length($sep), length $sep) eq $sep 0 82 82
848 82 ▶ return( $self->error( "URI path must be an absolute path starting with '/'. Path provided was \"$uri_path\"." ) ) if( substr( $uri_path, 0, 1 ) ne '/' );
Branch: 1 branch
BranchTrueFalseTotal
if substr($uri_path, 0, 1) ne "/" 0 82 82
849 # No need to go further
850 82 ▶ if( -e( "${doc_root}${path}" ) )
Branch: 2 branches
BranchTrueFalseTotal
if (-e "$doc_root$path") { } 75 7 82
elsif ($uri_path eq '/') { } 0 7 7
851 {
852 return({
853 75 filepath => "${doc_root}${path}",
854 path => $uri_path,
855 query_string => $qs,
856 code => 200,
857 });
858 }
859 elsif( $uri_path eq '/' )
860 {
861 return({
862 0 ▶ filepath => $doc_root,
Branch: 1 branch
BranchTrueFalseTotal
-e $doc_root ? : 0 0 0
863 path => $uri_path,
864 path_info => undef(),
865 query_string => $qs,
866 code => ( -e( $doc_root ) ? 200 : 404 ),
867 });
868 }
869 7 my @parts = split( '/', substr( $uri_path, 1 ) );
870 7 my $trypath = '';
871 7 my $trypath_uri = '';
872 7 my $pathinfo = '';
873 7 foreach my $p ( @parts )
874 {
875 # The last path was a directory, and we cannot find the element within. So, the rest of the path is not path info, but rather a 404 missing document hierarchy
876 # We test the $pathinfo string, so we do not bother checking further if it is already set.
877 16 ▶ if( !$pathinfo && -d( "${doc_root}${trypath}" ) && !-e( "${doc_root}${trypath}/${p}" ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not $pathinfo and -d "$doc_root$trypath" and not -e "$doc_root$trypath/$p") { } 3 13 16
elsif (not $pathinfo and -e "$doc_root$trypath/$p") { } 9 4 13
Condition83%5/6
not $pathinfo and -d "$doc_root$trypath" and not -e "$doc_root$trypath/$p"
!ll&&!rl&&r
493
not $pathinfo and -d "$doc_root$trypath"
!ll&&!rl&&r
0412
MC/DC66%2/3
not $pathinfo and -d "$doc_root$trypath" and not -e "$doc_root$trypath/$p"
not $pathinfo -d "$doc_root$trypath" not -e "$doc_root$trypath/$p"
Truth table75%3/4
not(A) and B and not(C)
A= not $pathinfoB= -d "$doc_root$trypath"C= not -e "$doc_root$trypath/$p"
ABCresult
0XX0
10X0
1100
1111
878 {
879 # We return the original path provided (minus any query string)
880 return({
881 3 ▶ filepath => $doc_root . ( length( $trypath ) ? $trypath : $path ),
Branch: 1 branch
BranchTrueFalseTotal
length $trypath ? : 1 2 3
882 path => $uri_path,
883 code => 404,
884 query_string => $qs,
885 });
886 }
887 ▶ elsif( !$pathinfo && -e( "${doc_root}${trypath}/${p}" ) )
Condition66%2/3
not $pathinfo and -e "$doc_root$trypath/$p"
!ll&&!rl&&r
049
MC/DC50%1/2
not $pathinfo and -e "$doc_root$trypath/$p"
not $pathinfo -e "$doc_root$trypath/$p"
Truth table66%2/3
not(A) and B
A= not $pathinfoB= -e "$doc_root$trypath/$p"
ABresult
0X0
100
111
888 {
889 9 $trypath_uri .= "/${p}";
890 9 $trypath .= "${sep}${p}";
891 }
892 else
893 {
894 4 $pathinfo .= "/$p";
895 }
896 }
897 return({
898 4 filepath => "${doc_root}${trypath}",
899 path => $trypath_uri,
900 path_info => $pathinfo,
901 code => 200,
902 query_string => $qs,
903 });
904 }
905  
906 # *_set_env = \&Apache2::SSI::_set_env;
907 # This is different from the env() method. This one is obviously private
908 # whereas the env() one has triggers that could otherwise create an infinite loop.
909 sub _set_env
910 234 ▶{
Subroutine _set_env: called CC=5 SCAR=16.3
911 234 my $self = shift( @_ );
912 234 my $name = shift( @_ );
913 234 ▶ return( $self->error( "No environment variable name provided." ) ) if( !length( $name ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 234 234
914 234 ▶ $self->{_env} = {} if( !ref( $self->{_env} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"_env"} 65 169 234
915 234 my $env = $self->{_env};
916 234 my $r = $self->apache_request;
917 234 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 234 0 234
918 {
919 234 my $v = shift( @_ );
920 234 ▶ $r->subprocess_env( $name => $v ) if( $r );
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 234 234
921 234 $env->{ $name } = $v;
922 }
923 234 return( $self );
924 }
925  
926 sub _trim_trailing_slash
927 0 ▶{
Subroutine _trim_trailing_slash: not called CC=6 SCAR=37.4
928 0 my $self = shift( @_ );
929 0 my $uri = shift( @_ );
930 0 ▶ return( $self->error( "No uri provided to trim trailing slash." ) ) if( !length( "$uri" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length "$uri" 0 0 0
931 0 ▶ unless( Scalar::Util::blessed( $uri ) && $uri->isa( 'URI' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($uri) and $uri->isa("URI")) 0 0 0
Condition0%0/3
builtin::blessed($uri) and $uri->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("URI")
builtin::blessed($uri) $uri->isa("URI")
Truth table0%0/3
A and B
A= builtin::blessed($uri)B= $uri->isa("URI")
ABresult
0X0
100
111
932 {
933 0 $uri = $self->new_uri( "$uri" );
934 }
935 0 ▶ if( substr( $uri->path, -1, 1 ) eq '/' && length( $uri->path ) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($uri->path, -1, 1) eq "/" and length $uri->path > 1) 0 0 0
Condition0%0/3
substr($uri->path, -1, 1) eq "/" and length $uri->path > 1
!ll&&!rl&&r
000
MC/DC0%0/2
substr($uri->path, -1, 1) eq "/" and length $uri->path > 1
substr($uri->path, -1, 1) eq "/" length $uri->path > 1
Truth table0%0/3
A and B
A= substr($uri->path, -1, 1) eq "/"B= length $uri->path > 1
ABresult
0X0
100
111
936 {
937 # By splitting the string on '/' and without the last argument for split being -1, perl will remove trailing blank entries
938 0 $uri->path( join( '/', split( '/', $uri->path ) ) );
939 }
940 0 return( $uri );
941 }
942  
943 9 1;
944 # NOTE: POD
945 __END__
« SSI/File/Type.pm ↑ Summary SSI/SharedMem.pm »