AnyEvent-Net-Curl-Queued
view release on metacpan or search on metacpan
inc/Test/HTTP/AnyEvent/Server.pm view on Meta::CPAN
sub BUILD {
my ($self) = @_;
## no critic (RequireLocalizedPunctuationVars)
@ENV{qw(no_proxy http_proxy ftp_proxy all_proxy)} = (q(localhost,127.0.0.1), (q()) x 3)
if $self->disable_proxy;
unless ($self->forked) {
$self->set_server(
$self->start_server(sub {
my (undef, $address, $port) = @_;
$self->set_address($address);
$self->set_port($port);
AE::log info =>
"bound to http://$address:$port/";
})
);
} else {
my ($rh, $wh) = portable_pipe;
given (fork) {
when (undef) {
AE::log fatal =>
"couldn't fork(): $!";
} when (0) {
# child
close $rh;
my $h = AnyEvent::Handle->new(
fh => $wh,
on_error => sub {
AE::log fatal =>
"couldn't syswrite() to pipe: $!";
},
);
$self->set_server(
$self->start_server(sub {
my (undef, $address, $port) = @_;
# have to postpone so the address/port gets actually bound
AE::postpone { $h->push_write(join("\t", $address, $port)) };
})
);
AE::cv->wait;
POSIX::_exit(0);
exit 1;
} default {
# parent
my $pid = $_;
close $wh;
my $buf;
my $len = sysread $rh, $buf, 65536;
AE::log fatal =>
"couldn't sysread() from pipe: $!"
if not defined $len or not $len;
my ($address, $port) = split m{\t}x, $buf;
$self->set_address($address);
$self->set_port($port);
$self->set_forked_pid($pid);
AE::log info =>
"forked as $pid and bound to http://$address:$port/";
}
}
}
return;
}
sub DEMOLISH {
my ($self) = @_;
if ($self->forked) {
my $pid = $self->forked_pid;
kill 9 => $pid;
AE::log info =>
"killed $pid";
}
return;
}
sub uri {
my ($self) = @_;
return sprintf('http://%s:%d/', $self->address, $self->port);
}
sub start_server {
my ($self, $cb) = @_;
return tcp_server(
$self->address => $self->port,
sub {
my ($fh, $host, $port) = @_;
if (scalar keys %pool > $self->maxconn) {
AE::log error =>
"deny connection from $host:$port (too many connections)\n";
return;
} else {
AE::log warn =>
"new connection from $host:$port\n";
}
my $h = AnyEvent::Handle->new(
fh => $fh,
on_eof => \&_cleanup,
on_error => \&_cleanup,
timeout => $self->timeout,
);
$pool{fileno($fh)} = $h;
AE::log debug =>
sprintf "%d connection(s) in pool\n", scalar keys %pool;
my ($req, $hdr);
( run in 1.157 second using v1.01-cache-2.11-cpan-140bd7fdf52 )