blib/lib/Plack/Middleware/Precompressed.pm

filter: Statementstmt 100% 63 / 63 Branchbran 95.8% 23 / 24 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 11 / 11 Podpod 50.0% 1 / 2 totaltotal 94.4% 102 / 108 CC 17 SCAR 18.8
CC
17
Cov
97%
call
12.0
__ANON__
4.0
rewrite
3.0
CRAP 6.6
↑ Summary
1 3 ▶use 5.006; 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::Precompressed;
4  
5 3 our $VERSION = '1.104';
6  
7 3 ▶BEGIN { require Plack::Middleware; our @ISA = 'Plack::Middleware' }
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Plack::Util::Accessor qw( match rules env_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Plack::MIME ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Array::RefElem ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶sub rewrite {
Subroutine rewrite: called CC=3 SCAR=11.0
Pod undocumented
15 7 my $self = shift;
16 7 my ( $env ) = @_;
17 7 my $rules = $self->rules;
18 7 ▶ $rules ? $rules->( defined $env ? $env : () ) : ( $_ .= '.gz' );
Branch: 2 branches
BranchTrueFalseTotal
$rules ? : 3 4 7
defined $env ? : 3 0 3
19 }
20  
21 14 ▶sub call {
Subroutine call: called CC=12 SCAR=24.9
Pod documented
22 14 my $self = shift;
23 14 my ( $env ) = @_;
24  
25 14 my $encoding;
26 14 my $path = $env->{'PATH_INFO'};
27 14 ▶ my $have_match = $self->match ? $path =~ $self->match : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->match ? : 10 4 14
28  
29 # the `deflate` encoding is unreliably messy so we won't support it
30 # c.f. http://zoompf.com/2012/02/lose-the-wait-http-compression
31 14 ▶ if ( $have_match ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_match) 10 4 14
32 ( $encoding ) =
33 8 ▶ grep { $_ eq 'gzip' or $_ eq 'x-gzip' }
Condition33%1/3
$_ eq 'gzip' || $_ eq 'x-gzip'
l!l&&r!l&&!r
800
MC/DC0%0/2
$_ eq 'gzip' || $_ eq 'x-gzip'
$_ eq 'gzip' $_ eq 'x-gzip'
Truth table33%1/3
A || B
A= $_ eq 'gzip'B= $_ eq 'x-gzip'
ABresult
1X1
011
000
34 8 map { s!\s+!!g; split /,/, lc }
35 10 grep { defined }
36 10 $env->{'HTTP_ACCEPT_ENCODING'};
37 }
38  
39 14 my $res = do {
40 14 ▶ my $keys = $self->env_keys || [];
Condition100%2/2
$self->env_keys || []
l!l
311
MC/DC100%1/1
$self->env_keys || []
$self->env_keys
Truth table100%2/2
A
A= $self->env_keys
Aresult
11
00
41 14 ▶ local @$env{ 'PATH_INFO', @$keys } = ( $path, @$env{ @$keys } ) if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 8 6 14
42 14 ▶ if ( $encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) 8 6 14
43 8 my %pass_env;
44 8 Array::RefElem::hv_store %pass_env, $_, $env->{ $_ } for @$keys;
45 8 $self->rewrite( \%pass_env ) for $env->{'PATH_INFO'};
46 }
47 14 ▶ delete local $env->{'HTTP_ACCEPT_ENCODING'} if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 8 6 14
48 14 $self->app->( $env );
49 };
50  
51 14 ▶ return $res unless $have_match;
Branch: 1 branch
BranchTrueFalseTotal
unless $have_match 4 10 14
52  
53 10 my $is_fail;
54 10 ▶ my $final_res = Plack::Util::response_cb( $res, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
55 10 my $res = shift;
56 10 $is_fail = $res->[0] != 200;
57 10 ▶ return if $is_fail;
Branch: 1 branch
BranchTrueFalseTotal
if $is_fail 4 6 10
58 6 push @{ $res->[1] }, 'Vary', 'Accept-Encoding';
59 6 ▶ if ( $encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) 5 1 6
60 5 my $mime = Plack::MIME->mime_type( $path );
61 5 ▶ Plack::Util::header_set( $res->[1], 'Content-Type', $mime ) if $mime;
Branch: 1 branch
BranchTrueFalseTotal
if $mime 2 3 5
62 5 push @{ $res->[1] }, 'Content-Encoding', $encoding;
63 }
64 6 return;
65 10 } );
66  
67 10 ▶ return $is_fail ? $self->app->( $env ) : $final_res;
Branch: 1 branch
BranchTrueFalseTotal
$is_fail ? : 4 6 10
68 }
69  
70 3 1;
71  
72 __END__
↑ Summary