blib/lib/HTTP/Daemon/App.pm

filter: Statementstmt 33.0% 36 / 109 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 68.7% 11 / 16 Podpod 100% 3 / 3 totaltotal 24.7% 50 / 202 CC 38 SCAR 61.9
CC
38
Cov
20%
run
870.0
__ANON__
72.0
send_basic_auth_request
6.0
CRAP 487.5
↑ Summary
1 package HTTP::Daemon::App;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use version;our $VERSION = qv('0.0.9');
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use HTTP::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Daemon::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Acme::Spork;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Unix::PID;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 our @EXPORT_OK = qw(run decode_basic_auth send_basic_auth_request);
17  
18 0 ▶sub send_basic_auth_request {
Subroutine send_basic_auth_request: not called CC=2 SCAR=17.9
Pod documented
19 0 my ($c, $realm) = @_;
20 0 ▶ $realm = 'Restricted Area' if !$realm;
Branch: 1 branch
BranchTrueFalseTotal
unless $realm 0 0 0
21 0 my $auth_request_res = HTTP::Response->new(401, 'Unauthorized');
22 0 $auth_request_res->header('WWW-Authenticate' => qq{Basic realm="$realm"});
23 0 $auth_request_res->is_error(1);
24 0 $auth_request_res->error_as_HTML(1);
25 0 $c->send_response($auth_request_res);
26 }
27  
28 0 ▶sub decode_basic_auth {
Subroutine decode_basic_auth: not called CC=2 SCAR=11.6
Pod documented
29 0 my ($auth) = @_;
30 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 0 ▶ $auth = ( split /\s+/, $auth->header('Authorization') )[1] if ref $auth;
Branch: 1 branch
BranchTrueFalseTotal
if ref $auth 0 0 0
32 0 require MIME::Base64;
33 0 return split(/:/, MIME::Base64::decode( $auth ), 2);
34 }
35  
36 0 ▶sub run {
Subroutine run: not called CC=29 SCAR=67.7
Pod documented
37 0 my($daemons_hashref, $conf) = @_;
38
39 0 ▶ $conf = {} if ref $conf ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $conf ne "CODE" 0 0 0
40 0 ▶ $conf->{'pid_dir'} = File::Spec->catdir(qw(/ var run)) if !$conf->{'pid_dir'};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"pid_dir"} 0 0 0
41 0 ▶ $conf->{'pid_ext'} = '.pid' if !$conf->{'pid_ext'};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"pid_ext"} 0 0 0
42 0 ▶ $conf->{'self'} = "perl $0" if !$conf->{'self'};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"self"} 0 0 0
43
44 0 my $additional = '';
45 0 for my $opt (sort keys %{ $conf->{'opts'} }) {
46 0 ▶ if($opt eq '--start' || $opt eq '--stop' || $opt eq '--restart') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq "--start" or $opt eq "--stop" or $opt eq "--restart") 0 0 0
Condition0%0/6
$opt eq "--start" or $opt eq "--stop" or $opt eq "--restart"
l!l&&r!l&&!r
000
$opt eq "--start" or $opt eq "--stop"
l!l&&r!l&&!r
000
MC/DC0%0/3
$opt eq "--start" or $opt eq "--stop" or $opt eq "--restart"
$opt eq "--start" $opt eq "--stop" $opt eq "--restart"
Truth table0%0/4
A or B or C
A= $opt eq "--start"B= $opt eq "--stop"C= $opt eq "--restart"
ABCresult
1XX1
01X1
0011
0000
47 0 delete $conf->{'opts'}{$opt};
48 0 next;
49 }
50 0 $additional .= "|$opt";
51 }
52  
53 0 ▶ $ARGV[0] = '--help' if !defined $ARGV[0]; # no uninit warnings and logical visual clue to coders of what will happen if its not specified...
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ARGV[0] 0 0 0
54 0 ▶ if($ARGV[0] eq '--restart') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] eq "--restart") 0 0 0
55 0 system qq($conf->{'self'} --stop $$);
56 0 sleep 1;
57 0 system qq($conf->{'self'} --start);
58 0 exit;
59 }
60
61 0 ▶ if($ARGV[0] eq '--start') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ARGV[0] eq '--start') { } 0 0 0
elsif ($ARGV[0] eq '--stop') { } 0 0 0
elsif (exists $conf->{'opts'}{$ARGV[0]}) { } 0 0 0
62 0 for my $daemon (sort keys %{ $daemons_hashref }) {
63 0 ▶ next if ref $daemons_hashref->{$daemon}{'handler'} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $daemons_hashref->{$daemon}{"handler"} ne "CODE" 0 0 0
64 0 ▶ next if ref $daemons_hashref->{$daemon}{'daemon'} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $daemons_hashref->{$daemon}{"daemon"} ne "HASH" 0 0 0
65
66 0 my $pidfile = File::Spec->catfile($conf->{'pid_dir'}, "$daemon$conf->{'pid_ext'}");
67
68 my $objkt = $daemons_hashref->{$daemon}{'ssl'}
69 0 ? HTTP::Daemon::SSL->new( %{ $daemons_hashref->{$daemon}{'daemon'} } )
70 0 ▶ : HTTP::Daemon->new( %{ $daemons_hashref->{$daemon}{'daemon'} } )
Branch: 1 branch
BranchTrueFalseTotal
$daemons_hashref->{$daemon}{'ssl'} ? : 0 0 0
71 ;
72 0 ▶ if(!$objkt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($objkt) 0 0 0
73 0 print "$daemon: $!\n";
74 0 next;
75 }
76  
77 print "Starting $daemons_hashref->{$daemon}{'label'}: <URL:" . $objkt->url . ">\n"
78 0 ▶ if defined $daemons_hashref->{$daemon}{'label'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $daemons_hashref->{$daemon}{"label"} 0 0 0
79  
80 my $http_pid = spork(
81 0 ▶ sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
82 0 my($handler, $d, $name, $pidfile, $conf) = @_;
83 0 local $0 = $name;
84 0 ▶ while (my $c = $d->accept) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $d->accept) 0 0 0
85 0 ▶ $conf->{'pre_fork'}->(@_) if ref $conf->{'pre_fork'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $conf->{"pre_fork"} eq "CODE" 0 0 0
86
87 0 ▶ if(my $kid = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kid = fork) { } 0 0 0
88 0 ▶ $c->can('get_cipher') ? $c->close('SSL_no_shutdown' => 1) : $c->close;
Branch: 1 branch
BranchTrueFalseTotal
$c->can('get_cipher') ? : 0 0 0
89 0 undef($c);
90 }
91 else {
92 0 ▶ $conf->{'get_tmpfile'} = sub { return } if ref $conf->{'get_tmpfile'} ne 'CODE';
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ref $conf->{"get_tmpfile"} ne "CODE" 0 0 0
93 0 ▶ while (my $r = $c->get_request( $conf->{'get_tmpfile'}->( $conf ) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $c->get_request($conf->{"get_tmpfile"}->($conf))) 0 0 0
94 0 $handler->($d, $c, $r, $conf);
95 }
96 # $c->can('get_cipher') ? $c->close('SSL_no_shutdown' => 1) : $c->close;
97 # undef($c);
98 0 exit 0;
99 }
100
101 0 ▶ $conf->{'pst_fork'}->(@_) if ref $conf->{'pst_fork'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $conf->{"pst_fork"} eq "CODE" 0 0 0
102 }
103 0 }, $daemons_hashref->{$daemon}{'handler'}, $objkt, $daemon, $pidfile, $conf,
104 );
105  
106 0 ▶ Unix::PID->new()->pid_file_no_unlink($pidfile, $http_pid)
Branch: 1 branch
BranchTrueFalseTotal
"Unix::PID"->new->pid_file_no_unlink($pidfile, $http_pid) or die "The PID in $pidfile is still running." 0 0 0
107 or die "The PID in $pidfile is still running.";
108 }
109 }
110 elsif($ARGV[0] eq '--stop') {
111 0 for my $daemon (sort keys %{ $daemons_hashref }) {
112 0 my $pidfile = File::Spec->catfile($conf->{'pid_dir'}, "$daemon$conf->{'pid_ext'}");
113  
114 0 my $uxp = Unix::PID->new();
115 0 my $pid = $uxp->kill_pid_file($pidfile);
116  
117 0 ▶ if($pid == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid == 1) { } 0 0 0
elsif ($pid eq '-1') { } 0 0 0
118 0 print "$daemon is not running\n";
119 }
120 elsif($pid eq '-1') {
121 0 print "$daemon pidfile: $!\n";
122 }
123 else {
124 0 print "$daemon ($pid) was stopped\n";
125 }
126  
127 0 print "\tCollecting $daemon children...\n";
128 0 for my $kid ($uxp->get_pidof($daemon) ) {
129 0 ▶ next if defined $ARGV[1] && $kid eq $ARGV[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $ARGV[1] and $kid eq $ARGV[1] 0 0 0
Condition0%0/3
defined $ARGV[1] and $kid eq $ARGV[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ARGV[1] and $kid eq $ARGV[1]
defined $ARGV[1] $kid eq $ARGV[1]
Truth table0%0/3
A and B
A= defined $ARGV[1]B= $kid eq $ARGV[1]
ABresult
0X0
100
111
130 0 ▶ $uxp->kill( $kid ) or print "\t\tCould not kill $daemon child $kid: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$uxp->kill($kid) or print "\t\tCould not kill $daemon child ${kid}: $!\n" 0 0 0
131 }
132 }
133 }
134 elsif(exists $conf->{'opts'}{$ARGV[0]}) {
135 0 $conf->{'opts'}->{$ARGV[0]}->(@_);
136 }
137 else {
138 0 print "Useage: $0 [--start|--stop||--restart$additional]\n";
139 0 ▶ print "$conf->{'help'}\n" if $conf->{'help'};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"help"} 0 0 0
140 }
141 }
142  
143 1 1;
144  
145 __END__
↑ Summary