HTTPServer/StaticHandler.pm

filter: Statementstmt 58.4% 45 / 77 Branchbran 30.7% 8 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 55.6% 64 / 115 CC 14 SCAR 29.8
CC
14
Cov
51%
handle_directory
56.0
handle
4.4
_init
3.2
CRAP 19.7
↑ Summary HTTPServer.pm »
1 package POE::Component::Server::HTTPServer::StaticHandler;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POE::Component::Server::HTTPServer::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'POE::Component::Server::HTTPServer::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # _init( $root [, index_file => $index_file] [, auto_index => $auto_index ] )
9 # Files will be served relative to $root
10 # If a request is made for a directory, then:
11 # If $index_file is defined and found, it will be returned.
12 # Else, if $auto_index is defined, a directory index will be generated.
13 # Otherwise, this handler will pass on the request.
14 1 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.5
15 1 my $self = shift;
16 1 my $root = shift;
17 1 my %args = @_;
18 1 $self->{root} = $root;
19 1 $self->{mimetypes} = MIME::Types->new(); # ugh
20 1 ▶ if ( exists($args{index_file}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'index_file'}) { } 0 1 1
21 0 $self->{index_file} = $args{index_file};
22 } else {
23 1 $self->{index_file} = 'index.html';
24 }
25 1 ▶ if ( exists($args{auto_index}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'auto_index'}) { } 0 1 1
26 0 $self->{auto_index} = $args{auto_index};
27 } else {
28 1 $self->{auto_index} = 0;
29 }
30 }
31  
32 2 ▶sub handle {
Subroutine handle: called CC=4 SCAR=14.8
Pod documented
33 2 my $self = shift;
34 2 my $context = shift;
35 #print "Handling static request (", __PACKAGE__, ")\n";
36 2 my $cpath = $context->{contextpath};
37 #print "Context path=$cpath\n";
38 # scrub path (badly): XXX fix this with file::spec or the like
39 2 ▶ if ( $cpath =~ m[(^|/)\.\.(/|$)] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cpath =~ m[(^|/)\.\.(/|$)]) 0 2 2
40 0 warn "Will not serve dangerous path '$cpath'\n"; # should keep silent here
41 0 return H_CONT;
42 }
43 2 my $filepath = "$self->{root}/$cpath";
44 #print "static root: $filepath\n";
45 2 ▶ if ( -d $filepath ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $filepath) { } 0 2 2
elsif (-f $filepath) { } 1 1 2
46 0 return $self->handle_directory($filepath, $context);
47 } elsif ( -f $filepath ) {
48 1 return $self->handle_plainfile($filepath, $context);
49 } else {
50 #print "Request for non-existant file: $filepath\n";
51 1 return H_CONT;
52 }
53 }
54  
55 1 ▶sub handle_plainfile {
Subroutine handle_plainfile: called CC=3 SCAR=11.3
Pod documented
56 1 my $self = shift;
57 1 my $filepath = shift;
58 1 my $context = shift;
59 1 ▶ if ( open(my $in, $filepath) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $in, $filepath) { } 1 0 1
60 1 binmode($in);
61 1 local $/ = undef;
62 1 $context->{response}->code( RC_OK );
63 1 my $type = $self->{mimetypes}->mimeTypeOf( $filepath );
64 1 ▶ $type='text/plain' unless defined($type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 1 1
65 1 $context->{response}->content_type( $type );
66 1 $context->{response}->content(<$in>);
67 1 return H_FINAL;
68 } else {
69 #print "Failed to open $filepath ($!)\n";
70 0 $context->{error_message} = $!; # XXX security: returned with 404 response
71 0 return H_CONT;
72 }
73 }
74  
75 0 ▶sub handle_directory {
Subroutine handle_directory: not called CC=7 SCAR=40.3
Pod documented
76 0 my $self = shift;
77 0 my $filepath = shift;
78 0 my $context = shift;
79 #print "directory( $filepath )\n";
80 0 ▶ if ( $self->{index_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"index_file"}) 0 0 0
81 0 my $index_file = "$filepath/$self->{index_file}";
82 0 ▶ if ( -e $index_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $index_file) 0 0 0
83 0 return $self->handle_plainfile( $index_file, $context );
84 }
85 }
86 0 ▶ if ( $self->{auto_index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto_index"}) 0 0 0
87 # nasty hack: should probably just not include this option
88 0 ▶ if ( opendir(my $dir, $filepath) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dir, $filepath) { } 0 0 0
89 0 my $page = qq{<HTML><HEAD><TITLE>Directory Index</TITLE></HEAD>\n};
90 0 $page .= qq{<BODY bgcolor="#aaccaa">\n};
91 0 $page .= qq{<P>Directory Index</P><HR />\n};
92 0 $page .= qq{<UL>\n};
93 0 my $base = $context->{request}->uri;
94 0 ▶ while(my $fn = readdir($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dir)) 0 0 0
95 0 ▶ next if $fn=~/^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $fn =~ /^\./ 0 0 0
96 0 $page .= qq{<LI><A HREF="$base/$fn">$fn</A></LI>\n};
97 }
98 0 $page .= qq{</UL>\n};
99 0 $page .= qq{</BODY></HTML>\n};
100 0 $context->{response}->code( RC_OK );
101 0 $context->{response}->content( $page );
102 0 return H_FINAL;
103 } else {
104 # XXX should this class of problem be a 500?
105 #print "Failed to opendir ($!)\n";
106 0 $context->{error_message} = $!; # XXX security: returned with 404 resp
107 0 return H_CONT;
108 }
109 }
110 0 return H_CONT;
111 }
112  
113 1 1;
114 __END__
↑ Summary HTTPServer.pm »