简体   繁体   中英

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

I am using HTTP::Daemon for a 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);
}

I try to overwrite the HTTP-header entry for Server. But, all I get is a 2nd entry with my value "MyServer".

Any idea how to overwrite the original value "libwww-perl-daemon"?

There is a method product_tokens for getting the value, but it is not able to set the value.

The docs say you should make a subclass:

=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.

So, you write a small subclass and then use your subclass to make the 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( ... );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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