如何使用Net :: FastCGI在Perl中获取FCGI进程的requestId?

时间:2013-09-11 00:43:07

标签: perl fastcgi

我有一个应用程序调用FCGI响应程序来处理某些任务,我需要查找FCGI响应程序是否接收并返回相同的请求ID。

FCGI响应者是用Perl编写的,使用FCGI module

根据FastCGI specification,我可以通过查找FastCGI记录来查找信息。

我发现Net::FastCGI库可能适合解决此问题,但我不确定如何使用该库。

如果我的fcgi脚本如下所示,我如何使用Net :: FastCGI转储FastCGI记录的内容?

use FCGI;

my $count = 0;
my $request = FCGI::Request();

while($request->Accept() >= 0) {
    print("Content-type: text/html\r\n\r\n", ++$count);
}

2 个答案:

答案 0 :(得分:1)

如果要转储FastCGI记录,可以使用Net::FastCGI。 Net :: FastCGI的级别非常低,需要了解FastCGI protocol

以下代码显示了一个简单的客户端,它连接到作为第一个参数给出的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}}

答案 1 :(得分:0)

你不会。当你已经在使用FCGI时,使用Net :: FastCGI是没有意义的。如果需要,请求ID可在调用$request->{id}后在$request->Accept中提供。目前尚不清楚“收到并返回相同的请求ID”是什么意思。

相关问题