line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::GzipStatic; |
2
|
1
|
|
|
1
|
|
755
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
809
|
use IO::Compress::Gzip 'gzip'; |
|
1
|
|
|
|
|
28278
|
|
|
1
|
|
|
|
|
394
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub register { |
8
|
1
|
|
|
1
|
1
|
48
|
my ($self, $app) = @_; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$app->hook(after_static => sub { |
11
|
1
|
|
|
1
|
|
25067
|
my $c = shift; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
|
|
4
|
my $type = $c->res->headers->content_type; |
14
|
1
|
50
|
33
|
|
|
40
|
if (defined($type) |
|
|
|
50
|
|
|
|
|
|
|
|
33
|
|
|
|
|
15
|
|
|
|
|
|
|
&& $type =~ /text|xml|javascript|json/ |
16
|
|
|
|
|
|
|
&& ($c->req->headers->accept_encoding // '') =~ /gzip/i) { |
17
|
1
|
|
|
|
|
26
|
$c->res->headers->append(Vary => 'Accept-Encoding'); |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
56
|
$c->res->headers->content_encoding('gzip'); |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
|
|
23
|
my $asset = $c->res->content->asset; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
|
|
22
|
gzip \$asset->slurp, \my $compressed; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
2515
|
$asset = Mojo::Asset::Memory->new; |
26
|
1
|
|
|
|
|
15
|
$asset->add_chunk($compressed); |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
14
|
$c->res->content->asset($asset); |
29
|
|
|
|
|
|
|
} |
30
|
1
|
|
|
|
|
17
|
}); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
1; |
34
|
|
|
|
|
|
|
__END__ |