HTTPServer.pm

filter: Statementstmt 55.7% 82 / 147 Branchbran 45.4% 20 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 72.7% 16 / 22 Podpod 88.8% 8 / 9 totaltotal 56.7% 126 / 222 CC 23 SCAR 18.1
CC
23
Cov
53%
create_server
20.0
dispatch
9.1
_init
9.0
CRAP 6.1
« HTTPServer/StaticHandler.pm ↑ Summary HTTPServer/TemplateHandler.pm »
1 package POE::Component::Server::HTTPServer;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use POE qw( Component::Server::TCP Filter::HTTPD );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use POE::Component::Server::HTTPServer::Handler qw( H_CONT H_FINAL );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 our @EXPORT = qw( new_handler );
10  
11 5 our $VERSION = '0.9.2';
12  
13 6 ▶sub new_handler {
Subroutine new_handler: called CC=2 SCAR=7.3
Pod documented
14 6 my $package_suffix = shift;
15 5 ▶ eval "use POE::Component::Server::HTTPServer::$package_suffix;";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
17 0 warn $@;
18 0 die "Failed to intialize handler";
19 }
20 6 return "POE::Component::Server::HTTPServer::$package_suffix"->new(@_);
21 }
22  
23 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24 1 my $class = shift;
25 my $self = bless {
26 _port => 8080,
27 _handlers => [],
28 _log_file => "httpserver.log",
29 _backstop_handler => new_handler('NotFoundHandler'),
30 45 ▶ _debug => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1 }, $class;
32 1 $self->_init(@_);
33 1 return $self;
34 }
35  
36 1 ▶sub _init {
Subroutine _init: called CC=6 SCAR=21.9
37 1 my $self = shift;
38 1 my %args = @_;
39 1 ▶ if ( defined( $args{_debug} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"_debug"}) 0 1 1
40 0 $self->{_debug} = $args{_debug};
41 0 $self->{_debug}->("Debugging: on.\n");
42 }
43 1 ▶ if ( defined( $args{handlers} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"handlers"}) 0 1 1
44 0 $self->handlers( $args{handlers} );
45 }
46 1 ▶ if ( defined( $args{port} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"port"}) 1 0 1
47 1 $self->port( $args{port} );
48 }
49 1 ▶ if ( defined( $args{log_file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"log_file"}) 0 1 1
50 0 $self->log_file( $args{log_file} );
51 }
52 1 ▶ if ( defined( $args{backstop_handler} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"backstop_handler"}) 0 1 1
53 0 $self->backstop_handler( $args{backstop_handler} );
54 }
55 }
56  
57 0 ▶sub log_file {
Subroutine log_file: not called CC=2 SCAR=17.9
Pod documented
58 0 my $self = shift;
59 0 my $log_file = shift;
60 0 ▶ if ( defined($log_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log_file) { } 0 0 0
61 0 $self->{_debug}->("Log file: $log_file\n");
62 0 return $self->{_log_file} = $log_file;
63 } else {
64 0 return $self->{_log_file};
65 }
66 }
67  
68 1 ▶sub port {
Subroutine port: called CC=2 SCAR=7.2
Pod documented
69 1 my $self = shift;
70 1 my $port = shift;
71 1 ▶ if ( defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) { } 1 0 1
72 1 $self->{_debug}->("Port: $port\n");
73 1 return $self->{_port} = $port;
74 } else {
75 0 return $self->{_port};
76 }
77 }
78  
79 7 ▶sub handlers {
Subroutine handlers: called CC=2 SCAR=6.9
Pod documented
80 7 my $self = shift;
81 7 my $handlers = shift;
82 7 ▶ if ( defined($handlers) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handlers) { } 2 5 7
83 2 $self->{_debug}->("Handlers: ", map(" $_\n", @$handlers), "\n");
84 2 return $self->{_handlers} = $handlers;
85 } else {
86 5 return $self->{_handlers};
87 }
88 }
89  
90 # usage: $s->add_handler( '/foo' => new_handler('NotFoundHandler') )
91 0 ▶sub add_handler {
Subroutine add_handler: not called CC=1 SCAR=6.9
Pod undocumented
92 0 my $self = shift;
93 0 my( $path, $handler ) = @_;
94 0 push( @{$self->{_handlers}}, $path, $handler );
95 }
96  
97 0 ▶sub create_server {
Subroutine create_server: not called CC=4 SCAR=30.0
Pod documented
98 0 my $self = shift;
99 0 ▶ unless ( @{$self->handlers()} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->handlers;}) 0 0 0
100 0 $self->{_debug}->("No handlers: setting NotFoundHandler for all\n");
101 0 $self->handlers( ['/' => new_handler('NotFoundHandler')] );
102 }
103 0 ▶ if ( defined($self->{_log_file}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_log_file"}) 0 0 0
104 0 $self->{_debug}->("Opening log: $self->{_log_file}\n");
105 0 ▶ open( $self->{_log_fh}, ">> $self->{_log_file}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"_log_fh"}, qq[>> $self->{"_log_file"}] or warn qq[Could not open log file '$self->{"_log_file"}' ($!)\n] 0 0 0
106 warn "Could not open log file '$self->{_log_file}' ($!)\n";
107 }
108 0 $self->{_debug}->("Creating server component\n");
109 return
110 0 POE::Component::Server::TCP->new( Port => $self->port,
111 ClientInput => $self->_get_dispatcher,
112 ClientFilter => 'POE::Filter::HTTPD',
113 );
114 }
115  
116 # dispatch( $context [, $fullpath] )
117 # can be used by handlers ($context->{dispatcher}->dispatch) for re-dispatch
118 5 ▶sub dispatch {
Subroutine dispatch: called CC=8 SCAR=22.0
Pod documented
119 5 my $self = shift;
120 5 my $context = shift;
121 5 my $fullpath = shift;
122 5 ▶ if ( defined($fullpath) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fullpath) 0 5 5
123 0 $context->{fullpath} = $fullpath;
124 }
125  
126 5 ▶ if ($context->{_dispatch_count}++ > 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"_dispatch_count"}++ > 10) 0 5 5
127 0 warn "Detected deep dispatch for '$context->{fullpath}', aborting!\n";
128 0 return H_FINAL;
129 }
130  
131 5 eval {
132 5 $self->{_debug}->("Dispatching request\n");
133 # copy handler list for splicing
134 5 my @handlers = ( @{$self->handlers}, '.' => $self->backstop_handler );
135 5 ▶ while ( @handlers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@handlers) 14 0 14
136 # shift two elts from handlers in to prefix, handler
137 14 my( $prefix, $handler ) = splice( @handlers, 0, 2 );
138 14 $self->{_debug}->("Checking path:$context->{fullpath} =~ prefix:$prefix ($handler)\n");
139 14 ▶ if ( $context->{fullpath} =~ /^$prefix/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"fullpath"} =~ /^$prefix/) 9 5 14
140 9 $self->{_debug}->("Fullpath: $context->{fullpath}\n");
141 9 $self->{_debug}->("Prefix: $prefix\n");
142 9 ($context->{contextpath} = $context->{fullpath}) =~ s/^$prefix//;
143 9 my $retval;
144 9 ▶ if (UNIVERSAL::can($handler, 'handle')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($handler, 'handle')) { } 5 4 9
145 5 $retval = $handler->handle($context);
146 } else {
147 # assume handler's a coderef (might wanna check that sometime)
148 4 $retval = $handler->($context);
149 }
150 9 ▶ if ( $retval == H_FINAL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval == 1) 5 4 9
151 5 $self->{_debug}->("Handler returned H_FINAL, stopping\n");
152 5 last;
153 }
154 }
155 }
156 };
157 5 ▶ if ($@) { # internal server error
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
158 0 my $error = $@;
159 0 warn "Caught error: $@\n";
160 0 $context->{response}->code(500);
161 0 $context->{response}->content_type("text/plain");
162 0 $context->{response}->content("An error occured while processing request:\n$error\n");
163 }
164 5 return H_FINAL; # in case this is being called from a handler
165  
166 } # dispatch()
167  
168 5 ▶sub backstop_handler {
Subroutine backstop_handler: called CC=2 SCAR=7.1
Pod documented
169 5 my $self = shift;
170 5 ▶ $self->{_backstop_handler} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 5 5
171 5 return $self->{_backstop_handler};
172 }
173  
174 0 ▶sub _get_dispatcher {
Subroutine _get_dispatcher: not called CC=1 SCAR=6.9
175 0 my $self = shift;
176 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
177 0 my( $kernel, $heap, $request ) = @_[ KERNEL, HEAP, ARG0 ];
178  
179 0 ▶ if ( $request->isa('HTTP::Response') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->isa("HTTP::Response")) 0 0 0
180 # if a processing error occurrs, POE::Filter::HTTPD generates
181 # a suitable response for you to send back
182 0 $heap->{client}->put( $request );
183 0 $kernel->yield('shutdown');
184 0 return;
185 }
186  
187 0 $self->{_debug}->("Handling request\n");
188  
189 my $context = { request => $request,
190 response => HTTP::Response->new( RC_OK ),
191 remote_ip => $heap->{remote_ip},
192 0 fullpath => $request->uri->path,
193 dispatcher => $self,
194 _dispatch_count => 0,
195 };
196  
197 0 $self->dispatch($context);
198  
199 0 $self->_request_log( $context );
200 0 $heap->{client}->put( $context->{response} );
201 0 undef($context);
202 0 $kernel->yield( 'shutdown' ); # signal that we're done sending to client
203 0 };
204  
205 } # _get_dispatcher
206  
207 # pretty lame, so far
208 0 ▶sub _request_log {
Subroutine _request_log: not called CC=2 SCAR=17.9
209 0 my $self = shift;
210 0 my $context = shift;
211 0 my($req,$resp) = ($context->{request}, $context->{response});
212 0 my @log;
213 0 push(@log, $context->{remote_ip});
214 0 ▶ if ( defined($context->{username}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{'username'}) { } 0 0 0
215 0 push(@log, $context->{username});
216 } else {
217 0 push(@log, '-');
218 }
219 0 push(@log, "[".scalar(localtime())."]"); # wrong format
220 0 push(@log, $req->method);
221 0 push(@log, '"'.$req->uri.'"');
222 0 push(@log, $resp->code);
223 0 push(@log, $resp->content_length);
224 0 my $fh = $self->{_log_fh};
225 0 print $fh join(" ", @log), "\n";
226 }
227  
228  
229 5 1;
230 __END__
« HTTPServer/StaticHandler.pm ↑ Summary HTTPServer/TemplateHandler.pm »