如何使用FCGI获取cookie

时间:2014-01-27 19:21:20

标签: perl nginx fastcgi

我有以下Perl FastCGI脚本:

#!/usr/bin/perl
use warnings;
use strict;
use FCGI;
use POSIX;
use FindBin qw($Bin);
use FCGI::ProcManager qw(pm_manage pm_pre_dispatch pm_post_dispatch);   

use Data::Dumper;


&demonize();

my $socket = FCGI::OpenSocket(":9000", 5); 

my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket, FCGI::FAIL_ACCEPT_ON_INTR);

pm_manage(n_processes => 2);

chdir $Bin or die "Error::$!\n";

while($request->Accept() >= 0 ) {
     pm_pre_dispatch();

         print "\%ENV => \n ", Dumper($env), "\n\n";

         pm_post_dispatch();
}

.....

我使用以下配置在nginx上部署了我的应用程序:

server {
    listen 80;
    server_name eventise.my;

    root /home/tigran/Documents/Development/perl/eventise/www;

    location / {
        index   index.html;
   }    

    location ~* \.(gif|jpg|png|js|css|html)$ {
        expires 30d;
   }

    location /perl/ {
        fastcgi_pass   localhost:9000;  

        fastcgi_param SCRIPT_FILENAME /home/tigran/Documents/Development/perl/eventise/fcgi-bin$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
        fastcgi_param REQUEST_METHOD  $request_method;
        fastcgi_param CONTENT_TYPE    $content_type;
        fastcgi_param CONTENT_LENGTH  $content_length;  
        fastcgi_param REQUEST_URI     $request_uri;
        fastcgi_param DOCUMENT_URI    $document_uri;
        fastcgi_param REMOTE_ADDR     $remote_addr;
        fastcgi_param REMOTE_PORT     $remote_port;
        fastcgi_param SERVER_ADDR     $server_addr;
        fastcgi_param SERVER_PORT     $server_port;
        fastcgi_param SERVER_NAME     $server_name; 
        fastcgi_pass_header Cookie;     
    }
}

当我使用Chrome的高级REST客户端向我的应用发送带有Cookie的请求时,我在%ENV中看不到该Cookie。相反,我看到了:

%ENV => 
 $VAR1 = {
          'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
          'REQUEST_METHOD' => 'GET',
          'SCRIPT_FILENAME' => '/home/tigran/Documents/Development/perl/eventise/fcgi-bin/perl/',
          'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36',
          'REMOTE_PORT' => '56832',
          'QUERY_STRING' => '',
          'HTTP_CACHE_CONTROL' => 'max-age=0',
          'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
          'FCGI_ROLE' => 'RESPONDER',
          'CONTENT_TYPE' => '',
          'DOCUMENT_URI' => '/perl/',
          'SERVER_NAME' => 'eventise.my',
          'HTTP_ACCEPT_ENCODING' => 'gzip,deflate,sdch',
          'HTTP_CONNECTION' => 'keep-alive',
          'CONTENT_LENGTH' => '',
          'SERVER_PORT' => '80',
          'REMOTE_ADDR' => '127.0.0.1',
          'REQUEST_URI' => '/perl/',
          'SERVER_ADDR' => '127.0.1.1',
          'HTTP_HOST' => 'eventise.my'
        };

我哪里出错了?

1 个答案:

答案 0 :(得分:1)

它看起来像Chrome高级REST客户端does not support cookies

  

但由于规范,无法设置“Cookie”标头   不允许它。看到   http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method   更多信息。

更多详情: http://blog.getpostman.com/index.php/2013/09/18/legacy-app-update-cookies-and-devtools-for-v0-9-x/

您可以使用较旧版本的Postman发送/编辑Cookie,或者您可以使用邮递员代理:

  

一种解决方法是使用Postman Proxy设置cookie标头   使用从浏览器获得的cookie值。

     

我认为这很笨重,可以改进。如果API你   正在测试使用cookie,我建议坚持保持v0.8.x.   安装。由于v0.8.x和v0.9.x可作为单独的应用程序使用,因此您   可以并行使用它们以充分利用Postman。