| 1 | package Mojolicious::Static::Role::Compressed; | ||||||||||||||||||||||||||||
| 2 | 14 | ▶ | use Mojo::Base -role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use Mojo::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 14 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 14 | my $served_compressed_asset; | |||||||||||||||||||||||||||
| 10 | 14 | my @compression_types = ( | |||||||||||||||||||||||||||
| 11 | {ext => 'br', encoding => 'br', _lc_encoding => 'br'}, | ||||||||||||||||||||||||||||
| 12 | {ext => 'gz', encoding => 'gzip', _lc_encoding => 'gzip'}); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 47 | ▶ | sub compression_types { | ||||||||||||||||||||||||||
|
Subroutine
compression_types: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 47 | ▶ | return \@compression_types if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 40 | my ($self, $types) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 40 | ▶ | Carp::croak 'compression_types cannot be changed once serve_asset has served a compressed asset' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | if $served_compressed_asset; | ||||||||||||||||||||||||||||
| 20 | 38 | ▶ | Carp::croak 'compression_types requires an ARRAY ref' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined builtin::reftype($types) and builtin::reftype($types) eq "ARRAY"
MC/DC100%2/2
defined builtin::reftype($types) and builtin::reftype($types) eq "ARRAY"
defined builtin::reftype($types)
builtin::reftype($types) eq "ARRAY"
Truth table100%3/3
A and B
A= defined builtin::reftype($types)B= builtin::reftype($types) eq "ARRAY"
| |||||||||||||||||||||||||||||
| 21 | unless defined Scalar::Util::reftype($types) | ||||||||||||||||||||||||||||
| 22 | and Scalar::Util::reftype($types) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 23 | 34 | ▶ | Carp::croak 'compression_types requires a non-empty ARRAY ref' unless @$types; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 33 | my @new_types; | |||||||||||||||||||||||||||
| 26 | my %exts; | ||||||||||||||||||||||||||||
| 27 | 33 | my %encodings; | |||||||||||||||||||||||||||
| 28 | 33 | for (@$types) { | |||||||||||||||||||||||||||
| 29 | 54 | my $reftype = Scalar::Util::reftype($_); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 54 | ▶ | if (not defined $reftype) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 32 | 22 | ▶ | Carp::croak 'passed empty value in compression_types' unless defined $_ and $_ ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_ and $_ ne ""
MC/DC100%2/2
defined $_ and $_ ne ""
defined $_
$_ ne ""
Truth table100%3/3
A and B
A= defined $_B= $_ ne ""
| |||||||||||||||||||||||||||||
| 33 | 20 | ▶ | Carp::croak "duplicate ext '$_'" if exists $exts{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 18 | ▶ | Carp::croak "duplicate encoding '$_'" if exists $encodings{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 16 | $exts{$_} = $encodings{$_} = 1; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 16 | push @new_types, {ext => $_, encoding => $_, _lc_encoding => lc($_)}; | |||||||||||||||||||||||||||
| 39 | } elsif ($reftype eq 'HASH') { | ||||||||||||||||||||||||||||
| 40 | 29 | my ($ext, $encoding) = (delete $_->{ext}, delete $_->{encoding}); | |||||||||||||||||||||||||||
| 41 | 29 | ▶ | Carp::croak 'passed empty ext' unless defined $ext and $ext ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ext and $ext ne ""
MC/DC100%2/2
defined $ext and $ext ne ""
defined $ext
$ext ne ""
Truth table100%3/3
A and B
A= defined $extB= $ext ne ""
| |||||||||||||||||||||||||||||
| 42 | 25 | ▶ | Carp::croak 'passed empty encoding' unless defined $encoding and $encoding ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $encoding and $encoding ne ""
MC/DC100%2/2
defined $encoding and $encoding ne ""
defined $encoding
$encoding ne ""
Truth table100%3/3
A and B
A= defined $encodingB= $encoding ne ""
| |||||||||||||||||||||||||||||
| 43 | 22 | ▶ | Carp::croak q{extra keys and values passed in hash besides 'ext' and 'encoding': } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | . Mojo::Util::dumper $_ | ||||||||||||||||||||||||||||
| 45 | if keys %$_; | ||||||||||||||||||||||||||||
| 46 | 20 | ▶ | Carp::croak "duplicate ext '$ext'" if exists $exts{$ext}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 18 | ▶ | Carp::croak "duplicate encoding '$encoding'" if exists $encodings{$encoding}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 16 | $exts{$ext} = $encodings{$encoding} = 1; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 16 | push @new_types, {ext => $ext, encoding => $encoding, _lc_encoding => lc($encoding)}; | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 3 | Carp::croak 'passed illegal value to compression_types. Each value of ' | |||||||||||||||||||||||||||
| 54 | . 'the ARRAY ref must be a scalar or a HASH ref with only the ' | ||||||||||||||||||||||||||||
| 55 | . "keys 'ext' and 'encoding' and their values, but reftype was '$reftype'"; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 11 | @compression_types = @new_types; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 11 | return $self; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 121 | ▶ | my $should_serve_asset = sub { $_->path !~ /\.(pdf|jpe?g|gif|png|webp)$/i }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 14 | my $should_serve_asset_is_code = 1; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 57 | ▶ | sub should_serve_asset { | ||||||||||||||||||||||||||
|
Subroutine
should_serve_asset: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 57 | ▶ | return $should_serve_asset if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 26 | my ($self, $sub_or_scalar) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 26 | my $reftype = Scalar::Util::reftype($sub_or_scalar); | |||||||||||||||||||||||||||
| 72 | 26 | ▶ | Carp::croak | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $reftype or $reftype eq "CODE"
MC/DC100%2/2
not defined $reftype or $reftype eq "CODE"
not defined $reftype
$reftype eq "CODE"
Truth table100%3/3
not(A) or B
A= not defined $reftypeB= $reftype eq "CODE"
| |||||||||||||||||||||||||||||
| 73 | "reftype of should_serve_asset must be either undef (scalar) or 'CODE', but was '$reftype'" | ||||||||||||||||||||||||||||
| 74 | unless not defined $reftype or $reftype eq 'CODE'; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 24 | $should_serve_asset = $sub_or_scalar; | |||||||||||||||||||||||||||
| 77 | 24 | $should_serve_asset_is_code = defined $reftype; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 24 | ▶ | warn 'should_serve_asset is a scalar that is always false, so compressed ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$should_serve_asset_is_code or $should_serve_asset
MC/DC100%2/2
$should_serve_asset_is_code or $should_serve_asset
$should_serve_asset_is_code
$should_serve_asset
Truth table100%3/3
A or B
A= $should_serve_asset_is_codeB= $should_serve_asset
| |||||||||||||||||||||||||||||
| 80 | . 'assets will never be served. If this is because you are in development ' | ||||||||||||||||||||||||||||
| 81 | . 'mode, you should instead just not load this role.' | ||||||||||||||||||||||||||||
| 82 | unless $should_serve_asset_is_code or $should_serve_asset; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 24 | return $self; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 14 | my $_stash_asset_key = 'mojolicious_static_role_compressed.asset'; | |||||||||||||||||||||||||||
| 88 | 14 | my $_stash_compression_type_key = 'mojolicious_static_role_compressed.compression_type'; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 110 | ▶ | before serve_asset => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=22 SCAR=30.9
| |||||||||||||||||||||||||||||
| 91 | 110 | my ($self, $c, $asset) = @_; | |||||||||||||||||||||||||||
| 92 | 110 | ▶ | return unless $asset->is_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 109 | ▶ | if ($should_serve_asset_is_code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 99 | local $_ = $asset; | |||||||||||||||||||||||||||
| 96 | 99 | ▶ | return unless $should_serve_asset->(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 10 | ▶ | return unless $should_serve_asset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 99 | my $req_headers = $c->req->headers; | |||||||||||||||||||||||||||
| 102 | 99 | my ($compressed_asset, $compression_type); | |||||||||||||||||||||||||||
| 103 | 99 | ▶ | if (my $if_none_match_header = $req_headers->if_none_match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 20 | my @if_none_matches = map { Mojo::Util::trim $_ } split ',', $if_none_match_header; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 20 | for my $if_none_match (@if_none_matches) { | |||||||||||||||||||||||||||
| 107 | 24 | ▶ | if (my ($expected_encoding) = $if_none_match =~ /-(.+)"$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 21 | ▶ | if (my ($type) = grep { $_->{encoding} eq $expected_encoding } @compression_types) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 16 | my $compressed_asset_path = $asset->path . '.' . $type->{ext}; | |||||||||||||||||||||||||||
| 111 | 16 | ▶ | if (-f -r $compressed_asset_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 10 | my $comp_asset = Mojo::Asset::File->new(path => $compressed_asset_path); | |||||||||||||||||||||||||||
| 113 | my $etag | ||||||||||||||||||||||||||||
| 114 | = '"' | ||||||||||||||||||||||||||||
| 115 | . Mojo::Util::md5_sum($asset->mtime) . '-' | ||||||||||||||||||||||||||||
| 116 | 10 | . $type->{encoding} . '"'; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 10 | ▶ | if ($etag eq $if_none_match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 10 | $compressed_asset = $comp_asset; | |||||||||||||||||||||||||||
| 120 | 10 | $compression_type = $type; | |||||||||||||||||||||||||||
| 121 | 10 | last; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 6 | warn "Found compression type with encoding of $type->{encoding} " | |||||||||||||||||||||||||||
| 125 | . "in If-None-Match '$if_none_match', but asset at $compressed_asset_path does not exist, is a directory, or is unreadable."; | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 5 | warn | |||||||||||||||||||||||||||
| 129 | "Found expected compression encoding of '$expected_encoding' in If-None-Match '$if_none_match' for asset '" | ||||||||||||||||||||||||||||
| 130 | . $asset->path | ||||||||||||||||||||||||||||
| 131 | . q{', but encoding does not exist.}; | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 3 | my $etag = '"' . Mojo::Util::md5_sum($asset->mtime) . '"'; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # return if If-None-Match matches the uncompressed asset | ||||||||||||||||||||||||||||
| 137 | 3 | ▶ | return if $etag eq $if_none_match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 96 | my $accept_encoding = $req_headers->accept_encoding; | |||||||||||||||||||||||||||
| 144 | 96 | ▶ | return unless defined $accept_encoding && $accept_encoding ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $accept_encoding and $accept_encoding ne ""
MC/DC100%2/2
defined $accept_encoding and $accept_encoding ne ""
defined $accept_encoding
$accept_encoding ne ""
Truth table100%3/3
A and B
A= defined $accept_encodingB= $accept_encoding ne ""
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 83 | my $lc_accept_encoding = lc($accept_encoding); | |||||||||||||||||||||||||||
| 147 | return | ||||||||||||||||||||||||||||
| 148 | unless my @compression_possibilities | ||||||||||||||||||||||||||||
| 149 | 83 | ▶ | = grep { index($lc_accept_encoding, $_->{_lc_encoding}) != -1 } @compression_types; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 77 | ▶ | unless ($compressed_asset and $compression_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$compressed_asset and $compression_type
MC/DC50%1/2
$compressed_asset and $compression_type
$compressed_asset
$compression_type
Truth table66%2/3
A and B
A= $compressed_assetB= $compression_type
| |||||||||||||||||||||||||||||
| 152 | 67 | for my $type (@compression_possibilities) { | |||||||||||||||||||||||||||
| 153 | 85 | my $path = $asset->path . '.' . $type->{ext}; | |||||||||||||||||||||||||||
| 154 | 85 | ▶ | next unless -f -r $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 55 | my $comp_asset = Mojo::Asset::File->new(path => $path); | |||||||||||||||||||||||||||
| 157 | 55 | ▶ | if ($comp_asset->size >= $asset->size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 4 | warn 'Compressed asset ' | |||||||||||||||||||||||||||
| 159 | . $comp_asset->path . ' is ' | ||||||||||||||||||||||||||||
| 160 | . $comp_asset->size | ||||||||||||||||||||||||||||
| 161 | . ' bytes, and uncompressed asset ' | ||||||||||||||||||||||||||||
| 162 | . $asset->path . ' is ' | ||||||||||||||||||||||||||||
| 163 | . $asset->size | ||||||||||||||||||||||||||||
| 164 | . ' bytes. Continuing search for compressed assets.'; | ||||||||||||||||||||||||||||
| 165 | 4 | next; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 51 | ($compressed_asset, $compression_type) = ($comp_asset, $type); | |||||||||||||||||||||||||||
| 169 | 51 | last; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 77 | ▶ | return unless $compressed_asset and $compression_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$compressed_asset and $compression_type
MC/DC50%1/2
$compressed_asset and $compression_type
$compressed_asset
$compression_type
Truth table66%2/3
A and B
A= $compressed_assetB= $compression_type
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 61 | my $res_headers = $c->res->headers; | |||||||||||||||||||||||||||
| 175 | 61 | $res_headers->append(Vary => 'Accept-Encoding'); | |||||||||||||||||||||||||||
| 176 | 61 | $res_headers->content_encoding($compression_type->{encoding}); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # in case Mojolicious::Static::serve wasn't called first | ||||||||||||||||||||||||||||
| 179 | 61 | $c->app->types->content_type($c, {file => $asset->path}); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # set stash with asset for use in is_fresh before method modifier | ||||||||||||||||||||||||||||
| 182 | 61 | $c->stash($_stash_asset_key => $asset, $_stash_compression_type_key => $compression_type); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 61 | $_[2] = $compressed_asset; | |||||||||||||||||||||||||||
| 185 | 61 | $served_compressed_asset = 1; | |||||||||||||||||||||||||||
| 186 | 14 | }; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 110 | ▶ | before is_fresh => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 189 | 110 | my ($self, $c, $options) = @_; | |||||||||||||||||||||||||||
| 190 | 110 | my ($asset, $compression_type) = @{$c->stash}{$_stash_asset_key, $_stash_compression_type_key}; | |||||||||||||||||||||||||||
| 191 | 110 | ▶ | return unless $asset and $compression_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$asset and $compression_type
MC/DC50%1/2
$asset and $compression_type
$asset
$compression_type
Truth table66%2/3
A and B
A= $assetB= $compression_type
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 61 | my $mtime = $asset->mtime; | |||||||||||||||||||||||||||
| 194 | 61 | my $etag = Mojo::Util::md5_sum($mtime) . '-' . $compression_type->{encoding}; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 61 | @$options{qw/last_modified etag/} = ($mtime, $etag); | |||||||||||||||||||||||||||
| 197 | 14 | }; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 14 | 1; | |||||||||||||||||||||||||||
| 200 | __END__ | ||||||||||||||||||||||||||||