[英]How to get methods from HTTP::Daemon
How can I find out the $code
and $mess
in HTTP::Daemon module? 如何在HTTP :: Daemon模块中找到$code
和$mess
? In cpan the usage is as 在cpan中的用法是
$c->send_status_line( $code, $mess, $proto )
but I dont know where/how to get $code
, $mess
from. 但是我不知道从哪里/如何获得$code
, $mess
。
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? 就像send_error($code)
用作send_error(RC_FORBIDDEN)
,我是从某人的在线代码中找到的,他从哪里获得RC_FORBIDDEN
? Have been playing with the following code. 一直在玩以下代码。 Sorry for the formatting and many thanks to @choroba for formatting it for me. 很抱歉格式化,非常感谢@choroba为我格式化。
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;
}
}
The documentation also states 该文档还指出
If
$code
is omitted 200 is assumed. 如果省略$code
则假定为200。 If$mess
is omitted, then a message corresponding to$code
is inserted. 如果省略$mess
,则插入与$code
对应的消息。 If$proto
is missing the content of the$HTTP::Daemon::PROTO
variable is used. 如果缺少$proto
则使用$HTTP::Daemon::PROTO
变量的内容。
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. 否则,只需为$code
使用任何可能的HTTP状态 $code
,或者不指定$mess
获取该代码的默认消息,或者使用您喜欢的任何消息。
RC_FORBIDEN is exported from HTTP::Status . RC_FORBIDEN是从HTTP :: Status导出的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.