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

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