| 1 | package Catalyst::Plugin::ServeFile; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moo::Role; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 1 | ▶ | use Plack::Util (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 1 | ▶ | use Plack::MIME (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 1 | ▶ | use HTTP::Date (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Spec (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | 1 | ▶ | use Cwd (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.004'; | |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||
| 12 | # We break this out since its the first step in making this plugin 'pluggable' | ||||||||||||||||||||||||
| 13 | # in terms of where the static assets are found. Maybe we might have a | ||||||||||||||||||||||||
| 14 | # $get_asset_from_database someday | ||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||
| 16 | 7 | ▶ | my $get_asset_from_filesystem = sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=24.1
| |||||||||||||||||||||||||
| 17 | 7 | my ($c, $full_path) = @_; | |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||
| 19 | # Does it exist? | ||||||||||||||||||||||||
| 20 | 7 | ▶ | unless(-f $full_path) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 21 | 2 | ▶ | $c->log->debug("Static File at '$full_path' not found!") if $c->debug; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 22 | 2 | return; | |||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||
| 24 | 5 | ▶ | open(my $fh, '<', $full_path) || do { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 25 | 0 | ▶ | $c->log->debug("Static file at '$full_path' cannot be open'd for read with error: $@") | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 26 | if $c->debug; | ||||||||||||||||||||||||
| 27 | 0 | return; | |||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||
| 30 | 5 | my @stat = stat($full_path); | |||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||
| 32 | 5 | ▶ | unless(@stat) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 33 | 0 | ▶ | $c->log->debug("Static file at '$full_path' cannot be stat'd with error: $@") | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 34 | if $c->debug; | ||||||||||||||||||||||||
| 35 | 0 | return; | |||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | 5 | Plack::Util::set_io_path($fh, Cwd::realpath($full_path)); # Support Xsendfile | |||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | 5 | return ($fh, @stat); | |||||||||||||||||||||||
| 41 | 1 | }; | |||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||
| 43 | 7 | ▶ | sub serve_file { | ||||||||||||||||||||||
|
Subroutine
serve_file: called CC=15 SCAR=30.6
| |||||||||||||||||||||||||
| 44 | 7 | my $c = shift; | |||||||||||||||||||||||
| 45 | 7 | ▶ | my $options = ref($_[-1]) ? pop @_ : +{}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 46 | 7 | ▶ | my $config = $c->config->{'Plugin::ServeFile'} || +{}; | ||||||||||||||||||||||
|
Condition50%1/2
$c->config->{'Plugin::ServeFile'} || {}
MC/DC0%0/1
$c->config->{'Plugin::ServeFile'} || {}
$c->config->{'Plugin::ServeFile'}
Truth table50%1/2
A
A= $c->config->{'Plugin::ServeFile'}
| |||||||||||||||||||||||||
| 47 | 7 | my %settings = (%$config, %$options); | |||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||
| 49 | 7 | ▶ | $c->log->abort(1) unless $settings{show_log}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||
| 51 | 7 | ▶ | my $root = $settings{root} || $c->config->{root}; | ||||||||||||||||||||||
|
Condition66%2/3
$settings{'root'} || $c->config->{'root'}
MC/DC0%0/2
$settings{'root'} || $c->config->{'root'}
$settings{'root'}
$c->config->{'root'}
Truth table66%2/3
A || B
A= $settings{'root'}B= $c->config->{'root'}
| |||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||
| 53 | my $file_proto = File::Spec->catdir( | ||||||||||||||||||||||||
| 54 | File::Spec->no_upwards( | ||||||||||||||||||||||||
| 55 | 7 | map { File::Spec->splitdir($_) } @_ )); | |||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||
| 57 | 7 | my $full_path = File::Spec->catfile($root, $file_proto); | |||||||||||||||||||||||
| 58 | 7 | ▶ | my $content_type = Plack::MIME->mime_type($full_path) || 'application/octet'; | ||||||||||||||||||||||
|
Condition50%1/2
'Plack::MIME'->mime_type($full_path) || 'application/octet'
MC/DC0%0/1
'Plack::MIME'->mime_type($full_path) || 'application/octet'
'Plack::MIME'->mime_type($full_path)
Truth table50%1/2
A
A= 'Plack::MIME'->mime_type($full_path)
| |||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||
| 60 | 7 | ▶ | if(my $allowed_content_types = $settings{allowed_content_types}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 61 | 0 | ▶ | unless(scalar( grep { lc($content_type) eq lc($_) } @$allowed_content_types)) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 62 | 0 | ▶ | $c->log->debug("Static file has disallowed content-type of '$content_type'") if $c->debug; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | 7 | my ($fh, @stat) = $c->$get_asset_from_filesystem($full_path); | |||||||||||||||||||||||
| 68 | 7 | ▶ | return unless $fh; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | 5 | ▶ | if ($content_type =~ m!^text/!) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 71 | 5 | ▶ | my $encoding = $settings{encoding} || "utf-8"; | ||||||||||||||||||||||
|
Condition50%1/2
$settings{'encoding'} || 'utf-8'
MC/DC0%0/1
$settings{'encoding'} || 'utf-8'
$settings{'encoding'}
Truth table50%1/2
A
A= $settings{'encoding'}
| |||||||||||||||||||||||||
| 72 | 5 | $content_type .= "; charset=$encoding"; | |||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||
| 75 | 5 | ▶ | $c->log->debug("Serving Static File: $full_path") if $c->debug; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||
| 77 | 5 | ▶ | my $status = $settings{status} || $settings{code} || 200; | ||||||||||||||||||||||
|
Condition80%4/5
$settings{'status'} || $settings{'code'} || 200
$settings{"status"} or $settings{"code"}
MC/DC50%1/2
$settings{'status'} || $settings{'code'} || 200
$settings{"status"}
$settings{"code"}
Truth table66%2/3
A or B
A= $settings{"status"}B= $settings{"code"}
| |||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | 5 | $c->res->status($status); | |||||||||||||||||||||||
| 80 | 5 | $c->res->headers->header( | |||||||||||||||||||||||
| 81 | 'Content-Type' => $content_type, | ||||||||||||||||||||||||
| 82 | 'Content-Length' => $stat[7], | ||||||||||||||||||||||||
| 83 | 'Last-Modified' => HTTP::Date::time2str( $stat[9] ), | ||||||||||||||||||||||||
| 84 | 'Cache-control' => 'public'); | ||||||||||||||||||||||||
| 85 | 5 | $c->res->body($fh); | |||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||
| 87 | 5 | return $fh; | |||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | 1 | 1; | |||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||
| 92 | =head1 NAME | ||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||
| 94 | Catalyst::Plugin::ServeFile - A less opinionated, minimal featured way to serve static files. | ||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||
| 98 | package MyApp; | ||||||||||||||||||||||||
| 99 | use Catalyst 'ServeFile'; | ||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | MyApp->setup; | ||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||
| 103 | package MyApp::Controller::Root; | ||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||
| 105 | use Moose; | ||||||||||||||||||||||||
| 106 | use MooseX::MethodAttributes; | ||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||
| 108 | extends 'Catalyst::Controller'; | ||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||
| 110 | # Serves => https://localhost/license | ||||||||||||||||||||||||
| 111 | sub license :Path(license) Args(0) { | ||||||||||||||||||||||||
| 112 | my ($self, $c) = @_; | ||||||||||||||||||||||||
| 113 | $c->serve_file("license.txt"); | ||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||
| 116 | # Servers => https://localhost/static/... | ||||||||||||||||||||||||
| 117 | sub static :Path(static) Args { | ||||||||||||||||||||||||
| 118 | my ($self, $c, @args) = @_; | ||||||||||||||||||||||||
| 119 | $c->serve_file('static',@args) || do { | ||||||||||||||||||||||||
| 120 | $c->res->status(404); | ||||||||||||||||||||||||
| 121 | $c->res->body('Not Found!'); | ||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||
| 127 | L<Catalyst::Plugin::Static::Simple> is venerable but I find it has too many default | ||||||||||||||||||||||||
| 128 | opinions. I generally only use it for the simple job of when I have a single static file | ||||||||||||||||||||||||
| 129 | or so that lives behind authentication that I want to serve. For that simple job | ||||||||||||||||||||||||
| 130 | L<Catalyst::Plugin::Static::Simple> does provide a method 'serve_static_file', but there's | ||||||||||||||||||||||||
| 131 | two problems with it. First, the plugin out of the box will attempt to serve all files | ||||||||||||||||||||||||
| 132 | requested at the '/static/...' path. If you don't want that its configuration effort. | ||||||||||||||||||||||||
| 133 | Also, it doesn't currently support L<Plack::Middleware::XSendfile> (Although I want to | ||||||||||||||||||||||||
| 134 | point out adding such support would be trivial, and I would be happy to help if needed). | ||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||
| 136 | Even when I want the automatic serving of files under '/static' I find the old plugin | ||||||||||||||||||||||||
| 137 | has some opinions that don't work with my expectations (for example it by default doesn't | ||||||||||||||||||||||||
| 138 | serve *.html files). These assumptions probably made sense in 2006 but I prefer something | ||||||||||||||||||||||||
| 139 | with less default opinions. So this is a plugin that just does a simple one thing. It | ||||||||||||||||||||||||
| 140 | gives you a method 'serve_file' which tries to safely serve a static file located in | ||||||||||||||||||||||||
| 141 | '$c->config->{root}', with support for L<Plack::Middleware::XSendfile>. It does basic | ||||||||||||||||||||||||
| 142 | sanity / safety checking such as not allowing you to have a path with '..' for example. | ||||||||||||||||||||||||
| 143 | And that's it. It does automatically serve up all files under 'static', or anything. If | ||||||||||||||||||||||||
| 144 | you want that, use the old plugin, or write a trivial action that does it (example below). | ||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||
| 146 | =head1 METHODS | ||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||
| 148 | This plugin adds the following methods to your L<Catalyst> application. | ||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||
| 150 | =head2 serve_file (@path_parts, ?\%options?) | ||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||
| 152 | Will serve a static file using '$c->config->{root}' as the path prefix. For example | ||||||||||||||||||||||||
| 153 | if you have a file '$c->config->{root}/static/license/html' you can serve it with | ||||||||||||||||||||||||
| 154 | $c->serve_file('static','license.html'). | ||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||
| 156 | This method will return true (the $fh actually) if it is successful in locating and serving | ||||||||||||||||||||||||
| 157 | a file. False otherwise. It doesn't automatically set any 'not found' response, you need to | ||||||||||||||||||||||||
| 158 | handle that yourself. | ||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||
| 160 | If the last argument is a HashRef, we will use it as an overlay on any configuration options. | ||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||
| 162 | See the L<\SYNOPSIS> for a longer example. | ||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||
| 164 | =head1 CONFIGURATION | ||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||
| 166 | This plugin supports the following configuration. You may set configuration globally | ||||||||||||||||||||||||
| 167 | via the 'Plugin::ServeFile' configuration key, and you may set/override when making | ||||||||||||||||||||||||
| 168 | the request. For example: | ||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||
| 170 | package MyApp; | ||||||||||||||||||||||||
| 171 | use Catalyst 'ServeFile'; | ||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||
| 173 | myApp->config( | ||||||||||||||||||||||||
| 174 | 'Plugin::ServeFile' => { | ||||||||||||||||||||||||
| 175 | show_log => 1, | ||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||
| 178 | MyApp->setup; | ||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||
| 180 | package MyApp::Controller::Root; | ||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||
| 182 | use Moose; | ||||||||||||||||||||||||
| 183 | use MooseX::MethodAttributes; | ||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||
| 185 | extends 'Catalyst::Controller'; | ||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||
| 187 | sub license :Path(license) Args(0) { | ||||||||||||||||||||||||
| 188 | my ($self, $c) = @_; | ||||||||||||||||||||||||
| 189 | $c->serve_file("license.txt", +{ show_log=>1}); | ||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||
| 192 | =head2 root | ||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||
| 194 | The default root directory we look in to find files is defined by $c->config->{root}. | ||||||||||||||||||||||||
| 195 | That's a nice and safe place for static assets. However, you might have a need to look | ||||||||||||||||||||||||
| 196 | for static assets elsewhere. For example your application may generate images and place | ||||||||||||||||||||||||
| 197 | the in /var/www/images (you might not wish to put them in $APPHOME/static because that | ||||||||||||||||||||||||
| 198 | directory is under source control and you want a clean separation between files in git | ||||||||||||||||||||||||
| 199 | and files that are generated by the application during normal usage). In that case you | ||||||||||||||||||||||||
| 200 | can set whatever root directory you want, just take care what you are exposing to the | ||||||||||||||||||||||||
| 201 | world! | ||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||
| 203 | =head2 show_log | ||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||
| 205 | By default we supress detailed logging of the request. This is the same behavior as | ||||||||||||||||||||||||
| 206 | L<Catalyst::Plugin::Static::Simple>. If you want to see those logs, you can enable it | ||||||||||||||||||||||||
| 207 | by setting this to true. | ||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||
| 209 | =head2 allowed_content_types | ||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||
| 211 | By default we allow you to serve any file. This may be dangerous if you are building | ||||||||||||||||||||||||
| 212 | your path arguments dynamically from uncontrolled sources. In that case you can set | ||||||||||||||||||||||||
| 213 | this to an arrayref of allowed mime types ('text/html', 'application/javascript', etc.). | ||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||
| 215 | =head2 status | ||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||
| 217 | =head2 code | ||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||
| 219 | Allows you to control the HTTP status code returned by the response. Probably more | ||||||||||||||||||||||||
| 220 | useful in the controller rather than in config: | ||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||
| 222 | $c->serve_file(@args) || $c->serve_file('not_found', {code=>404}); | ||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||
| 224 | Both 'status' and 'code' are allowed. | ||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||
| 228 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||
| 230 | =head1 SEE ALSO | ||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||
| 232 | L<Catalyst> | ||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||
| 234 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||
| 236 | Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||
| 238 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||
| 239 | the same terms as Perl itself. | ||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||