将 Vanilla Perl CGI 中的请求标头克隆到 LWP UserAgent

发布于 2024-09-05 22:39:25 字数 615 浏览 5 评论 0原文

我有一个 Perl CGI 应用程序,我想获取用户请求标头,并将其转换为 LWP::UserAgent get 请求。基本上,目标是复制传入的用户标头并使用它们发出单独的请求。

我尝试自己创建标头,但是当我尝试显示 CGI 标头,然后显示我的克隆 UserAgent 标头时,它们并不完全相同。这就是我得到的:

my $cgi = new CGI;
my %headers = map { $_ => $cgi->http($_) } $cgi->http;
my $req_headers = HTTP::Headers->new( %headers );
my $ua = LWP::UserAgent->new( default_headers => $req_headers );
print Dumper $ua->default_headers;

基本上, %headers 和 $ua->default_headers 并不相同。 $ua->default_headers 有一个将自身标识为 perl 脚本的代理。我可以手动设置 $ua->agent("") 但还有其他缺陷,并且标头仍然不相同。

做我想做的事的最好方法是什么?必须有一个更简单的解决方案......

I have a perl CGI application that I want to take the users request headers, and turn those around into an LWP::UserAgent get request. Basically the goal is to replicate the incoming users headers and use those to make a separate request.

I've tried to create the headers myself but when I attempt to display the CGI headers and then my clone UserAgent headers, they aren't exactly the same. Here's what I got:

my $cgi = new CGI;
my %headers = map { $_ => $cgi->http($_) } $cgi->http;
my $req_headers = HTTP::Headers->new( %headers );
my $ua = LWP::UserAgent->new( default_headers => $req_headers );
print Dumper $ua->default_headers;

Basically, %headers and $ua->default_headers are not identical. $ua->default_headers has an agent that identifies itself as a perl script. I can manually set $ua->agent("") but there are other imperfections and the headers still aren't identical.

What's the best way to do what I want? There's got to be an easier solution...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-09-12 22:39:25

与 HTTP::Headers 使用的内容相比,问题似乎与传入 http 标头的命名有关。

传入参数均具有 HTTP_ 前缀,其中 HTTP::Headers 不使用该命名约定(这是有道理的)。另外,看起来(快速阅读代码)HTTP::Headers 将“-”转换为“_”以供自己使用是正确的。

我建议将您的 map 更改为以下内容,以删除前缀:

# remove leading HTTP_ from keys, note: this assumes all keys have pattern
# HTTP_*
%headers = map { ( /^HTTP_(.*?)$/ ) => $cgi->http($_) } $cgi->http;

这是我使用的调试脚本:

my $cgi = CGI->new;
my %headers = map { $_ => $cgi->http($_) } $cgi->http;
my $req_headers = HTTP::Headers->new( %headers );
my $ua = LWP::UserAgent->new( default_headers => $req_headers );

print "Content-type: text/plain\n\n";
print Dumper($ua->default_headers);
print Dumper( \%headers );

# remove HTTP_ from $_
%headers = map { ( /^HTTP_(.*?)$/ ) => $cgi->http($_) } $cgi->http;
$req_headers = HTTP::Headers->new( %headers );
$ua = LWP::UserAgent->new( default_headers => $req_headers );

print "headers part deux:\n";
print Dumper( $ua );

希望有帮助

It looks like the problem has to do with naming of incoming http headers compared to what HTTP::Headers uses.

The incoming parameters all have HTTP_ prefix in them where HTTP::Headers doesn't use that naming convention (which makes sense). Plus it looks like (a quick read in the code) that HTTP::Headers does the right thing in converting '-' into '_' for its own use.

I would recommended changing your map to following that removes the prefix:

# remove leading HTTP_ from keys, note: this assumes all keys have pattern
# HTTP_*
%headers = map { ( /^HTTP_(.*?)$/ ) => $cgi->http($_) } $cgi->http;

Here is the debugging script I used:

my $cgi = CGI->new;
my %headers = map { $_ => $cgi->http($_) } $cgi->http;
my $req_headers = HTTP::Headers->new( %headers );
my $ua = LWP::UserAgent->new( default_headers => $req_headers );

print "Content-type: text/plain\n\n";
print Dumper($ua->default_headers);
print Dumper( \%headers );

# remove HTTP_ from $_
%headers = map { ( /^HTTP_(.*?)$/ ) => $cgi->http($_) } $cgi->http;
$req_headers = HTTP::Headers->new( %headers );
$ua = LWP::UserAgent->new( default_headers => $req_headers );

print "headers part deux:\n";
print Dumper( $ua );

Hope that Helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文