将 Vanilla Perl CGI 中的请求标头克隆到 LWP UserAgent
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与 HTTP::Headers 使用的内容相比,问题似乎与传入 http 标头的命名有关。
传入参数均具有
HTTP_
前缀,其中 HTTP::Headers 不使用该命名约定(这是有道理的)。另外,看起来(快速阅读代码)HTTP::Headers 将“-”转换为“_”以供自己使用是正确的。我建议将您的
map
更改为以下内容,以删除前缀:这是我使用的调试脚本:
希望有帮助
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:Here is the debugging script I used:
Hope that Helps