简体   繁体   English

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

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

I am using HTTP::Daemon for a HTTP-server.我正在使用HTTP::Daemon作为 HTTP 服务器。

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);
}

I try to overwrite the HTTP-header entry for Server.我尝试覆盖服务器的 HTTP 标头条目。 But, all I get is a 2nd entry with my value "MyServer".但是,我得到的只是第二个条目,其值为“MyServer”。

Any idea how to overwrite the original value "libwww-perl-daemon"?知道如何覆盖原始值“libwww-perl-daemon”吗?

There is a method product_tokens for getting the value, but it is not able to set the value.有一种方法product_tokens用于获取值,但它无法设置值。

The docs say you should make a subclass:文档说你应该创建一个子类:

=item $d->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.这是与 C 响应 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.默认值为字符串“libwww-perl-daemon/#.##”,其中“#.##”替换为该模块的版本号。

So, you write a small subclass and then use your subclass to make the object:因此,您编写了一个小子类,然后使用您的子类制作 object:

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

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM