SSI/File.pm

filter: Statementstmt 67.7% 101 / 149 Branchbran 42.6% 29 / 68 Conditioncond 18.5% 5 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 84.0% 21 / 25 Podpod 100% 10 / 10 totaltotal 56.2% 166 / 295 CC 45 SCAR 30.0
CC
45
Cov
55%
filename
61.2
parent
30.0
finfo
12.3
CRAP 20.1
« SSI/Notes.pm ↑ Summary SSI/Common.pm »
1 ##----------------------------------------------------------------------------
2 ## Apache2 Server Side Include Parser - ~/lib/Apache2/SSI/File.pm
3 ## Version v0.1.2
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 #----------------------------------------------------------------------------
14 # Apache2 Server Side Include Parser - ~/lib/Apache2/SSI/File.pm
15 # Version v0.1.1
16 # Copyright(c) 2021 DEGUEST Pte. Ltd.
17 # Author: Jacques Deguest <jack@deguest.jp>
18 # Created 2020/12/18
19 # Modified 2022/10/21
20 # All rights reserved
21 #
22 # This program is free software; you can redistribute it and/or modify it
23 # under the same terms as Perl itself.
24 #----------------------------------------------------------------------------
25 package Apache2::SSI::File;
26 BEGIN
27 9 ▶{
Subroutine BEGIN: called CC=2 SCAR=7.1
28 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶ use parent qw( Apache2::SSI::Common );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 9 ▶ use vars qw( $DEBUG $VERSION $DIR_SEP );
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 ▶ use Apache2::SSI::Finfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶ use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶ use URI::file ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶ if( $ENV{MOD_PERL} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOD_PERL"}) 0 9 9
38 {
39 0 require Apache2::RequestRec;
40 0 require Apache2::RequestUtil;
41 0 require Apache2::SubRequest;
42 0 require Apache2::Access;
43 0 require Apache2::Const;
44 0 Apache2::Const->import( compile => qw( :common :http OK DECLINED ) );
45 0 require APR::Const;
46 0 APR::Const->import( -compile => qw( :filetype FINFO_NORM ) );
47 }
48 9 our( $DEBUG );
49 9 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.2
50 0 ▶ q{""} => sub { $_[0]->filename },
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 bool => sub () { 1 },
52 9 fallback => 1,
53 9 );
54 9 our $VERSION = 'v0.1.2';
55 9 our $DIR_SEP = $Apache2::SSI::Common::DIR_SEP;
56 };
57  
58 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 sub init
62 1 ▶{
Subroutine init: called CC=9 SCAR=24.8
Pod documented
63 1 my $self = shift( @_ );
64 1 my $file = shift( @_ );
65 1 ▶ return( $self->error( "No file was provided." ) ) if( !defined( $file ) || !length( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 1 1
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
001
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
66 1 $self->{apache_request} = '';
67 1 ▶ $self->{base_dir} = '' unless( length( $self->{base_dir} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"base_dir"} 1 0 1
68 1 $self->{base_file} = '';
69 1 $self->{code} = 200;
70 1 $self->{finfo} = '';
71 1 $self->{_init_strict_use_sub} = 1;
72 1 ▶ $self->SUPER::init( @_ ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return 1 0 1
73 1 my $base_dir = '';
74 1 ▶ if( length( $self->{base_file} ) )
Branch: 2 branches
BranchTrueFalseTotal
if (length $self->{'base_file'}) { } 1 0 1
elsif (not length $self->{'base_dir'}) { } 0 0 0
75 {
76 1 ▶ if( -d( $self->{base_file} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'base_file'}) { } 0 1 1
77 {
78 0 $base_dir = $self->{base_file};
79 }
80 else
81 {
82 1 my @segments = split( "\Q${DIR_SEP}\E", $self->{base_file}, -1 );
83 1 pop( @segments );
84 1 $base_dir = join( $DIR_SEP, @segments );
85 }
86 1 $self->{base_dir} = $base_dir;
87 }
88 elsif( !length( $self->{base_dir} ) )
89 {
90 0 $base_dir = URI->new( URI::file->cwd )->file( $^O );
91 0 $self->{base_dir} = $base_dir;
92 }
93 1 ▶ $self->filename( $file ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->filename($file) or return 1 0 1
94 1 return( $self );
95 }
96  
97 7 ▶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
98  
99 1 ▶sub base_dir { return( shift->_make_abs( 'base_dir', @_ ) ); }
Subroutine base_dir: called CC=1 SCAR=0.0
Pod documented
100  
101 1 ▶sub base_file { return( shift->_make_abs( 'base_file', @_ ) ); }
Subroutine base_file: called CC=1 SCAR=0.0
Pod documented
102  
103 sub clone
104 0 ▶{
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
105 0 my $self = shift( @_ );
106 0 my $new = {};
107 0 my @fields = grep( !/^(apache_request|finfo)$/, keys( %$self ) );
108 0 @$new{ @fields } = @$self{ @fields };
109 0 $new->{apache_request} = $self->{apache_request};
110 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
111 }
112  
113 sub code
114 2 ▶{
Subroutine code: called CC=4 SCAR=17.2
Pod documented
115 2 my $self = shift( @_ );
116 2 my $r = $self->apache_request;
117 2 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 2 2
118 {
119 0 ▶ $r->status( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
120 0 return( $r->status );
121 }
122 else
123 {
124 2 ▶ $self->{code} = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
125 2 return( $self->{code} );
126 }
127 }
128  
129 sub filename
130 3 ▶{
Subroutine filename: called CC=15 SCAR=41.1
Pod documented
131 3 my $self = shift( @_ );
132 3 my $newfile;
133 3 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 2 3
134 {
135 1 $newfile = shift( @_ );
136 1 ▶ 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 1 1
Condition33%1/3
defined $newfile and length $newfile
!ll&&!rl&&r
001
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
137 }
138
139 3 my $r = $self->apache_request;
140 3 ▶ if( $r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 3 3
141 {
142 0 ▶ if( defined( $newfile ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $newfile) { } 0 0 0
elsif (not length $self->{'filename'}) { } 0 0 0
143 {
144 0 ▶ $r = $r->is_initial_req ? $r : $r->main;
Branch: 1 branch
BranchTrueFalseTotal
$r->is_initial_req ? : 0 0 0
145 0 my $rr = $r->lookup_file( $newfile );
146 # Amazingly, lookup_file will return ok even if it does not find the file
147 0 ▶ if( $rr->status == &Apache2::Const::HTTP_OK &&
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->status == &Apache2::Const::HTTP_OK and $rr->finfo and $rr->finfo->filetype != &APR::Const::FILETYPE_NOFILE) { } 0 0 0
Condition0%0/6
$rr->status == &Apache2::Const::HTTP_OK and $rr->finfo and $rr->finfo->filetype != &APR::Const::FILETYPE_NOFILE
!ll&&!rl&&r
000
$rr->status == &Apache2::Const::HTTP_OK and $rr->finfo
!ll&&!rl&&r
000
MC/DC0%0/3
$rr->status == &Apache2::Const::HTTP_OK and $rr->finfo and $rr->finfo->filetype != &APR::Const::FILETYPE_NOFILE
$rr->status == &Apache2::Const::HTTP_OK $rr->finfo $rr->finfo->filetype != &APR::Const::FILETYPE_NOFILE
Truth table0%0/4
A and B and C
A= $rr->status == &Apache2::Const::HTTP_OKB= $rr->finfoC= $rr->finfo->filetype != &APR::Const::FILETYPE_NOFILE
ABCresult
0XX0
10X0
1100
1111
148 $rr->finfo &&
149 $rr->finfo->filetype != &APR::Const::FILETYPE_NOFILE )
150 {
151 0 $self->apache_request( $rr );
152 0 $newfile = $rr->filename;
153 0 my $finfo = $rr->finfo;
154 0 ▶ if( $finfo )
Branch: 1 branch
BranchTrueFalseTotal
if ($finfo) 0 0 0
155 {
156 }
157 }
158 else
159 {
160 0 $self->code( 404 );
161 0 $newfile = $self->collapse_dots( $newfile, { separator => $DIR_SEP });
162 # We don't pass it the Apache2::RequestRec object, because it would trigger a fatal error since the file does not exist. Instead, we use the api without Apache2::RequestRec which is more tolerant
163 # We do this so the user can call our object $file->finfo->filetype == Apache2::SSI::Finfo::FILETYPE_NOFILE
164 0 $self->{finfo} = Apache2::SSI::Finfo->new( $newfile );
165 }
166 0 $self->{filename} = $newfile;
167 }
168 elsif( !length( $self->{filename} ) )
169 {
170 0 $self->{filename} = $r->filename;
171 }
172 }
173 else
174 {
175 3 ▶ if( defined( $newfile ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newfile) 1 2 3
176 {
177 1 my $base_dir = $self->base_dir;
178 1 ▶ $base_dir .= $DIR_SEP unless( substr( $base_dir, -length( $DIR_SEP ), length( $DIR_SEP ) ) eq $DIR_SEP );
Branch: 1 branch
BranchTrueFalseTotal
unless substr($base_dir, -length($Apache2::SSI::File::DIR_SEP), length $Apache2::SSI::File::DIR_SEP) eq $Apache2::SSI::File::DIR_SEP 1 0 1
179 # If we provide a string for the abs() method it works on Unix, but not on Windows
180 # By providing an object, we make it work
181 1 $newfile = URI::file->new( $newfile )->abs( URI::file->new( $base_dir ) )->file( $^O );
182 1 $self->{filename} = $self->collapse_dots( $newfile, { separator => $DIR_SEP })->file( $^O );
183 1 $self->finfo( $newfile );
184 1 my $finfo = $self->finfo;
185 1 ▶ if( !$finfo->exists )
Branch: 1 branch
BranchTrueFalseTotal
unless ($finfo->exists) 0 1 1
186 {
187 0 $self->code( 404 );
188 }
189 # Force to create new Apache2::SSI::URI object
190 }
191 }
192 3 return( $self->{filename} );
193 }
194  
195 # Alias
196 0 ▶sub filepath { return( shift->filename( @_ ) ); }
Subroutine filepath: not called CC=1 SCAR=6.9
Pod documented
197  
198 sub finfo
199 2 ▶{
Subroutine finfo: called CC=9 SCAR=25.1
Pod documented
200 2 my $self = shift( @_ );
201 2 my $r = $self->apache_request;
202 2 my $newfile;
203 2 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 1 1 2
elsif (not $self->{'finfo'}) { } 0 1 1
204 {
205 1 $newfile = shift( @_ );
206 1 ▶ 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 1 1
Condition33%1/3
defined $newfile and length $newfile
!ll&&!rl&&r
001
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
207 }
208 elsif( !$self->{finfo} )
209 {
210 0 $newfile = $self->filename;
211 0 ▶ return( $self->error( "No file path set. This should not happen." ) ) if( !$newfile );
Branch: 1 branch
BranchTrueFalseTotal
unless $newfile 0 0 0
212 }
213
214 2 ▶ if( defined( $newfile ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newfile) 1 1 2
215 {
216 1 ▶ $self->{finfo} = Apache2::SSI::Finfo->new( $newfile, ( $r ? ( apache_request => $r ) : () ), debug => $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 1 1
217 1 ▶ return( $self->pass_error( Apache2::SSI::Finfo->error ) ) if( !$self->{finfo} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"finfo"} 0 1 1
218 }
219 2 return( $self->{finfo} );
220 }
221  
222 sub parent
223 0 ▶{
Subroutine parent: not called CC=5 SCAR=34.0
Pod documented
224 0 my $self = shift( @_ );
225 0 my $r = $self->apache_request;
226 # I deliberately did not do split( '/', $path, -1 ) so that if there is a trailing '/', it will not be counted
227 # 2021-03-27: Was working well, but only on Unix systems...
228 # my @segments = split( '/', $self->filename, -1 );
229 0 my( $vol, $parent, $file ) = File::Spec->splitpath( $self->filename );
230 0 ▶ $vol //= '';
Condition0%0/2
$vol //= ""
l!l
00
MC/DC0%0/1
$vol //= ""
$vol
Truth table0%0/2
A
A= $vol
Aresult
11
00
231 0 ▶ $file //= '';
Condition0%0/2
$file //= ""
l!l
00
MC/DC0%0/1
$file //= ""
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
232 0 my @segments = File::Spec->splitpath( File::Spec->catfile( $parent, $file ) );
233 0 pop( @segments );
234 0 ▶ return( $self ) if( !scalar( @segments ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @segments 0 0 0
235 # return( $self->new( join( '/', @segments ), ( $r ? ( apache_request => $r ) : () ) ) );
236 0 ▶ return( $self->new( $vol . File::Spec->catdir( @segments ), ( $r ? ( apache_request => $r ) : () ) ) );
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 0 0
237 }
238  
239 sub _make_abs
240 2 ▶{
Subroutine _make_abs: called CC=6 SCAR=20.2
241 2 my $self = shift( @_ );
242 2 ▶ my $field = shift( @_ ) || return( $self->error( "No field provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No field provided.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No field provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
243 2 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
244 {
245 1 my $this = shift( @_ );
246 1 ▶ if( Scalar::Util::blessed( $this ) && $this->isa( 'URI::file' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($this) and $this->isa('URI::file')) { } 0 1 1
elsif (not 'File::Spec'->file_name_is_absolute($this)) { } 0 1 1
Condition33%1/3
builtin::blessed($this) and $this->isa("URI::file")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($this) and $this->isa("URI::file")
builtin::blessed($this) $this->isa("URI::file")
Truth table33%1/3
A and B
A= builtin::blessed($this)B= $this->isa("URI::file")
ABresult
0X0
100
111
247 {
248 0 $this = URI->new_abs( $this )->file( $^O );
249 }
250 # elsif( substr( $this, 0, 1 ) ne '/' )
251 elsif( !File::Spec->file_name_is_absolute( $this ) )
252 {
253 0 $this = URI::file->new_abs( $this )->file( $^O );
254 }
255 1 $self->{ $field } = $this;
256 }
257 2 return( $self->{ $field } );
258 }
259  
260 9 1;
261 # NOTE: POD
262 __END__
« SSI/Notes.pm ↑ Summary SSI/Common.pm »