3

我正在使用HTTP::DaemonHTTP 服务器。

use strict;
use warnings;
use HTTP::Daemon;

my $d = HTTP::Daemon->new (Listen => 1, LocalPort => 8080, ReuseAddr => 1, Blocking => 0) or die "error daemon: " . $!;
while (1)
{
    my $c = $d->accept ();
    if (defined ($c))
    {
        my $req = $c->get_request ();
        my $res = HTTP::Response->new (200);
        $res->header ("Server" => "MyServer");   # try to overwrite the internel builtin value
        $res->content ("OK");
        
        $c->send_response ($res);
        $c->autoflush ();
        undef ($c);
    }
    sleep (1);
}

我尝试覆盖服务器的 HTTP 标头条目。但是,我得到的只是第二个条目,其值为“MyServer”。

知道如何覆盖原始值“libwww-perl-daemon”吗?

有一种product_tokens获取值的方法,但无法设置值。

4

1 回答 1

3

文档说你应该创建一个子类:

=item $d->product_tokens

返回此服务器用于标识自身的名称。这是与 C 响应标头一起发送的字符串。拥有此方法的主要原因是子类如果想使用另一个产品名称可以覆盖它。

默认值为字符串“libwww-perl-daemon/#.##”,其中“#.##”替换为该模块的版本号。

因此,您编写了一个小子类,然后使用您的子类来制作对象:

use v5.12;
package Local::HTTP::Daemon {
    use parent qw(HTTP::Daemon);

    sub product_tokens { 
        ... # whatever you want
        }
    }


my $d = Local::HTTP::Daemon->new( ... );
于 2021-01-19T08:01:53.127 回答