Forking ssl webserver

Forking ssl webserver

am 18.04.2005 05:22:11 von jkeller

------=_NextPart_000_0069_01C5442A.6475BCC0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

I'm trying to build a ssl web server that will allow mutiple connections =
at once. I'm using HTTP::Daemon::SSL, my code is pasted below.
The forking seems to be working however if I telnet to the port the =
daemon is running on, then send lots of request to the daemon via a web =
browser, after about 10 reloads the browser stops getting responses from =
the serer. Once I close the other connection to the daemon, the browser =
gets the response its suppose to. The $rand is just to show that the =
response is different from the last one.

The same thing seems to happen to a non ssl daemon.

Could someone point me in the right direction for making an ssl daemon =
that supports multiple connections?

Thanks in advance.


#!/usr/bin/perl

$| =3D 1;

use HTTP::Daemon::SSL;
use HTTP::Status;
use Net::SSLeay;
use URI::URL;
use strict;

# Make sure you have a certs/ directory with "server-cert.pem"
# and "server-key.pem" in it before running this!
my $d =3D HTTP::Daemon::SSL->new(
LocalPort =3D> $ARGV[0],
Listen =3D> 20,
Timeout =3D> 5,
Reuse =3D> 1
) || die;

print "Please contact me at: url, ">\n";
while (my ($c) =3D $d->accept) {
my $pid =3D fork();
if ($pid == 0) {
while (my $r =3D $c->get_request) {
my $rand =3D rand(100);
print "Got Request $rand " . $r->method . "\n";
my $res =3D HTTP::Response->new(200);
$res->content("Hello there $rand");
$c->send_response($res);
}
$c->close;
undef($c);
exit(0);
}
}
close($d);


------=_NextPart_000_0069_01C5442A.6475BCC0--

Re: Forking ssl webserver

am 30.04.2005 02:13:34 von rho

On Mon, Apr 18, 2005 at 03:22:11PM +1200, Jon Keller wrote:

> I'm trying to build a ssl web server that will allow mutiple
> connections at once. I'm using HTTP::Daemon::SSL, my code is pasted
> below. The forking seems to be working however if I telnet to the
> port the daemon is running on, then send lots of request to the
> daemon via a web browser, after about 10 reloads the browser stops
> getting responses from the serer. Once I close the other connection
> to the daemon, the browser gets the response its suppose to. The
> $rand is just to show that the response is different from the last
> one.

> The same thing seems to happen to a non ssl daemon.

Jon,

So it is not a HTTP::Daemon::SSL problem, but a HTTP::Daemon problem?

> Could someone point me in the right direction for making an ssl
> daemon that supports multiple connections?

I am not sure about your process/children handling, but in your code I
do not see any reaping of children. Isn't it the case that a parent
has to do something like this (see Perl Cookbook somewhere):

use POSIX qw(WNOHANG);
sub REAPER {
1 until (-1 == waitpid(-1, WNOHANG));
}

$SIG{CHLD} = \&REAPER;

Otherwise you end up with a lot of zombie processes, I guess.

> while (my ($c) = $d->accept) {
> my $pid = fork();
> if ($pid == 0) {
> while (my $r = $c->get_request) {
> my $rand = rand(100);
> print "Got Request $rand " . $r->method . "\n";
> my $res = HTTP::Response->new(200);
> $res->content("Hello there $rand");
> $c->send_response($res);
> }
> $c->close;
> undef($c);
> exit(0);
> }
> }

Not sure whether the $c->close AND the undef($c) is necessary, but if
the parent AND the child have a copy of $c, isn't it the case that the
kernel now has 2 links and should the parent not get rid of its link
to keep a clean house? Something like

if (i am the parent) {
$c->close;
} else { # I am the child
# do something
$c->close;
exit 0;
}

\rho