| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 54 | if $self->max_clients > @$kids; | ||||||||||||||||||||||||||||
| 55 | 0 | next; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # the parent process | ||||||||||||||||||||||||||||
| 59 | 76 | ▶ | if ($child) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$pid == 0 or $pid == -1
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||