blib/lib/Plack/Middleware/Deflater.pm

filter: Statementstmt 91.8% 101 / 110 Branchbran 50.0% 29 / 58 Conditioncond 32.1% 9 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 88.2% 15 / 17 Podpod 100% 2 / 2 totaltotal 67.5% 156 / 231 CC 43 SCAR 36.0
CC
43
Cov
71%
__ANON__
66.0
print
18.5
new
3.1
CRAP 36.4
↑ Summary
1 package Plack::Middleware::Deflater;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 our $VERSION = '0.14';
5 3 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Plack::Util::Accessor qw( content_type vary_user_agent);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶sub prepare_app {
Subroutine prepare_app: called CC=3 SCAR=11.0
Pod documented
10 3 my $self = shift;
11 3 ▶ if (my $match_cts = $self->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match_cts = $self->content_type) 1 2 3
12 1 ▶ $match_cts = [$match_cts] if !ref $match_cts;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $match_cts 1 0 1
13 1 $self->content_type($match_cts);
14 }
15 }
16  
17 1 ▶sub call {
Subroutine call: called CC=1 SCAR=0.4
Pod documented
18 1 my($self, $env) = @_;
19  
20 1 my $res = $self->app->($env);
21  
22 $self->response_cb(
23 $res,
24 1 ▶ sub {
Subroutine __ANON__: called CC=29 SCAR=41.9
25 1 my $res = shift;
26  
27 # can't operate on Content-Ranges
28 1 ▶ return if $env->{HTTP_CONTENT_RANGE};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"HTTP_CONTENT_RANGE"} 0 1 1
29  
30 1 ▶ return if $env->{"plack.skip-deflater"};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"plack.skip-deflater"} 0 1 1
31  
32 1 my $h = Plack::Util::headers($res->[1]);
33 1 ▶ my $content_type = $h->get('Content-Type') || '';
Condition50%1/2
$h->get('Content-Type') || ''
l!l
10
MC/DC0%0/1
$h->get('Content-Type') || ''
$h->get('Content-Type')
Truth table50%1/2
A
A= $h->get('Content-Type')
Aresult
11
00
34 1 $content_type =~ s/(;.*)$//;
35 1 ▶ if (my $match_cts = $self->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match_cts = $self->content_type) 1 0 1
36 1 my $match = 0;
37 1 for my $match_ct (@{$match_cts}) {
38 1 ▶ if ($content_type eq $match_ct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type eq $match_ct) 1 0 1
39 1 $match++;
40 1 last;
41 }
42 }
43 1 ▶ return unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 1 1
44 }
45  
46 1 ▶ if (Plack::Util::status_with_no_entity_body($res->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (Plack::Util::status_with_no_entity_body($res->[0]) or $h->exists("Cache-Control") and $h->get("Cache-Control") =~ /\bno-transform\b/) 0 1 1
Condition33%2/6
Plack::Util::status_with_no_entity_body($res->[0]) or $h->exists("Cache-Control") and $h->get("Cache-Control") =~ /\bno-transform\b/
l!l&&r!l&&!r
001
$h->exists("Cache-Control") and $h->get("Cache-Control") =~ /\bno-transform\b/
!ll&&!rl&&r
100
MC/DC0%0/3
Plack::Util::status_with_no_entity_body($res->[0]) or $h->exists("Cache-Control") and $h->get("Cache-Control") =~ /\bno-transform\b/
Plack::Util::status_with_no_entity_body($res->[0]) $h->exists("Cache-Control") $h->get("Cache-Control") =~ /\bno-transform\b/
Truth table25%1/4
A or B and C
A= Plack::Util::status_with_no_entity_body($res->[0])B= $h->exists("Cache-Control")C= $h->get("Cache-Control") =~ /\bno-transform\b/
ABCresult
1XX1
0111
00X0
0100
47 or $h->exists('Cache-Control') && $h->get('Cache-Control') =~ /\bno-transform\b/)
48 {
49 0 return;
50 }
51  
52 1 ▶ my @vary = split /\s*,\s*/, ($h->get('Vary') || '');
Condition50%1/2
$h->get('Vary') || ''
l!l
01
MC/DC0%0/1
$h->get('Vary') || ''
$h->get('Vary')
Truth table50%1/2
A
A= $h->get('Vary')
Aresult
11
00
53 1 push @vary, 'Accept-Encoding';
54 1 ▶ push @vary, 'User-Agent' if $self->vary_user_agent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->vary_user_agent 1 0 1
55 1 $h->set('Vary' => join(",", @vary));
56  
57 # Do not clobber already existing encoding
58 1 ▶ return if $h->exists('Content-Encoding') && $h->get('Content-Encoding') ne 'identity';
Branch: 1 branch
BranchTrueFalseTotal
if $h->exists("Content-Encoding") and $h->get("Content-Encoding") ne "identity" 0 1 1
Condition33%1/3
$h->exists("Content-Encoding") and $h->get("Content-Encoding") ne "identity"
!ll&&!rl&&r
100
MC/DC0%0/2
$h->exists("Content-Encoding") and $h->get("Content-Encoding") ne "identity"
$h->exists("Content-Encoding") $h->get("Content-Encoding") ne "identity"
Truth table33%1/3
A and B
A= $h->exists("Content-Encoding")B= $h->get("Content-Encoding") ne "identity"
ABresult
0X0
100
111
59  
60 # some browsers might have problems, so set no-compress
61 1 ▶ return if $env->{"psgix.no-compress"};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"psgix.no-compress"} 0 1 1
62  
63 # Some browsers might have problems with content types
64 # other than text/html, so set compress-only-text/html
65 1 ▶ if ($env->{"psgix.compress-only-text/html"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgix.compress-only-text/html"}) 0 1 1
66 0 ▶ return if $content_type ne 'text/html';
Branch: 1 branch
BranchTrueFalseTotal
if $content_type ne "text/html" 0 0 0
67 }
68  
69 # TODO check quality
70 1 my $encoding = 'identity';
71 1 ▶ if (defined $env->{HTTP_ACCEPT_ENCODING}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"HTTP_ACCEPT_ENCODING"}) 1 0 1
72 1 for my $enc (qw(gzip deflate identity)) {
73 1 ▶ if ($env->{HTTP_ACCEPT_ENCODING} =~ /\b$enc\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"HTTP_ACCEPT_ENCODING"} =~ /\b$enc\b/) 1 0 1
74 1 $encoding = $enc;
75 1 last;
76 }
77 }
78 }
79  
80 1 my $encoder;
81 1 ▶ if ($encoding eq 'gzip' || $encoding eq 'deflate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq "gzip" or $encoding eq "deflate") 1 0 1
Condition33%1/3
$encoding eq "gzip" or $encoding eq "deflate"
l!l&&r!l&&!r
100
MC/DC0%0/2
$encoding eq "gzip" or $encoding eq "deflate"
$encoding eq "gzip" $encoding eq "deflate"
Truth table33%1/3
A or B
A= $encoding eq "gzip"B= $encoding eq "deflate"
ABresult
1X1
011
000
82 1 $encoder = Plack::Middleware::Deflater::Encoder->new($encoding);
83 }
84  
85 1 ▶ if ($encoder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoder) 1 0 1
86 1 $h->set('Content-Encoding' => $encoding);
87 1 $h->remove('Content-Length');
88  
89 # normal response
90 1 ▶ if ($res->[2] && ref($res->[2]) && ref($res->[2]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[2] and ref $res->[2] and ref $res->[2] eq "ARRAY") 1 0 1
Condition33%2/6
$res->[2] and ref $res->[2] and ref $res->[2] eq "ARRAY"
!ll&&!rl&&r
001
$res->[2] and ref $res->[2]
!ll&&!rl&&r
001
MC/DC0%0/3
$res->[2] and ref $res->[2] and ref $res->[2] eq "ARRAY"
$res->[2] ref $res->[2] ref $res->[2] eq "ARRAY"
Truth table25%1/4
A and B and C
A= $res->[2]B= ref $res->[2]C= ref $res->[2] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
91 1 my $buf = '';
92 1 foreach (@{ $res->[2] }) {
93 1 ▶ $buf .= $encoder->print($_) if defined $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 1 0 1
94 }
95 1 $buf .= $encoder->close();
96 1 $res->[2] = [$buf];
97 1 return;
98 }
99  
100 # delayed or stream
101 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 $encoder->print(shift);
103 0 };
104 }
105 }
106 1 );
107 }
108  
109 3 1;
110  
111 package Plack::Middleware::Deflater::Encoder;
112  
113 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 3 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 3 ▶use constant GZIP_MAGIC => 0x1f8b;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
120 1 my $class = shift;
121 1 my $encoding = shift;
122 1 ▶ my($encoder, $status) =
Branch: 1 branch
BranchTrueFalseTotal
$encoding eq 'gzip' ? : 1 0 1
123 $encoding eq 'gzip'
124 ? deflateInit(-WindowBits => -MAX_WBITS())
125 : deflateInit(-WindowBits => MAX_WBITS());
126 1 ▶ die 'Cannot create a deflation stream' if $status != Z_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $status != Z_OK() 0 1 1
127  
128 1 bless {
129 header => 0,
130 closed => 0,
131 encoding => $encoding,
132 encoder => $encoder,
133 crc => crc32(undef),
134 length => 0,
135 }, $class;
136 }
137  
138 2 ▶sub print : method {
Subroutine print: called CC=11 SCAR=29.2
139 2 my $self = shift;
140 2 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 2 2
141 2 my $chunk = shift;
142 2 ▶ if (!defined $chunk) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chunk) 1 1 2
143 1 my($buf, $status) = $self->{encoder}->flush();
144 1 ▶ die "deflate failed: $status" if ($status != Z_OK);
Branch: 1 branch
BranchTrueFalseTotal
if $status != Z_OK() 0 1 1
145 1 ▶ if (!$self->{header} && $self->{encoding} eq 'gzip') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"header"} and $self->{"encoding"} eq "gzip") 1 0 1
Condition33%1/3
not $self->{"header"} and $self->{"encoding"} eq "gzip"
!ll&&!rl&&r
001
MC/DC0%0/2
not $self->{"header"} and $self->{"encoding"} eq "gzip"
not $self->{"header"} $self->{"encoding"} eq "gzip"
Truth table33%1/3
not(A) and B
A= not $self->{"header"}B= $self->{"encoding"} eq "gzip"
ABresult
0X0
100
111
146 1 $buf = pack("nccVcc", GZIP_MAGIC, Z_DEFLATED, 0, time(), 0, $Compress::Raw::Zlib::gzip_os_code) . $buf;
147 }
148 $buf .= pack("LL", $self->{crc}, $self->{length})
149 1 ▶ if $self->{encoding} eq 'gzip';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding"} eq "gzip" 1 0 1
150 1 $self->{closed} = 1;
151 1 return $buf;
152 }
153  
154 1 my($buf, $status) = $self->{encoder}->deflate($chunk);
155 1 ▶ die "deflate failed: $status" if ($status != Z_OK);
Branch: 1 branch
BranchTrueFalseTotal
if $status != Z_OK() 0 1 1
156 1 $self->{length} += length $chunk;
157 1 $self->{crc} = crc32($chunk, $self->{crc});
158 1 ▶ if (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 0 1 1
159 0 ▶ if (!$self->{header} && $self->{encoding} eq 'gzip') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"header"} and $self->{"encoding"} eq "gzip") 0 0 0
Condition0%0/3
not $self->{"header"} and $self->{"encoding"} eq "gzip"
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"header"} and $self->{"encoding"} eq "gzip"
not $self->{"header"} $self->{"encoding"} eq "gzip"
Truth table0%0/3
not(A) and B
A= not $self->{"header"}B= $self->{"encoding"} eq "gzip"
ABresult
0X0
100
111
160 0 $buf = pack("nccVcc", GZIP_MAGIC, Z_DEFLATED, 0, time(), 0, $Compress::Raw::Zlib::gzip_os_code) . $buf;
161 }
162 0 $self->{header} = 1;
163 0 return $buf;
164 }
165 1 return '';
166 }
167  
168 1 ▶sub close : method {
Subroutine close: called CC=1 SCAR=0.0
169 1 $_[0]->print(undef);
170 }
171  
172 0 ▶sub closed {
Subroutine closed: not called CC=1 SCAR=6.9
173 0 $_[0]->{closed};
174 }
175  
176 3 1;
177  
178 __END__
↑ Summary