Use
sysread and
alarm
to time out the client if necesary. The example below
is pretty much straight from the alarm entry in perlfunc.
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $server = IO::Socket::INET->new(LocalPort => 6969,
Type => SOCK_STREAM,
Proto => 'tcp',
Reuse => 1,
Listen => 10,
)
or die "Can't create listener socket: $!\n";
while ( my $client = $server->accept() ) {
my ($data, $buf);
my $timeout = 10;
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $timeout;
while (sysread $client, $buf, 1024) {
$data .= $buf;
last if $data =~ /(?:\015?\012){2}/;
# reset timeout if we read data
alarm $timeout;
}
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n";
}
# do stuff with $data
print ">>$data<<\n";
}
I imagine HTTP::Daemon would do all this stuff for you.
Of course, a line without the \015\012 (or at least \012)
terminator isn't a valid HTTP request, so you shouldn't
bother looking at it.