Proxy/Engine/Legacy.pm

filter: Statementstmt 87.3% 55 / 63 Branchbran 68.7% 11 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 4 / 4 totaltotal 86.3% 82 / 95 CC 11 SCAR 16.9
CC
11
Cov
84%
run
9.4
reap_zombies
3.0
stop
2.0
CRAP 5.4
« Proxy/BodyFilter/htmltext.pm ↑ Summary Proxy/BodyFilter/lines.pm »
1 package HTTP::Proxy::Engine::Legacy;
2 63 $HTTP::Proxy::Engine::Legacy::VERSION = '0.304';
3 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 63 ▶use POSIX 'WNOHANG';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 63 ▶use HTTP::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 63 our @ISA = qw( HTTP::Proxy::Engine );
8 63 our %defaults = (
9 max_clients => 12,
10 );
11  
12 63 __PACKAGE__->make_accessors( qw( kids select ), keys %defaults );
13  
14 34 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
15 34 my $self = shift;
16 34 $self->kids( [] );
17 34 $self->select( IO::Select->new( $self->proxy->daemon ) );
18 }
19  
20 97 ▶sub run {
Subroutine run: called CC=8 SCAR=22.4
Pod documented
21 97 my $self = shift;
22 97 my $proxy = $self->proxy;
23 97 my $kids = $self->kids;
24  
25 # check for new connections
26 97 my @ready = $self->select->can_read(1);
27 97 for my $fh (@ready) { # there's only one, anyway
28  
29 # single-process proxy (useful for debugging)
30 82 ▶ if ( $self->max_clients == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_clients == 0) 6 76 82
31 6 $proxy->max_keep_alive_requests(1); # do not block simultaneous connections
32 6 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS",
33 "No fork allowed, serving the connection" );
34 6 $proxy->serve_connections($fh->accept);
35 6 $proxy->new_connection;
36 6 next;
37 }
38  
39 76 ▶ if ( @$kids >= $self->max_clients ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$kids >= $self->max_clients) 0 76 76
40 0 $proxy->log( HTTP::Proxy::ERROR, "PROCESS",
41 "Too many child process, serving the connection" );
42 0 $proxy->serve_connections($fh->accept);
43 0 $proxy->new_connection;
44 0 next;
45 }
46  
47 # accept the new connection
48 76 my $conn = $fh->accept;
49 76 my $child = fork;
50 76 ▶ if ( !defined $child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 0 76 76
51 0 $conn->close;
52 0 $proxy->log( HTTP::Proxy::ERROR, "PROCESS", "Cannot fork" );
53 0 ▶ $self->max_clients( $self->max_clients - 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_clients > @$kids 0 0 0
54 if $self->max_clients > @$kids;
55 0 next;
56 }
57  
58 # the parent process
59 76 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) { } 54 22 76
60 54 $conn->close;
61 54 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS", "Forked child process $child" );
62 54 push @$kids, $child;
63 }
64  
65 # the child process handles the whole connection
66 else {
67 22 $SIG{INT} = 'DEFAULT';
68 22 $proxy->serve_connections($conn);
69 22 exit; # let's die!
70 }
71 }
72  
73 75 ▶ $self->reap_zombies if @$kids;
Branch: 1 branch
BranchTrueFalseTotal
if @$kids 69 6 75
74 }
75  
76 12 ▶sub stop {
Subroutine stop: called CC=2 SCAR=7.0
Pod documented
77 12 my $self = shift;
78 12 my $kids = $self->kids;
79  
80 # wait for remaining children
81 # EOLOOP
82 12 kill INT => @$kids;
83 12 ▶ $self->reap_zombies while @$kids;
Branch: 1 branch
BranchTrueFalseTotal
@$kids and do { $self->reap_zombies } 0 12 12
84 }
85  
86 # private reaper sub
87 69 ▶sub reap_zombies {
Subroutine reap_zombies: called CC=3 SCAR=11.0
Pod documented
88 69 my $self = shift;
89 69 my $kids = $self->kids;
90 69 my $proxy = $self->proxy;
91  
92 69 while (1) {
93 91 my $pid = waitpid( -1, WNOHANG );
94 91 ▶ last if $pid == 0 || $pid == -1; # AS/Win32 returns negative PIDs
Branch: 1 branch
BranchTrueFalseTotal
if $pid == 0 or $pid == -1 69 22 91
Condition100%3/3
$pid == 0 or $pid == -1
l!l&&r!l&&!r
62722
MC/DC100%2/2
$pid == 0 or $pid == -1
$pid == 0 $pid == -1
Truth table100%3/3
A or B
A= $pid == 0B= $pid == -1
ABresult
1X1
011
000
95 22 @$kids = grep { $_ != $pid } @$kids;
96 22 $proxy->{conn}++; # Cannot use the interface for RO attributes
97 22 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS", "Reaped child process $pid" );
98 22 $proxy->log( HTTP::Proxy::PROCESS, "PROCESS", @$kids . " remaining kids: @$kids" );
99 }
100 }
101  
102 63 1;
103  
104 __END__
« Proxy/BodyFilter/htmltext.pm ↑ Summary Proxy/BodyFilter/lines.pm »