blib/lib/HTTP/Server/Simple/Static.pm

filter: Statementstmt 38.4% 35 / 91 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 83.3% 10 / 12 Podpod 50.0% 1 / 2 totaltotal 31.7% 46 / 145 CC 16 SCAR 44.1
CC
16
Cov
28%
serve_static
163.5
get_mimetype
20.0
BEGIN
1.0
CRAP 82.0
↑ Summary Build »
1 package HTTP::Server::Simple::Static;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 our @EXPORT = qw(serve_static);
15  
16 1 our $VERSION = '0.13';
17  
18 1 my $line_end = "\015\012";
19  
20 1 my $magic;
21  
22 1 my @mime_types = (
23 [ qr/\.htm(l)?$/ => 'text/html' ],
24 [ qr/\.txt$/ => 'text/plain' ],
25 [ qr/\.css$/ => 'text/css' ],
26 [ qr/\.js$/ => 'application/javascript' ],
27 );
28  
29 sub get_mimetype {
30 0 ▶ my ($path) = @_;
Subroutine get_mimetype: not called CC=4 SCAR=30.0
Pod undocumented
31  
32 0 for my $type (@mime_types) {
33 0 ▶ if ( $path =~ $type->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$type->[0]/) 0 0 0
34 0 return $type->[1];
35 }
36 }
37  
38 0 ▶ if ( !defined $magic ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $magic) 0 0 0
39 0 require File::LibMagic;
40 0 $magic = File::LibMagic->new();
41 }
42  
43 0 return $magic->checktype_filename($path);
44 }
45  
46 sub serve_static {
47 0 ▶ my ( $self, $cgi, $base ) = @_;
Subroutine serve_static: not called CC=13 SCAR=51.0
Pod documented
48 0 ▶ $base //= q{.};
Condition0%0/2
$base //= "."
l!l
00
MC/DC0%0/1
$base //= "."
$base
Truth table0%0/2
A
A= $base
Aresult
11
00
49  
50 0 my $path = $cgi->url( -absolute => 1, -path_info => 1 );
51  
52 # Internet Explorer provides the full URI in the GET section
53 # of the request header, so remove the protocol, domain name,
54 # and port if they exist.
55 0 $path =~ s{^https?://([^/:]+)(:\d+)?/}{/};
56  
57 0 $path = URI::Escape::uri_unescape($path);
58  
59 # The splitting of the URL path and then concatenating with the
60 # base ensures the correct directory separators are used for the
61 # file path.
62  
63 0 my @parts = split q{/+}, $path;
64 0 my $fullpath = catfile( $base, @parts );
65  
66 # Sanitize the path and try it.
67  
68 0 my $realpath = Cwd::realpath($fullpath);
69 0 ▶ if ( !$realpath || $realpath !~ m/^\Q$base\E/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($realpath and $realpath =~ /^\Q$base\E/) 0 0 0
Condition0%0/3
$realpath and $realpath =~ /^\Q$base\E/
!ll&&!rl&&r
000
MC/DC0%0/2
$realpath and $realpath =~ /^\Q$base\E/
$realpath $realpath =~ /^\Q$base\E/
Truth table0%0/3
A and B
A= $realpathB= $realpath =~ /^\Q$base\E/
ABresult
0X0
100
111
70 # directory traversal attack!
71 0 return 0;
72 }
73  
74 0 my $fh = IO::File->new();
75 0 ▶ if ( -f $realpath && $fh->open($realpath) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $realpath and $fh->open($realpath)) 0 0 0
Condition0%0/3
-f $realpath and $fh->open($realpath)
!ll&&!rl&&r
000
MC/DC0%0/2
-f $realpath and $fh->open($realpath)
-f $realpath $fh->open($realpath)
Truth table0%0/3
A and B
A= -f $realpathB= $fh->open($realpath)
ABresult
0X0
100
111
76 0 my $mtime = ( stat $realpath )[9];
77 0 my $now = time;
78  
79 # RFC-2616 Section 14.25 "If-Modified-Since":
80  
81 0 my $if_modified_since = $cgi->http('If-Modified-Since');
82 0 ▶ if ($if_modified_since) {
Branch: 1 branch
BranchTrueFalseTotal
if ($if_modified_since) 0 0 0
83 0 $if_modified_since = HTTP::Date::str2time($if_modified_since);
84  
85 0 ▶ if ( defined $if_modified_since && # parsed ok
Branch: 1 branch
BranchTrueFalseTotal
if (defined $if_modified_since and $if_modified_since <= $now and $mtime <= $if_modified_since) 0 0 0
Condition0%0/6
defined $if_modified_since and $if_modified_since <= $now and $mtime <= $if_modified_since
!ll&&!rl&&r
000
defined $if_modified_since and $if_modified_since <= $now
!ll&&!rl&&r
000
MC/DC0%0/3
defined $if_modified_since and $if_modified_since <= $now and $mtime <= $if_modified_since
defined $if_modified_since $if_modified_since <= $now $mtime <= $if_modified_since
Truth table0%0/4
A and B and C
A= defined $if_modified_sinceB= $if_modified_since <= $nowC= $mtime <= $if_modified_since
ABCresult
0XX0
10X0
1100
1111
86 $if_modified_since <= $now && # not in future
87 $mtime <= $if_modified_since ) { # not changed
88  
89 0 print 'HTTP/1.1 304 Not Modified' . $line_end;
90 0 print $line_end;
91  
92 0 return 1;
93 }
94 }
95  
96 # Read the file contents and get the length in bytes
97  
98 0 binmode $fh;
99 0 binmode $self->stdout_handle;
100  
101 0 my $content;
102 {
103 0 local $/;
104 0 $content = <$fh>;
105 }
106 0 $fh->close;
107  
108 0 my $content_length;
109 0 ▶ if ( defined $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) { } 0 0 0
110 1 ▶ use bytes; # Content-Length in bytes, not characters
Subroutine BEGIN: called CC=1 SCAR=0.0
111 0 $content_length = length $content;
112 }
113 else {
114 0 $content_length = 0;
115 0 $content = q{};
116 }
117  
118 # Find MIME type
119  
120 0 my $mimetype = get_mimetype($realpath);
121  
122 # RFC-2616 Section 14.29 "Last-Modified":
123 #
124 # An origin server MUST NOT send a Last-Modified date which is
125 # later than the server's time of message origination. In such
126 # cases, where the resource's last modification would indicate
127 # some time in the future, the server MUST replace that date
128 # with the message origination date.
129  
130 0 ▶ if ( $mtime > $now ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtime > $now) 0 0 0
131 0 $mtime = $now;
132 }
133  
134 0 my $last_modified = HTTP::Date::time2str($mtime);
135 0 my $date = HTTP::Date::time2str($now);
136  
137 0 print 'HTTP/1.1 200 OK' . $line_end;
138 0 print 'Date: ' . $date . $line_end;
139 0 print 'Last-Modified: ' . $last_modified . $line_end;
140 0 print 'Content-Type: ' . $mimetype . $line_end;
141 0 print 'Content-Length: ' . $content_length . $line_end;
142 0 print $line_end;
143  
144 0 ▶ if ( $cgi->request_method() ne 'HEAD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cgi->request_method ne "HEAD") 0 0 0
145 0 print $content;
146 }
147  
148 0 return 1;
149 }
150 0 return 0;
151 }
152  
153 1 1;
154 __END__
↑ Summary Build »