WWW/Server.pm

filter: Statementstmt 24.0% 45 / 187 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 48.2% 14 / 29 Podpod 100% 11 / 11 totaltotal 18.4% 70 / 380 CC 57 SCAR 44.2
CC
57
Cov
15%
run
272.0
prepareLocal
90.0
reapClient
72.0
CRAP 82.8
« WWW/Handler/cgi.pm ↑ Summary WWW/CGI.pm »
1 ##-*- Mode: CPerl; coding: utf-8; -*-
2 ##
3 ## File: DiaColloDB/WWW/Server.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: collocation db, www wrappers: standalone tiny www server
6 ## + adapted from DTA::CAB::Server::HTTP
7  
8 package DiaColloDB::WWW::Server;
9 1 ▶use DiaColloDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DiaColloDB::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DiaColloDB::WWW::CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DiaColloDB::WWW::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use DiaColloDB::WWW::Handler::cgi;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use DiaColloDB::WWW::Handler::static;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use HTTP::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use MIME::Types; ##-- for guessing mime types
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use POSIX ':sys_wait_h'; ##-- for WNOHANG
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Socket qw(SOMAXCONN);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 ##======================================================================
26 ## globals
27  
28 1 our $VERSION = "0.02.005";
29 1 our @ISA = qw(DiaColloDB::Logger);
30  
31 ##======================================================================
32 ## constructors etc.
33  
34 ## $srv = $that->new(%args)
35 ## + %args, %$srv:
36 ## (
37 ## ##-- underlying HTTP::Daemon server
38 ## daemonMode => $daemonMode, ##-- one of 'serial' or 'fork' [default='serial']
39 ## daemonArgs => \%daemonArgs, ##-- args to HTTP::Daemon->new(); default={LocalAddr=>'0.0.0.0',LocalPort=>6066}
40 ## daemon => $daemon, ##-- underlying HTTP::Daemon object
41 ## cgiArgs => \%cgiArgs, ##-- args to DiaColloDB::WWW::CGI->new(); default=none
42 ## mimetypes => $mt, ##-- a MIME::Types object for guessing mime types
43 ## ##
44 ## ##-- user data
45 ## wwwdir => $wwwdir, ##-- root directory for www wrapper data (default=File::ShareDir::dist_dir("DiaColloDB-WWW")."/htdocs"
46 ## dburl => $dburl, ##-- DiaColloDB client URL (e.g. local indexed directory; alias='dbdir')
47 ## ##
48 ## ##-- logging
49 ## logAttempt => $level, ##-- log connection attempts at $level (default='trace')
50 ## logConnect => $level, ##-- log successful connections (client IP and requested path) at $level (default='debug')
51 ## logRquestData => $level, ##-- log full client request data at $level (default='trace')
52 ## logResponse => $level, ##-- log full client response at $level (default='trace')
53 ## logClientError => $level, ##-- log errors to client at $level (default='debug')
54 ## logClose => $level, ##-- log close client connections (default='trace')
55 ## )
56 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
57 0 my $that = shift;
58 0 ▶ my $srv = bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
59 ##-- underlying server
60 daemon => undef,
61 daemonArgs => {
62 LocalAddr=>'0.0.0.0', ##-- all
63 LocalPort=>6066,
64 ReuseAddr=>1,
65 #ReusePort=>1, ##-- don't set this; it causes errors "Your vendor has not defined Socket macro SO_REUSEPORT"
66 },
67 cgiArgs => {},
68 mimetypes => undef, ##-- see prepareLocal()
69  
70 ##-- user data
71 dburl => undef,
72 wwwdir => undef, ##-- see prepareLocal()
73  
74 ##-- logging
75 logAttempt => 'trace',
76 logConnect => 'debug',
77 logRequestData => 'trace',
78 logResponse => 'trace',
79 logCache => 'debug',
80 logClose => 'trace',
81 logClientError => 'debug',
82  
83 ##-- user args
84 @_,
85 }, ref($that)||$that);
86 0 ▶ $srv->{dburl} = $srv->{dbdir} if ($srv->{dbdir} && !defined($srv->{dburl}));
Branch: 1 branch
BranchTrueFalseTotal
if $srv->{"dbdir"} and not defined $srv->{"dburl"} 0 0 0
Condition0%0/3
$srv->{"dbdir"} and not defined $srv->{"dburl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$srv->{"dbdir"} and not defined $srv->{"dburl"}
$srv->{"dbdir"} not defined $srv->{"dburl"}
Truth table0%0/3
A and not(B)
A= $srv->{"dbdir"}B= not defined $srv->{"dburl"}
ABresult
0X0
100
111
87  
88 0 return $srv;
89 }
90  
91 ##==============================================================================
92 ## Methods: Generic Server API
93 ##==============================================================================
94  
95 ## $rc = $srv->prepare()
96 ## + default implementation initializes logger & pre-loads all analyzers
97 0 ▶sub prepare {
Subroutine prepare: not called CC=7 SCAR=40.3
Pod documented
98 0 my $srv = shift;
99 0 my $rc = 1;
100  
101 ##-- prepare: logger
102 0 DiaColloDB::Logger->ensureLog();
103  
104 ##-- prepare: PID file
105 0 ▶ if (defined($srv->{pidfile})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $srv->{"pidfile"}) 0 0 0
106 0 ▶ my $pidfh = IO::File->new(">$srv->{pidfile}")
Branch: 1 branch
BranchTrueFalseTotal
my $pidfh = "IO::File"->new(qq[>$srv->{"pidfile"}]) or $srv->logconfess(qq[prepare(): could not write PID file '$srv->{"pidfile"}': $!]) 0 0 0
107 or $srv->logconfess("prepare(): could not write PID file '$srv->{pidfile}': $!");
108 0 ▶ $pidfh->print(($srv->{pid} || $$), "\n");
Condition0%0/3
$srv->{'pid'} || $$
l!l&&r!l&&!r
000
MC/DC0%0/2
$srv->{'pid'} || $$
$srv->{'pid'} $$
Truth table0%0/3
A || B
A= $srv->{'pid'}B= $$
ABresult
1X1
011
000
109 0 $pidfh->close()
110 }
111  
112 ##-- prepare: signal handlers
113 0 ▶ $rc &&= $srv->prepareSignalHandlers();
Condition0%0/3
$rc &&= $srv->prepareSignalHandlers
!ll&&!rl&&r
000
MC/DC0%0/2
$rc &&= $srv->prepareSignalHandlers
$rc $srv->prepareSignalHandlers
Truth table0%0/3
A &&= B
A= $rcB= $srv->prepareSignalHandlers
ABresult
0X0
100
111
114  
115 ##-- prepare: subclass-local
116 0 ▶ $rc &&= $srv->prepareLocal(@_);
Condition0%0/3
$rc &&= $srv->prepareLocal(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$rc &&= $srv->prepareLocal(@_)
$rc $srv->prepareLocal(@_)
Truth table0%0/3
A &&= B
A= $rcB= $srv->prepareLocal(@_)
ABresult
0X0
100
111
117  
118 ##-- prepare: timestamp
119 0 ▶ $srv->{t_started} //= time();
Condition0%0/2
$srv->{"t_started"} //= time
l!l
00
MC/DC0%0/1
$srv->{"t_started"} //= time
$srv->{"t_started"}
Truth table0%0/2
A
A= $srv->{"t_started"}
Aresult
11
00
120  
121 ##-- return
122 0 $srv->info("initialization complete");
123  
124 0 return $rc;
125 }
126  
127 ## $rc = $srv->prepareSignalHandlers()
128 ## + initialize signal handlers
129 0 ▶sub prepareSignalHandlers {
Subroutine prepareSignalHandlers: not called CC=2 SCAR=17.9
Pod documented
130 0 my $srv = shift;
131 0 ▶ $SIG{'__DIE__'} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
132 0 ▶ die @_ if ($^S); ##-- normal operation if executing inside an eval{} block
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 0 0
133 0 $srv->finish();
134 0 $srv->logconfess("__DIE__ handler called - exiting: ", @_);
135 0 exit(255);
136 0 };
137 0 ▶ my $sig_catcher = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 my $signame = shift;
139 0 $srv->finish();
140 0 $srv->logwarn("caught signal SIG$signame - exiting");
141 0 exit(255);
142 0 };
143 0 my ($sig);
144 0 foreach $sig (qw(TERM KILL QUIT INT HUP ABRT SEGV)) {
145 0 $SIG{$sig} = $sig_catcher;
146 }
147 #$SIG{$sig} = $sig_catcher foreach $sig (qw(IO URG SYS USR1 USR2)); ##-- DEBUG
148 0 return $sig_catcher;
149 }
150  
151 ## $rc = $srv->prepareLocal(@args_to_prepare)
152 ## + subclass-local initialization
153 ## + called by prepare() after default prepare() guts have run
154 0 ▶sub prepareLocal {
Subroutine prepareLocal: not called CC=9 SCAR=45.0
Pod documented
155 0 my $srv = shift;
156  
157 ##-- setup wwwdir
158 0 ▶ $srv->{wwwdir} //= dist_dir("DiaColloDB-WWW")."/htdocs";
Condition0%0/2
$srv->{"wwwdir"} //= dist_dir("DiaColloDB-WWW") . '/htdocs'
l!l
00
MC/DC0%0/1
$srv->{"wwwdir"} //= dist_dir("DiaColloDB-WWW") . '/htdocs'
$srv->{"wwwdir"}
Truth table0%0/2
A
A= $srv->{"wwwdir"}
Aresult
11
00
159  
160 ##-- setup mimetypes object
161 0 ▶ if (!($srv->{mimetypes} //= MIME::Types->new())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($srv->{"mimetypes"} //= "MIME::Types"->new) 0 0 0
Condition0%0/3
$srv->{"mimetypes"} //= "MIME::Types"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$srv->{"mimetypes"} //= "MIME::Types"->new
$srv->{"mimetypes"} "MIME::Types"->new
Truth table0%0/3
A //= B
A= $srv->{"mimetypes"}B= "MIME::Types"->new
ABresult
1X1
011
000
162 0 $srv->logconfess("could not create MIME::Types object: $!");
163 }
164  
165 ##-- setup HTTP::Daemon object
166 0 ▶ if (!($srv->{daemon}=HTTP::Daemon->new(%{$srv->{daemonArgs}}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($srv->{"daemon"} = "HTTP::Daemon"->new(%{$$srv{"daemonArgs"};})) 0 0 0
167 0 $srv->logconfess("could not create HTTP::Daemon object: $!");
168 }
169 0 my $daemon = $srv->{daemon};
170  
171 ##-- setup mode-specific options
172 0 ▶ $srv->{daemonMode} //= 'serial';
Condition0%0/2
$srv->{"daemonMode"} //= "serial"
l!l
00
MC/DC0%0/1
$srv->{"daemonMode"} //= "serial"
$srv->{"daemonMode"}
Truth table0%0/2
A
A= $srv->{"daemonMode"}
Aresult
11
00
173 0 ▶ if ($srv->{daemonMode} eq 'fork') {
Branch: 1 branch
BranchTrueFalseTotal
if ($srv->{"daemonMode"} eq "fork") 0 0 0
174 0 ▶ $srv->{children} //= {};
Condition0%0/2
$srv->{"children"} //= {}
l!l
00
MC/DC0%0/1
$srv->{"children"} //= {}
$srv->{"children"}
Truth table0%0/2
A
A= $srv->{"children"}
Aresult
11
00
175 0 ▶ $srv->{pid} //= $$;
Condition0%0/3
$srv->{"pid"} //= $$
l!l&&r!l&&!r
000
MC/DC0%0/2
$srv->{"pid"} //= $$
$srv->{"pid"} $$
Truth table0%0/3
A //= B
A= $srv->{"pid"}B= $$
ABresult
1X1
011
000
176 0 $SIG{CHLD} = $srv->reaper();
177 }
178  
179 0 return 1;
180 }
181  
182  
183 ## $rc = $srv->run()
184 ## + run the server (just a dummy method)
185 0 ▶sub run {
Subroutine run: not called CC=16 SCAR=56.1
Pod documented
186 0 my $srv = shift;
187 0 ▶ $srv->prepare() if (!$srv->{daemon}); ##-- sanity check
Branch: 1 branch
BranchTrueFalseTotal
unless $srv->{"daemon"} 0 0 0
188 0 ▶ $srv->logconfess("run(): no underlying HTTP::Daemon object!") if (!$srv->{daemon});
Branch: 1 branch
BranchTrueFalseTotal
unless $srv->{"daemon"} 0 0 0
189  
190 0 my $daemon = $srv->{daemon};
191 0 ▶ my $mode = $srv->{daemonMode} || 'serial';
Condition0%0/2
$srv->{'daemonMode'} || 'serial'
l!l
00
MC/DC0%0/1
$srv->{'daemonMode'} || 'serial'
$srv->{'daemonMode'}
Truth table0%0/2
A
A= $srv->{'daemonMode'}
Aresult
11
00
192 0 $srv->info("server starting in $mode mode on host ", $daemon->sockhost, ", port ", $daemon->sockport, "\n");
193  
194 ##-- setup SIGPIPE handler (avoid heinous death)
195 ## + following suggestion on http://www.perlmonks.org/?node_id=580411
196 0 ▶ $SIG{PIPE} = sub { $srv->vlog('warn',"got SIGPIPE (ignoring)"); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
197  
198 0 my ($csock,$chost,$hreq,$urikey,$handler,$pid,$rsp);
199 0 while (1) {
200 ##-- call accept() within the loop to avoid breaking out in fork mode
201 0 ▶ if (!defined($csock=$daemon->accept())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($csock = $daemon->accept)) 0 0 0
202 #sleep(1);
203 0 next;
204 }
205  
206 ##-- got client $csock (HTTP::Daemon::ClientConn object; see HTTP::Daemon(3pm))
207 0 $chost = $csock->peerhost();
208  
209 ##-- serve client: parse HTTP request
210 0 ${*$csock}{'httpd_client_proto'} = HTTP::Daemon::ClientConn::_http_version("HTTP/1.0"); ##-- HACK: force status line on send_error() from $csock->get_request()
211 0 $hreq = $csock->get_request();
212 0 ▶ if (!$hreq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hreq) 0 0 0
213 0 ▶ $srv->clientError($csock, RC_BAD_REQUEST, "could not parse HTTP request: ", ($csock->reason || 'get_request() failed'));
Condition0%0/2
$csock->reason || 'get_request() failed'
l!l
00
MC/DC0%0/1
$csock->reason || 'get_request() failed'
$csock->reason
Truth table0%0/2
A
A= $csock->reason
Aresult
11
00
214 0 next;
215 }
216  
217 ##-- log basic request, and possibly request data
218 0 $urikey = $hreq->uri->as_string;
219 0 $srv->vlog($srv->{logConnect}, "client $chost: ", $hreq->method, ' ', $urikey);
220 0 $srv->vlog($srv->{logRequestData}, "client $chost: HTTP::Request={\n", $hreq->as_string, "}");
221  
222 ##-- map request to handler
223 0 $handler = $srv->getPathHandler($hreq->uri);
224 0 ▶ if (!defined($handler)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $handler) 0 0 0
225 0 $srv->clientError($csock, RC_NOT_FOUND, "cannot resolve URI ", $hreq->uri);
226 0 next;
227 }
228  
229 ##-- child|serial code: pass request to handler
230 0 eval {
231 0 $rsp = $handler->run($srv,$csock,$hreq);
232 };
233 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not defined $rsp) { } 0 0 0
234 0 ▶ $srv->clientError($csock,RC_INTERNAL_SERVER_ERROR,"handler ", (ref($handler)||$handler), " died:<br/><pre>$@</pre>");
Condition0%0/3
ref $handler || $handler
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $handler || $handler
ref $handler $handler
Truth table0%0/3
A || B
A= ref $handlerB= $handler
ABresult
1X1
011
000
235 0 $srv->reapClient($csock,$handler,$chost);
236 }
237 elsif (!defined($rsp)) {
238 0 ▶ $srv->clientError($csock,RC_INTERNAL_SERVER_ERROR,"handler ", (ref($handler)||$handler), " failed for ", $hreq->uri->path);
Condition0%0/3
ref $handler || $handler
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $handler || $handler
ref $handler $handler
Truth table0%0/3
A || B
A= ref $handlerB= $handler
ABresult
1X1
011
000
239 0 $srv->reapClient($csock,$handler,$chost);
240 }
241  
242 ##-- ... and dump response to client
243 0 ▶ if (!$csock->opened) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $csock->opened) { } 0 0 0
elsif ($csock->error) { } 0 0 0
244 0 $srv->logwarn("client socket closed unexpectedly");
245 0 next;
246 } elsif ($csock->error) {
247 0 $srv->logwarn("client socket has errors");
248 0 next;
249 }
250 0 ▶ $srv->vlog($srv->{logResponse}, "returning response: ", $rsp->as_string) if ($srv->{logResponse});
Branch: 1 branch
BranchTrueFalseTotal
if $srv->{"logResponse"} 0 0 0
251 0 $csock->send_response($rsp);
252 }
253 continue {
254 ##-- cleanup after client
255 0 ▶ $srv->reapClient($csock,undef,$chost) if (!$pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
256 0 $hreq=$handler=$pid=$rsp=undef;
257 }
258  
259  
260 0 $srv->info("server exiting\n");
261 0 return $srv->finish();
262 }
263  
264 ## $rc = $srv->finish()
265 ## + cleanup method; should be called when server dies or after run() has completed
266 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
Pod documented
267 0 my $srv = shift;
268 0 delete @SIG{qw(HUP TERM KILL __DIE__)}; ##-- unset signal handlers
269 0 ▶ unlink($srv->{pidfile}) if ($srv->{pidfile});
Branch: 1 branch
BranchTrueFalseTotal
if $srv->{"pidfile"} 0 0 0
270 0 return 1;
271 }
272  
273 ##==============================================================================
274 ## Methods: Local: spawn and reap
275  
276 ## \&reaper = $srv->reaper()
277 ## + zombie-harvesting code; installed to local %SIG
278 0 ▶sub reaper {
Subroutine reaper: not called CC=1 SCAR=6.9
Pod documented
279 0 my $srv = shift;
280 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
281 0 my ($child);
282 0 ▶ while (($child = waitpid(-1,WNOHANG)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($child = waitpid(-1, 1)) > 0) 0 0 0
283 0 $srv->vlog($srv->{logReap},"reaped subprocess pid=$child, status=$?");
284 0 delete $srv->{children}{$child};
285 }
286 #$SIG{CHLD}=$srv->reaper() if ($srv->{installReaper}); ##-- re-install reaper for SysV
287 0 };
288 }
289  
290 ## undef = $srv->reapClient($csock, $handler_or_undef, $chost_or_undef)
291 0 ▶sub reapClient {
Subroutine reapClient: not called CC=8 SCAR=42.8
Pod documented
292 0 my ($srv,$csock,$handler,$chost) = @_;
293 0 ▶ return if (!$csock);
Branch: 1 branch
BranchTrueFalseTotal
unless $csock 0 0 0
294 0 ▶ $srv->vlog($srv->{logClose}, "closing connection to client ", ($chost // ($csock->opened ? $csock->peerhost : '-undef-')));
Branch: 1 branch
BranchTrueFalseTotal
$csock->opened ? : 0 0 0
Condition0%0/3
$chost // ($csock->opened ? $csock->peerhost : "-undef-")
l!l&&r!l&&!r
000
MC/DC0%0/2
$chost // ($csock->opened ? $csock->peerhost : "-undef-")
$chost ($csock->opened ? $csock->peerhost : "-undef-")
Truth table0%0/3
A // B
A= $chostB= ($csock->opened ? $csock->peerhost : "-undef-")
ABresult
1X1
011
000
295 0 ▶ if ($csock->opened) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csock->opened) 0 0 0
296 0 $csock->force_last_request();
297 0 $csock->shutdown(2);
298 }
299 0 ▶ $handler->finish($srv,$csock) if (UNIVERSAL::can($handler,'finish'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($handler, "finish") 0 0 0
300 0 ▶ exit 0 if ($srv->{pid} && $srv->{pid} != $$);
Branch: 1 branch
BranchTrueFalseTotal
if $srv->{"pid"} and $srv->{"pid"} != $$ 0 0 0
Condition0%0/3
$srv->{"pid"} and $srv->{"pid"} != $$
!ll&&!rl&&r
000
MC/DC0%0/2
$srv->{"pid"} and $srv->{"pid"} != $$
$srv->{"pid"} $srv->{"pid"} != $$
Truth table0%0/3
A and B
A= $srv->{"pid"}B= $srv->{"pid"} != $$
ABresult
0X0
100
111
301 0 return;
302 }
303  
304 ##==============================================================================
305 ## Methods: Local: path handlers
306  
307 ## $handler = $srv->getPathHandler($hreq_uri)
308 ## + returns a callback for handling $hreq_uri, called as $handler->($clientSocket,$httpRequest)
309 0 ▶sub getPathHandler {
Subroutine getPathHandler: not called CC=8 SCAR=42.8
Pod documented
310 0 my ($srv,$uri) = @_;
311 0 (my $path = $uri->path) =~ s{/+$}{};
312 0 ▶ $path ||= 'index.perl';
Condition0%0/2
$path ||= "index.perl"
l!l
00
MC/DC0%0/1
$path ||= "index.perl"
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
313 0 $path =~ s{/+}{/}g;
314 0 $path =~ s{^/}{};
315 0 my $wwwdir = $srv->{wwwdir};
316  
317 0 ▶ if ($path =~ /Makefile|README|\.svn|(?:\.(?:ttk|rc|pod|txt|pm)$)|~$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($path =~ /Makefile|README|\.svn|(?:\.(?:ttk|rc|pod|txt|pm)$)|~$/) { } 0 0 0
elsif ($path =~ /\.perl$/) { } 0 0 0
elsif (-e "$wwwdir/$path.ttk") { } 0 0 0
elsif (-r "$wwwdir/$path") { } 0 0 0
318 ##-- ignore special paths
319 0 return undef;
320 }
321 elsif ($path =~ /\.perl$/) {
322 ##-- handle "*.perl" requests via cgi (e.g. http://HOST:PORT/profile.perl?q=foo)
323 0 (my $base = $path) =~ s/\.perl$//;
324 0 ▶ return DiaColloDB::WWW::Handler::cgi->new(template=>"$wwwdir/$base.ttk")
Branch: 1 branch
BranchTrueFalseTotal
if -e "$wwwdir/$base.perl" and -r "$wwwdir/$base.ttk" 0 0 0
Condition0%0/3
-e "$wwwdir/$base.perl" and -r "$wwwdir/$base.ttk"
!ll&&!rl&&r
000
MC/DC0%0/2
-e "$wwwdir/$base.perl" and -r "$wwwdir/$base.ttk"
-e "$wwwdir/$base.perl" -r "$wwwdir/$base.ttk"
Truth table0%0/3
A and B
A= -e "$wwwdir/$base.perl"B= -r "$wwwdir/$base.ttk"
ABresult
0X0
100
111
325 if (-e "$wwwdir/$base.perl" && -r "$wwwdir/$base.ttk");
326 0 return undef; ##-- don't serve up raw perl files
327 }
328 elsif (-e "$wwwdir/$path.ttk") {
329 ##-- handle template requests via cgi (e.g. http://HOST:PORT/profile?q=foo)
330 0 return DiaColloDB::WWW::Handler::cgi->new(template=>"$wwwdir/$path.ttk");
331 }
332 elsif (-r "$wwwdir/$path") {
333 ##-- handle static files
334 0 return DiaColloDB::WWW::Handler::static->new(file=>"$wwwdir/$path");
335 }
336  
337 0 return undef;
338 }
339  
340 ## $type_or_undef = $srv->mimetype($filename)
341 ## + gets stringified MIME-type of $filename via MIME::Types::mimeTypeOf()
342 0 ▶sub mimetype {
Subroutine mimetype: not called CC=3 SCAR=24.8
Pod documented
343 0 my ($srv,$file) = @_;
344 0 ▶ $srv->logconfess("mimetype() called but no {mimetypes} key defined!") if (!defined($srv->{mimetypes}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $srv->{"mimetypes"} 0 0 0
345 0 my $type = $srv->{mimetypes}->mimeTypeOf($file);
346 0 ▶ return defined($type) ? $type->type : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 0 0 0
347 }
348  
349 ##======================================================================
350 ## Methods: Local: error handling
351  
352 ## undef = $srv->clientError($clientSock,$status,@message)
353 ## + send an error message to the client
354 ## + $status defaults to RC_INTERNAL_SERVER_ERROR
355 ## + shuts down the client socket
356 0 ▶sub clientError {
Subroutine clientError: not called CC=5 SCAR=34.0
Pod documented
357 0 my ($srv,$csock,$status,@msg) = @_;
358 0 ▶ if ($csock->opened) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csock->opened) 0 0 0
359 0 my $chost = $csock->peerhost();
360 0 my $msg = join('',@msg);
361 0 ▶ $status = RC_INTERNAL_SERVER_ERROR if (!defined($status));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 0 0
362 0 $srv->vlog($srv->{logClientError}, "clientError($chost): $msg");
363 0 ▶ if ($msg !~ /: client closed$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg =~ /: client closed$/i) 0 0 0
364 ##-- don't try to write to sockets reporting 'client closed': this crashes the running server inexplicably!
365 0 my $_warn=$^W;
366 0 $^W=0;
367 0 $csock->send_error($status, $msg);
368 0 $^W=$_warn;
369 }
370 0 $csock->force_last_request();
371 0 $csock->shutdown(2);
372 }
373 0 ▶ $csock->close() if (UNIVERSAL::can($csock,'close'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($csock, "close") 0 0 0
374 0 $@ = undef; ##-- unset eval error
375 0 return undef;
376 }
377  
378  
379 1 1; ##-- be happy
380  
381 __END__
« WWW/Handler/cgi.pm ↑ Summary WWW/CGI.pm »