Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sockets and HTTP

Status
Not open for further replies.

lones

Programmer
Apr 5, 2006
4
DK
Hi there,

I have written a small programme that simulates a web server. It reads the first line of incoming requests, which is generally "GET / HTTP1.1" and delivers the requested file if it exists. I wish to modify my programme so that it can read the entire HTTP header and any POST data that may be present, but
have been running into problems.

Can anyone give me a little help to help me with my project?
Thanks in advance.
 
Without seeing it, the answer would be no ;-)

If you post what you're having a problem with we might be able to help

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
if you're toy around with writing a web server then its a different thing but if this HTTP server is a part of a bigger project then you should try using HTTP::Daemon, a standard perl Module distributed with lib (LWP). Plus you'll have the same amount of control as writing your own.

hth,
san

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Hi there,

Here is the code. I will comment it to clarify what I want to accomplish:

sub service::_handleclienten($$$$) {
my $self = shift();
my $client = shift();
my $server = shift();
my $clientip = shift();

# I want $request to contain the entire HTTP header
# GET / HTTP/1.1
# Content-Type: html/gif
# Host: # ...and so on....
# The following line only grabs the first line
# I thought about doing a:
# while (header) { add to hash...; }
my $request = <$client>;
my @request = split(/ /, $request);
my $file = $self->{_ROOT}.$request[1];

if (!(-e $file)) {
$file = $self->{_ROOT}.$self->{_DEFAULTDOCUMENT};
} elsif ($request[1] eq "/") {
$file = $self->{_ROOT}.$self->{_DEFAULTDOCUMENT};
}

open(F_OUT, "<", $file);
while(<F_OUT>) { print($client $_); }
close(F_OUT);

shutdown($client, 2);
}


I really appreciate your help!
 
You're reading from $client as a filehandle. In my experience, unless you flag another Perl global variable, returning a filehandle into a $scalar returns only the first line. Put it into an @array

Code:
my @message = <$client>;
chomp @message; # remove newline characters \n
 
Hi,

When I do this, my browser just hangs, as my programme doesn't know when to stop reading from the handle (I presume)
 
The ideal situation would be to read from the file handle <$client> until we reach a blank line. Then we are finished the header. If on the next line we find more data, then it is the POST payload.

Thnx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top