I was reading some of my old posts, and came across this thread HTTP::Daemon not working in threads?.
The entire thread leads people to believe HTTP::Daemon does not work with thread, including my own reply at that time, but that is wrong.
The problem with the code in the original post, is that it didn't detach those threads. With those huge memory leaks in 5.8 threads, that is fatal. But this could be avoided by detach, in which you clearly tell Perl that you don't want to join later.
I simplified my own web server, which is HTTP::Daemon + threads, and put it here. My original code has been running for months already and there is no problem.
use HTTP::Daemon;
use threads;
my $webServer;
my $d = HTTP::Daemon->new(LocalAddr => $ARGV[0],
LocalPort => 80,
Listen => 20) || die;
print "Web Server started!\n";
print "Server Address: ", $d->sockhost(), "\n";
print "Server Port: ", $d->sockport(), "\n";
while (my $c = $d->accept) {
threads->create(\&process_one_req, $c)->detach();
}
sub process_one_req {
my $c = shift;
my $r = $c->get_request;
if ($r) {
if ($r->method eq "GET") {
my $path = $r->url->path();
$c->send_file_response($path);
#or do whatever you want here
}
}
$c->close;
undef($c);
}