line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Gzip; |
2
|
4
|
|
|
4
|
|
25244
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
30
|
|
3
|
4
|
|
|
4
|
|
937
|
use Mojo::Util qw/gzip/; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
227
|
|
4
|
4
|
|
|
4
|
|
24
|
use Scalar::Util qw/reftype/; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
2291
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
18
|
|
|
18
|
1
|
12607
|
my (undef, $app, $config) = @_; |
10
|
|
|
|
|
|
|
|
11
|
18
|
|
|
|
|
35
|
my $min_size; |
12
|
18
|
50
|
|
|
|
53
|
if (defined $config) { |
13
|
18
|
100
|
|
|
|
84
|
die 'config must be a hash reference' |
14
|
|
|
|
|
|
|
unless reftype($config) eq 'HASH'; |
15
|
17
|
100
|
|
|
|
56
|
if (keys %$config) { |
16
|
12
|
|
100
|
|
|
45
|
$min_size = delete $config->{min_size} // ''; |
17
|
12
|
100
|
|
|
|
52
|
die 'invalid key passed to Mojolicious::Plugin::Gzip (only min_size is allowed)' |
18
|
|
|
|
|
|
|
if keys %$config; |
19
|
10
|
100
|
100
|
|
|
110
|
die 'min_size must be a positive integer' |
20
|
|
|
|
|
|
|
unless $min_size =~ /^\d+$/ and $min_size > 0; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
} |
23
|
9
|
|
100
|
|
|
44
|
$min_size //= 860; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$app->hook(after_dispatch => sub { |
26
|
32
|
|
|
32
|
|
329159
|
my ($c) = @_; |
27
|
32
|
|
|
|
|
104
|
my $req = $c->req; |
28
|
32
|
|
|
|
|
295
|
my $res = $c->res; |
29
|
|
|
|
|
|
|
|
30
|
32
|
|
50
|
|
|
294
|
my $accept_encoding = $req->headers->accept_encoding // ''; |
31
|
32
|
|
|
|
|
602
|
my $body = $res->body; |
32
|
32
|
|
50
|
|
|
582
|
my $body_size = $res->body_size // 0; |
33
|
|
|
|
|
|
|
|
34
|
32
|
100
|
100
|
|
|
869
|
return unless $accept_encoding =~ /gzip/i |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
35
|
|
|
|
|
|
|
and $body_size >= $min_size |
36
|
|
|
|
|
|
|
and $res->code == 200 |
37
|
|
|
|
|
|
|
and not $res->headers->content_encoding; |
38
|
|
|
|
|
|
|
|
39
|
19
|
100
|
|
|
|
402
|
if (my $etag = $res->headers->etag) { |
40
|
10
|
100
|
100
|
|
|
214
|
if (length $etag > 2 and substr($etag, 0, 1) eq '"' and substr($etag, -1, 1) eq '"') { |
|
|
|
66
|
|
|
|
|
41
|
5
|
|
|
|
|
19
|
$etag = substr($etag, 1, length($etag) - 2); |
42
|
|
|
|
|
|
|
} else { |
43
|
5
|
|
|
|
|
21
|
$app->log->warn("Found either empty ETag or ETag not surrounded by quotes: '$etag'"); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
10
|
|
|
|
|
100
|
$res->headers->etag(qq{"$etag-gzip"}); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
19
|
|
|
|
|
293
|
my $zipped_body = gzip $body; |
50
|
19
|
|
|
|
|
35784
|
$res->body($zipped_body); |
51
|
19
|
|
|
|
|
840
|
$res->headers->content_length(length $zipped_body); |
52
|
19
|
|
|
|
|
294
|
$res->headers->append(Vary => 'Accept-Encoding'); |
53
|
19
|
|
|
|
|
855
|
$res->headers->content_encoding('gzip'); |
54
|
9
|
|
|
|
|
80
|
}); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
__END__ |