line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::GzipStatic; |
2
|
1
|
|
|
1
|
|
592
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
178
|
use Mojo::Util qw(monkey_patch); |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
466
|
use IO::Compress::Gzip 'gzip'; |
|
1
|
|
|
|
|
22323
|
|
|
1
|
|
|
|
|
358
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
1
|
|
|
1
|
1
|
70
|
my ($self, $app) = @_; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
monkey_patch 'Mojolicious::Static', serve => sub { |
12
|
1
|
|
|
1
|
|
21751
|
my ($self, $c, $rel) = @_; |
|
|
|
|
1
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
50
|
|
|
|
4
|
return undef unless my $asset = $self->file($rel); |
15
|
1
|
|
|
|
|
127
|
my $headers = $c->res->headers; |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
17
|
my $types = $c->app->types; |
18
|
1
|
50
|
|
|
|
15
|
my $type = $rel =~ /\.(\w+)$/ ? $types->type($1) : undef; |
19
|
1
|
50
|
33
|
|
|
31
|
if (defined($type) |
|
|
|
50
|
|
|
|
|
|
|
|
33
|
|
|
|
|
20
|
|
|
|
|
|
|
&& $type =~ /text|xml|javascript|json/ |
21
|
|
|
|
|
|
|
&& ($c->req->headers->accept_encoding // '') =~ /gzip/i) { |
22
|
1
|
|
|
|
|
27
|
$c->res->headers->append(Vary => 'Accept-Encoding'); |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
45
|
$c->res->headers->content_encoding('gzip'); |
25
|
1
|
|
|
|
|
19
|
gzip \$asset->slurp, \my $compressed; |
26
|
1
|
|
|
|
|
2275
|
$asset = Mojo::Asset::Memory->new; |
27
|
1
|
|
|
|
|
29
|
$asset->add_chunk($compressed); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
1
|
50
|
|
|
|
12
|
return !!$self->serve_asset($c, $asset) if $headers->content_type; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Content-Type |
33
|
1
|
|
33
|
|
|
11
|
$headers->content_type($type || $types->type('txt')); |
34
|
1
|
|
|
|
|
8
|
return !!$self->serve_asset($c, $asset); |
35
|
1
|
|
|
|
|
7
|
}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
1; |
39
|
|
|
|
|
|
|
__END__ |