Reputation: 71
How can I find out the $code
and $mess
in HTTP::Daemon module? In cpan the usage is as
$c->send_status_line( $code, $mess, $proto )
but I dont know where/how to get $code
, $mess
from.
Like, send_error($code)
is used as send_error(RC_FORBIDDEN)
which I found from someone's code online, where did he get RC_FORBIDDEN
from?
Have been playing with the following code. Sorry for the formatting and many thanks to @choroba for formatting it for me.
use warnings;
use strict;
use HTTP::Daemon;
use HTTP::Status;
use LWP;
my $daemon = HTTP::Daemon->new or die;
my $d = HTTP::Daemon->new(
LocalAddr => '0.0.0.0',
LocalPort => '5000',
);
printf ("\n\n URL of webserver is %s, show this script with %stest\n",
$d->url, $d->url);
while (my $client_connection = $d->accept)
{
new_connection($client_connection);
}
sub new_connection
{
my $client_connection = shift;
printf "new connection\n";
while (my $request = $client_connection->get_request)
{
if (my $pid = fork)
{
print "Child created : $pid\n";
}
elsif (!defined $pid)
{
die "Cannot fork $!\n";
}
else
{
my $address_of_client = $client_connection->peerhost();
my $port_of_client = $client_connection->peerport();
print "Connection from client $address_of_client on port
$port_of_client\n";
print " request\n";
if ($request->method eq 'GET' and $request->uri->path
eq "/test")
{
$client_connection->send_file_response(RC_OK);
#$client_connection->send_status_line(200);
#print "OK ";
#$client_connection->send_file_response($0);
}
else
{
$client_connection->send_error(RC_NOT_FOUND);
}
}
$client_connection->close;
}
}
Upvotes: 0
Views: 197
Reputation: 242333
The documentation also states
If
$code
is omitted 200 is assumed. If$mess
is omitted, then a message corresponding to$code
is inserted. If$proto
is missing the content of the$HTTP::Daemon::PROTO
variable is used.
So, you don't have to specify the arguments at all. Otherwise, just use any of the possible HTTP status codes for $code
, and either don't specify the $mess
to get the default message for the code, or use any message you like.
RC_FORBIDEN is exported from HTTP::Status.
Upvotes: 1