Mojolicious/Static.pm

filter: Statementstmt 90.3% 121 / 134 Branchbran 69.4% 50 / 72 Conditioncond 53.0% 26 / 49 MC/DCmcdc 34.4% 10 / 29 Subroutinesub 95.6% 22 / 23 Podpod 100% 8 / 8 totaltotal 75.2% 237 / 315 CC 60 SCAR 24.7
CC
60
Cov
77%
serve_asset
32.3
asset_path
20.0
is_fresh
17.9
CRAP 11.8
« Mojolicious/Command/get.pm ↑ Summary Mojolicious/Plugin/DefaultHelpers.pm »
1 package Mojolicious::Static;
2 20 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::Asset::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Mojo::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Mojo::File qw(curfile path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Mojo::Loader qw(data_section file_is_binary);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Mojo::Util qw(encode md5_sum trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Bundled files
12 20 my $PUBLIC = curfile->sibling('resources', 'public');
13 340 ▶my %EXTRA = $PUBLIC->list_tree->map(sub { join('/', @{$_->to_rel($PUBLIC)}), $_->realpath->to_string })->each;
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 20 has asset_dir => 'assets';
16 42 ▶has classes => sub { ['main'] };
Subroutine __ANON__: called CC=1 SCAR=0.0
17 14 ▶has extra => sub { +{%EXTRA} };
Subroutine __ANON__: called CC=1 SCAR=0.0
18 83 ▶has paths => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 20 has 'prefix';
20  
21 0 ▶sub asset_path {
Subroutine asset_path: not called CC=4 SCAR=30.0
Pod documented
22 0 my ($self, $asset) = @_;
23 0 ▶ $asset = "/$asset" unless $asset =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $asset =~ m[^/]u 0 0 0
24 0 ▶ my $assets = $self->{assets} //= {};
Condition0%0/2
$self->{"assets"} //= {}
l!l
00
MC/DC0%0/1
$self->{"assets"} //= {}
$self->{"assets"}
Truth table0%0/2
A
A= $self->{"assets"}
Aresult
11
00
25 0 ▶ return $self->file_path('/' . $self->asset_dir . ($assets->{$asset} // $asset));
Condition0%0/3
$assets->{$asset} // $asset
l!l&&r!l&&!r
000
MC/DC0%0/2
$assets->{$asset} // $asset
$assets->{$asset} $asset
Truth table0%0/3
A // B
A= $assets->{$asset}B= $asset
ABresult
1X1
011
000
26 }
27  
28 162 ▶sub dispatch {
Subroutine dispatch: called CC=9 SCAR=22.0
Pod documented
29 162 my ($self, $c) = @_;
30  
31 # Method (GET or HEAD)
32 162 my $req = $c->req;
33 162 my $method = $req->method;
34 162 ▶ return undef unless $method eq 'GET' || $method eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "GET" or $method eq "HEAD" 14 148 162
Condition66%2/3
$method eq "GET" or $method eq "HEAD"
l!l&&r!l&&!r
148014
MC/DC50%1/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET" $method eq "HEAD"
Truth table66%2/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
ABresult
1X1
011
000
35  
36 # Canonical path
37 148 my $stash = $c->stash;
38 148 my $path = $req->url->path;
39 148 ▶ $path = $stash->{path} ? $path->new($stash->{path}) : $path->clone;
Branch: 1 branch
BranchTrueFalseTotal
$stash->{'path'} ? : 1 147 148
40 148 ▶ return undef unless my @parts = @{$path->canonicalize->parts};
Branch: 1 branch
BranchTrueFalseTotal
unless my(@parts) = @{$path->canonicalize->parts;} 8 140 148
41  
42 # Serve static file and prevent path traversal
43 140 my $canon_path = join '/', @parts;
44 140 ▶ return undef if $canon_path =~ /^\.\.\/|\\/ || !$self->serve($c, $canon_path);
Branch: 1 branch
BranchTrueFalseTotal
if $canon_path =~ m[^\.\./|\\]u or not $self->serve($c, $canon_path) 122 18 140
Condition100%3/3
$canon_path =~ m[^\.\./|\\]u or not $self->serve($c, $canon_path)
l!l&&r!l&&!r
1111118
MC/DC100%2/2
$canon_path =~ m[^\.\./|\\]u or not $self->serve($c, $canon_path)
$canon_path =~ m[^\.\./|\\]u not $self->serve($c, $canon_path)
Truth table100%3/3
A or not(B)
A= $canon_path =~ m[^\.\./|\\]uB= not $self->serve($c, $canon_path)
ABresult
1X1
011
000
45 18 $stash->{'mojo.static'} = 1;
46  
47 # Development assets will be rebuilt a lot, do not let browsers cache them
48 18 ▶ $c->res->headers->cache_control('no-cache')
Branch: 1 branch
BranchTrueFalseTotal
if $c->app->mode eq "development" and index($canon_path, $self->asset_dir) == 0 0 18 18
Condition66%2/3
$c->app->mode eq "development" and index($canon_path, $self->asset_dir) == 0
!ll&&!rl&&r
8100
MC/DC0%0/2
$c->app->mode eq "development" and index($canon_path, $self->asset_dir) == 0
$c->app->mode eq "development" index($canon_path, $self->asset_dir) == 0
Truth table66%2/3
A and B
A= $c->app->mode eq "development"B= index($canon_path, $self->asset_dir) == 0
ABresult
0X0
100
111
49 if $c->app->mode eq 'development' && index($canon_path, $self->asset_dir) == 0;
50  
51 18 return !!$c->rendered;
52 }
53  
54 135 ▶sub file {
Subroutine file: called CC=6 SCAR=17.9
Pod documented
55 135 my ($self, $rel) = @_;
56  
57 135 ▶ $self->warmup unless $self->{index};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"index"} 12 123 135
58  
59 # Search all paths
60 135 my @parts = split /\//, $rel;
61 135 for my $path (@{$self->paths}) {
62 191 ▶ next unless my $asset = _get_file(path($path, @parts)->to_string);
Branch: 1 branch
BranchTrueFalseTotal
unless my $asset = _get_file(path($path, @parts)->to_string) 173 18 191
63 18 return $asset;
64 }
65  
66 # Search DATA
67 117 ▶ if (my $asset = $self->_get_data_file($rel)) { return $asset }
Branch: 1 branch
BranchTrueFalseTotal
if (my $asset = $self->_get_data_file($rel)) 3 114 117
68  
69 # Search extra files
70 114 my $extra = $self->extra;
71 114 ▶ return exists $extra->{$rel} ? _get_file($extra->{$rel}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra->{$rel} ? : 0 114 114
72 }
73  
74 128 ▶sub file_path {
Subroutine file_path: called CC=3 SCAR=11.4
Pod documented
75 128 my ($self, $file) = @_;
76 128 ▶ $file = "/$file" unless $file =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ m[^/]u 17 111 128
77 128 ▶ return $file unless my $prefix = $self->prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless my $prefix = $self->prefix 128 0 128
78 0 return "$prefix$file";
79 }
80  
81 19 ▶sub is_fresh {
Subroutine is_fresh: called CC=15 SCAR=28.8
Pod documented
82 19 my ($self, $c, $options) = @_;
83  
84 19 my $res_headers = $c->res->headers;
85 19 my ($last, $etag) = @$options{qw(last_modified etag)};
86 19 ▶ $res_headers->last_modified(Mojo::Date->new($last)->to_string) if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 19 0 19
87 19 ▶ $res_headers->etag($etag = ($etag =~ m!^W/"! ? $etag : qq{"$etag"})) if $etag;
Branch: 2 branches
BranchTrueFalseTotal
if $etag 19 0 19
$etag =~ m[^W/"]u ? : 0 19 19
88  
89 # Unconditional
90 19 my $req_headers = $c->req->headers;
91 19 my $match = $req_headers->if_none_match;
92 19 ▶ return undef unless (my $since = $req_headers->if_modified_since) || $match;
Branch: 1 branch
BranchTrueFalseTotal
unless my $since = $req_headers->if_modified_since or $match 13 6 19
Condition100%3/3
my $since = $req_headers->if_modified_since or $match
l!l&&r!l&&!r
3313
MC/DC100%2/2
my $since = $req_headers->if_modified_since or $match
my $since = $req_headers->if_modified_since $match
Truth table100%3/3
A or B
A= my $since = $req_headers->if_modified_sinceB= $match
ABresult
1X1
011
000
93  
94 # If-None-Match
95 6 ▶ $etag //= $res_headers->etag // '';
Condition25%1/4
$etag //= $res_headers->etag // ""
l!l
60
$res_headers->etag // ""
l!l
00
MC/DC0%0/1
$etag //= $res_headers->etag // ""
$etag
Truth table50%1/2
A
A= $etag
Aresult
11
00
96 6 ▶ return undef if $match && !grep { $_ eq $etag || "W/$_" eq $etag } map { trim($_) } split /,/, $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match and not grep({$_ eq $etag or "W/$_" eq $etag;} map({trim($_);} split(/,/u, $match, 0))) 2 4 6
Condition83%5/6
$match and not grep({$_ eq $etag or "W/$_" eq $etag;} map({trim($_);} split(/,/u, $match, 0)))
!ll&&!rl&&r
132
$_ eq $etag || "W/$_" eq $etag
l!l&&r!l&&!r
302
MC/DC100%2/2
$match and not grep({$_ eq $etag or "W/$_" eq $etag;} map({trim($_);} split(/,/u, $match, 0)))
$match not grep({$_ eq $etag or "W/$_" eq $etag;} map({trim($_);} split(/,/u, $match, 0)))
MC/DC50%1/2
$_ eq $etag || "W/$_" eq $etag
$_ eq $etag "W/$_" eq $etag
Truth table100%3/3
A and not(B)
A= $matchB= not grep({$_ eq $etag or "W/$_" eq $etag;} map({trim($_);} split(/,/u, $match, 0)))
ABresult
0X0
100
111
Truth table66%2/3
A || B
A= $_ eq $etagB= "W/$_" eq $etag
ABresult
1X1
011
000
97  
98 # If-Modified-Since
99 4 ▶ return !!$match unless ($last //= $res_headers->last_modified) && $since;
Branch: 1 branch
BranchTrueFalseTotal
unless $last //= $res_headers->last_modified and $since 2 2 4
Condition50%3/6
$last //= $res_headers->last_modified and $since
!ll&&!rl&&r
022
$last //= $res_headers->last_modified
l!l&&r!l&&!r
400
MC/DC0%0/3
$last //= $res_headers->last_modified and $since
$last $res_headers->last_modified $since
Truth table0%0/5
A //= B and C
A= $lastB= $res_headers->last_modifiedC= $since
ABCresult
00X0
1X00
0100
1X11
0111
100 2 ▶ return _epoch($last) <= (_epoch($since) // 0);
Condition50%1/2
_epoch($since) // 0
l!l
20
MC/DC0%0/1
_epoch($since) // 0
_epoch($since)
Truth table50%1/2
A
A= _epoch($since)
Aresult
11
00
101 }
102  
103 130 ▶sub serve {
Subroutine serve: called CC=4 SCAR=15.9
Pod documented
104 130 my ($self, $c, $rel) = @_;
105  
106 # Prefix
107 130 ▶ if (my $prefix = $self->prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefix = $self->prefix) 0 130 130
108 0 $rel = "/$rel";
109 0 ▶ return undef unless $rel =~ s/^\Q$prefix\E\///;
Branch: 1 branch
BranchTrueFalseTotal
unless $rel =~ s[^\Q$prefix\E/][]u 0 0 0
110 }
111  
112 130 ▶ return undef unless my $asset = $self->file($rel);
Branch: 1 branch
BranchTrueFalseTotal
unless my $asset = $self->file($rel) 111 19 130
113 19 $c->app->types->content_type($c, {file => $rel});
114 19 return !!$self->serve_asset($c, $asset);
115 }
116  
117 19 ▶sub serve_asset {
Subroutine serve_asset: called CC=10 SCAR=34.7
Pod documented
118 19 my ($self, $c, $asset) = @_;
119  
120 # Content-Type
121 19 ▶ $c->app->types->content_type($c, {file => $asset->path}) if $asset->is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $asset->is_file 16 3 19
122  
123 # Last-Modified and ETag
124 19 my $res = $c->res;
125 19 $res->code(200)->headers->accept_ranges('bytes');
126 19 my $mtime = $asset->mtime;
127 19 my $options = {etag => md5_sum($mtime), last_modified => $mtime};
128 19 ▶ return $res->code(304) if $self->is_fresh($c, $options);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_fresh($c, $options) 3 16 19
129  
130 # Range
131 16 ▶ return $res->content->asset($asset) unless my $range = $c->req->headers->range;
Branch: 1 branch
BranchTrueFalseTotal
unless my $range = $c->req->headers->range 16 0 16
132  
133 # Not satisfiable
134 0 ▶ return $res->code(416) unless my $size = $asset->size;
Branch: 1 branch
BranchTrueFalseTotal
unless my $size = $asset->size 0 0 0
135 0 ▶ return $res->code(416) unless $range =~ /^bytes=(\d+)?-(\d+)?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $range =~ /^bytes=(\d+)?-(\d+)?/u 0 0 0
136 0 ▶ my ($start, $end) = ($1 // 0, defined $2 && $2 < $size ? $2 : $size - 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 && $2 < $size ? : 0 0 0
Condition0%0/5
$1 // 0
l!l
00
defined $2 and $2 < $size
!ll&&!rl&&r
000
MC/DC0%0/1
$1 // 0
$1
MC/DC0%0/2
defined $2 and $2 < $size
defined $2 $2 < $size
Truth table0%0/2
A
A= $1
Aresult
11
00
Truth table0%0/3
A and B
A= defined $2B= $2 < $size
ABresult
0X0
100
111
137 0 ▶ return $res->code(416) if $start > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end 0 0 0
138  
139 # Satisfiable
140 0 $res->code(206)->headers->content_length($end - $start + 1)->content_range("bytes $start-$end/$size");
141 0 return $res->content->asset($asset->start_range($start)->end_range($end));
142 }
143  
144 30 ▶sub warmup {
Subroutine warmup: called CC=10 SCAR=23.1
Pod documented
145 30 my $self = shift;
146  
147 # DATA sections
148 30 my $index = $self->{index} = {};
149 30 for my $class (reverse @{$self->classes}) { $index->{$_} = $class for keys %{data_section $class} }
150  
151 # Static assets
152 30 my $assets = $self->{assets} = {};
153 30 my $asset_dir = $self->asset_dir;
154 30 for my $path (@{$self->paths}) {
155 45 my $asset_path = path($path, $asset_dir);
156 45 ▶ next unless -d $asset_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $asset_path 26 19 45
157  
158 19 for my $asset_file ($asset_path->list_tree({recursive => 1})->each) {
159 133 my $parts = $asset_file->to_rel($asset_path)->to_array;
160 133 my $filename = pop @$parts;
161 133 my $prefix = join '/', @$parts;
162  
163 133 ▶ next unless $filename =~ /^([^.]+)\.([^.]+)\.(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ /^([^.]+)\.([^.]+)\.(.+)$/u 19 114 133
164 114 my $checksum = $2;
165 114 ▶ my $short = $prefix eq '' ? "/$1.$3" : "/$prefix/$1.$3";
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '' ? : 38 76 114
166 114 my $long = '/' . join('/', @$parts, $filename);
167  
168 114 ▶ $assets->{$short} = $long if !exists($assets->{$short}) || $checksum eq 'development';
Branch: 1 branch
BranchTrueFalseTotal
if not exists $assets->{$short} or $checksum eq "development" 114 0 114
Condition66%2/3
not exists $assets->{$short} or $checksum eq "development"
l!l&&r!l&&!r
95190
MC/DC0%0/2
not exists $assets->{$short} or $checksum eq "development"
not exists $assets->{$short} $checksum eq "development"
Truth table66%2/3
not(A) or B
A= not exists $assets->{$short}B= $checksum eq "development"
ABresult
1X1
011
000
169 }
170 }
171 }
172  
173 4 ▶sub _epoch { Mojo::Date->new(shift)->epoch }
Subroutine _epoch: called CC=1 SCAR=0.0
174  
175 117 ▶sub _get_data_file {
Subroutine _get_data_file: called CC=5 SCAR=16.2
176 117 my ($self, $rel) = @_;
177  
178 # Protect files without extensions and templates with two extensions
179 117 ▶ return undef if $rel !~ /\.\w+$/ || $rel =~ /\.\w+\.\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if not $rel =~ /\.\w+$/u or $rel =~ /\.\w+\.\w+$/u 106 11 117
Condition66%2/3
not $rel =~ /\.\w+$/u or $rel =~ /\.\w+\.\w+$/u
l!l&&r!l&&!r
106011
MC/DC50%1/2
not $rel =~ /\.\w+$/u or $rel =~ /\.\w+\.\w+$/u
not $rel =~ /\.\w+$/u $rel =~ /\.\w+\.\w+$/u
Truth table66%2/3
not(A) or B
A= not $rel =~ /\.\w+$/uB= $rel =~ /\.\w+\.\w+$/u
ABresult
1X1
011
000
180  
181 # Find file
182 11 my @args = ($self->{index}{$rel}, $rel);
183 11 ▶ return undef unless defined(my $data = data_section(@args));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $data = data_section(@args)) 8 3 11
184 3 ▶ return Mojo::Asset::Memory->new->add_chunk(file_is_binary(@args) ? $data : encode 'UTF-8', $data);
Branch: 1 branch
BranchTrueFalseTotal
file_is_binary(@args) ? : 0 3 3
185 }
186  
187 191 ▶sub _get_file {
Subroutine _get_file: called CC=3 SCAR=11.0
188 191 my $path = shift;
189 20 ▶ no warnings 'newline';
Subroutine BEGIN: called CC=1 SCAR=0.0
190 191 ▶ return -f $path && -r _ ? Mojo::Asset::File->new(path => $path) : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $path && -r _ ? : 18 173 191
Condition66%2/3
-f $path and -r _
!ll&&!rl&&r
173018
MC/DC50%1/2
-f $path and -r _
-f $path -r _
Truth table66%2/3
A and B
A= -f $pathB= -r _
ABresult
0X0
100
111
191 }
192  
193 20 1;
194  
195 =encoding utf8
196  
197 =head1 NAME
198  
199 Mojolicious::Static - Serve static files
200  
201 =head1 SYNOPSIS
202  
203 use Mojolicious::Static;
204  
205 my $static = Mojolicious::Static->new;
206 push @{$static->classes}, 'MyApp::Controller::Foo';
207 push @{$static->paths}, '/home/sri/public';
208  
209 =head1 DESCRIPTION
210  
211 L<Mojolicious::Static> is a static file server with C<Range>, C<If-Modified-Since> and C<If-None-Match> support, based
212 on L<RFC 7232|https://tools.ietf.org/html/rfc7232> and L<RFC 7233|https://tools.ietf.org/html/rfc7233>.
213  
214 =head1 ATTRIBUTES
215  
216 L<Mojolicious::Static> implements the following attributes.
217  
218 =head2 asset_dir
219  
220 my $dir = $static->asset_dir;
221 $static = $static->asset_dir('assets');
222  
223 Subdirectory used for all static assets, defaults to C<assets>.
224  
225 =head2 classes
226  
227 my $classes = $static->classes;
228 $static = $static->classes(['main']);
229  
230 Classes to use for finding files in C<DATA> sections with L<Mojo::Loader>, first one has the highest precedence,
231 defaults to C<main>. Only files with exactly one extension will be used, like C<index.html>. Note that for files to be
232 detected, these classes need to have already been loaded and added before L</"warmup"> is called, which usually happens
233 automatically during application startup.
234  
235 # Add another class with static files in DATA section
236 push @{$static->classes}, 'Mojolicious::Plugin::Fun';
237  
238 # Add another class with static files in DATA section and higher precedence
239 unshift @{$static->classes}, 'Mojolicious::Plugin::MoreFun';
240  
241 =head2 extra
242  
243 my $extra = $static->extra;
244 $static = $static->extra({'foo/bar.txt' => '/home/sri/myapp/bar.txt'});
245  
246 Paths for extra files to be served from locations other than L</"paths">, such as the images used by the built-in
247 exception and not found pages. Note that extra files are only served if no better alternative could be found in
248 L</"paths"> and L</"classes">.
249  
250 # Remove built-in favicon
251 delete $static->extra->{'favicon.ico'};
252  
253 =head2 paths
254  
255 my $paths = $static->paths;
256 $static = $static->paths(['/home/sri/public']);
257  
258 Directories to serve static files from, first one has the highest precedence.
259  
260 # Add another "public" directory
261 push @{$static->paths}, '/home/sri/public';
262  
263 # Add another "public" directory with higher precedence
264 unshift @{$static->paths}, '/home/sri/themes/blue/public';
265  
266 =head2 prefix
267  
268 my $prefix = $static->prefix;
269 $static = $static->prefix('/static');
270  
271 Prefix to use for all static files, defaults to C<undef>. This can be very useful for production deployments where the
272 reverse proxy server should take over serving static files.
273  
274 =head1 METHODS
275  
276 L<Mojolicious::Static> inherits all methods from L<Mojo::Base> and implements the following new ones.
277  
278 =head2 asset_path
279  
280 my $path = $static->asset_path('/app.js');
281  
282 Get static asset path.
283  
284 =head2 dispatch
285  
286 my $bool = $static->dispatch(Mojolicious::Controller->new);
287  
288 Serve static file for L<Mojolicious::Controller> object.
289  
290 =head2 file
291  
292 my $asset = $static->file('images/logo.png');
293 my $asset = $static->file('../lib/MyApp.pm');
294  
295 Build L<Mojo::Asset::File> or L<Mojo::Asset::Memory> object for a file, relative to L</"paths"> or from L</"classes">,
296 or return C<undef> if it doesn't exist. Note that this method uses a relative path, but does not protect from
297 traversing to parent directories.
298  
299 my $content = $static->file('foo/bar.html')->slurp;
300  
301 =head2 file_path
302  
303 my $path = $static->file_path('/index.html');
304  
305 Get static file path with L</"prefix"> if it has been configured.
306  
307 =head2 is_fresh
308  
309 my $bool = $static->is_fresh(Mojolicious::Controller->new, {etag => 'abc'});
310 my $bool = $static->is_fresh(
311 Mojolicious::Controller->new, {etag => 'W/"def"'});
312  
313 Check freshness of request by comparing the C<If-None-Match> and C<If-Modified-Since> request headers to the C<ETag>
314 and C<Last-Modified> response headers.
315  
316 These options are currently available:
317  
318 =over 2
319  
320 =item etag
321  
322 etag => 'abc'
323 etag => 'W/"abc"'
324  
325 Add C<ETag> header before comparing.
326  
327 =item last_modified
328  
329 last_modified => $epoch
330  
331 Add C<Last-Modified> header before comparing.
332  
333 =back
334  
335 =head2 serve
336  
337 my $bool = $static->serve(Mojolicious::Controller->new, 'images/logo.png');
338 my $bool = $static->serve(Mojolicious::Controller->new, '../lib/MyApp.pm');
339  
340 Serve a specific file, relative to L</"paths"> or from L</"classes">. Note that this method uses a relative path, but
341 does not protect from traversing to parent directories.
342  
343 =head2 serve_asset
344  
345 $static->serve_asset(Mojolicious::Controller->new, Mojo::Asset::File->new);
346  
347 Serve a L<Mojo::Asset::File> or L<Mojo::Asset::Memory> object with C<Range>, C<If-Modified-Since> and C<If-None-Match>
348 support.
349  
350 =head2 warmup
351  
352 $static->warmup();
353  
354 Prepare static files from L</"classes"> and static assets for future use.
355  
356 =head1 SEE ALSO
357  
358 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
359  
360 =cut
« Mojolicious/Command/get.pm ↑ Summary Mojolicious/Plugin/DefaultHelpers.pm »