blib/lib/HTTP/Server/Brick.pm

filter: Statementstmt 25.0% 45 / 180 Branchbran 2.7% 3 / 108 Conditioncond 7.5% 3 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 50.0% 11 / 22 Podpod 100% 4 / 4 totaltotal 17.4% 66 / 378 CC 70 SCAR 49.4
CC
70
Cov
16%
_handle_dynamic_request
306.0
start
272.0
mount
156.0
CRAP 140.0
↑ Summary Build »
1 package HTTP::Server::Brick;
2  
3 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 our $VERSION = qv(0.1.5);
5  
6 # $Id$
7  
8 =head1 NAME
9  
10 HTTP::Server::Brick - Simple pure perl http server for prototyping "in the style of" Ruby's WEBrick
11  
12  
13 =head1 VERSION
14  
15 This document describes HTTP::Server::Brick version 0.1.5
16  
17  
18 =head1 SYNOPSIS
19  
20 use HTTP::Server::Brick;
21 use HTTP::Status;
22
23 my $server = HTTP::Server::Brick->new( port => 8888 );
24
25 $server->mount( '/foo/bar' => {
26 path => '/some/directory/htdocs',
27 });
28
29 $server->mount( '/test/proc' => {
30 handler => sub {
31 my ($req, $res) = @_;
32 $res->add_content("<html><body>
33 <p>Path info: $req->{path_info}</p>
34 </body></html>");
35 1;
36 },
37 wildcard => 1,
38 });
39
40 $server->mount( '/test/proc/texty' => {
41 handler => sub {
42 my ($req, $res) = @_;
43 $res->add_content("flubber");
44 $res->header('Content-type', 'text/plain');
45 1;
46 },
47 wildcard => 1,
48 });
49
50 # these next two are equivalent
51 $server->mount( '/favicon.ico' => {
52 handler => sub { RC_NOT_FOUND },
53 });
54 $server->mount( '/favicon.ico' => {
55 handler => sub {
56 my ($req, $res) = @_;
57 $res->code(RC_NOT_FOUND);
58 1;
59 },
60 });
61
62 # start accepting requests (won't return unless/until process
63 # receives a HUP signal)
64 $server->start;
65  
66 For an SSL (https) server, replace the C<new()> line above with:
67  
68 use HTTP::Daemon::SSL;
69
70 my $server = HTTP::Server::Brick->new(
71 port => 8889,
72 daemon_class => 'HTTP::Daemon::SSL',
73 daemon_args => [
74 SSL_key_file => 'my_ssl_key.pem',
75 SSL_cert_file => 'my_ssl_cert.pem',
76 ],
77 );
78  
79 See the docs of L<HTTP::Daemon::SSL> for other options.
80  
81 =head1 DESCRIPTION
82  
83 HTTP::Server::Brick allows you to quickly wrap a prototype web server around some
84 Perl code. The underlying server daemon is HTTP::Daemon and the performance should
85 be fine for demo's, light internal systems, etc.
86
87 =head1 METHODS
88  
89 =cut
90  
91 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 3 ▶use HTTP::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 3 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 3 ▶use LWP::MediaTypes;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 3 ▶use constant DEBUG => $ENV{DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'DEBUG'} || 0
l!l
03
MC/DC0%0/1
$ENV{'DEBUG'} || 0
$ENV{'DEBUG'}
Truth table50%1/2
A
A= $ENV{'DEBUG'}
Aresult
11
00
100  
101  
102 3 my $__singleton;
103 3 my $__server_should_run = 0;
104  
105 0 ▶$SIG{__WARN__} = sub { $__singleton ? $__singleton->_log( error => '[warn] ' . shift ) : CORE::warn(@_) };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$__singleton ? : 0 0 0
106 $SIG{__DIE__} = sub {
107 1 ▶ CORE::die (@_) if $^S; # don't interfere with eval
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 1 1
108 1 ▶ $__singleton->_log( error => '[die] ' . $_[0] ) if $__singleton;
Branch: 1 branch
BranchTrueFalseTotal
if $__singleton 1 0 1
109 1 CORE::die (@_)
110 3 };
111 0 ▶$SIG{HUP} = sub { $__server_should_run = 0; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
112  
113  
114 =head2 new
115  
116 C<new> takes nine named arguments (all of which are optional):
117  
118 =over
119  
120 =item error_log, access_log
121  
122 Should be self-explanatory - can be anything that responds to C<print> eg.
123 file handle, IO::Handle, etc. Default to stderr and stdout respectively.
124  
125 =item port
126  
127 The port to listen on. Defaults to a random high port (you'll see it in the error log).
128  
129 =item host
130  
131 The server hostname. Defaults to something sensible.
132  
133 =item timeout
134  
135 Used for various timout values - see L<HTTP::Daemon> for more information.
136  
137 =item directory_index_file
138  
139 The filename for directory indexing. Note that this only applies to static path mounts.
140 Defaults to C<index.html>.
141  
142 =item directory_indexing
143  
144 If no index file is available (for a static path mount), do you want a clickable list
145 of files in the directory be rendered? Defaults to true.
146  
147 =item leave_sig_pipe_handler_alone
148  
149 HTTP::Daemon, the http server module this package is built on, chokes in certain multiple-request
150 situations unless you ignore PIPE signals. By default PIPE signals are ignored as soon as you start
151 the server (and restored if the server exits via HUP). If you want to handle PIPE signals your own
152 way, pass in a true value for this.
153  
154 If this makes no sense to you, just ignore it - the "right thing" will happen by default.
155  
156 =item daemon_class
157  
158 The class which actually handles webserving. The default is C<HTTP::Daemon>.
159 If you want SSL, use C<HTTP::Daemon::SSL>. Whatever class you use must inherit
160 from HTTP::Daemon.
161  
162 =item daemon_args
163  
164 Sometimes you need to pass extra arguments to your C<daemon_class>, e.g. SSL
165 configuration. This arrayref will be dereferenced and passed to C<new>.
166  
167 =item fork
168  
169 Set to true if you want a forking server.
170  
171 =back
172  
173 =cut
174  
175 sub new {
176 1 ▶ my ($this, %args) = @_;
Subroutine new: called CC=4 SCAR=16.8
Pod documented
177 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
178  
179 1 ▶ if ($args{daemon_class} and not
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"daemon_class"} and not eval { do { $args{"daemon_class"}->isa("HTTP::Daemon") } }) 0 1 1
Condition33%1/3
$args{"daemon_class"} and not eval { do { $args{"daemon_class"}->isa("HTTP::Daemon") } }
!ll&&!rl&&r
100
MC/DC0%0/2
$args{"daemon_class"} and not eval { do { $args{"daemon_class"}->isa("HTTP::Daemon") } }
$args{"daemon_class"} not eval { do { $args{"daemon_class"}->isa("HTTP::Daemon") } }
Truth table33%1/3
A and not(B)
A= $args{"daemon_class"}B= not eval { do { $args{"daemon_class"}->isa("HTTP::Daemon") } }
ABresult
0X0
100
111
180 0 eval { $args{daemon_class}->isa('HTTP::Daemon') }) {
181 0 die "daemon_class argument '$args{daemon_class}'" .
182 " must inherit from HTTP::Daemon";
183 }
184  
185 1 my $self = bless {
186 _site_map => [],
187 error_log => \*STDERR,
188 access_log => \*STDOUT,
189 directory_index_file => 'index.html',
190 directory_indexing => 1,
191 daemon_class => 'HTTP::Daemon',
192 daemon_args => [],
193 %args,
194 }, $class;
195  
196 1 $__singleton = $self;
197  
198 1 return $self;
199 }
200  
201 =head2 mount
202  
203 C<mount> takes two positional arguments. The first a full uri (as
204 a string beginning with '/' - any trailing '/' will be stripped). The
205 second is a hashref which serves as a spec for the mount. The allowable
206 hash keys in this spec are:
207  
208 =over
209  
210 =item path
211  
212 A full path to a local filesystem directory or file for static serving.
213 Mutually exclusive with C<handler>.
214  
215 =item handler
216  
217 A coderef. See L</Handlers> below. Mutually exclusive with C<path>.
218  
219 =item wildcard
220  
221 If false, only exact matches will be served. If true, any requests based
222 on the uri will be served. eg. if C<wildcard> is false, C<'/foo/bar'> will
223 only match C<http://mysite.com/foo/bar> and not, say, C<http://mysite.com/foo/bar/sheep>.
224 If C<wildcard> is true, on the other hand, it will match. A handler can
225 access the path extension as described below in L</Handlers>.
226  
227 Static handlers that are directories default to wildcard true.
228  
229 =back
230  
231 The site map is always searched depth-first, in other words a more specific
232 uri will trump a less-specific one.
233  
234 =head3 return value
235  
236 C<mount> returns C<$self> so that it can be chained into a one-liner if desired.
237  
238 =head3 shortcut invocation
239  
240 As a shortcut also to aid one-liners, instead of a hashref the second argument can be
241 either a path string or a coderef, mapped like so:
242  
243 =over
244  
245 =item string
246  
247 equivalent to C<{ path => the_string, wildcard => 1 }>
248  
249 =item coderef
250  
251 equivalent to C<{ handler => coderef, wildcard => 0 }>
252  
253 =back
254  
255 Eg. to quickly server your current directory:
256  
257 perl -MHTTP::Server::Brick -e 'HTTP::Server::Brick->new(fork=>1)->mount(qw(/ .))->start'
258  
259 =cut
260  
261 sub mount {
262 0 ▶ my ($self, $uri, $args) = @_;
Subroutine mount: not called CC=12 SCAR=50.5
Pod documented
263  
264 0 ▶ if (ref($args) eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $args eq 'CODE') { } 0 0 0
elsif (not ref $args) { } 0 0 0
elsif (ref $args ne 'HASH') { } 0 0 0
265 0 $args = { handler => $args, wildcard => 0 };
266 } elsif (!ref($args)) {
267 0 $args = { path => $args, wildcard => 1 };
268 } elsif (ref($args) ne 'HASH') {
269 0 die 'third arg to mount must be a hashref, coderef or path string';
270 }
271  
272 0 my $depth;
273 0 ▶ if ($uri eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq '/') { } 0 0 0
274 0 $depth = 0;
275 } else {
276 0 $uri =~ s!/$!!;
277 0 my @parts = split( m!/!, $uri );
278 0 $depth = scalar(@parts) - 1; # leading / adds one
279 }
280  
281 0 ▶ $self->{_site_map}[$depth] ||= {};
Condition0%0/2
$self->{"_site_map"}[$depth] ||= {}
l!l
00
MC/DC0%0/1
$self->{"_site_map"}[$depth] ||= {}
$self->{"_site_map"}[$depth]
Truth table0%0/2
A
A= $self->{"_site_map"}[$depth]
Aresult
11
00
282 0 $self->{_site_map}[$depth]{$uri} = $args;
283  
284 # we should default a static path to a wildcard mount if it's a directory
285 0 ▶ if (!exists $args->{wildcard} && exists $args->{path} && -d $args->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args->{"wildcard"} and exists $args->{"path"} and -d $args->{"path"}) 0 0 0
Condition0%0/6
not exists $args->{"wildcard"} and exists $args->{"path"} and -d $args->{"path"}
!ll&&!rl&&r
000
not exists $args->{"wildcard"} and exists $args->{"path"}
!ll&&!rl&&r
000
MC/DC0%0/3
not exists $args->{"wildcard"} and exists $args->{"path"} and -d $args->{"path"}
not exists $args->{"wildcard"} exists $args->{"path"} -d $args->{"path"}
Truth table0%0/4
not(A) and B and C
A= not exists $args->{"wildcard"}B= exists $args->{"path"}C= -d $args->{"path"}
ABCresult
0XX0
10X0
1100
1111
286 0 $args->{wildcard} = 1;
287 }
288  
289 my $mount_type = exists $args->{handler} ? 'handler' :
290 0 ▶ exists $args->{path} ? 'directory' : '(unknown)';
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'handler'} ? : 0 0 0
exists $args->{'path'} ? : 0 0 0
291 0 ▶ $self->_log( error => 'Mounted' . ($args->{wildcard} ? ' wildcard' : '') . " $mount_type at $uri" );
Branch: 1 branch
BranchTrueFalseTotal
$args->{'wildcard'} ? : 0 0 0
292  
293 0 return $self;
294 }
295  
296 =head2 start
297  
298 Actually starts the server - this will loop indefinately, or until
299 the process recieves a C<HUP> signal in which case it will return after servicing
300 any current request, or waiting for the next timeout (which defaults to 5s - see L</new>).
301  
302 =cut
303  
304 sub start {
305 0 ▶ my $self = shift;
Subroutine start: not called CC=16 SCAR=56.1
Pod documented
306  
307 0 $__server_should_run = 1;
308  
309 # HTTP::Daemon chokes on multiple simultaneous requests
310 0 ▶ unless ($self->{leave_sig_pipe_handler_alone}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"leave_sig_pipe_handler_alone"}) 0 0 0
311 0 $self->{_old_sig_pipe_handler} = $SIG{'PIPE'};
312 0 $SIG{'PIPE'} = 'IGNORE';
313 }
314  
315 0 ▶ $SIG{CHLD} = 'IGNORE' if $self->{fork};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fork"} 0 0 0
316  
317 $self->{daemon} = $self->{daemon_class}->new(
318 ReuseAddr => 1,
319 LocalPort => $self->{port},
320 LocalHost => $self->{host},
321 Timeout => 5,
322 0 ▶ @{ $self->{daemon_args} },
Branch: 1 branch
BranchTrueFalseTotal
$self->{"daemon"} = $self->{"daemon_class"}->new("ReuseAddr", 1, "LocalPort", $self->{"port"}, "LocalHost", $self->{"host"}, "Timeout", 5, @{$$self{"daemon_args"};}) or die "Can't start daemon: $!" 0 0 0
323 ) or die "Can't start daemon: $!";
324  
325 # HTTP::Server::Daemon seems inconsistent in returning a string vs URI object
326 my $url_string = UNIVERSAL::can($self->{daemon}->url, 'as_string') ?
327 $self->{daemon}->url->as_string :
328 0 ▶ $self->{daemon}->url;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($self->{'daemon'}->url, 'as_string') ? : 0 0 0
329  
330 0 $self->_log(error => "Server started on $url_string");
331  
332 0 ▶ while ($__server_should_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($__server_should_run) 0 0 0
333 0 ▶ my $conn = $self->{daemon}->accept or next;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->{"daemon"}->accept or next 0 0 0
334  
335 # if we're a forking server, fork. The parent will wait for the next request.
336 # TODO: limit number of children
337 0 ▶ next if $self->{fork} and fork;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fork"} and fork 0 0 0
Condition0%0/3
$self->{"fork"} and fork
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"fork"} and fork
$self->{"fork"} fork
Truth table0%0/3
A and B
A= $self->{"fork"}B= fork
ABresult
0X0
100
111
338 0 ▶ while (my $req = $conn->get_request) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $req = $conn->get_request) 0 0 0
339  
340 # Provide an X-Brick-Remote-IP header
341 0 my ($r_port, $r_iaddr) = Socket::unpack_sockaddr_in($conn->peername);
342 0 my $ip = Socket::inet_ntoa($r_iaddr);
343 0 $req->headers->remove_header('X-Brick-Remote-IP');
344 0 ▶ $req->header('X-Brick-Remote-IP' => $ip) if defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip 0 0 0
345  
346 0 my ($submap, $match) = $self->_map_request($req);
347  
348 0 ▶ if ($submap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($submap) { } 0 0 0
349 0 ▶ if (exists $submap->{path}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $submap->{'path'}) { } 0 0 0
elsif (exists $submap->{'handler'}) { } 0 0 0
350 0 $self->_handle_static_request( $conn, $req, $submap, $match);
351
352 } elsif (exists $submap->{handler}) {
353 0 $self->_handle_dynamic_request( $conn, $req, $submap, $match);
354  
355 } else {
356 0 $self->_send_error($conn, $req, RC_INTERNAL_SERVER_ERROR, 'Corrupt Site Map');
357 }
358  
359 } else {
360 0 $self->_send_error($conn, $req, RC_NOT_FOUND, ' Not Found in Site Map');
361 }
362 }
363 # should use a guard object here to protect against early exit leaving zombies
364 0 ▶ exit if $self->{fork};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fork"} 0 0 0
365 }
366  
367
368 0 ▶ unless ($self->{leave_sig_pipe_handler_alone}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"leave_sig_pipe_handler_alone"}) 0 0 0
369 0 $SIG{'PIPE'} = $self->{_old_sig_pipe_handler};
370 }
371  
372 0 1;
373 }
374  
375 sub _handle_static_request {
376 0 ▶ my ($self, $conn, $req, $submap, $match) = @_;
Subroutine _handle_static_request: not called CC=8 SCAR=42.8
377  
378 0 my $path = $submap->{path} . '/' . $match->{path_info};
379  
380 0 ▶ if (-d $path && $match->{full_path} !~ m!/$! ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path and not $match->{"full_path"} =~ m[/$]) 0 0 0
Condition0%0/3
-d $path and not $match->{"full_path"} =~ m[/$]
!ll&&!rl&&r
000
MC/DC0%0/2
-d $path and not $match->{"full_path"} =~ m[/$]
-d $path not $match->{"full_path"} =~ m[/$]
Truth table0%0/3
A and not(B)
A= -d $pathB= not $match->{"full_path"} =~ m[/$]
ABresult
0X0
100
111
381 0 $conn->send_redirect( $match->{full_path} . '/', RC_SEE_OTHER );
382 0 DEBUG && $self->_log(error => 'redirecting to path with / appended: ' . $match->{full_path});
383 0 return;
384 }
385  
386 0 ▶ my $serve_path = -d $path ? "$path/$self->{directory_index_file}" : $path;
Branch: 1 branch
BranchTrueFalseTotal
-d $path ? : 0 0 0
387  
388 0 ▶ if (-r $serve_path) {
Branch: 3 branches
BranchTrueFalseTotal
if (-r $serve_path) { } 0 0 0
elsif (-d $path and $self->{'directory_indexing'}) { } 0 0 0
elsif (-d $path) { } 0 0 0
389 0 my $code = $conn->send_file_response($serve_path);
390 0 $self->_log_status($req, $code);
391
392 ▶ } elsif (-d $path && $self->{directory_indexing}) {
Condition0%0/3
-d $path and $self->{"directory_indexing"}
!ll&&!rl&&r
000
MC/DC0%0/2
-d $path and $self->{"directory_indexing"}
-d $path $self->{"directory_indexing"}
Truth table0%0/3
A and B
A= -d $pathB= $self->{"directory_indexing"}
ABresult
0X0
100
111
393  
394 0 my $res = $self->_render_directory($path, $match->{full_path});
395 0 $conn->send_response( $res );
396 0 $self->_log( access => '[' . RC_OK . "] $match->{full_path}" );
397  
398
399 } elsif (-d $path) {
400 0 $self->_send_error($conn, $req, RC_FORBIDDEN, 'Directory Indexing Not Allowed' );
401  
402 } else {
403 0 $self->_send_error($conn, $req, RC_NOT_FOUND, 'File Not Found' );
404 }
405 }
406  
407 sub _handle_dynamic_request {
408 0 ▶ my ($self, $conn, $req, $submap, $match) = @_;
Subroutine _handle_dynamic_request: not called CC=17 SCAR=57.2
409  
410 0 my $res = HTTP::Response->new;
411 0 $res->base($match->{full_path});
412  
413 # stuff the match info into the request
414 0 $req->{mount_path} = $match->{mount_path};
415 0 ▶ $req->{path_info} = $match->{path_info} ? '/' . $match->{path_info} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$match->{'path_info'} ? : 0 0 0
416  
417 # and some other useful bits TODO: document (and, actually, subclass HTTP::Request...)
418  
419 # It seems that in some cases (specifically when the url contains no explicit port),
420 # HTTP::Daemon returns a uri string instead of an object. RT #29042
421 0 my $url = $self->{daemon}->url;
422 0 ▶ $url = URI->new($url) if ! ref $url;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $url 0 0 0
423  
424 0 ▶ if ($req->header('Host') =~ /^(.*):(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->header('Host') =~ /^(.*):(.*)$/) { } 0 0 0
elsif ($req->header('Host')) { } 0 0 0
425 0 $req->{hostname} = $1;
426 0 $req->{port} = $2;
427 } elsif ($req->header('Host')) {
428 0 $req->{hostname} = $req->header('Host');
429 0 $req->{port} = $url->port;
430 } else {
431 0 $req->{hostname} = $url->host;
432 0 $req->{port} = $url->port;
433 }
434  
435 # actually call the handler
436 0 ▶ if ( my $return_code = eval { $submap->{handler}->($req, $res) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $return_code = eval { do { $submap->{'handler'}->($req, $res) } }) { } 0 0 0
437  
438 # choose the status in this order:
439 # 1. if the handler died or returned false => RC_INTERNAL_SERVER_ERROR
440 # 2. if the handler set a code on the response object, use that
441 # 3. if the handler returned something that looks like a return code
442 # 4. RC_OK
443
444 0 ▶ my $code = !$return_code ? RC_INTERNAL_SERVER_ERROR :
Branch: 3 branches
BranchTrueFalseTotal
!$return_code ? : 0 0 0
$res->code ? : 0 0 0
$return_code >= 100 ? : 0 0 0
445 $res->code ? $res->code :
446 $return_code >= 100 ? $return_code : RC_OK;
447
448 0 $res->code($code);
449  
450 # default mime type to text/html
451 0 ▶ $res->header( 'Content-Type' ) || $res->header( 'Content-Type', 'text/html' );
Branch: 1 branch
BranchTrueFalseTotal
$res->header("Content-Type") or $res->header("Content-Type", "text/html") 0 0 0
452
453 0 ▶ if ($res->is_success) {
Branch: 3 branches
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
elsif ($res->is_error) { } 0 0 0
elsif ($res->is_redirect) { } 0 0 0
454 0 $conn->send_response( $res );
455 0 $self->_log( access => "[$code] $match->{full_path}" );
456  
457 } elsif ($res->is_error) {
458 # TODO: should allow a way to specify custom error content
459 0 $self->_send_error( $conn, $req, $res->code, $res->message );
460  
461 } elsif ($res->is_redirect) {
462 0 ▶ if (UNIVERSAL::can($res->{target_uri}, 'path')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($res->{'target_uri'}, 'path')) { } 0 0 0
463 0 my $target = $res->{target_uri}->path;
464  
465 0 ▶ if ($target !~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target =~ m[^/]) 0 0 0
466 # prepend dirname of original request
467 0 ▶ $match->{full_path} =~ m!^(.*/)! and
Branch: 1 branch
BranchTrueFalseTotal
$match->{"full_path"} =~ m[^(.*/)] and $target = $1 . $target 0 0 0
468 $target = $1 . $target;
469 }
470 0 $conn->send_redirect($target, $code);
471 0 $self->_log( access => "[$code] Redirecting to " . $target );
472 } else {
473 0 $self->_send_error($conn, $req, RC_INTERNAL_SERVER_ERROR,
474 'Handler Tried to Redirect Without Setting Target URI');
475 }
476  
477 } else {
478 0 $self->_send_error($conn, $req,
479 RC_NOT_IMPLEMENTED,
480 'Handler Returned an Unimplemented Response Code: ' . $code);
481 }
482 } else {
483 0 $self->_send_error($conn, $req, RC_INTERNAL_SERVER_ERROR, 'Handler Failed');
484 0 $self->_log( error => "Handler Failed for mount: " . $match->{mount_path});
485 0 ▶ $self->_log( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
486 }
487  
488 0 1;
489 }
490  
491 sub _render_directory {
492 0 ▶ my ($self, $path, $uri ) = @_;
Subroutine _render_directory: not called CC=3 SCAR=24.8
493  
494 0 my $res = HTTP::Response->new( RC_OK );
495 0 $res->header( 'Content-type', 'text/html' );
496
497 0 $res->add_content(<<END_HEADER);
498 <html>
499 <head>
500 <title>Directory for $uri</title>
501 </head>
502 <body>
503 <h1>Directory for $uri</h1>
504 <blockquote><pre>
505 <a href="..">.. (Parent directory)</a>
506 END_HEADER
507  
508 0 for (sort glob "$path/*") {
509 0 my $is_directory = -d $_;
510 0 s!.*/!!;
511 0 ▶ $_ .= '/' if $is_directory;
Branch: 1 branch
BranchTrueFalseTotal
if $is_directory 0 0 0
512 0 $res->add_content("<a href=\"$_\">$_</a>\n");
513 }
514  
515 0 $res->add_content(<<END_FOOTER);
516 </pre></blockquote>
517 </body>
518 </html>
519 END_FOOTER
520  
521 0 return $res;
522 }
523  
524 sub _send_error {
525 0 ▶ my ($self, $conn, $req, $code, $text) = @_;
Subroutine _send_error: not called CC=1 SCAR=6.9
526  
527 0 $conn->send_error($code, $text);
528  
529 0 $self->_log_status($req, $code, $text);
530 }
531  
532 sub _log_status {
533 0 ▶ my ($self, $req, $code, $text) = @_;
Subroutine _log_status: not called CC=6 SCAR=37.4
534  
535 0 ▶ if ($code == RC_OK || $code == RC_UNAUTHORIZED || $code == RC_NOT_FOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200 or $code == 401 or $code == 404) 0 0 0
Condition0%0/6
$code == 200 or $code == 401 or $code == 404
l!l&&r!l&&!r
000
$code == 200 or $code == 401
l!l&&r!l&&!r
000
MC/DC0%0/3
$code == 200 or $code == 401 or $code == 404
$code == 200 $code == 401 $code == 404
Truth table0%0/4
A or B or C
A= $code == 200B= $code == 401C= $code == 404
ABCresult
1XX1
01X1
0011
0000
536 0 $self->_log( access => "[$code] " . $req->uri->path );
537 }
538  
539 0 ▶ $self->_log( error => "[$code] [" . $req->uri->path . '] ' . ($text || status_message($code)) )
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 200 0 0 0
Condition0%0/3
$text || &status_message($code)
l!l&&r!l&&!r
000
MC/DC0%0/2
$text || &status_message($code)
$text &status_message($code)
Truth table0%0/3
A || B
A= $textB= &status_message($code)
ABresult
1X1
011
000
540 unless $code == RC_OK;
541 }
542  
543 # this is not the best data structure for a complex site map, but it's
544 # easy to insert and query (although very hard to move things around).
545 # basically for every path depth (ie. number of /) there is a hash of
546 # full paths and their associated handler and meta-data.
547  
548 # this would be an obvious performance point if you wanted to use this
549 # for actual serving.
550  
551 sub _map_request {
552 0 ▶ my ($self, $request) = @_;
Subroutine _map_request: not called CC=7 SCAR=40.3
553  
554 0 my $map = $self->{_site_map};
555  
556 0 my $uri = $request->uri->path;
557  
558 0 my @parts = split( m!/!, $uri );
559  
560 0 my $depth = scalar(@parts) - 1;
561 # the test is reall for $uri eq '/', but an integer comparison is faster
562 0 ▶ $depth = 0 if $depth == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $depth == -1 0 0 0
563 0 my $match_depth = $depth;
564  
565 0 ▶ while ($match_depth >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_depth >= 0) 0 0 0
566  
567 0 my $mount_path = '/' . join('/', @parts[1..$match_depth]);
568  
569 0 ▶ if ($map->[$match_depth] && exists $map->[$match_depth]{$mount_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map->[$match_depth] and exists $map->[$match_depth]{$mount_path}) 0 0 0
Condition0%0/3
$map->[$match_depth] and exists $map->[$match_depth]{$mount_path}
!ll&&!rl&&r
000
MC/DC0%0/2
$map->[$match_depth] and exists $map->[$match_depth]{$mount_path}
$map->[$match_depth] exists $map->[$match_depth]{$mount_path}
Truth table0%0/3
A and B
A= $map->[$match_depth]B= exists $map->[$match_depth]{$mount_path}
ABresult
0X0
100
111
570  
571 # if we find a depth-first match, but it's not flagged as a wildcard
572 # mount, then don't match
573 0 ▶ if ($match_depth != $depth && !$map->[$match_depth]{$mount_path}{wildcard}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_depth != $depth and not $map->[$match_depth]{$mount_path}{"wildcard"}) 0 0 0
Condition0%0/3
$match_depth != $depth and not $map->[$match_depth]{$mount_path}{"wildcard"}
!ll&&!rl&&r
000
MC/DC0%0/2
$match_depth != $depth and not $map->[$match_depth]{$mount_path}{"wildcard"}
$match_depth != $depth not $map->[$match_depth]{$mount_path}{"wildcard"}
Truth table0%0/3
A and not(B)
A= $match_depth != $depthB= not $map->[$match_depth]{$mount_path}{"wildcard"}
ABresult
0X0
100
111
574 0 return;
575 }
576  
577 return(
578 0 $map->[$match_depth]{$mount_path},
579 {
580 full_path => $uri,
581 mount_path => $mount_path,
582 path_info => join('/', @parts[$match_depth+1..$depth]),
583 },
584 );
585 }
586  
587 0 $match_depth--;
588 }
589 }
590  
591 =head2 add_type
592  
593 The mime-type of static files is automatically determined by L<LWP::MediaTypes>. You
594 can add any types it doesn't know about via this method.
595  
596 The first argument is the mime type, all subsequent arguments form a list of
597 possible file extensions for that mime type. See L<LWP::MediaTypes> for more info.
598  
599 =cut
600  
601 # Improve LWP::MediaTypes' mime-type knowledge.
602 3 LWP::MediaTypes::add_type('image/png' => qw(png));
603 3 LWP::MediaTypes::add_type('text/css' => qw(css));
604 3 LWP::MediaTypes::add_type('text/javascript' => qw(js));
605  
606 sub add_type {
607 0 ▶ my ($self, @args) = @_;
Subroutine add_type: not called CC=1 SCAR=6.9
Pod documented
608  
609 0 LWP::MediaTypes::add_type(@args);
610 }
611  
612 sub _log {
613 1 ▶ my ($self, $log_key, $text) = @_;
Subroutine _log: called CC=1 SCAR=0.0
614  
615 1 $self->{"${log_key}_log"}->print( '[' . localtime() . "] [$$] ", $text, "\n" );
616 }
617  
618  
619 3 1; # Magic true value required at end of module
620 __END__
↑ Summary Build »