如何在 CGI 脚本中访问请求的 HTTP 标头? [英] How do I access the HTTP Header of request in a CGI script?

查看:40
本文介绍了如何在 CGI 脚本中访问请求的 HTTP 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在小型应用程序和测试代码中使用过一些 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 -H "HeaderAttribute: 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:
";
for my $header ( keys %headers ) {
    print "$header: $headers{$header}
";
}

试试看;以上给了我:

$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆