flor~ has asked for the wisdom of the Perl Monks concerning the following question:
I have a simple webserver that simply prints to the client, line by line, the file requested by the client.
On the other side, I have the client. The problem lies in the response that the client receives. If the requested file on the server side did not end with a \n character, the client will receive a portion of the file, but not the last line! (It follows that if the file did end with a \n, somewhere along the way, that newline will be eaten up; the client will receive the entire file but minus that last \n)
My question is, is there some way to indicate that the content is ended, without having the webserver to print out a newline regardless? I am building an automated test environment that must eventually match the response content with the original content. Among others, this is one of the reasons I'd not prefer to have extra \n's lying around. I've enclosed the corresponding client and server code are in readmore tags.
The client:
my $remote = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => $ADDRESS, PeerPort => $PORT ) or die "cannot connect to server"; $remote->autoflush(1); open(OUT, ">out.txt"); print $remote "GET /theFile.txt HTTP/1.1\r\n"; print $remote "Host: $ADDRESS:$PORT\r\n"; print $remote "Connection: close\r\n"; print $remote "\r\n"; while( <$remote> ) { print OUT $_; } close $remote;
The webserver:
$| = 1; $server = IO::Socket::INET->new( Proto => 'tcp', LocalAddr => $ADDRESS, LocalPort => $SPORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; while ($client = $server->accept()) { $client->autoflush(1); my $request = <$client>; if ($request =~ m|^GET /(.+) HTTP/1.[0,1]|) { $file = $1; $size = -s $file; $_ = $file; ($ext) = /.+\.(\w+)/; $type = MimeTypes::GetType($ext); # some function to get mime +type print $client "HTTP/1.0 200 OK\r\n"; print $client "Connection: Keep-Alive\r\n"; print $client "Content-Type: $type\r\n"; print $client "Content-Length: $size\r\n"; print $client "\r\n"; open(FILE, $file); while(<FILE>) { print $client $_ ; } close(FILE); } close $client; }
Any guidance would be muchos appreciated!!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: webserver -- incomplete content prob
by iburrell (Chaplain) on Apr 08, 2004 at 19:56 UTC | |
by flor~ (Initiate) on Apr 08, 2004 at 20:21 UTC |