blib/lib/Plack/App/File.pm

filter: Statementstmt 92.6% 76 / 82 Branchbran 78.5% 22 / 28 Conditioncond 78.5% 11 / 14 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 17 / 17 Podpod 12.5% 1 / 8 totaltotal 83.9% 131 / 156 CC 21 SCAR 18.6
CC
21
Cov
88%
locate_file
13.1
serve_path
7.0
call
4.2
CRAP 6.4
« blib/lib/Plack/Middleware/HTTPExceptions.pm ↑ Summary blib/lib/Plack/Middleware/LogDispatch.pm »
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)
l!l&&r!l&&!r
411116
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)
ABresult
1X1
011
000
23 131 ▶ return $file if ref $file eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $file eq "ARRAY" 111 20 131
24  
25 20 ▶ if ($path_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_info) { } 0 20 20
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'} || ''
l!l
1270
MC/DC0%0/1
$env->{'PATH_INFO'} || ''
$env->{'PATH_INFO'}
Truth table50%1/2
A
A= $env->{'PATH_INFO'}
Aresult
11
00
41  
42 127 ▶ if ($path =~ /\0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\0/) 1 126 127
43 1 return $self->return_400;
44 }
45  
46 126 ▶ my $docroot = $self->root || ".";
Condition50%1/2
$self->root || '.'
l!l
1260
MC/DC0%0/1
$self->root || '.'
$self->root
Truth table50%1/2
A
A= $self->root
Aresult
11
00
47 126 my @path = split /[\\\/]/, $path, -1; # -1 *MUST* be here to avoid security issues!
48 126 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 126 0 126
49 126 ▶ shift @path if $path[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $path[0] eq "" 123 3 126
50 } else {
51 0 @path = ('.');
52 }
53  
54 126 ▶ if (grep /^\.{2,}$/, @path) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^\.{2,}$/, @path) 5 121 126
55 5 return $self->return_403;
56 }
57  
58 121 my($file, @path_info);
59 121 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 121 0 121
60 121 my $try = File::Spec::Unix->catfile($docroot, @path);
61 121 ▶ if ($self->should_handle($try)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->should_handle($try)) { } 16 105 121
elsif (not $self->allow_path_info) { } 105 0 105
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
BranchTrueFalseTotal
unless ($file) 105 16 121
71 105 return $self->return_404;
72 }
73  
74 16 ▶ if (!-r $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 16 16
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'
l!l
152
$self->content_type or "Plack::MIME"->mime_type($file)
l!l&&r!l&&!r
3122
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)
ABresult
1X1
011
000
87 || 'text/plain';
88  
89 17 ▶ if ("CODE" eq ref $content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $content_type) 1 16 17
90 1 $content_type = $content_type->($file);
91 }
92  
93 17 ▶ if ($content_type =~ m!^text/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type =~ m[^text/]) 15 2 17
94 15 ▶ $content_type .= "; charset=" . ($self->encoding || "utf-8");
Condition50%1/2
$self->encoding || 'utf-8'
l!l
015
MC/DC0%0/1
$self->encoding || 'utf-8'
$self->encoding
Truth table50%1/2
A
A= $self->encoding
Aresult
11
00
95 }
96  
97 17 ▶ open my $fh, "<:raw", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return $self->return_403 17 0 17
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__
« blib/lib/Plack/Middleware/HTTPExceptions.pm ↑ Summary blib/lib/Plack/Middleware/LogDispatch.pm »