perl - HTTP::Daemon:如何在 HTTP-Header 中为服务器设置自定义值?
问题描述
我正在使用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);
}
我尝试覆盖服务器的 HTTP 标头条目。但是,我得到的只是第二个条目,其值为“MyServer”。
知道如何覆盖原始值“libwww-perl-daemon”吗?
有一种product_tokens
获取值的方法,但无法设置值。
解决方案
文档说你应该创建一个子类:
=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( ... );
推荐阅读
- swift - 目标“幅度”的标题布局无效:伞形标题位于
- python - 使用 Django ORM 进行多重连接
- sql - 分配计数到最大值
- flutter - 暂停或暂停时 Flutter 应用程序上的 Dart 计时器
- javascript - Firebase 存储下载 url 未接收/不工作
- three.js - Three.js 改变顶点位置最高效的方法是什么?
- python - python:创建校验位函数
- sql-server - 如何在 SQL Graph 表中创建递归边?
- laravel - Laravel 的工作失败了,正在开发中
- python-3.x - GitHub Actions:如何在 Docker 操作中使用来自 GitHub 注册表的 Docker 映像?