如何在CGI脚本中访问请求的HTTP标头? [英] How do I access the HTTP Header of request in a CGI script?
问题描述
我已经将Perl用于小型应用程序和测试代码,但我是网络和CGI的新手。
I've used Perl a bit for small applications and test code, but I'm new to networking and CGI.
我得到了如何制作标题请求(使用CGI.pm并打印header()函数的结果),但无法找到有关如何访问发送到我的CGI脚本的标头的任何信息。有人能指出我正确的方向吗?
I get how to make the header of a request (using CGI.pm and printing the results of the header() function), but haven't been able to find any info on how to access the headers being sent to my CGI script. Could someone point me in the right direction?
这可能来自这样的请求:
This could be from a request like this:
curl http://127.0.0.1:80/cgi-bin/headers.cgi -HHeaderAttribute:value
推荐答案
CGI模块有一个 http()
函数可用于此目的:
The CGI module has a http()
function you can use to that purpose:
#!/usr/bin/perl --
use strict;
use warnings;
use CGI;
my $q = CGI->new;
my %headers = map { $_ => $q->http($_) } $q->http();
print $q->header('text/plain');
print "Got the following headers:\n";
for my $header ( keys %headers ) {
print "$header: $headers{$header}\n";
}
尝试一下;上面给了我:
Try it out; the above gives me:
$ curl http://localhost/test.cgi -H "HeaderAttribute: value"
Got the following headers:
HTTP_HEADERATTRIBUTE: value
HTTP_ACCEPT: */*
HTTP_HOST: localhost
HTTP_USER_AGENT: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
这篇关于如何在CGI脚本中访问请求的HTTP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!