PathInfo/Static.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 77.7% 14 / 18 Conditioncond 56.6% 17 / 30 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 76.1% 96 / 126 CC 22 SCAR 22.0
CC
22
Cov
82%
match
14.4
new
8.8
_type_uri
4.0
CRAP 9.0
« PathInfo.pm ↑ Summary
1 package Router::PathInfo::Static;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 B<Router::PathInfo::Static> - static routing
8  
9 =head1 DESCRIPTION
10  
11 Class to describe the routing of statics.
12 Allows us to describe the statics as follows:
13  
14 - Specify the starting segment of the URI
15  
16 - Specify a directory on disk, which will host the search for static
17  
18 Statics is divided into two parts:
19  
20 - C<allready> - already exists (the "classic") static
21  
22 - C<on_demand> - created on demand
23  
24 Case C<allready> it's different css, js, images. C<on_demand> it's archives and another.
25 If the file to C<on_demand> not found, C<match> return undef - a signal that makes sense to continue search of L<Router::PathInfo::Controller> routing.
26  
27 If successful, returns hashref:
28  
29 {
30 type => 'static',
31 mime_type => $mime_type,
32 file_name => '/path/to/found.static',
33 };
34
35 This ensures that the file exists, has size, and is readable.
36  
37  
38 If static is not found (for C<allready>) an error is returned:
39  
40 {
41 type => 'error',
42 code => 404,
43 desc => sprintf('not found static for PATH_INFO = %s', $env->{PATH_INFO})
44 }
45  
46 If PATH_INFO contains illegal characters (such as C</../> or C</.file>)an error is returned:
47  
48 {
49 type => 'error',
50 code => 403,
51 desc => sprintf('forbidden for PATH_INFO = %s', $env->{PATH_INFO})
52 }
53  
54 Return C<undef> means that it makes sense to continue search of L<Router::PathInfo::Controller> routing.
55  
56 =head1 SYNOPSIS
57
58 my $s = Router::PathInfo::Static->new(
59 # describe simple static
60 allready => {
61 path => $allready_path,
62 first_uri_segment => 'static'
63 },
64 # describe on demand created static
65 on_demand => {
66 path => $on_demand_path,
67 first_uri_segment => 'archives',
68 }
69 );
70
71 my $env = {PATH_INFO => '/static/some.jpg'};
72 my @segment = split '/', $env->{PATH_INFO}, -1;
73 shift @segment;
74 $env->{'psgix.tmp.RouterPathInfo'} = {
75 segments => [@segment],
76 depth => scalar @segment
77 };
78  
79 my $res = $s->match($env);
80
81 # $res = {
82 # type => 'static',
83 # file => $path_to_some_jpg,
84 # mime => 'image/jpeg'
85 # }
86  
87 =head1 METHODS
88  
89 =cut
90  
91 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 2 ▶use File::MimeInfo::Magic qw(mimetype);
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 =head2 new(allready => {path => $dir, first_uri_segment => $uri_segment}, on_demand => {...})
97  
98 The constructor accepts the description of the statics (allready) and/or static generated on demand (on_demand).
99 Each description is a hashref with the keys C<'path'> (directory path)
100 and C<'first_uri_segment'> (the first segment of a PATH_INFO, which defines namespace for designated purposes).
101  
102 All arguments are optional. If no arguments are given, the object is not created.
103  
104 =cut
105 2 ▶sub new {
Subroutine new: called CC=7 SCAR=21.8
Pod documented
106 2 my $class = shift;
107 2 my %param = @_;
108
109 2 my $hash = {};
110
111 2 for (qw(allready on_demand)) {
112 4 my $token = delete $param{$_};
113 4 ▶ if (ref $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token) 4 0 4
114 4 ▶ if (-e $token->{path} and -d _ and $token->{first_uri_segment}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $token->{'path'} and -d _ and $token->{'first_uri_segment'}) { } 4 0 4
Condition33%2/6
-e $token->{"path"} and -d _ and $token->{"first_uri_segment"}
!ll&&!rl&&r
004
-e $token->{"path"} and -d _
!ll&&!rl&&r
004
MC/DC0%0/3
-e $token->{"path"} and -d _ and $token->{"first_uri_segment"}
-e $token->{"path"} -d _ $token->{"first_uri_segment"}
Truth table25%1/4
A and B and C
A= -e $token->{"path"}B= -d _C= $token->{"first_uri_segment"}
ABCresult
0XX0
10X0
1100
1111
115 4 $hash->{$_.'_path'} = $token->{path};
116 4 $hash->{$_.'_uri_segment'} = $token->{first_uri_segment};
117 4 $hash->{$_} = 1;
118 } else {
119 0 $hash->{$_} = 0;
120 }
121 }
122 }
123  
124 2 ▶ return keys %$hash ? bless($hash, $class) : undef;
Branch: 1 branch
BranchTrueFalseTotal
keys %$hash ? : 2 0 2
125 }
126  
127 12 ▶sub _type_uri {
Subroutine _type_uri: called CC=4 SCAR=13.9
128 12 my $self = shift;
129 12 my $first_segment = shift;
130
131 12 for (qw(allready on_demand)) {
132 18 ▶ return $_ if ($self->{$_} and $first_segment eq $self->{$_.'_uri_segment'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} and $first_segment eq $$self{$_ . '_uri_segment'} 10 8 18
Condition66%2/3
$self->{$_} and $first_segment eq $$self{$_ . '_uri_segment'}
!ll&&!rl&&r
0810
MC/DC50%1/2
$self->{$_} and $first_segment eq $$self{$_ . '_uri_segment'}
$self->{$_} $first_segment eq $$self{$_ . '_uri_segment'}
Truth table66%2/3
A and B
A= $self->{$_}B= $first_segment eq $$self{$_ . '_uri_segment'}
ABresult
0X0
100
111
133 }
134
135 2 return;
136 }
137  
138 =head2 match({'psgix.tmp.RouterPathInfo' => {...}})
139  
140 Objects method.
141 Receives a uri and return:
142  
143 For C<on_demand> created static, return undef if file not found.
144  
145 =cut
146 12 ▶sub match {
Subroutine match: called CC=13 SCAR=26.6
Pod documented
147 12 my $self = shift;
148 12 my $env = shift;
149
150 12 my @segment = @{$env->{'psgix.tmp.RouterPathInfo'}->{segments}};
151  
152 12 my $serch_file = pop @segment;
153 12 ▶ return unless ($serch_file and @segment);
Branch: 1 branch
BranchTrueFalseTotal
unless $serch_file and @segment 0 12 12
Condition33%1/3
$serch_file and @segment
!ll&&!rl&&r
0012
MC/DC0%0/2
$serch_file and @segment
$serch_file @segment
Truth table33%1/3
A and B
A= $serch_fileB= @segment
ABresult
0X0
100
111
154
155 # проверим первый сегмент uri на принадлежность к статике
156 12 my $type = $self->_type_uri(shift @segment);
157 12 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 2 10 12
158  
159 # среди прочего небольшая защита для никсойдов, дабы не отдать секьюрные файлы
160 return {
161 type => 'error',
162 code => 403,
163 desc => sprintf('forbidden for PATH_INFO = %s', $env->{PATH_INFO})
164 10 ▶ } if ($serch_file =~ /^\./ or $serch_file =~ /~/ or grep {$_ =~ /^\./ or $_ =~ /~/} @segment);
Branch: 1 branch
BranchTrueFalseTotal
if $serch_file =~ /^\./ or $serch_file =~ /~/ or grep {$_ =~ /^\./ or $_ =~ /~/;} @segment 3 7 10
Condition77%7/9
$serch_file =~ /^\./ or $serch_file =~ /~/ or grep {$_ =~ /^\./ or $_ =~ /~/;} @segment
l!l&&r!l&&!r
127
$serch_file =~ /^\./ or $serch_file =~ /~/
l!l&&r!l&&!r
109
$_ =~ /^\./ || $_ =~ /~/
l!l&&r!l&&!r
205
MC/DC66%2/3
$serch_file =~ /^\./ or $serch_file =~ /~/ or grep {$_ =~ /^\./ or $_ =~ /~/;} @segment
$serch_file =~ /^\./ $serch_file =~ /~/ grep {$_ =~ /^\./ or $_ =~ /~/;} @segment
MC/DC50%1/2
$_ =~ /^\./ || $_ =~ /~/
$_ =~ /^\./ $_ =~ /~/
Truth table75%3/4
A or B or C
A= $serch_file =~ /^\./B= $serch_file =~ /~/C= grep {$_ =~ /^\./ or $_ =~ /~/;} @segment
ABCresult
1XX1
01X1
0011
0000
Truth table66%2/3
A || B
A= $_ =~ /^\./B= $_ =~ /~/
ABresult
1X1
011
000
165  
166 7 $serch_file = File::Spec->catfile($self->{$type.'_path'}, @segment, $serch_file);
167 7 ▶ if (-f $serch_file and -s _ and -r _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $serch_file and -s _ and -r _) { } 3 4 7
Condition66%4/6
-f $serch_file and -s _ and -r _
!ll&&!rl&&r
403
-f $serch_file and -s _
!ll&&!rl&&r
403
MC/DC33%1/3
-f $serch_file and -s _ and -r _
-f $serch_file -s _ -r _
Truth table50%2/4
A and B and C
A= -f $serch_fileB= -s _C= -r _
ABCresult
0XX0
10X0
1100
1111
168 return {
169 3 ▶ type => 'static',
Condition33%1/3
'Plack::MIME'->mime_type($serch_file) || mimetype($serch_file)
l!l&&r!l&&!r
300
MC/DC0%0/2
'Plack::MIME'->mime_type($serch_file) || mimetype($serch_file)
'Plack::MIME'->mime_type($serch_file) mimetype($serch_file)
Truth table33%1/3
A || B
A= 'Plack::MIME'->mime_type($serch_file)B= mimetype($serch_file)
ABresult
1X1
011
000
170 file => $serch_file,
171 mime => Plack::MIME->mime_type($serch_file) || mimetype($serch_file)
172 }
173 } else {
174 return $type eq 'allready' ?
175 {
176 type => 'error',
177 code => 404,
178 desc => sprintf('not found static for PATH_INFO = %s', $env->{PATH_INFO})
179 4 ▶ } :
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'allready' ? : 1 3 4
180 undef;
181 }
182 }
183  
184 =head1 DEPENDENCIES
185  
186 L<Plack::MIME>, L<File::MimeInfo::Magic>
187  
188 =head1 SEE ALSO
189  
190 L<Router::PathInfo>, L<Router::PathInfo::Controller>
191  
192 =head1 AUTHOR
193  
194 mr.Rico <catamoose at yandex.ru>
195  
196 =cut
197 2 1;
198 __END__
« PathInfo.pm ↑ Summary