SSI/Common.pm

filter: Statementstmt 91.9% 91 / 99 Branchbran 68.0% 34 / 50 Conditioncond 44.4% 12 / 27 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 74.7% 157 / 210 CC 36 SCAR 27.0
CC
36
Cov
78%
collapse_dots
22.5
slurp
20.9
slurp_utf8
7.0
CRAP 14.8
« SSI/File.pm ↑ Summary SSI/Finfo.pm »
1 ##----------------------------------------------------------------------------
2 ## Apache2 Server Side Include Parser - ~/lib/Apache2/SSI/Common.pm
3 ## Version v0.1.2
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/01/13
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::Common;
14 BEGIN
15 10 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶ use vars qw( $VERSION $OS2SEP $DIR_SEP );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶ use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 our $VERSION = 'v0.1.2';
25 # https://en.wikipedia.org/wiki/Path_(computing)
26 # perlport
27 10 our $OS2SEP =
28 {
29 amigaos => '/',
30 android => '/',
31 aix => '/',
32 bsdos => '/',
33 beos => '/',
34 bitrig => '/',
35 cygwin => '/',
36 darwin => '/',
37 dec_osf => '/',
38 dgux => '/',
39 dos => "\\",
40 dragonfly => '/',
41 dynixptx => '/',
42 freebsd => '/',
43 gnu => '/',
44 gnukfreebsd => '/',
45 haiku => '/',
46 hpux => '/',
47 interix => '/',
48 iphoneos => '/',
49 irix => '/',
50 linux => '/',
51 machten => '/',
52 macos => ':',
53 midnightbsd => '/',
54 minix => '/',
55 mirbsd => '/',
56 mswin32 => "\\",
57 msys => '/',
58 netbsd => '/',
59 netware => "\\",
60 next => '/',
61 nto => '/',
62 openbsd => '/',
63 os2 => '/',
64 # Extended Binary Coded Decimal Interchange Code
65 os390 => '/',
66 os400 => '/',
67 qnx => '/',
68 riscos => '.',
69 sco => '/',
70 sco_sv => '/',
71 solaris => '/',
72 sunos => '/',
73 svr4 => '/',
74 svr5 => '/',
75 symbian => "\\",
76 unicos => '/',
77 unicosmk => '/',
78 vms => '/',
79 vos => '>',
80 win32 => "\\",
81 };
82 10 our $DIR_SEP = $OS2SEP->{ lc( $^O ) };
83 };
84  
85 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 # RFC 3986 section 5.2.4
89 # This is aimed for web URI initially, but is also used for filesystems in a simple way
90 sub collapse_dots
91 249 ▶{
Subroutine collapse_dots: called CC=20 SCAR=31.1
Pod documented
92 249 my $self = shift( @_ );
93 249 my $path = shift( @_ );
94 249 my $opts = $self->_get_args_as_hash( @_ );
95 # To avoid warnings
96 249 ▶ $opts->{separator} //= '';
Condition100%2/2
$opts->{"separator"} //= ""
l!l
75174
MC/DC100%1/1
$opts->{"separator"} //= ""
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
Aresult
11
00
97 # A path separator is provided when dealing with filesystem and not web URI
98 # We use this to know what to return and how to behave
99 249 ▶ my $sep = length( $opts->{separator} ) ? $opts->{separator} : '/';
Branch: 1 branch
BranchTrueFalseTotal
length $opts->{'separator'} ? : 75 174 249
100 249 ▶ return( '' ) if( !length( $path ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 0 249 249
101 249 ▶ my $u = $opts->{separator} ? URI::file->new( $path ) : URI->new( $path );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'separator'} ? : 75 174 249
102 249 my( @callinfo ) = caller;
103 249 ▶ $path = $opts->{separator} ? $u->file( $^O ) : $u->path;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'separator'} ? : 75 174 249
104 249 my @new = ();
105 249 my $len = length( $path );
106
107 # "If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer"
108 249 ▶ if( substr( $path, 0, 2 ) eq ".${sep}" )
Branch: 6 branches
BranchTrueFalseTotal
if (substr($path, 0, 2) eq ".$sep") { } 1 248 249
elsif (substr($path, 0, 3) eq "..$sep") { } 0 248 248
elsif (substr($path, 0, 3) eq "$sep.$sep") { } 0 248 248
elsif (substr($path, 0, 2) eq "$sep." and 2 == $len) { } 0 248 248
elsif ($path eq '..' or $path eq '.') { } 0 248 248
elsif ($path eq $sep) { } 2 246 248
109 {
110 1 substr( $path, 0, 2 ) = '';
111 }
112 elsif( substr( $path, 0, 3 ) eq "..${sep}" )
113 {
114 0 substr( $path, 0, 3 ) = '';
115 }
116 # "if the input buffer begins with a prefix of "/./" or "/.", where "." is a complete path segment, then replace that prefix with "/" in the input buffer"
117 elsif( substr( $path, 0, 3 ) eq "${sep}.${sep}" )
118 {
119 0 substr( $path, 0, 3 ) = $sep;
120 }
121 ▶ elsif( substr( $path, 0, 2 ) eq "${sep}." && 2 == $len )
Condition66%2/3
substr($path, 0, 2) eq "$sep." and 2 == $len
!ll&&!rl&&r
24260
MC/DC0%0/2
substr($path, 0, 2) eq "$sep." and 2 == $len
substr($path, 0, 2) eq "$sep." 2 == $len
Truth table66%2/3
A and B
A= substr($path, 0, 2) eq "$sep."B= 2 == $len
ABresult
0X0
100
111
122 {
123 0 substr( $path, 0, 2 ) = $sep;
124 }
125 ▶ elsif( $path eq '..' || $path eq '.' )
Condition33%1/3
$path eq ".." or $path eq "."
l!l&&r!l&&!r
00248
MC/DC0%0/2
$path eq ".." or $path eq "."
$path eq ".." $path eq "."
Truth table33%1/3
A or B
A= $path eq ".."B= $path eq "."
ABresult
1X1
011
000
126 {
127 0 $path = '';
128 }
129 elsif( $path eq $sep )
130 {
131 2 return( $u );
132 }
133
134 # -1 is used to ensure trailing blank entries do not get removed
135 247 my @segments = split( "\Q$sep\E", $path, -1 );
136 247 ▶ for( my $i = 0; $i < scalar( @segments ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @segments) 1295 247 1542
137 {
138 1295 my $segment = $segments[$i];
139 # "if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer"
140 1295 ▶ if( $segment eq '..' )
Branch: 2 branches
BranchTrueFalseTotal
if ($segment eq '..') { } 18 1277 1295
elsif ($segment eq '.') { } 2 1275 1277
141 {
142 18 pop( @new );
143 }
144 elsif( $segment eq '.' )
145 {
146 2 next;
147 }
148 else
149 {
150 1275 ▶ push( @new, ( defined( $segment ) ? $segment : '' ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $segment ? : 1275 0 1275
151 }
152 }
153 # Finally, the output buffer is returned as the result of remove_dot_segments.
154 247 my $new_path = join( $sep, @new );
155 # substr( $new_path, 0, 0 ) = $sep unless( substr( $new_path, 0, 1 ) eq '/' );
156 247 ▶ substr( $new_path, 0, 0 ) = $sep unless( File::Spec->file_name_is_absolute( $new_path ) );
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($new_path) 7 240 247
157 247 ▶ if( $opts->{separator} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'separator'}) { } 75 172 247
158 {
159 75 $u = URI::file->new( $new_path );
160 }
161 else
162 {
163 172 $u->path( $new_path );
164 }
165 247 return( $u );
166 }
167  
168 # Credits: Path::Tiny
169 sub slurp
170 30 ▶{
Subroutine slurp: called CC=12 SCAR=30.4
Pod documented
171 30 my $self = shift( @_ );
172 30 my $args = {};
173 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
174 30 ▶ $args = Scalar::Util::reftype( $_[0] ) eq 'HASH'
Branch: 2 branches
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 30 0 30
!(scalar(@_) % 2) ? : 0 0 0
175 ? shift( @_ )
176 : !( scalar( @_ ) % 2 )
177 ? { @_ }
178 : {};
179 30 ▶ my $file = $args->{filename} || $args->{file} || $self->filename;
Condition33%2/6
$args->{'filename'} || $args->{'file'} || $self->filename
l!l&&r!l&&!r
3000
$args->{"filename"} or $args->{"file"}
l!l&&r!l&&!r
3000
MC/DC0%0/3
$args->{'filename'} || $args->{'file'} || $self->filename
$args->{"filename"} $args->{"file"} $self->filename
Truth table25%1/4
A or B || C
A= $args->{"filename"}B= $args->{"file"}C= $self->filename
ABCresult
1XX1
01X1
0011
0000
180 30 ▶ return( $self->error( "No filename found." ) ) if( !length( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 0 30 30
181 30 ▶ my $binmode = $args->{binmode} // '';
Condition50%1/2
$args->{"binmode"} // ""
l!l
300
MC/DC0%0/1
$args->{"binmode"} // ""
$args->{"binmode"}
Truth table50%1/2
A
A= $args->{"binmode"}
Aresult
11
00
182 30 local $@;
183 # try-catch
184 my $rv = eval
185 30 {
186 30 ▶ my $fh = IO::File->new( "<$file" ) ||
Condition50%1/2
'IO::File'->new("<$file") || (return $self->error(qq[Unable to open file "$file" in read mode: $!]))
l!l
300
MC/DC0%0/1
'IO::File'->new("<$file") || (return $self->error(qq[Unable to open file "$file" in read mode: $!]))
'IO::File'->new("<$file")
Truth table50%1/2
A
A= 'IO::File'->new("<$file")
Aresult
11
00
187 return( $self->error( "Unable to open file \"$file\" in read mode: $!" ) );
188 30 ▶ $fh->binmode( $binmode ) if( length( $binmode ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $binmode 30 0 30
189 30 my $size;
190 30 ▶ if( $binmode eq ':unix' && ( $size = -s( $fh ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($binmode eq ':unix' and $size = -s $fh) { } 0 30 30
Condition33%1/3
$binmode eq ":unix" and $size = -s $fh
!ll&&!rl&&r
3000
MC/DC0%0/2
$binmode eq ":unix" and $size = -s $fh
$binmode eq ":unix" $size = -s $fh
Truth table33%1/3
A and B
A= $binmode eq ":unix"B= $size = -s $fh
ABresult
0X0
100
111
191 {
192 0 my $buf;
193 0 $fh->read( $buf, $size );
194 0 return( $buf );
195 }
196 else
197 {
198 30 local $/;
199 30 return( scalar( <$fh> ) );
200 }
201 };
202 30 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 30 30
203 {
204 0 return( $self->error( "An error occured while trying to open and read file \"$file\": $@" ) );
205 }
206 30 return( $rv );
207 }
208  
209 sub slurp_utf8
210 30 ▶{
Subroutine slurp_utf8: called CC=6 SCAR=19.5
Pod documented
211 30 my $self = shift( @_ );
212 30 my $args = {};
213 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
214 30 ▶ $args = Scalar::Util::reftype( $_[0] ) eq 'HASH'
Branch: 2 branches
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 0 30 30
!(scalar(@_) % 2) ? : 30 0 30
215 ? shift( @_ )
216 : !( scalar( @_ ) % 2 )
217 ? { @_ }
218 : {};
219 30 $args->{binmode} = ':utf8';
220 30 ▶ my $file = $args->{filename} || $args->{file} || $self->filename;
Condition33%2/6
$args->{'filename'} || $args->{'file'} || $self->filename
l!l&&r!l&&!r
0300
$args->{"filename"} or $args->{"file"}
l!l&&r!l&&!r
0030
MC/DC0%0/3
$args->{'filename'} || $args->{'file'} || $self->filename
$args->{"filename"} $args->{"file"} $self->filename
Truth table25%1/4
A or B || C
A= $args->{"filename"}B= $args->{"file"}C= $self->filename
ABCresult
1XX1
01X1
0011
0000
221 30 ▶ return( $self->error( "No filename found." ) ) if( !length( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 0 30 30
222 30 $args->{filename} = $file;
223 30 return( $self->slurp( $args ) );
224 }
225  
226  
227 10 1;
228 # NOTE: POD
229 __END__
« SSI/File.pm ↑ Summary SSI/Finfo.pm »