| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | die 'config must be a hash reference' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | unless reftype($config) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 17 | ▶ | if (keys %$config) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | $min_size = delete $config->{min_size} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $config->{"min_size"} // ""
MC/DC100%1/1
delete $config->{"min_size"} // ""
delete $config->{"min_size"}
Truth table100%2/2
A
A= delete $config->{"min_size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 12 | ▶ | die 'invalid key passed to Mojolicious::Plugin::Gzip (only min_size is allowed)' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | if keys %$config; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | ▶ | die 'min_size must be a positive integer' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$min_size =~ /^\d+$/u and $min_size > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | unless $min_size =~ /^\d+$/ and $min_size > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | $min_size //= 860; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$min_size //= 860
MC/DC100%1/1
$min_size //= 860
$min_size
Truth table100%2/2
A
A= $min_size
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$req->headers->accept_encoding // ""
$req->headers->accept_encoding
Truth table50%1/2
A
A= $req->headers->accept_encoding
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 32 | my $body = $res->body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 32 | ▶ | my $body_size = $res->body_size // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$res->body_size // 0
MC/DC0%0/1
$res->body_size // 0
$res->body_size
Truth table50%1/2
A
A= $res->body_size
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 32 | ▶ | return unless $accept_encoding =~ /gzip/i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$accept_encoding =~ /gzip/iu and $body_size >= $min_size and $res->code == 200 and not $res->headers->content_encoding
$accept_encoding =~ /gzip/iu and $body_size >= $min_size and $res->code == 200
$accept_encoding =~ /gzip/iu and $body_size >= $min_size
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if (length $etag > 2 and substr($etag, 0, 1) eq '"' and substr($etag, -1, 1) eq '"') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
length $etag > 2 and substr($etag, 0, 1) eq "\"" and substr($etag, -1, 1) eq "\""
length $etag > 2 and substr($etag, 0, 1) eq "\""
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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||