-
-
23 May 2022 15:17:22 UTC
- Distribution: Mojolicious
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Repository
- Issues
- Testers (980 / 6 / 30)
- Kwalitee
Bus factor: 6- 90.64% Coverage
- License: artistic_2
- Perl: v5.16.0
- Activity
24 month- Tools
- Download (827.39KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
NAME
Mojolicious::Static - Serve static files
SYNOPSIS
use Mojolicious::Static; my $static = Mojolicious::Static->new; push @{$static->classes}, 'MyApp::Controller::Foo'; push @{$static->paths}, '/home/sri/public';
DESCRIPTION
Mojolicious::Static is a static file server with
Range
,If-Modified-Since
andIf-None-Match
support, based on RFC 7232 and RFC 7233.ATTRIBUTES
Mojolicious::Static implements the following attributes.
classes
my $classes = $static->classes; $static = $static->classes(['main']);
Classes to use for finding files in
DATA
sections with Mojo::Loader, first one has the highest precedence, defaults tomain
. Only files with exactly one extension will be used, likeindex.html
. Note that for files to be detected, these classes need to have already been loaded and added before "warmup" is called, which usually happens automatically during application startup.# Add another class with static files in DATA section push @{$static->classes}, 'Mojolicious::Plugin::Fun'; # Add another class with static files in DATA section and higher precedence unshift @{$static->classes}, 'Mojolicious::Plugin::MoreFun';
extra
my $extra = $static->extra; $static = $static->extra({'foo/bar.txt' => '/home/sri/myapp/bar.txt'});
Paths for extra files to be served from locations other than "paths", such as the images used by the built-in exception and not found pages. Note that extra files are only served if no better alternative could be found in "paths" and "classes".
# Remove built-in favicon delete $static->extra->{'favicon.ico'};
paths
my $paths = $static->paths; $static = $static->paths(['/home/sri/public']);
Directories to serve static files from, first one has the highest precedence.
# Add another "public" directory push @{$static->paths}, '/home/sri/public'; # Add another "public" directory with higher precedence unshift @{$static->paths}, '/home/sri/themes/blue/public';
METHODS
Mojolicious::Static inherits all methods from Mojo::Base and implements the following new ones.
dispatch
my $bool = $static->dispatch(Mojolicious::Controller->new);
Serve static file for Mojolicious::Controller object.
file
my $asset = $static->file('images/logo.png'); my $asset = $static->file('../lib/MyApp.pm');
Build Mojo::Asset::File or Mojo::Asset::Memory object for a file, relative to "paths" or from "classes", or return
undef
if it doesn't exist. Note that this method uses a relative path, but does not protect from traversing to parent directories.my $content = $static->file('foo/bar.html')->slurp;
is_fresh
my $bool = $static->is_fresh(Mojolicious::Controller->new, {etag => 'abc'}); my $bool = $static->is_fresh( Mojolicious::Controller->new, {etag => 'W/"def"'});
Check freshness of request by comparing the
If-None-Match
andIf-Modified-Since
request headers to theETag
andLast-Modified
response headers.These options are currently available:
- etag
-
etag => 'abc' etag => 'W/"abc"'
Add
ETag
header before comparing. - last_modified
-
last_modified => $epoch
Add
Last-Modified
header before comparing.
serve
my $bool = $static->serve(Mojolicious::Controller->new, 'images/logo.png'); my $bool = $static->serve(Mojolicious::Controller->new, '../lib/MyApp.pm');
Serve a specific file, relative to "paths" or from "classes". Note that this method uses a relative path, but does not protect from traversing to parent directories.
serve_asset
$static->serve_asset(Mojolicious::Controller->new, Mojo::Asset::File->new);
Serve a Mojo::Asset::File or Mojo::Asset::Memory object with
Range
,If-Modified-Since
andIf-None-Match
support.warmup
$static->warmup;
Prepare static files from "classes" for future use.
SEE ALSO
Module Install Instructions
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm Mojolicious
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.