blib/lib/Catalyst/Plugin/Static/Simple.pm

filter: Statementstmt 96.3% 156 / 162 Branchbran 75.6% 59 / 78 Conditioncond 62.5% 25 / 40 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 82.5% 265 / 321 CC 60 SCAR 23.3
CC
60
Cov
86%
__ANON__
18.3
_locate_static_file
16.6
_ext_to_type
9.1
CRAP 10.3
↑ Summary
1 package Catalyst::Plugin::Static::Simple;
2  
3 13 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use MIME::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 our $VERSION = '0.38';
12  
13 13 has _static_file => ( is => 'rw' );
14 13 has _static_debug_message => ( is => 'rw', isa => 'ArrayRef[Str]' );
15  
16 12 ▶after setup_finalize => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
17 12 my $c = shift;
18  
19 # New: Turn off new 'autoflush' flag in logger (see Catalyst::Log).
20 # This is needed to surpress output of debug log messages for
21 # static requests:
22 12 ▶ $c->log->autoflush(0) if $c->log->can('autoflush');
Branch: 1 branch
BranchTrueFalseTotal
if $c->log->can("autoflush") 12 0 12
23 13 };
24  
25 28 ▶before prepare_action => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
26 28 my $c = shift;
27 28 my $path = $c->req->path;
28 28 my $config = $c->config->{'Plugin::Static::Simple'};
29  
30 28 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
31  
32 # is the URI in a static-defined path?
33 28 foreach my $dir ( @{ $config->{dirs} } ) {
34 20 my $dir_re = quotemeta $dir;
35  
36 # strip trailing slashes, they'll be added in our regex
37 20 $dir_re =~ s{/$}{};
38  
39 20 my $re;
40  
41 20 ▶ if ( $dir =~ m{^qr/}xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ m[^qr/]msx) { } 6 14 20
42 6 $re = eval $dir;
43  
44 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
45 0 $c->error( "Error compiling static dir regex '$dir': $@" );
46 }
47 }
48 else {
49 14 $re = qr{^${dir_re}/};
50 }
51  
52 20 ▶ if ( $path =~ $re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$re/) 5 15 20
53 5 ▶ if ( $c->_locate_static_file( $path, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->_locate_static_file($path, 1)) { } 3 2 5
54 $c->_debug_msg( 'from static directory' )
55 3 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 3 3
56 } else {
57 $c->_debug_msg( "404: file not found: $path" )
58 2 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 1 1 2
59 2 $c->res->status( 404 );
60 2 $c->res->content_type( 'text/html' );
61 }
62 }
63 }
64  
65 # Does the path have an extension?
66 28 ▶ if ( $path =~ /\.([^\/\\]+)$/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m(\.([^/\\]+)$)m) 23 5 28
67 # and does it exist?
68 23 $c->_locate_static_file( $path );
69 }
70 13 };
71  
72 28 ▶around dispatch => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
73 28 my $orig = shift;
74 28 my $c = shift;
75  
76 28 ▶ return if ( $c->res->status != 200 );
Branch: 1 branch
BranchTrueFalseTotal
if $c->res->status != 200 2 26 28
77  
78 26 ▶ if ( $c->_static_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->_static_file) { } 16 10 26
79 16 ▶ if ( $c->config->{'Plugin::Static::Simple'}->{no_logs} && $c->log->can('abort') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->config->{"Plugin::Static::Simple"}{"no_logs"} and $c->log->can("abort")) 16 0 16
Condition33%1/3
$c->config->{"Plugin::Static::Simple"}{"no_logs"} and $c->log->can("abort")
!ll&&!rl&&r
0016
MC/DC0%0/2
$c->config->{"Plugin::Static::Simple"}{"no_logs"} and $c->log->can("abort")
$c->config->{"Plugin::Static::Simple"}{"no_logs"} $c->log->can("abort")
Truth table33%1/3
A and B
A= $c->config->{"Plugin::Static::Simple"}{"no_logs"}B= $c->log->can("abort")
ABresult
0X0
100
111
80 16 $c->log->abort( 1 );
81 }
82 16 return $c->_serve_static;
83 }
84 else {
85 10 return $c->$orig(@_);
86 }
87 13 };
88  
89 28 ▶before finalize => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
90 28 my $c = shift;
91  
92 # display all log messages
93 28 ▶ if ( $c->config->{'Plugin::Static::Simple'}->{debug} && scalar @{$c->_debug_msg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->config->{"Plugin::Static::Simple"}{"debug"} and scalar @{$c->_debug_msg;}) 1 27 28
Condition100%2/2
$c->config->{"Plugin::Static::Simple"}{"debug"} and scalar @{$c->_debug_msg;}
!ll
271
MC/DC100%1/1
$c->config->{"Plugin::Static::Simple"}{"debug"} and scalar @{$c->_debug_msg;}
$c->config->{"Plugin::Static::Simple"}{"debug"}
Truth table100%2/2
A
A= $c->config->{"Plugin::Static::Simple"}{"debug"}
Aresult
00
11
94 1 $c->log->debug( 'Static::Simple: ' . join q{ }, @{$c->_debug_msg} );
95 }
96 13 };
97  
98 12 ▶before setup_finalize => sub {
Subroutine __ANON__: called CC=15 SCAR=29.1
99 12 my $c = shift;
100  
101 $c->log->warn("Deprecated 'static' config key used, please use the key 'Plugin::Static::Simple' instead")
102 12 ▶ if exists $c->config->{static};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->config->{"static"} 1 11 12
103  
104 12 ▶ if (exists $c->config->{static}->{include_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->config->{"static"}{"include_path"}) 1 11 12
105 $c->config->{'Plugin::Static::Simple'}->{include_path} = [
106 1 ▶ @{$c->config->{'Plugin::Static::Simple'}->{include_path} || []},
Condition50%1/2
$c->config->{'Plugin::Static::Simple'}{'include_path'} || []
l!l
10
MC/DC0%0/1
$c->config->{'Plugin::Static::Simple'}{'include_path'} || []
$c->config->{'Plugin::Static::Simple'}{'include_path'}
Truth table50%1/2
A
A= $c->config->{'Plugin::Static::Simple'}{'include_path'}
Aresult
11
00
107 1 ▶ @{delete $c->config->{static}->{include_path} || []}
Condition50%1/2
delete $c->config->{'static'}{'include_path'} || []
l!l
10
MC/DC0%0/1
delete $c->config->{'static'}{'include_path'} || []
delete $c->config->{'static'}{'include_path'}
Truth table50%1/2
A
A= delete $c->config->{'static'}{'include_path'}
Aresult
11
00
108 ];
109 }
110
111 my $config
112 = $c->config->{'Plugin::Static::Simple'}
113 = $c->config->{'static'}
114 = Catalyst::Utils::merge_hashes(
115 $c->config->{'Plugin::Static::Simple'} || {},
116 $c->config->{static} || {}
117 12 ▶ );
Condition75%3/4
$c->config->{'Plugin::Static::Simple'} || {}
l!l
111
$c->config->{'static'} || {}
l!l
120
MC/DC100%1/1
$c->config->{'Plugin::Static::Simple'} || {}
$c->config->{'Plugin::Static::Simple'}
MC/DC0%0/1
$c->config->{'static'} || {}
$c->config->{'static'}
Truth table100%2/2
A
A= $c->config->{'Plugin::Static::Simple'}
Aresult
11
00
Truth table50%1/2
A
A= $c->config->{'static'}
Aresult
11
00
118  
119 12 ▶ $config->{dirs} ||= [];
Condition50%1/2
$config->{"dirs"} ||= []
l!l
012
MC/DC0%0/1
$config->{"dirs"} ||= []
$config->{"dirs"}
Truth table50%1/2
A
A= $config->{"dirs"}
Aresult
11
00
120 12 ▶ $config->{include_path} ||= [ $c->config->{root} ];
Condition100%2/2
$config->{"include_path"} ||= [$c->config->{"root"}]
l!l
111
MC/DC100%1/1
$config->{"include_path"} ||= [$c->config->{"root"}]
$config->{"include_path"}
Truth table100%2/2
A
A= $config->{"include_path"}
Aresult
11
00
121 12 ▶ $config->{mime_types} ||= {};
Condition50%1/2
$config->{"mime_types"} ||= {}
l!l
012
MC/DC0%0/1
$config->{"mime_types"} ||= {}
$config->{"mime_types"}
Truth table50%1/2
A
A= $config->{"mime_types"}
Aresult
11
00
122 12 ▶ $config->{ignore_extensions} ||= [ qw/tmpl tt tt2 html xhtml/ ];
Condition50%1/2
$config->{"ignore_extensions"} ||= ["tmpl", "tt", "tt2", "html", "xhtml"]
l!l
012
MC/DC0%0/1
$config->{"ignore_extensions"} ||= ["tmpl", "tt", "tt2", "html", "xhtml"]
$config->{"ignore_extensions"}
Truth table50%1/2
A
A= $config->{"ignore_extensions"}
Aresult
11
00
123 12 ▶ $config->{ignore_dirs} ||= [];
Condition50%1/2
$config->{"ignore_dirs"} ||= []
l!l
012
MC/DC0%0/1
$config->{"ignore_dirs"} ||= []
$config->{"ignore_dirs"}
Truth table50%1/2
A
A= $config->{"ignore_dirs"}
Aresult
11
00
124 12 ▶ $config->{debug} ||= $c->debug;
Condition33%1/3
$config->{"debug"} ||= $c->debug
l!l&&r!l&&!r
0012
MC/DC0%0/2
$config->{"debug"} ||= $c->debug
$config->{"debug"} $c->debug
Truth table33%1/3
A ||= B
A= $config->{"debug"}B= $c->debug
ABresult
1X1
011
000
125 12 ▶ $config->{no_logs} = 1 unless defined $config->{no_logs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"no_logs"} 12 0 12
126 12 ▶ $config->{no_logs} = 0 if $config->{logging};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"logging"} 0 12 12
127  
128 # load up a MIME::Types object, only loading types with
129 # at least 1 file extension
130 12 $config->{mime_types_obj} = MIME::Types->new( only_complete => 1 );
131 13 };
132  
133 # Search through all included directories for the static file
134 # Based on Template Toolkit INCLUDE_PATH code
135 28 ▶sub _locate_static_file {
Subroutine _locate_static_file: called CC=16 SCAR=28.1
136 28 my ( $c, $path, $in_static_dir ) = @_;
137  
138 28 $path = File::Spec->catdir(
139 File::Spec->no_upwards( File::Spec->splitdir( $path ) )
140 );
141  
142 28 my $config = $c->config->{'Plugin::Static::Simple'};
143 28 my @ipaths = @{ $config->{include_path} };
144 28 my $dpaths;
145 28 my $count = 64; # maximum number of directories to search
146  
147 DIR_CHECK:
148 28 ▶ while ( @ipaths && --$count) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ipaths and --$count) 37 11 48
Condition66%2/3
@ipaths and --$count
!ll&&!rl&&r
11037
MC/DC50%1/2
@ipaths and --$count
@ipaths --$count
Truth table66%2/3
A and B
A= @ipathsB= --$count
ABresult
0X0
100
111
149 37 ▶ my $dir = shift @ipaths || next DIR_CHECK;
Condition50%1/2
shift @ipaths || (next DIR_CHECK)
l!l
370
MC/DC0%0/1
shift @ipaths || (next DIR_CHECK)
shift @ipaths
Truth table50%1/2
A
A= shift @ipaths
Aresult
11
00
150  
151 37 ▶ if ( ref $dir eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dir eq 'CODE') { } 2 35 37
152 2 eval { $dpaths = &$dir( $c ) };
153 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
154 0 $c->log->error( 'Static::Simple: include_path error: ' . $@ );
155 } else {
156 2 unshift @ipaths, @$dpaths;
157 2 next DIR_CHECK;
158 }
159 } else {
160 35 $dir =~ s/(\/|\\)$//xms;
161 35 ▶ if ( -d $dir && -f $dir . '/' . $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir and -f $dir . '/' . $path) 23 12 35
Condition66%2/3
-d $dir and -f $dir . '/' . $path
!ll&&!rl&&r
01223
MC/DC50%1/2
-d $dir and -f $dir . '/' . $path
-d $dir -f $dir . '/' . $path
Truth table66%2/3
A and B
A= -d $dirB= -f $dir . '/' . $path
ABresult
0X0
100
111
162  
163 # Don't ignore any files in static dirs defined with 'dirs'
164 23 ▶ unless ( $in_static_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_static_dir) 20 3 23
165 # do we need to ignore the file?
166 20 for my $ignore ( @{ $config->{ignore_dirs} } ) {
167 6 $ignore =~ s{(/|\\)$}{};
168 6 ▶ if ( $path =~ /^$ignore(\/|\\)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^$ignore(/|\\)]) 3 3 6
169 $c->_debug_msg( "Ignoring directory `$ignore`" )
170 3 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 3 3
171 3 next DIR_CHECK;
172 }
173 }
174  
175 # do we need to ignore based on extension?
176 17 for my $ignore_ext ( @{ $config->{ignore_extensions} } ) {
177 80 ▶ if ( $path =~ /.*\.${ignore_ext}$/ixms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /.*\.$ignore_ext$/imsx) 3 77 80
178 $c->_debug_msg( "Ignoring extension `$ignore_ext`" )
179 3 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 3 3
180 3 next DIR_CHECK;
181 }
182 }
183 }
184  
185 $c->_debug_msg( 'Serving ' . $dir . '/' . $path )
186 17 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 17 17
187 17 return $c->_static_file( $dir . '/' . $path );
188 }
189 }
190 }
191  
192 11 return;
193 }
194  
195 17 ▶sub _serve_static {
Subroutine _serve_static: called CC=4 SCAR=14.2
196 17 my $c = shift;
197 17 my $config = $c->config->{'Plugin::Static::Simple'};
198  
199 17 ▶ my $full_path = shift || $c->_static_file;
Condition66%2/3
shift() || $c->_static_file
l!l&&r!l&&!r
1160
MC/DC0%0/2
shift() || $c->_static_file
shift() $c->_static_file
Truth table66%2/3
A || B
A= shift()B= $c->_static_file
ABresult
1X1
011
000
200 17 my $type = $c->_ext_to_type( $full_path );
201 17 my $stat = stat $full_path;
202  
203 17 $c->res->headers->content_type( $type );
204 17 $c->res->headers->content_length( $stat->size );
205 17 $c->res->headers->last_modified( $stat->mtime );
206 # Tell Firefox & friends its OK to cache, even over SSL:
207 17 $c->res->headers->header('Cache-control' => 'public');
208 # Optionally, set a fixed expiry time:
209 17 ▶ if ($config->{expires}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"expires"}) 0 17 17
210 0 $c->res->headers->expires(time() + $config->{expires});
211 }
212  
213 17 my $fh = IO::File->new( $full_path, 'r' );
214 17 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 17 0 17
215 17 binmode $fh;
216 17 $c->res->body( $fh );
217 }
218 else {
219 0 Catalyst::Exception->throw(
220 message => "Unable to open $full_path for reading" );
221 }
222  
223 17 return 1;
224 }
225  
226 2 ▶sub serve_static_file {
Subroutine serve_static_file: called CC=4 SCAR=14.0
Pod documented
227 2 my ( $c, $full_path ) = @_;
228  
229 2 my $config = $c->config->{'Plugin::Static::Simple'};
230  
231 2 ▶ if ( -e $full_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $full_path) { } 1 1 2
232 $c->_debug_msg( "Serving static file: $full_path" )
233 1 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 1 1
234 }
235 else {
236 $c->_debug_msg( "404: file not found: $full_path" )
237 1 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 1 1
238 1 $c->res->status( 404 );
239 1 $c->res->content_type( 'text/html' );
240 1 return;
241 }
242  
243 1 $c->_serve_static( $full_path );
244 }
245  
246 # looks up the correct MIME type for the current file extension
247 17 ▶sub _ext_to_type {
Subroutine _ext_to_type: called CC=8 SCAR=22.1
248 17 my ( $c, $full_path ) = @_;
249  
250 17 my $config = $c->config->{'Plugin::Static::Simple'};
251  
252 17 ▶ if ( $full_path =~ /.*\.(\S{1,})$/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_path =~ /.*\.(\S{1,})$/msx) { } 17 0 17
253 17 my $ext = $1;
254 my $type = $config->{mime_types}{$ext}
255 17 ▶ || $config->{mime_types_obj}->mimeTypeOf( $ext );
Condition100%3/3
$config->{'mime_types'}{$ext} || $config->{'mime_types_obj'}->mimeTypeOf($ext)
l!l&&r!l&&!r
2132
MC/DC100%2/2
$config->{'mime_types'}{$ext} || $config->{'mime_types_obj'}->mimeTypeOf($ext)
$config->{'mime_types'}{$ext} $config->{'mime_types_obj'}->mimeTypeOf($ext)
Truth table100%3/3
A || B
A= $config->{'mime_types'}{$ext}B= $config->{'mime_types_obj'}->mimeTypeOf($ext)
ABresult
1X1
011
000
256 17 ▶ if ( $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 15 2 17
257 15 ▶ $c->_debug_msg( "as $type" ) if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 15 15
258 15 ▶ return ( ref $type ) ? $type->type : $type;
Branch: 1 branch
BranchTrueFalseTotal
ref $type ? : 13 2 15
259 }
260 else {
261 $c->_debug_msg( "as text/plain (unknown extension $ext)" )
262 2 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 2 2
263 2 return 'text/plain';
264 }
265 }
266 else {
267 $c->_debug_msg( 'as text/plain (no extension)' )
268 0 ▶ if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
269 0 return 'text/plain';
270 }
271 }
272  
273 3 ▶sub _debug_msg {
Subroutine _debug_msg: called CC=3 SCAR=11.0
274 3 my ( $c, $msg ) = @_;
275  
276 3 ▶ if ( !defined $c->_static_debug_message ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c->_static_debug_message) 1 2 3
277 1 $c->_static_debug_message( [] );
278 }
279  
280 3 ▶ if ( $msg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 1 2 3
281 1 push @{ $c->_static_debug_message }, $msg;
282 }
283  
284 3 return $c->_static_debug_message;
285 }
286  
287 13 1;
288 __END__
↑ Summary