| 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.7); | |||||||||||||||||||||||||||||||||||||||||
| 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.7 | ||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$ENV{'DEBUG'} || 0
$ENV{'DEBUG'}
Truth table50%1/2
A
A= $ENV{'DEBUG'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | $SIG{__DIE__} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | CORE::die (@_) if $^S; # don't interfere with eval | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | $__singleton->_log( error => '[die] ' . $_[0] ) if $__singleton; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | my ($this, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | if ($args{daemon_class} and not | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args{"daemon_class"} and not eval {
do {
$args{"daemon_class"}->isa("HTTP::Daemon")
}
}
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")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub mount { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mount: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | my ($self, $uri, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | if (ref($args) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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] ||= {}
MC/DC0%0/1
$self->{"_site_map"}[$depth] ||= {}
$self->{"_site_map"}[$depth]
Truth table0%0/2
A
A= $self->{"_site_map"}[$depth]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
not exists $args->{"wildcard"} and exists $args->{"path"} and -d $args->{"path"}
not exists $args->{"wildcard"} and exists $args->{"path"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | $self->_log( error => 'Mounted' . ($args->{wildcard} ? ' wildcard' : '') . " $mount_type at $uri" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | $self->{daemon} = $self->{daemon_class}->new( | ||||||||||||||||||||||||||||||||||||||||||
| 318 | ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 319 | LocalPort => $self->{port}, | ||||||||||||||||||||||||||||||||||||||||||
| 320 | LocalHost => $self->{host}, | ||||||||||||||||||||||||||||||||||||||||||
| 321 | Timeout => exists $self->{timeout} ? $self->{timeout} : 5, | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | @{ $self->{daemon_args} }, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | $self->_log(error => "Server started on $url_string"); | |||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | while ($__server_should_run) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | my $conn = $self->{daemon}->accept or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->{"fork"} and fork
MC/DC0%0/2
$self->{"fork"} and fork
$self->{"fork"}
fork
Truth table0%0/3
A and B
A= $self->{"fork"}B= fork
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | while (my $req = $conn->get_request) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my ($submap, $match) = $self->_map_request($req); | |||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | if ($submap) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if (exists $submap->{path}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | ▶ | unless ($self->{leave_sig_pipe_handler_alone}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | $SIG{'PIPE'} = $self->{_old_sig_pipe_handler}; | |||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | sub _handle_static_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_static_request: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | my ($self, $conn, $req, $submap, $match) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | my $path = $submap->{path} . '/' . $match->{path_info}; | |||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | if (-d $path && $match->{full_path} !~ m!/$! ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-d $path and not $match->{"full_path"} =~ m[/$]
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[/$]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | if (-r $serve_path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _handle_dynamic_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_dynamic_request: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | my ($self, $conn, $req, $submap, $match) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | if ($req->header('Host') =~ /^(.*):(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | my $target = $res->{target_uri}->path; | |||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | if ($target !~ m!^/!) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 466 | # prepend dirname of original request | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | ▶ | $match->{full_path} =~ m!^(.*/)! and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | ▶ | sub _render_directory { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_directory: not called CC=3 SCAR=20.6
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | my ($self, $path, $uri ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 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/*") { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | my $is_directory = -d $_; | |||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | s!.*/!!; | |||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | ▶ | $_ .= '/' if $is_directory; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _send_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | my ($self, $conn, $req, $code, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | $conn->send_error($code, $text); | |||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | $self->_log_status($req, $code, $text); | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | ▶ | sub _log_status { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log_status: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | my ($self, $req, $code, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | 0 | ▶ | if ($code == RC_OK || $code == RC_UNAUTHORIZED || $code == RC_NOT_FOUND) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$code == 200 or $code == 401 or $code == 404
$code == 200 or $code == 401
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$text || &status_message($code)
MC/DC0%0/2
$text || &status_message($code)
$text
&status_message($code)
Truth table0%0/3
A || B
A= $textB= &status_message($code)
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _map_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_map_request: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | my ($self, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | my $match_depth = $depth; | |||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | 0 | ▶ | while ($match_depth >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$map->[$match_depth] and exists $map->[$match_depth]{$mount_path}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$match_depth != $depth and not $map->[$match_depth]{$mount_path}{"wildcard"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub add_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | LWP::MediaTypes::add_type(@args); | |||||||||||||||||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | 1 | ▶ | sub _log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 613 | 1 | my ($self, $log_key, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||