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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read body of http request

Status
Not open for further replies.

shezam

Programmer
Jan 30, 2009
11
IE
I have a test webserver, i want to write a script to check what headers are being passed in the request and send back a 302 if all headers are present, but i also want to get the body of what was sent in the request and write it to a file - how do i do this? My code for checking headers;

#!/usr/bin/perl

$user_agent = $ENV{"HTTP_USER_AGENT"};
$accept = $ENV{"HTTP_ACCEPT"};
$accept_language = $ENV{"HTTP_ACCEPT_LANGUAGE"};
$content_disposition = $ENV{"HTTP_CONTENT_DISPOSITION"};
$content_type = $ENV{"HTTP_CONTENT_TYPE"};
$content_length = $ENV{"HTTP_CONTENT_LENGTH"};
$referer = $ENV{"HTTP_REFERER"};
%cookie = $ENV{"HTTP_COOKIE"};

if ($accept && $user_agent && $accept_language && $content_disposition &&
$content_type && $content_length && $referer && $cookie)
{
print "status: 302\n";
print "location: print "Content-type: text/plain\n";
print "\n";
print <<"EOM";
Go read the news
EOM
}
else
{
print "Status: 404 Not Found\n\n"

}

Help much appreciated. Thanks.
 
Hi

There is a "body" only if the method was POST. In that case you will have the "body" available on stdin.
Code:
$method=$ENV{'REQUEST_METHOD'};
undef $/;
$body=$method eq 'POST'?<>:'';
Note that would be better to use the CGI module.

Feherke.
 
I have done the following,the logic seems right but it doesn't work;

$content_length = $ENV{"HTTP_CONTENT_LENGTH"};
$method = $ENV{"REQUEST_METHOD"};
if ($method eq "POST")
{
read(STDIN, $buffer, $content_length);
}

open FH, "> /test/doc.txt";
print FH $buffer;
close FH;
}


The file doc.txt is not even being created. Anyone any ideas? Thanks.

 
Code:
open FH, "> /test/doc.txt" or die "can't open doc.txt: $!";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top