A Perl web server: reading a client's request

S

saridski

Hi there,

I have been traversing the groups looking for an answer to a problem
that i am having. I have written a web server in Perl. I want to be
able to read clients' requests from the beginning of the request (GET /
HTTP/1.0) all the way to the end of the request (after the header if a
GET or after the payload if a POST). Currently, I can read the request
till the end but the browser (and the server) hangs at the end of the
request, as there is no end of request marker.

sub _bind($) {
socket($server, AF_INET, SOCK_STREAM, $self->{_CONFIG}->protocol()) ||
die("socket(): $!");
my $paddr = sockaddr_in($self->{_CONFIG}->port(), INADDR_ANY);
bind($server, $paddr) || die("bind(): $!");
listen($server, SOMAXCONN) || die("listen(): $!");

while(my $clientAddr = accept($client, $server)) {
$client->autoflush(1);
my ($port, $packed_ip) = sockaddr_in($clientAddr);
my $dotted_quad = inet_ntoa($packed_ip);
print("Connection [".localtime(time)."] from [$dotted_quad]\n");
handleclient($client, $server, $dotted_quad);
}
}
....

sub handleclient ($$$) {
my $self = shift();
my $client = shift();
my $server = shift();
my $clientip = shift();

my $line = <$client>;
# GET /index.html HTTP/1.1
$line =~ m/^(\w+)\s+(\S+)(?:\s+(\S+))?\r?$/;
$self->{_METHOD} = $1 || '';
$self->{_RESOURSE} = $2 || '';
$self->{_PROTOCOL} = $3 || '';

my @array = split(/\?/, $self->{_RESOURSE});
@{$self->{_FOLDERS}} = split(/\//, $array[0]);
$self->{_FILE} = pop(@{$self->{_FOLDERS}});
my $seperator = rindex($self->{_FILE}, "\.");
$self->{_FILEBASE} = substr($self->{_FILE}, 0, $seperator);
$self->{_FILEEXTENSION} = substr($self->{_FILE}, $seperator);

my @line = ();
my @temp = ();
%headers = ();
my $buffer = "";
my $count = 0;
my $li = undef;
while (defined(my $data = <$client>)) {
@line = split(/:/, $data);
%headers->{$line[0]} = $line[1];
}

}

The problem is clearly that once the the entire request is read, the
connection between the client's browser and the server hangs. I would
like the server to be as robust as possible and to be as generic as
possible. With this I mean that I want it to be RFC compliant but at
the same time, the server must be able to break the connection with the
client if the request has no end of request marker (of some kind).

I look forward to hearing from you all!
Best,
S.
 
P

Peter J. Holzer

I have been traversing the groups looking for an answer to a problem
that i am having. I have written a web server in Perl. I want to be
able to read clients' requests from the beginning of the request (GET
/ HTTP/1.0) all the way to the end of the request (after the header if
a GET or after the payload if a POST). Currently, I can read the
request till the end but the browser (and the server) hangs at the end
of the request, as there is no end of request marker.

There is. You are just not checking for it. Have a look at the last line
before the connection "hangs" and then read RFC 2616 and/or RFC 1945
again.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top