| 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.14'; | |||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub get_mimetype { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_mimetype: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | for my $type (@mime_types) { | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( $path =~ $type->[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | return $type->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( !defined $magic ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | require File::LibMagic; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $magic = File::LibMagic->new(); | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | return $magic->checktype_filename($path); | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub serve_static { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serve_static: not called CC=13 SCAR=51.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my ( $self, $cgi, $base ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $base //= q{.}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$base //= "."
MC/DC0%0/1
$base //= "."
$base
Truth table0%0/2
A
A= $base
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $realbase = Cwd::realpath($base); | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $path = $cgi->url( -absolute => 1, -path_info => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # Internet Explorer provides the full URI in the GET section | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # of the request header, so remove the protocol, domain name, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # and port if they exist. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $path =~ s{^https?://([^/:]+)(:\d+)?/}{/}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $path = URI::Escape::uri_unescape($path); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # The splitting of the URL path and then concatenating with the | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # base ensures the correct directory separators are used for the | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # file path. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my @parts = split q{/+}, $path; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $fullpath = catfile( $realbase, @parts ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # Sanitize the path and try it. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $realpath = Cwd::realpath($fullpath); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ( !$realpath || $realpath !~ m/^\Q$realbase\E/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$realpath and $realpath =~ /^\Q$realbase\E/
MC/DC0%0/2
$realpath and $realpath =~ /^\Q$realbase\E/
$realpath
$realpath =~ /^\Q$realbase\E/
Truth table0%0/3
A and B
A= $realpathB= $realpath =~ /^\Q$realbase\E/
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | # directory traversal attack! | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $fh = IO::File->new(); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( -f $realpath && $fh->open($realpath) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $realpath and $fh->open($realpath)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $mtime = ( stat $realpath )[9]; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $now = time; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # RFC-2616 Section 14.25 "If-Modified-Since": | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $if_modified_since = $cgi->http('If-Modified-Since'); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($if_modified_since) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $if_modified_since = HTTP::Date::str2time($if_modified_since); | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ( defined $if_modified_since && # parsed ok | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $if_modified_since and $if_modified_since <= $now and $mtime <= $if_modified_since
defined $if_modified_since and $if_modified_since <= $now
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
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | $if_modified_since <= $now && # not in future | ||||||||||||||||||||||||||||||||||||||||||
| 89 | $mtime <= $if_modified_since ) { # not changed | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | print 'HTTP/1.1 304 Not Modified' . $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | print $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # Read the file contents and get the length in bytes | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | binmode $fh; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | binmode $self->stdout_handle; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $content; | |||||||||||||||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $content = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $fh->close; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $content_length; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( defined $content ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | use bytes; # Content-Length in bytes, not characters | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $content_length = length $content; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $content_length = 0; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $content = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # Find MIME type | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my $mimetype = get_mimetype($realpath); | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | # RFC-2616 Section 14.29 "Last-Modified": | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # An origin server MUST NOT send a Last-Modified date which is | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # later than the server's time of message origination. In such | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # cases, where the resource's last modification would indicate | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # some time in the future, the server MUST replace that date | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # with the message origination date. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( $mtime > $now ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $mtime = $now; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $last_modified = HTTP::Date::time2str($mtime); | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $date = HTTP::Date::time2str($now); | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | print 'HTTP/1.1 200 OK' . $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | print 'Date: ' . $date . $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | print 'Last-Modified: ' . $last_modified . $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | print 'Content-Type: ' . $mimetype . $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | print 'Content-Length: ' . $content_length . $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | print $line_end; | |||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ( $cgi->request_method() ne 'HEAD' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | print $content; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||||||||||||||||