如果要转储 FastCGI 记录,可以使用Net::FastCGI 。Net::FastCGI 非常低级,需要了解 FastCGI协议。
下面的代码显示了一个简单的客户端,它连接到作为第一个参数给出的 FastCGI 应用程序,并输出应用程序发送的记录的字符串表示形式。
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket qw[];
use Net::FastCGI::Constant qw[:type :role];
use Net::FastCGI::IO qw[read_record write_record write_stream];
use Net::FastCGI::Protocol qw[build_params dump_record build_begin_request_body];
use warnings FATAL => 'Net::FastCGI::IO';
use constant TRUE => !!1;
my $command = shift @ARGV;
my $socket = IO::Socket::INET->new(Proto => 'tcp', Listen => 5)
or die qq/Could not create a listener socket: '$!'/;
my $host = $socket->sockhost;
my $port = $socket->sockport;
defined(my $pid = fork())
or die qq/Could not fork(): '$!'/;
if (!$pid) {
close STDIN;
open(STDIN, '+>&', $socket)
or die qq/Could not dup socket to STDIN: '$!'/;
exec { $command } $command
or die qq/Could not exec '$command': '$!'/;
}
close $socket;
$socket = IO::Socket::INET->new(Proto => 'tcp', PeerHost => $host, PeerPort => $port)
or die qq/Could not connect to '$host:$port': '$@'/;
write_record($socket, FCGI_BEGIN_REQUEST, 1, build_begin_request_body(FCGI_RESPONDER, 0));
write_stream($socket, FCGI_PARAMS, 1, build_params({}), TRUE);
write_stream($socket, FCGI_STDIN, 1, '', TRUE);
while () {
my ($type, $request_id, $content) = read_record($socket)
or exit;
warn dump_record($type, $request_id, $content), "\n";
last if $type == FCGI_END_REQUEST;
}
示例输出:
fcgi-echo.pl
是您在问题中提供的示例应用程序,并且fcgi-dump.pl
是上面的代码。
$ perl fcgi-dump.pl ./fcgi-echo.pl
{FCGI_STDOUT, 1, "Content-type: text/html\r\n\r\n1"}
{FCGI_STDOUT, 1, ""}
{FCGI_END_REQUEST, 1, {0, FCGI_REQUEST_COMPLETE}}