HTTP::Daemon: 如何在HTTP-Header中为服务器设置自定义值?

HTTP::Daemon: How to set a custom value for Server in HTTP-Header?

我正在使用 HTTP::Daemon 作为 HTTP-server。

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-header 条目。 但是,我得到的只是第二个条目,我的值为“MyServer”。

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

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

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

=item $d->product_tokens

Returns the name that this server will use to identify itself. This is the string that is sent with the C response header. The main reason to have this method is that subclasses can override it if they want to use another product name.

The default is the string "libwww-perl-daemon/#.##" where "#.##" is replaced with the version number of this module.

所以,你写了一个小的子类,然后用你的子类来制作对象:

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

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


my $d = Local::HTTP::Daemon->new( ... );