如何使用 CGI.pm 获取整个请求正文?

发布于 2024-07-22 19:14:04 字数 250 浏览 9 评论 0原文

我正在尝试编写一个 Perl CGI 脚本来处理 XML-RPC 请求,其中 XML 文档作为 HTTP POST 请求的正文发送。

CGI.pm 模块在从 HTTP 请求中提取命名参数方面做得很好,但我不知道如何让它为我提供整个 HTTP 请求正文(即我正在处理的 XML-RPC 请求中的 XML 文档)处理)。

如果不是 CGI.pm,是否有另一个模块能够从请求中解析此信息? 我不想从环境变量中“手动”提取此信息。 谢谢你的帮助。

I'm trying to write a Perl CGI script to handle XML-RPC requests, in which an XML document is sent as the body of an HTTP POST request.

The CGI.pm module does a great job at extracting named params from an HTTP request, but I can't figure out how to make it give me the entire HTTP request body (i.e. the XML document in the XML-RPC request I'm handling).

If not CGI.pm, is there another module that would be able to parse this information out of the request? I'd prefer not to have to extract this information "by hand" from the environment variables. Thanks for any help.

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

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

发布评论

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

评论(3

╰つ倒转 2024-07-29 19:14:06

要处理所有情况,包括 Content-Typemultipart/form-data 时的情况,请在之前读取(并放回)原始数据CGI 确实如此。

use strict;
use warnings;

use IO::Handle;
use IO::Scalar;

STDIN->blocking(1); # ensure to read everything
my $cgi_raw = '';

{ 
  local $/; 
  $cgi_raw = <STDIN>;
  my $s;
  tie  *STDIN, 'IO::Scalar', \$s;
  print STDIN $cgi_raw;
  tied(*STDIN)->setpos(0);
}

use CGI qw /:standard/;
...

To handle all cases, including those when Content-Type is multipart/form-data, read (and put back) the raw data, before CGI does.

use strict;
use warnings;

use IO::Handle;
use IO::Scalar;

STDIN->blocking(1); # ensure to read everything
my $cgi_raw = '';

{ 
  local $/; 
  $cgi_raw = <STDIN>;
  my $s;
  tie  *STDIN, 'IO::Scalar', \$s;
  print STDIN $cgi_raw;
  tied(*STDIN)->setpos(0);
}

use CGI qw /:standard/;
...
近箐 2024-07-29 19:14:05

是的,可以使用 POSTDATA,但只有在请求内容类型未设置为“multipart/form-data”时才有效。

如果设置为“multipart/form-data”,CGI.pm 会进行自己的内容处理,并且 POSTDATA 不会初始化。

因此,其他选项包括 $cgi->query_string 和/或 $cgi->Dump

$cgi->query_string 以 GET 格式返回 POST 的内容(param=value&...),并且似乎没有方法简单地获取客户端传入的 POST STDIN 的内容。

因此,要获取 POST 请求的标准输入的实际内容,如果您可以选择修改 CGI.pm,则可以修改第 620 行左右,以将 @lines 的内容保存在变量中的某处,如:

$self->{standard_input} = join '', @lines;

然后通过$cgi->{standard_input}访问。

Right, one could use POSTDATA, but that only works if the request Content-Type has not been set to 'multipart/form-data'.

If it is set to 'multipart/form-data', CGI.pm does its own content processing and POSTDATA is not initialized.

So, other options include $cgi->query_string and/or $cgi->Dump.

The $cgi->query_string returns the contents of the POST in a GET format (param=value&...), and there doesn't seem to be a way to simply get the contents of the POST STDIN as they were passed in by the client.

So to get the actual content of the standard input of a POST request, if modifying CGI.pm is an option for you, you could modify around line 620 to save the content of @lines somewhere in a variable, such as:

$self->{standard_input} = join '', @lines;

And then access it through $cgi->{standard_input}.

離殇 2024-07-29 19:14:05

您可以使用特殊参数名称 POSTDATA 获取原始 POST 数据。

my $q = CGI->new;
my $xml = $q->param( 'POSTDATA' );

或者,您可以直接读取 STDIN,而不是使用 CGI.pm,但这样您就会丢失 CGI.pm 所做的所有其他有用的内容。

POSTDATA 技巧记录在优秀的 CGI.pm 文档中 在这里

You can get the raw POST data by using the special parameter name POSTDATA.

my $q = CGI->new;
my $xml = $q->param( 'POSTDATA' );

Alternatively, you could read STDIN directly instead of using CGI.pm, but then you lose all the other useful stuff that CGI.pm does.

The POSTDATA trick is documented in the excellent CGI.pm docs here.

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