Middleware/Static/Precompressed.pm

filter: Statementstmt 87.4% 139 / 159 Branchbran 72.2% 65 / 90 Conditioncond 65.6% 21 / 32 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 79.1% 19 / 24 Podpod 71.4% 5 / 7 totaltotal 76.4% 253 / 331 CC 64 SCAR 24.6
CC
64
Cov
80%
add_file
22.2
prepare_app
14.4
call
12.3
CRAP 11.7
↑ Summary App/File/Precompressed.pm »
1 5 ▶use 5.008001; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Plack::Middleware::Static::Precompressed;
4 5 our $VERSION = '1.002';
5 5 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 ext_map
8 default_charset
9 max_file_size
10 max_total_size
11 max_open_files
12 files
13 root
14 path_info
15 handle_cb
16 fh_error_cb
17 response_cb
18 5 );
19  
20 5 ▶use Plack::Middleware::NeverExpire 1.201 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Cache::LRU ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶sub new {
Subroutine new: called CC=11 SCAR=25.0
Pod documented
25 5 my $self = shift->SUPER::new( @_ );
26  
27 5 ▶ $self->ext_map( { gz => 'gzip', br => 'br' } ) unless defined $self->ext_map;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ext_map 5 0 5
28 5 ▶ $self->default_charset( 'utf-8' ) unless defined $self->default_charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->default_charset 4 1 5
29 5 ▶ $self->max_file_size( 8192 ) unless defined $self->max_file_size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->max_file_size 4 1 5
30 5 ▶ $self->max_total_size( 8192 * 1024 ) unless defined $self->max_total_size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->max_total_size 5 0 5
31 5 ▶ $self->max_open_files( 128 ) unless defined $self->max_open_files;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->max_open_files 4 1 5
32 5 ▶ $self->handle_cb( \&_default_handle_cb ) unless $self->handle_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->handle_cb 3 2 5
33 5 ▶ $self->fh_error_cb( \&_default_fh_error_cb ) unless $self->fh_error_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->fh_error_cb 4 1 5
34  
35 5 ▶ if ( my $root = $self->root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $root = $self->root) 0 5 5
36 0 require File::Find;
37 File::Find::find( {
38 0 ▶ wanted => sub { -f and $self->add_file( $_, ( stat _ )[ 7, 9 ] ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
-f $_ and $self->add_file($_, (stat _)[7, 9]) 0 0 0
39 0 no_chdir => 1
40 }, $root );
41 }
42  
43 5 ▶ if ( my $files = $self->files ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $files = $self->files) 1 4 5
44 1 $self->files( undef );
45 1 for my $path ( @$files ) {
46 5 $self->add_file( $path, ( stat $path )[ 7, 9 ] );
47 }
48 }
49  
50 5 $self;
51 }
52  
53 15 ▶sub prepare_app {
Subroutine prepare_app: called CC=14 SCAR=26.7
Pod documented
54 15 my $self = shift;
55 15 ▶ return if $self->{'fh_cache'}; # already prepared
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh_cache"} 10 5 15
56  
57 5 ▶ $self->app( sub { [ 404, [qw( Content-Type text/plain )], [ 'Not found' ] ] } ) unless $self->app;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app 4 1 5
58  
59 5 my $resources = \%{ $self->{'resources'} };
60 5 my @bad = map "\n $_", sort grep !exists $resources->{ $_ }{'size'}, keys %$resources;
61 5 ▶ die __PACKAGE__ . ': cannot handle resources without identity encoding:', @bad, "\n" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 5 5
62  
63 5 $self->{'fh_cache'} = Cache::LRU->new( size => $self->max_open_files );
64 5 my $total = 0;
65 5 for my $representation (
66 7 ▶ sort { $a->{'size'} <=> $b->{'size'} }
Branch: 1 branch
BranchTrueFalseTotal
$_->{'enc'} ? : 1 6 7
67 1 map +( $_, $_->{'enc'} ? values %{ $_->{'enc'} } : () ),
68 values %$resources
69 ) {
70 7 my $size = $representation->{'size'};
71 7 ▶ last if $size > $self->max_file_size;
Branch: 1 branch
BranchTrueFalseTotal
if $size > $self->max_file_size 1 6 7
72 6 ▶ last if $total + $size > $self->max_total_size;
Branch: 1 branch
BranchTrueFalseTotal
if $total + $size > $self->max_total_size 0 6 6
73 6 $total += $size;
74 6 my $path = $representation->{'path'};
75 6 ▶ my $fh = $self->handle_cb->( $path ) or _default_fh_error_cb( $path );
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->handle_cb->($path) or _default_fh_error_cb($path) 6 0 6
76 6 my $body_ref = _slurp_psgi_handle( $fh );
77 6 ( my $len = length $$body_ref ) += 0;
78 6 ▶ $size == $len or die __PACKAGE__ . ": mismatch between content length ($len) and the size passed to add_file ($size) for $path\n";
Branch: 1 branch
BranchTrueFalseTotal
$size == $len or die 'Plack::Middleware::Static::Precompressed' . ": mismatch between content length ($len) and the size passed to add_file ($size) for $path\n" 6 0 6
79 6 $representation->{'fh'} = _default_handle_cb( $body_ref );
80 }
81  
82 5 for my $representation ( values %$resources ) {
83 7 ▶ my $encodings = $representation->{'enc'} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $encodings = $representation->{"enc"} or next 1 6 7
84 1 ▶ $encodings->{'x-gzip'} = $_ for $encodings->{'gzip'} || (); # RFC 9110 section 8.4.1.3
Condition33%1/3
$encodings->{'gzip'} || ()
l!l&&r!l&&!r
100
MC/DC0%0/2
$encodings->{'gzip'} || ()
$encodings->{'gzip'} ()
Truth table33%1/3
A || B
A= $encodings->{'gzip'}B= ()
ABresult
1X1
011
000
85 1 $representation->{'avail_rx'} = join '|', map quotemeta, keys %$encodings;
86 }
87 }
88  
89 7 ▶sub get_content_encoding_from_extension {
Subroutine get_content_encoding_from_extension: called CC=5 SCAR=16.2
Pod undocumented
90 7 my $self = shift;
91 7 ▶ my $ext_map = $self->ext_map or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ext_map = $self->ext_map or return 7 0 7
92 7 ▶ my $ext_rx = ( $ext_map eq ( $self->{'cached_ext_map'} || '' ) ) ? $self->{'cached_ext_rx'} : do {
Branch: 1 branch
BranchTrueFalseTotal
$ext_map eq ($self->{'cached_ext_map'} || '') ? : 4 3 7
Condition100%2/2
$self->{'cached_ext_map'} || ''
l!l
43
MC/DC100%1/1
$self->{'cached_ext_map'} || ''
$self->{'cached_ext_map'}
Truth table100%2/2
A
A= $self->{'cached_ext_map'}
Aresult
11
00
93 3 $self->{'cached_ext_map'} = "$ext_map";
94 3 $self->{'cached_ext_rx'} = join '|', map quotemeta, sort keys %$ext_map;
95 };
96 7 ▶ $_[0] =~ s/\.($ext_rx)\z// ? $ext_map->{ $1 } : ();
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ s/\.($ext_rx)\z// ? : 0 7 7
97 }
98  
99 2 ▶sub add_files { my $self = shift; $self->add_file( @$_ ) for @_; $self }
Subroutine add_files: called CC=2 SCAR=6.9
Pod documented
100  
101 5 my %is_rx = map +( $_ => 1 ), '', ref qr//;
102 8 ▶sub add_file {
Subroutine add_file: called CC=19 SCAR=31.0
Pod documented
103 8 my ( $self, $path, $size, $mtime ) = ( shift, @_ );
104  
105 8 my $headers = [];
106 8 ▶ $mtime and push @$headers, 'Last-Modified' => Plack::Middleware::NeverExpire::imf_fixdate( $mtime );
Branch: 1 branch
BranchTrueFalseTotal
$mtime and push @$headers, "Last-Modified", Plack::Middleware::NeverExpire::imf_fixdate($mtime) 5 3 8
107  
108 8 my $get_path_info = $self->path_info;
109 8 ▶ if ( $get_path_info and $is_rx{ ref $get_path_info } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($get_path_info and $is_rx{ref $get_path_info}) 0 8 8
Condition66%2/3
$get_path_info and $is_rx{ref $get_path_info}
!ll&&!rl&&r
350
MC/DC0%0/2
$get_path_info and $is_rx{ref $get_path_info}
$get_path_info $is_rx{ref $get_path_info}
Truth table66%2/3
A and B
A= $get_path_infoB= $is_rx{ref $get_path_info}
ABresult
0X0
100
111
110 0 my $rx = $get_path_info;
111 0 ▶ $self->path_info( $get_path_info = sub { /$rx/s ? $1 : $_ } );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/$rx/s ? : 0 0 0
112 }
113 8 ▶ my ( $path_info, $enc ) = $get_path_info ? map $get_path_info->( $_, $headers ), my $copy = $path : $path;
Branch: 1 branch
BranchTrueFalseTotal
$get_path_info ? : 5 3 8
114 8 $path_info =~ s!\A(?:\.?/)?!/!;
115  
116 8 ▶ if ( defined $enc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $enc) { } 1 7 8
117 1 ▶ $enc = 'gzip' if 'x-gzip' eq $enc; # RFC 9110 section 8.4.1.3 (see also further below)
Branch: 1 branch
BranchTrueFalseTotal
if "x-gzip" eq $enc 0 1 1
118 1 ▶ undef $enc if 'identity' eq $enc or '*' eq $enc;
Branch: 1 branch
BranchTrueFalseTotal
if "identity" eq $enc or "*" eq $enc 0 1 1
Condition33%1/3
"identity" eq $enc or "*" eq $enc
l!l&&r!l&&!r
001
MC/DC0%0/2
"identity" eq $enc or "*" eq $enc
"identity" eq $enc "*" eq $enc
Truth table33%1/3
A or B
A= "identity" eq $encB= "*" eq $enc
ABresult
1X1
011
000
119 } else {
120 7 $enc = $self->get_content_encoding_from_extension( $path_info );
121 }
122  
123 8 my $identity = \%{ $self->{'resources'}{ $path_info } };
124 8 ▶ my $representation = $enc ? \%{ $identity->{'enc'}{ $enc } } : $identity;
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 1 7 8
125 8 $representation->{'path'} = $path;
126 8 ▶ $representation->{'size'} = $size || 0;
Condition50%1/2
$size || 0
l!l
80
MC/DC0%0/1
$size || 0
$size
Truth table50%1/2
A
A= $size
Aresult
11
00
127  
128 8 ▶ if ( exists $identity->{'size'} and my $encodings = $identity->{'enc'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $identity->{"size"} and my $encodings = $identity->{"enc"}) 1 7 8
Condition66%2/3
exists $identity->{"size"} and my $encodings = $identity->{"enc"}
!ll&&!rl&&r
071
MC/DC50%1/2
exists $identity->{"size"} and my $encodings = $identity->{"enc"}
exists $identity->{"size"} my $encodings = $identity->{"enc"}
Truth table66%2/3
A and B
A= exists $identity->{"size"}B= my $encodings = $identity->{"enc"}
ABresult
0X0
100
111
129 1 my @useless = grep $encodings->{ $_ }{'size'} >= $identity->{'size'}, keys %$encodings;
130 @useless < keys %$encodings
131 ? delete @$encodings{ @useless }
132 1 ▶ : delete $identity->{'enc'};
Branch: 1 branch
BranchTrueFalseTotal
@useless < keys %$encodings ? : 1 0 1
133 }
134  
135 8 ▶ return $self if $enc;
Branch: 1 branch
BranchTrueFalseTotal
if $enc 1 7 8
136  
137 # we remove these here so we won't need to use Plack::Util::header_set at runtime
138 7 Plack::Util::header_remove( $headers, 'Vary' );
139 7 Plack::Util::header_remove( $headers, 'Content-Encoding' );
140 7 Plack::Util::header_remove( $headers, 'Content-Length' );
141  
142 7 ▶ unless ( Plack::Util::header_exists( $headers, 'Content-Type' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Plack::Util::header_exists($headers, "Content-Type")) 6 1 7
143 6 require Plack::MIME;
144 6 ▶ if ( my $type = Plack::MIME->mime_type( $path_info ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = "Plack::MIME"->mime_type($path_info)) 6 0 6
145 6 ▶ my $default = $type =~ m!^text/! && $self->default_charset;
Condition66%2/3
$type =~ m[^text/] && $self->default_charset
!ll&&!rl&&r
105
MC/DC50%1/2
$type =~ m[^text/] && $self->default_charset
$type =~ m[^text/] $self->default_charset
Truth table66%2/3
A && B
A= $type =~ m[^text/]B= $self->default_charset
ABresult
0X0
100
111
146 6 ▶ $type .= ";charset=$default" if $default;
Branch: 1 branch
BranchTrueFalseTotal
if $default 5 1 6
147 6 push @$headers, 'Content-Type' => $type;
148 }
149 }
150  
151 7 $representation->{'hdrs'} = $headers;
152  
153 7 $self;
154 }
155  
156 15 ▶sub call {
Subroutine call: called CC=12 SCAR=25.1
Pod documented
157 15 my ( $self, $env ) = ( shift, @_ );
158  
159 15 ▶ my $representation = $self->{'resources'}{ $env->{'PATH_INFO'} || '/' }
Branch: 1 branch
BranchTrueFalseTotal
my $representation = $self->{"resources"}{$env->{"PATH_INFO"} or "/"} or return $self->app->($env) 11 4 15
Condition50%1/2
$env->{'PATH_INFO'} || '/'
l!l
150
MC/DC0%0/1
$env->{'PATH_INFO'} || '/'
$env->{'PATH_INFO'}
Truth table50%1/2
A
A= $env->{'PATH_INFO'}
Aresult
11
00
160 or return $self->app->( $env );
161  
162 11 my ( $resource_headers, $headers ) = $representation->{'hdrs'};
163  
164 11 ▶ if ( my $encodings = $representation->{'enc'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encodings = $representation->{'enc'}) { } 2 9 11
165 2 my $accepted = 'identity';
166 2 ▶ if ( defined ( my $will_accept = $env->{'HTTP_ACCEPT_ENCODING'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $will_accept = $env->{"HTTP_ACCEPT_ENCODING"})) 1 1 2
167 1 my $candidate;
168 # we accept any q-value other than 0 but we ignore it
169 # we just send the smallest acceptable representation
170 1 for my $enc ( ( lc $will_accept ) =~ m[
171 (?: \A | , )
172 \s* ( $representation->{'avail_rx'} ) \s*
173 (?: ; \s* q= (?:
174 1(?:\.0{1,3})?
175 |
176 0\.(?:[1-9][0-9]{0,2}|0[1-9][0-9]?|00[1-9])
177 ) \s* )?
178 (?: \z | (?=,) )
179 ]gx ) {
180 ( $representation, $accepted ) = ( $candidate, $enc )
181 if $candidate = $encodings->{ $enc }
182 1 ▶ and $candidate->{'size'} < $representation->{'size'};
Branch: 1 branch
BranchTrueFalseTotal
if $candidate = $encodings->{$enc} and $candidate->{"size"} < $representation->{"size"} 1 0 1
Condition33%1/3
$candidate = $encodings->{$enc} and $candidate->{"size"} < $representation->{"size"}
!ll&&!rl&&r
001
MC/DC0%0/2
$candidate = $encodings->{$enc} and $candidate->{"size"} < $representation->{"size"}
$candidate = $encodings->{$enc} $candidate->{"size"} < $representation->{"size"}
Truth table33%1/3
A and B
A= $candidate = $encodings->{$enc}B= $candidate->{"size"} < $representation->{"size"}
ABresult
0X0
100
111
183 }
184 }
185 $headers = [
186 Vary => 'Accept-Encoding',
187 @$resource_headers,
188 'Content-Encoding' => $accepted,
189 2 'Content-Length' => $representation->{'size'},
190 ];
191 } else {
192 9 $headers = [ @$resource_headers, 'Content-Length' => $representation->{'size'} ];
193 }
194  
195 11 ▶ my $fh = $representation->{'fh'} || $self->{'fh_cache'}->get( $representation->{'path'} ) || do {
Condition100%6/6
$representation->{'fh'} || $self->{'fh_cache'}->get($representation->{'path'}) || do { my $path = $representation->{'path'}; $self->{'fh_cache'}->set($path, $self->handle_cb->($path) || (return $self->fh_error_cb->($path))) }
l!l&&r!l&&!r
821
$representation->{"fh"} or $self->{"fh_cache"}->get($representation->{"path"})
l!l&&r!l&&!r
713
MC/DC0%0/3
$representation->{'fh'} || $self->{'fh_cache'}->get($representation->{'path'}) || do { my $path = $representation->{'path'}; $self->{'fh_cache'}->set($path, $self->handle_cb->($path) || (return $self->fh_error_cb->($path))) }
$representation->{"fh"} $self->{"fh_cache"}->get($representation->{"path"}) do { my $path = $representation->{'path'}; $self->{'fh_cache'}->set($path, $self->handle_cb->($path) || (return $self->fh_error_cb->($path))) }
Truth table75%3/4
A or B || C
A= $representation->{"fh"}B= $self->{"fh_cache"}->get($representation->{"path"})C= do { my $path = $representation->{'path'}; $self->{'fh_cache'}->set($path, $self->handle_cb->($path) || (return $self->fh_error_cb->($path))) }
ABCresult
1XX1
01X1
0011
0000
196 3 my $path = $representation->{'path'};
197 3 ▶ $self->{'fh_cache'}->set( $path, $self->handle_cb->( $path ) || return $self->fh_error_cb->( $path ) );
Condition100%2/2
$self->handle_cb->($path) || (return $self->fh_error_cb->($path))
l!l
21
MC/DC100%1/1
$self->handle_cb->($path) || (return $self->fh_error_cb->($path))
$self->handle_cb->($path)
Truth table100%2/2
A
A= $self->handle_cb->($path)
Aresult
11
00
198 };
199  
200 10 my $res = [ 200, $headers, $fh ];
201 10 ▶ $self->{'response_cb'} && $self->{'response_cb'}->( $res );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"response_cb"} and $self->{"response_cb"}->($res) 0 10 10
202 10 $res;
203 }
204  
205 6 ▶sub _slurp_psgi_handle {
Subroutine _slurp_psgi_handle: called CC=2 SCAR=6.9
206 6 my ( $fh ) = @_;
207 6 local $/;
208 6 my $body = '';
209 6 ▶ while ( defined( my $line = $fh->getline ) ) { $body .= $line }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline)) 6 6 12
210 6 $fh->close;
211 6 \$body;
212 }
213  
214 0 ▶sub _default_fh_error_cb { die __PACKAGE__ . ": could not open $_[0]: $!\n" }
Subroutine _default_fh_error_cb: not called CC=1 SCAR=6.9
215  
216 11 ▶sub _default_handle_cb {
Subroutine _default_handle_cb: called CC=2 SCAR=7.1
217 11 ▶ open my $fh, '<:raw', $_[0] or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $_[0] or return undef 11 0 11
218 11 ( bless $fh, 'Plack::Util::IOWithPath::Reusable' )->path( $_[0] );
219 11 $fh;
220 }
221  
222 0 ▶sub dump_map {
Subroutine dump_map: not called CC=3 SCAR=24.8
Pod undocumented
223 0 my ( $self, $printer ) = ( shift, @_ );
224 0 $self->prepare_app;
225 0 my $resources = $self->{'resources'};
226 0 ▶ if ( 'CODE' ne ref $printer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" ne ref $printer) 0 0 0
227 0 my $fh = $printer;
228 0 ▶ $printer = sub { print $fh @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 }
230 $printer->( map {
231 0 my $encodings = $resources->{ $_ }{'enc'};
232 0 ▶ my @enc = $encodings ? sort keys %$encodings : ();
Branch: 1 branch
BranchTrueFalseTotal
$encodings ? : 0 0 0
233 "$_ ($resources->{ $_ }{'size'} bytes)\n",
234 0 map { " - $_ ($encodings->{ $_ }{'size'} bytes)\n" } @enc;
235 } sort keys %$resources );
236 0 $self;
237 }
238  
239 package Plack::Util::IOWithPath::Reusable;
240 5 ▶use parent -norequire => 'Plack::Util::IOWithPath';
Subroutine BEGIN: called CC=1 SCAR=0.0
241 12 ▶sub close { seek $_[0], 0, 0 } # subvert PSGI spec for our purposes
Subroutine close: called CC=1 SCAR=0.0
242  
243 5 1;
244  
245 __END__
↑ Summary App/File/Precompressed.pm »