Re: CGI ponderings
Mark Anderson (mda@pure.com)
Sat, 17 Jun 1995 18:57:43 -0700
> read_entity_body: read 734 of 1594 bytes
>I read the comments in the code, and was looking for more background
>on why the socket read would come up short. a deceiptful remote client?
Read from a file descriptor is not guaranteed to complete in one step
except when reading from a normal file.
That is just a fact of life of how streams are implement in the unix kernel,
nothing to do with a deceitful client. There are some ioctl's you can make
which affect the this behavior of the fd.
When reading from a handle which is a socket, you need to do something like
the attached. (But maybe you knew that, or maybe perl's "read" already does
this, or maybe CGI.pm is trying to do this and failing. I'm speaking here
as someone with some experience with network programming but not much with
perl or CGI.pm.)
-mda
sub read_on_socket
{
my($handle,$num_bytes) = @_;
my $nleft = $num_bytes;
my $buf;
my $offset = 0;
while($nleft > 0)
{
my $nread = sysread($handle, $buf, $nleft, $offset);
if ($nread < 0) {
die "read($handle,$num_bytes) failed: $!";
}
elsif ($nread == 0) {
die "read($handle,$num_bytes) when EOF: $!";
}
$nleft -= $num_bytes;
$offset += $nread;
}
return $buf;
}