| 1 | package Plack::App::File; | ||||||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 9 | ▶ | use parent qw/Plack::Component/; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 9 | ▶ | use File::Spec::Unix; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 9 | ▶ | use Cwd (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | 9 | ▶ | use Plack::Util; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | 9 | ▶ | use Plack::MIME; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | 9 | ▶ | use HTTP::Date; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | 9 | ▶ | use Plack::Util::Accessor qw( root file content_type encoding ); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||
| 13 | 118 | ▶ | sub should_handle { | ||||||||||||||||||||||
|
Subroutine
should_handle: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 14 | 118 | my($self, $file) = @_; | |||||||||||||||||||||||
| 15 | 118 | return -f $file; | |||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||
| 18 | 131 | ▶ | sub call { | ||||||||||||||||||||||
|
Subroutine
call: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||
| 19 | 131 | my $self = shift; | |||||||||||||||||||||||
| 20 | 131 | my $env = shift; | |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||
| 22 | 131 | ▶ | my($file, $path_info) = $self->file || $self->locate_file($env); | ||||||||||||||||||||||
|
Condition100%3/3
$self->file || $self->locate_file($env)
MC/DC100%2/2
$self->file || $self->locate_file($env)
$self->file
$self->locate_file($env)
Truth table100%3/3
A || B
A= $self->fileB= $self->locate_file($env)
| |||||||||||||||||||||||||
| 23 | 131 | ▶ | return $file if ref $file eq 'ARRAY'; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | 20 | ▶ | if ($path_info) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 26 | 0 | $env->{'plack.file.SCRIPT_NAME'} = $env->{SCRIPT_NAME} . $env->{PATH_INFO}; | |||||||||||||||||||||||
| 27 | 0 | $env->{'plack.file.SCRIPT_NAME'} =~ s/\Q$path_info\E$//; | |||||||||||||||||||||||
| 28 | 0 | $env->{'plack.file.PATH_INFO'} = $path_info; | |||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||
| 30 | 20 | $env->{'plack.file.SCRIPT_NAME'} = $env->{SCRIPT_NAME} . $env->{PATH_INFO}; | |||||||||||||||||||||||
| 31 | 20 | $env->{'plack.file.PATH_INFO'} = ''; | |||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||
| 34 | 20 | return $self->serve_path($env, $file); | |||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||
| 37 | 127 | ▶ | sub locate_file { | ||||||||||||||||||||||
|
Subroutine
locate_file: called CC=12 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||
| 38 | 127 | my($self, $env) = @_; | |||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | 127 | ▶ | my $path = $env->{PATH_INFO} || ''; | ||||||||||||||||||||||
|
Condition50%1/2
$env->{'PATH_INFO'} || ''
MC/DC0%0/1
$env->{'PATH_INFO'} || ''
$env->{'PATH_INFO'}
Truth table50%1/2
A
A= $env->{'PATH_INFO'}
| |||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||
| 42 | 127 | ▶ | if ($path =~ /\0/) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 43 | 1 | return $self->return_400; | |||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||
| 46 | 126 | ▶ | my $docroot = $self->root || "."; | ||||||||||||||||||||||
|
Condition50%1/2
$self->root || '.'
MC/DC0%0/1
$self->root || '.'
$self->root
Truth table50%1/2
A
A= $self->root
| |||||||||||||||||||||||||
| 47 | 126 | my @path = split /[\\\/]/, $path, -1; # -1 *MUST* be here to avoid security issues! | |||||||||||||||||||||||
| 48 | 126 | ▶ | if (@path) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 49 | 126 | ▶ | shift @path if $path[0] eq ''; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||
| 51 | 0 | @path = ('.'); | |||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||
| 54 | 126 | ▶ | if (grep /^\.{2,}$/, @path) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 55 | 5 | return $self->return_403; | |||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||
| 58 | 121 | my($file, @path_info); | |||||||||||||||||||||||
| 59 | 121 | ▶ | while (@path) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 60 | 121 | my $try = File::Spec::Unix->catfile($docroot, @path); | |||||||||||||||||||||||
| 61 | 121 | ▶ | if ($self->should_handle($try)) { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 62 | 16 | $file = $try; | |||||||||||||||||||||||
| 63 | 16 | last; | |||||||||||||||||||||||
| 64 | } elsif (!$self->allow_path_info) { | ||||||||||||||||||||||||
| 65 | 105 | last; | |||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||
| 67 | 0 | unshift @path_info, pop @path; | |||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | 121 | ▶ | if (!$file) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 71 | 105 | return $self->return_404; | |||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||
| 74 | 16 | ▶ | if (!-r $file) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 75 | 0 | return $self->return_403; | |||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||
| 78 | 16 | return $file, join("/", "", @path_info); | |||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | 105 | ▶ | sub allow_path_info { 0 } | ||||||||||||||||||||||
|
Subroutine
allow_path_info: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | 17 | ▶ | sub serve_path { | ||||||||||||||||||||||
|
Subroutine
serve_path: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||
| 84 | 17 | my($self, $env, $file) = @_; | |||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||
| 86 | 17 | ▶ | my $content_type = $self->content_type || Plack::MIME->mime_type($file) | ||||||||||||||||||||||
|
Condition100%5/5
$self->content_type || 'Plack::MIME'->mime_type($file) || 'text/plain'
$self->content_type or "Plack::MIME"->mime_type($file)
MC/DC100%2/2
$self->content_type || 'Plack::MIME'->mime_type($file) || 'text/plain'
$self->content_type
"Plack::MIME"->mime_type($file)
Truth table100%3/3
A or B
A= $self->content_typeB= "Plack::MIME"->mime_type($file)
| |||||||||||||||||||||||||
| 87 | || 'text/plain'; | ||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||
| 89 | 17 | ▶ | if ("CODE" eq ref $content_type) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 90 | 1 | $content_type = $content_type->($file); | |||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||
| 93 | 17 | ▶ | if ($content_type =~ m!^text/!) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 94 | 15 | ▶ | $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
| |||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | 17 | ▶ | open my $fh, "<:raw", $file | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 98 | or return $self->return_403; | ||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||
| 100 | 17 | my @stat = stat $fh; | |||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||
| 102 | 17 | Plack::Util::set_io_path($fh, Cwd::realpath($file)); | |||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||
| 104 | return [ | ||||||||||||||||||||||||
| 105 | 17 | 200, | |||||||||||||||||||||||
| 106 | [ | ||||||||||||||||||||||||
| 107 | 'Content-Type' => $content_type, | ||||||||||||||||||||||||
| 108 | 'Content-Length' => $stat[7], | ||||||||||||||||||||||||
| 109 | 'Last-Modified' => HTTP::Date::time2str( $stat[9] ) | ||||||||||||||||||||||||
| 110 | ], | ||||||||||||||||||||||||
| 111 | $fh, | ||||||||||||||||||||||||
| 112 | ]; | ||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||
| 115 | 5 | ▶ | sub return_403 { | ||||||||||||||||||||||
|
Subroutine
return_403: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 116 | 5 | my $self = shift; | |||||||||||||||||||||||
| 117 | 5 | return [403, ['Content-Type' => 'text/plain', 'Content-Length' => 9], ['forbidden']]; | |||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||
| 120 | 1 | ▶ | sub return_400 { | ||||||||||||||||||||||
|
Subroutine
return_400: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 121 | 1 | my $self = shift; | |||||||||||||||||||||||
| 122 | 1 | return [400, ['Content-Type' => 'text/plain', 'Content-Length' => 11], ['Bad Request']]; | |||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||
| 125 | # Hint: subclasses can override this to return undef to pass through 404 | ||||||||||||||||||||||||
| 126 | 105 | ▶ | sub return_404 { | ||||||||||||||||||||||
|
Subroutine
return_404: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 127 | 105 | my $self = shift; | |||||||||||||||||||||||
| 128 | 105 | return [404, ['Content-Type' => 'text/plain', 'Content-Length' => 9], ['not found']]; | |||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||
| 131 | 9 | 1; | |||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||