I'm trying to write a simple-minded WWW server in perl, which simply
echoes back all the protocol that the client sends. The purpose is
to display the MIME headers and to see exactly what gets sent when a form
or image-map is invoked.
Unfortunately, my perl script is* whenever the HTTP request type
is "POST" and the Content-Length: MIME header is present. I'm*
on the line
read(NS, $line, $length); # this hangs, why?
It hangs even if I change the "$length" to 1 !
But it doesn't hang if I comment it out and uncomment the following line
# $line = <NS>; # this doesn't hang
I'm misunderstanding either perl or the HTTP protocol. Which is it?
To test this, run the perl script (appended below) on the test form
(also appended below). Invoke the button marked "prostrate".
-------------------
#!/usr/bin/perl
require 'sys/socket.ph';
$sockaddr = 'S n a4 x8';
#serve TCP port 7999 unless a different one is specified on command line
$proto = (getprotobyname('tcp'))[2];
$port = $ARGV[0] || 7999;
$port = pack($sockaddr, &AF_INET, $port, "\0\0\0\0");
socket(S, &AF_INET, &SOCK_STREAM, $proto) || die "Can't open socket: $!";
bind(S, $port) || die "Can't bind socket: $!";
listen (S, 5);
while(1) {
accept(NS, S);
select (NS);
$| = 1;
# send standard HTTP "OK" reply
print "HTTP/1.0 200 OK\r\n";
print "Content-type: text/plain\r\n\r\n";
# read and echo HTTP command request; save request-type away for later use
($reqtype) = split(/ /, $_ = <NS>);
print;
# read and echo MIME headers; watch for Content-Length: header
while (<NS>) {
print;
/^Content-Length:\s*(.*)\r$/i && ($length = $1);
last if /^\r$/; # blank line ends MIME headers
}
if ($length) {
# Content-Length: header found; read the specified number of bytes
read(NS, $line, $length); # this hangs, why?
# $line = <NS>; # this doesn't hang
print "$line\r\n";
}
elsif (($reqtype eq "PUT") || ($reqtype eq "POST")) {
# no Content-Length: header, so look for terminating line
while (<NS>) {
last if /^.\r$/; # line containing only a period terminates input
print;
}
}
close NS;
}
-------------------------
<html>
<head>
<title>Form Test Page</title>
</head>
<body>
<h1>Form test page</h1>
<form action="http://localhost:7999/form/action" method="POST"><br>
A text field follows.
<input type=text name=teggst value=the-text size=30><br>
A password field follows.
<input type=password name=passwd value=the-password size=25><br>
A checkbox field follows.
<input type=checkbox name=czechbox value=prague><br>
Two buttons follow.
<input type=submit value=prostrate> <input type=reset value="forget it!">
</body>
</html>
-----------------
--
Web: http://www.veryComputer.com/:8001/people/rnewman/home.html
(I speak only for myself, not for any part of MIT.)