blib/lib/Mojolicious/Plugin/Gzip.pm

filter: Statementstmt 89.4% 34 / 38 Branchbran 75.0% 12 / 16 Conditioncond 65.3% 17 / 26 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 76.7% 76 / 99 CC 19 SCAR 25.3
CC
19
Cov
79%
__ANON__
17.7
register
10.2
BEGIN
1.0
CRAP 12.6
↑ Summary
1 package Mojolicious::Plugin::Gzip;
2 4 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Mojo::Util qw/gzip/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.04';
7  
8 18 ▶sub register {
Subroutine register: called CC=9 SCAR=23.2
Pod documented
9 18 my (undef, $app, $config) = @_;
10  
11 18 my $min_size;
12 18 ▶ if (defined $config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 18 0 18
13 18 ▶ die 'config must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($config) eq "HASH" 1 17 18
14 unless reftype($config) eq 'HASH';
15 17 ▶ if (keys %$config) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$config) 12 5 17
16 12 ▶ $min_size = delete $config->{min_size} // '';
Condition100%2/2
delete $config->{"min_size"} // ""
l!l
102
MC/DC100%1/1
delete $config->{"min_size"} // ""
delete $config->{"min_size"}
Truth table100%2/2
A
A= delete $config->{"min_size"}
Aresult
11
00
17 12 ▶ die 'invalid key passed to Mojolicious::Plugin::Gzip (only min_size is allowed)'
Branch: 1 branch
BranchTrueFalseTotal
if keys %$config 2 10 12
18 if keys %$config;
19 10 ▶ die 'min_size must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless $min_size =~ /^\d+$/u and $min_size > 0 6 4 10
Condition100%3/3
$min_size =~ /^\d+$/u and $min_size > 0
!ll&&!rl&&r
514
MC/DC100%2/2
$min_size =~ /^\d+$/u and $min_size > 0
$min_size =~ /^\d+$/u $min_size > 0
Truth table100%3/3
A and B
A= $min_size =~ /^\d+$/uB= $min_size > 0
ABresult
0X0
100
111
20 unless $min_size =~ /^\d+$/ and $min_size > 0;
21 }
22 }
23 9 ▶ $min_size //= 860;
Condition100%2/2
$min_size //= 860
l!l
45
MC/DC100%1/1
$min_size //= 860
$min_size
Truth table100%2/2
A
A= $min_size
Aresult
11
00
24  
25 32 ▶ $app->hook(after_dispatch => sub {
Subroutine __ANON__: called CC=11 SCAR=28.7
26 32 my ($c) = @_;
27 32 my $req = $c->req;
28 32 my $res = $c->res;
29  
30 32 ▶ my $accept_encoding = $req->headers->accept_encoding // '';
Condition50%1/2
$req->headers->accept_encoding // ""
l!l
320
MC/DC0%0/1
$req->headers->accept_encoding // ""
$req->headers->accept_encoding
Truth table50%1/2
A
A= $req->headers->accept_encoding
Aresult
11
00
31 32 my $body = $res->body;
32 32 ▶ my $body_size = $res->body_size // 0;
Condition50%1/2
$res->body_size // 0
l!l
320
MC/DC0%0/1
$res->body_size // 0
$res->body_size
Truth table50%1/2
A
A= $res->body_size
Aresult
11
00
33  
34 32 ▶ return unless $accept_encoding =~ /gzip/i
Branch: 1 branch
BranchTrueFalseTotal
unless $accept_encoding =~ /gzip/iu and $body_size >= $min_size and $res->code == 200 and not $res->headers->content_encoding 30 2 32
Condition88%8/9
$accept_encoding =~ /gzip/iu and $body_size >= $min_size and $res->code == 200 and not $res->headers->content_encoding
!ll&&!rl&&r
2912
$accept_encoding =~ /gzip/iu and $body_size >= $min_size and $res->code == 200
!ll&&!rl&&r
2903
$accept_encoding =~ /gzip/iu and $body_size >= $min_size
!ll&&!rl&&r
1283
MC/DC75%3/4
$accept_encoding =~ /gzip/iu and $body_size >= $min_size and $res->code == 200 and not $res->headers->content_encoding
$accept_encoding =~ /gzip/iu $body_size >= $min_size $res->code == 200 not $res->headers->content_encoding
Truth table80%4/5
A and B and C and not(D)
A= $accept_encoding =~ /gzip/iuB= $body_size >= $min_sizeC= $res->code == 200D= not $res->headers->content_encoding
ABCDresult
0XXX0
10XX0
110X0
11100
11111
35 and $body_size >= $min_size
36 and $res->code == 200
37 and not $res->headers->content_encoding;
38  
39 2 ▶ if (my $etag = $res->headers->etag) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $etag = $res->headers->etag) 0 2 2
40 0 ▶ if (length $etag > 2 and substr($etag, 0, 1) eq '"' and substr($etag, -1, 1) eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (length $etag > 2 and substr($etag, 0, 1) eq '"' and substr($etag, -1, 1) eq '"') { } 0 0 0
Condition0%0/6
length $etag > 2 and substr($etag, 0, 1) eq "\"" and substr($etag, -1, 1) eq "\""
!ll&&!rl&&r
000
length $etag > 2 and substr($etag, 0, 1) eq "\""
!ll&&!rl&&r
000
MC/DC0%0/3
length $etag > 2 and substr($etag, 0, 1) eq "\"" and substr($etag, -1, 1) eq "\""
length $etag > 2 substr($etag, 0, 1) eq "\"" substr($etag, -1, 1) eq "\""
Truth table0%0/4
A and B and C
A= length $etag > 2B= substr($etag, 0, 1) eq "\""C= substr($etag, -1, 1) eq "\""
ABCresult
0XX0
10X0
1100
1111
41 0 $etag = substr($etag, 1, length($etag) - 2);
42 } else {
43 0 $app->log->warn("Found either empty ETag or ETag not surrounded by quotes: '$etag'");
44 }
45  
46 0 $res->headers->etag(qq{"$etag-gzip"});
47 }
48  
49 2 my $zipped_body = gzip $body;
50 2 $res->body($zipped_body);
51 2 $res->headers->content_length(length $zipped_body);
52 2 $res->headers->append(Vary => 'Accept-Encoding');
53 2 $res->headers->content_encoding('gzip');
54 9 });
55 }
56  
57 4 1;
58 __END__
↑ Summary