blib/lib/Limper/SendFile.pm

filter: Statementstmt 34.7% 25 / 72 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 4 totaltotal 20.0% 30 / 150 CC 25 SCAR 45.0
CC
25
Cov
20%
__ANON__
156.0
send_file
110.0
parse_date
20.0
CRAP 90.3
↑ Summary
1 package Limper::SendFile;
2 2 $Limper::SendFile::VERSION = '0.003';
3 2 ▶use base 'Limper';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Limper;
9 2 $Limper::VERSION = '0.003';
10 2 ▶use Time::Local 'timegm';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 push @Limper::EXPORT, qw/public send_file/;
13 2 push @Limper::EXPORT_OK, qw/mime_types parse_date/;
14  
15 2 my %mime_types = map { chomp; split /\t/; } (<DATA>);
16  
17 0 ▶sub mime_types { \%mime_types }
Subroutine mime_types: not called CC=1 SCAR=6.9
Pod undocumented
18  
19 2 my $public = './public/';
20  
21 sub public {
22 0 ▶ if (defined wantarray) { $public } else { ($public) = @_ }
Subroutine public: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 0 0
23 }
24  
25 # parse whatever crappy date a client might give
26 2 my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
27 sub parse_date {
28 0 ▶ my ($d, $m, $y, $h, $n, $s) = $_[0] =~ qr/^(?:\w+), (\d\d)[ -](\w+)[ -](\d\d(?:\d\d)?) (\d\d):(\d\d):(\d\d) GMT$/;
Subroutine parse_date: not called CC=4 SCAR=30.0
Pod undocumented
29 0 ▶ ($m, $d, $h, $n, $s, $y) = $_[0] =~ qr/^(?:\w+) (\w+) ([ \d]\d) (\d\d):(\d\d):(\d\d) (\d{4})$/ unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 0 0
30 0 ▶ return 0 unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 0 0
31 0 ▶ timegm( $s, $n, $h, $d, (grep { $months[$_] eq $m } 0..$#months)[0], $y + (length $y == 2 ? 1900 : 0) );
Branch: 1 branch
BranchTrueFalseTotal
length $y == 2 ? : 0 0 0
32 }
33  
34 # support If-Modified-Since and If-Unmodified-Since
35 hook after => sub {
36 0 ▶ my ($request, $response) = @_;
Subroutine __ANON__: not called CC=12 SCAR=50.5
37 0 ▶ if ($request->{method} // '' eq 'GET' and substr($response->{status} // 200, 0, 1) == 2 and
Branch: 1 branch
BranchTrueFalseTotal
if () 0 0 0
Condition0%0/10
and my($lm) = grep({lc $_ eq "last-modified";} @{$$response{"headers"};})
!ll&&!rl&&r
000
and substr($response->{"status"} // 200, 0, 1) == 2
!ll&&!rl&&r
000
$request->{"method"} //
l!l
00
$response->{"status"} // 200
l!l
00
MC/DC0%0/3
and my($lm) = grep({lc $_ eq "last-modified";} @{$$response{"headers"};})
$request->{"method"} substr($response->{"status"} // 200, 0, 1) == 2 my($lm) = grep({lc $_ eq "last-modified";} @{$$response{"headers"};})
MC/DC0%0/1
$response->{"status"} // 200
$response->{"status"}
Truth table0%0/4
A and B and C
A= $request->{"method"}B= substr($response->{"status"} // 200, 0, 1) == 2C= my($lm) = grep({lc $_ eq "last-modified";} @{$$response{"headers"};})
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $response->{"status"}
Aresult
11
00
38 0 my ($lm) = grep { lc $_ eq 'last-modified' } @{$response->{headers}}) {
39 0 for my $since (grep { /if-(?:un)?modified-since/ } keys %{$request->{hheaders}}) {
40 0 ▶ next if $since eq 'if-modified-since' and ($response->{status} // 200) != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $since eq "if-modified-since" and ($response->{"status"} // 200) != 200 0 0 0
Condition0%0/5
$since eq "if-modified-since" and ($response->{"status"} // 200) != 200
!ll&&!rl&&r
000
$response->{"status"} // 200
l!l
00
MC/DC0%0/2
$since eq "if-modified-since" and ($response->{"status"} // 200) != 200
$since eq "if-modified-since" $response->{"status"}
Truth table0%0/3
A and B
A= $since eq "if-modified-since"B= $response->{"status"}
ABresult
0X0
100
111
41 0 ▶ if (parse_date($request->{hheaders}{$since}) >= parse_date({@{$response->{headers}}}->{$lm})) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_date($request->{"hheaders"}{$since}) >= parse_date({@{$response->{"headers"};}}->{$lm})) 0 0 0
42 0 $response->{body} = '';
43 0 ▶ $response->{status} = $since eq 'if-modified-since' ? 304 : 412;
Branch: 1 branch
BranchTrueFalseTotal
$since eq 'if-modified-since' ? : 0 0 0
44 }
45 }
46 }
47 2 };
48  
49 sub send_file {
50 0 ▶ my ($file) = @_ || request->{uri};
Subroutine send_file: not called CC=10 SCAR=47.0
Pod undocumented
Condition0%0/3
@_ || request()->{'uri'}
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ || request()->{'uri'}
@_ request()->{'uri'}
Truth table0%0/3
A || B
A= @_B= request()->{'uri'}
ABresult
1X1
011
000
51  
52 0 $file =~ s{^/}{$public/};
53 0 ▶ if ($file =~ qr{/\.\./}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ qr"/\.\./") 0 0 0
54 0 status 403;
55 0 return 'Forbidden';
56 }
57 0 ▶ if (-e $file and -r $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and -r $file) { } 0 0 0
Condition0%0/3
-e $file and -r $file
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and -r $file
-e $file -r $file
Truth table0%0/3
A and B
A= -e $fileB= -r $file
ABresult
0X0
100
111
58 0 ▶ if (-f $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $file) { } 0 0 0
elsif (-d $file) { } 0 0 0
59 0 ▶ if (!grep { $_ eq 'Content-Type' } keys %{{&headers}} and my ($ext) = $file =~ /\.(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_ eq "Content-Type";} keys %{+{&headers};}) and my($ext) = $file =~ /\.(\w+)$/) 0 0 0
Condition0%0/3
not grep({$_ eq "Content-Type";} keys %{+{&headers};}) and my($ext) = $file =~ /\.(\w+)$/
!ll&&!rl&&r
000
MC/DC0%0/2
not grep({$_ eq "Content-Type";} keys %{+{&headers};}) and my($ext) = $file =~ /\.(\w+)$/
not grep({$_ eq "Content-Type";} keys %{+{&headers};}) my($ext) = $file =~ /\.(\w+)$/
Truth table0%0/3
not(A) and B
A= not grep({$_ eq "Content-Type";} keys %{+{&headers};})B= my($ext) = $file =~ /\.(\w+)$/
ABresult
0X0
100
111
60 0 ▶ headers 'Content-Type' => $mime_types{$ext}, headers if exists $mime_types{$ext};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mime_types{$ext} 0 0 0
61 }
62 0 open my $fh, '<', $file;
63 0 headers 'Last-Modified' => rfc1123date((stat($fh))[9]), headers;
64 0 join '', map { $_ } (<$fh>);
65 } elsif (-d $file) {
66 0 opendir(my $dh, $file);
67 0 my @files = sort grep { !/^\./ } readdir $dh;
68 0 @files = map { "<a href=\"$_\">$_</a><br>" } @files;
69 0 headers 'Content-Type' => 'text/html';
70 0 join "\n", '<html><head><title>Directory listing of ' . request->{uri} . '</title></head><body>', @files, '</body></html>';
71 } else {
72 0 status 500;
73 0 $Limper::reasons->{500};
74 }
75 } else {
76 0 status 404;
77 0 'This is the void';
78 }
79 }
80  
81 2 1;
82  
83 =for Pod::Coverage
84  
85 =head1 NAME
86  
87 Limper::SendFile - add static content support to Limper
88  
89 =head1 VERSION
90  
91 version 0.003
92  
93 =head1 SYNOPSIS
94  
95 # order is important:
96 use Limper::SendFile;
97 use Limper;
98  
99 # some other routes
100  
101 get qr{^/} => sub {
102 send_file; # sends request->{uri} by default
103 };
104  
105 limp;
106  
107 =head1 DESCRIPTION
108  
109 B<Limper::SendFile> extends L<Limper> to also return actual files. Because sometimes that's needed.
110  
111 =head1 EXPORTS
112  
113 The following are all additionally exported by default:
114  
115 public send_file
116  
117 Also exportable:
118  
119 mime_types parse_date
120  
121 =head1 FUNCTIONS
122  
123 =head2 send_file
124  
125 Sends either the file name given, or the value of C<< request->{uri} >> if no file name given.
126  
127 The following as the last defined route will have B<Limper> look for the file as a last resort:
128  
129 get qr{^/} => sub { send_file }
130  
131 B<Content-Type> will be set by file extension if known and header has not already been defined.
132 Default is B<text/plain>.
133  
134 =head2 public
135  
136 Get or set the public root directory. Default is B<./public/>.
137  
138 my $public = public;
139  
140 public '/var/www/langlang.us/public_html';
141  
142 =head1 ADDITIONAL FUNCTIONS
143  
144 =head2 parse_date
145  
146 Liberally parses whatever date a client might give, returning a Unix timestamp.
147  
148 # these all return 784111777
149 my $date = parse_date("Sun, 06 Nov 1994 08:49:37 GMT");
150 my $date = parse_date("Sunday, 06-Nov-94 08:49:37 GMT");
151 my $date = parse_date("Sun Nov 6 08:49:37 1994");
152  
153 =head2 mime_types
154  
155 Returns a B<HASH> of file extension / content-type pairs.
156  
157 =head1 HOOKS
158  
159 =head2 after
160  
161 An B<after> hook is created to support B<If-Modified-Since> and B<If-Unmodified-Since>, comparing to B<Last-Modified>.
162 This runs for all defined routes, not just those using B<send_file>.
163  
164 =head1 COPYRIGHT AND LICENSE
165  
166 Copyright (C) 2014 by Ashley Willis E<lt>ashley+perl@gitable.orgE<gt>
167  
168 This library is free software; you can redistribute it and/or modify
169 it under the same terms as Perl itself, either Perl version 5.12.4 or,
170 at your option, any later version of Perl 5 you may have available.
171  
172 =head1 SEE ALSO
173  
174 L<Limper>
175  
176 L<Limper::Engine::PSGI>
177  
178 L<Limper::SendJSON>
179  
180 =cut
181  
182 __DATA__
↑ Summary