如何在 CGI 脚本中访问请求的 HTTP 标头?

发布于 2024-09-29 12:51:50 字数 262 浏览 0 评论 0原文

我在小型应用程序和测试代码中使用过一点 Perl,但我对网络和 CGI​​ 还很陌生。

我了解如何制作请求的标头(使用 CGI.pm 并打印 header() 函数的结果),但无法找到有关如何访问发送到我的 CGI 脚本的标头的任何信息。有人能指出我正确的方向吗?

这可能来自这样的请求:

curl http://127.0.0.1:80/cgi-bin/headers.cgi -H "HeaderAttribute: value"

I've used Perl a bit for small applications and test code, but I'm new to networking and 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"

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

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

发布评论

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

评论(3

茶色山野 2024-10-06 12:51:51

CGI 模块有一个 http() 函数,您可以使用它来实现此目的:

#!/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";
}

尝试一下;以上给了我:

$ 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

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
牛↙奶布丁 2024-10-06 12:51:51

除了 CGI.pm http() 方法可以从环境变量中获取 HTTP headers 信息。

因此,如果您使用类似 CGI::Minimal ,它没有http方法。你可以这样做:

  my $header = 'HTTP_X_REQUESTED_WITH';

  if (exists $ENV{$header} && lc $ENV{$header} eq 'xmlhttprequest') {
   _do_some_ajaxian_stuff();
  }

In addition to the CGI.pm http() method you can get HTTP headers information from the environment variables.

So in case you are using something like CGI::Minimal, which doesn't have the http method. you can do something like:

  my $header = 'HTTP_X_REQUESTED_WITH';

  if (exists $ENV{$header} && lc $ENV{$header} eq 'xmlhttprequest') {
   _do_some_ajaxian_stuff();
  }
鱼窥荷 2024-10-06 12:51:51

它们作为环境变量提供,例如

HTTP_HEADERATTRIBUTE=value

您可能需要做一些事情来配置您的 Web 服务器以提供这样的变量。

They're supplied as environment variables, such as

HTTP_HEADERATTRIBUTE=value

You may have to do something to configure your web server to supply such a variable, though.

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