| 1 | package Dancer2::Handler::File; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: class for handling file content rendering | ||||||||||||||||||||||||||||||||||
| 3 | 2 | $Dancer2::Handler::File::VERSION = '2.3.0'; | |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use HTTP::Date; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Dancer2::Core::MIME; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Dancer2::Core::Types; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 2 | with qw< | |||||||||||||||||||||||||||||||||
| 12 | Dancer2::Core::Role::Handler | ||||||||||||||||||||||||||||||||||
| 13 | Dancer2::Core::Role::StandardResponses | ||||||||||||||||||||||||||||||||||
| 14 | Dancer2::Core::Role::Hookable | ||||||||||||||||||||||||||||||||||
| 15 | >; | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | sub hook_aliases { | ||||||||||||||||||||||||||||||||
|
Subroutine
hook_aliases: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||||||||
| 19 | 5 | before_file_render => 'handler.file.before_render', | |||||||||||||||||||||||||||||||||
| 20 | after_file_render => 'handler.file.after_render', | ||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub supported_hooks { values %{ shift->hook_aliases } } | ||||||||||||||||||||||||||||||||
|
Subroutine
supported_hooks: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | has mime => ( | ||||||||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 28 | isa => InstanceOf ['Dancer2::Core::MIME'], | ||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | default => sub { Dancer2::Core::MIME->new }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 30 | 2 | ); | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | has encoding => ( | ||||||||||||||||||||||||||||||||||
| 33 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | default => sub {'utf-8'}, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 35 | 2 | ); | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 2 | has public_dir => ( | |||||||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 39 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 40 | builder => '_build_public_dir', | ||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 2 | has _public_dir_path => ( | |||||||||||||||||||||||||||||||||
| 44 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 45 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 46 | builder => '_build_public_dir_path', | ||||||||||||||||||||||||||||||||||
| 47 | init_arg => undef, | ||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | has regexp => ( | ||||||||||||||||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | default => sub {'/**'}, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 53 | 2 | ); | |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _build_public_dir { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_public_dir: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 57 | return $self->app->config->{public_dir} | ||||||||||||||||||||||||||||||||||
| 58 | || $ENV{DANCER_PUBLIC} | ||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | || Path::Tiny::path( $self->app->location, 'public' )->stringify; | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->app->config->{'public_dir'} || $ENV{'DANCER_PUBLIC'} || Path::Tiny::path($self->app->location, 'public')->stringify
$self->app->config->{"public_dir"} or $ENV{"DANCER_PUBLIC"}
MC/DC0%0/3
$self->app->config->{'public_dir'} || $ENV{'DANCER_PUBLIC'} || Path::Tiny::path($self->app->location, 'public')->stringify
$self->app->config->{"public_dir"}
$ENV{"DANCER_PUBLIC"}
Path::Tiny::path($self->app->location, 'public')->stringify
Truth table0%0/4
A or B || C
A= $self->app->config->{"public_dir"}B= $ENV{"DANCER_PUBLIC"}C= Path::Tiny::path($self->app->location, 'public')->stringify
| |||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | sub _build_public_dir_path { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_public_dir_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 63 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 64 | 1 | return Path::Tiny::path( $self->public_dir ); | |||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 3 | ▶ | sub register { | ||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 68 | 3 | my ( $self, $app ) = @_; | |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | # don't register the handler if no valid public dir | ||||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | return if !$self->_public_dir_path->is_dir; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | $app->add_route( | ||||||||||||||||||||||||||||||||||
| 74 | method => $_, | ||||||||||||||||||||||||||||||||||
| 75 | regexp => $self->regexp, | ||||||||||||||||||||||||||||||||||
| 76 | code => $self->code( $app->prefix ), | ||||||||||||||||||||||||||||||||||
| 77 | 3 | ) for $self->methods; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | sub methods { ( 'head', 'get' ) } | ||||||||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 6 | ▶ | sub code { | ||||||||||||||||||||||||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 83 | 6 | my ( $self, $prefix ) = @_; | |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 9 | ▶ | sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=27.1
| |||||||||||||||||||||||||||||||||||
| 86 | 9 | my $app = shift; | |||||||||||||||||||||||||||||||||
| 87 | 9 | my $prefix = shift; | |||||||||||||||||||||||||||||||||
| 88 | 9 | my $path = $app->request->path_info; | |||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 9 | ▶ | if ( $path =~ /\0/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 1 | return $self->standard_response( $app, 400 ); | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 8 | ▶ | if ( $prefix && $prefix ne '/' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$prefix and $prefix ne "/"
MC/DC0%0/2
$prefix and $prefix ne "/"
$prefix
$prefix ne "/"
Truth table33%1/3
A and B
A= $prefixB= $prefix ne "/"
| |||||||||||||||||||||||||||||||||||
| 95 | 0 | $path =~ s/^\Q$prefix\E//; | |||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 8 | my $file_path = Path::Tiny::path( $self->_public_dir_path, $path ); | |||||||||||||||||||||||||||||||||
| 99 | 8 | my $file_path_str = $file_path->stringify; | |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 8 | ▶ | if ( !-f $file_path_str ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 102 | 1 | $app->response->has_passed(1); | |||||||||||||||||||||||||||||||||
| 103 | 1 | return; | |||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | # Path::Tiny does not collapse '../' segments, so the joined path | ||||||||||||||||||||||||||||||||||
| 107 | # above may still point outside public_dir even though it is a | ||||||||||||||||||||||||||||||||||
| 108 | # readable file. Resolve it and refuse anything that escapes, | ||||||||||||||||||||||||||||||||||
| 109 | # the same containment check send_file uses at | ||||||||||||||||||||||||||||||||||
| 110 | # Dancer2/Core/App.pm:1180-1182. This is only safe to call now: the | ||||||||||||||||||||||||||||||||||
| 111 | # -f check above guarantees the file (and so every directory in its | ||||||||||||||||||||||||||||||||||
| 112 | # path) exists, so realpath cannot die here. | ||||||||||||||||||||||||||||||||||
| 113 | 7 | $file_path = $file_path->realpath; | |||||||||||||||||||||||||||||||||
| 114 | 7 | $file_path_str = $file_path->stringify; | |||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | 7 | ▶ | if ( !$self->_public_dir_path->realpath->subsumes($file_path) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 117 | 4 | return $self->standard_response( $app, 403 ); | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | 3 | ▶ | if ( !-r $file_path_str ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | 1 | return $self->standard_response( $app, 403 ); | |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | # Now we are sure we can render the file... | ||||||||||||||||||||||||||||||||||
| 125 | 2 | $self->execute_hook( 'handler.file.before_render', $file_path_str ); | |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | # Read file content as bytes | ||||||||||||||||||||||||||||||||||
| 128 | 2 | my $content = $file_path->slurp_raw; | |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | # Assume m/^text/ mime types are correctly encoded | ||||||||||||||||||||||||||||||||||
| 131 | 2 | ▶ | my $content_type = $self->mime->for_file($file_path_str) || 'text/plain'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->mime->for_file($file_path_str) || 'text/plain'
MC/DC0%0/1
$self->mime->for_file($file_path_str) || 'text/plain'
$self->mime->for_file($file_path_str)
Truth table50%1/2
A
A= $self->mime->for_file($file_path_str)
| |||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | if ( $content_type =~ m!^text/! ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 133 | 2 | ▶ | $content_type .= "; charset=" . ( $self->encoding || "utf-8" ); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->encoding || 'utf-8'
MC/DC0%0/1
$self->encoding || 'utf-8'
$self->encoding
Truth table50%1/2
A
A= $self->encoding
| |||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 2 | my @stat = stat $file_path_str; | |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | $app->response->header('Content-Type') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | or $app->response->header( 'Content-Type', $content_type ); | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 2 | ▶ | $app->response->header('Content-Length') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | or $app->response->header( 'Content-Length', $stat[7] ); | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | 2 | ▶ | $app->response->header('Last-Modified') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 145 | or $app->response->header( | ||||||||||||||||||||||||||||||||||
| 146 | 'Last-Modified', | ||||||||||||||||||||||||||||||||||
| 147 | HTTP::Date::time2str( $stat[9] ) | ||||||||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | 2 | $app->response->content($content); | |||||||||||||||||||||||||||||||||
| 151 | 2 | $app->response->is_encoded(1); # bytes are already encoded | |||||||||||||||||||||||||||||||||
| 152 | 2 | $self->execute_hook( 'handler.file.after_render', $app->response ); | |||||||||||||||||||||||||||||||||
| 153 | 2 | ▶ | return ( $app->request->method eq 'GET' ) ? $content : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 154 | 6 | }; | |||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||||||||