繁体   English   中英

Set-Cookie然后重定向在perl CGI下工作,但不在mod_perl下

[英]Set-Cookie then redirect working under perl CGI, but not mod_perl

设置cookie,然后在mod_perl下重定向时遇到了一个有趣的问题。 到目前为止,我一直使用常规的cgi环境,并且设置cookie /重定向从未成为问题。 一切都按预期进行。 但是,当我打开mod_perl时,我得到一个状态200和一个带有重定向URL的html正文。 即使我在重定向之前将其打印,cookie始终始终放置在标题之后和文档正文之前。 我将脚本修整为准,以便您了解我的意思:

#!/usr/local/bin/perl

use strict;
use warnings;

use CGI;

my $cgi = new CGI;

my $cookie = CGI::cookie(
  '-name'     => 'joe',
  '-value'    => 'fred',
  '-path'     => '/cgi-bin',
  '-httponly' => 1,
);
print "Set-Cookie: $cookie\n";

print $cgi->redirect(-uri => 'http://example.com/cgi-bin/joe.cgi', -status => 303);

当我在常规CGI下使用curl测试它时,我得到了(为了简洁起见,域名被替换并被剪掉):

< HTTP/1.1 303 See Other
< Date: Tue, 23 Oct 2012 16:26:55 GMT
< Server: Apache/2.2.22 (Ubuntu)
< Set-Cookie: joe=fred; path=/cgi-bin; HttpOnly
< Location: http://example.com/cgi-bin/joe.cgi
< Content-Length: 0

...这是我的期望。 当我在mod_perl下测试时,我得到:

< HTTP/1.1 200 OK
< Date: Tue, 23 Oct 2012 16:26:38 GMT
< Server: Apache/2.2.22 (Ubuntu)
< Location: http://example.com/cgi-bin/joe.cgi
< Transfer-Encoding: chunked
<
Set-Cookie: joe=fred; path=/cgi-bin; HttpOnly
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>200 OK</title>
</head><body>
<h1>OK</h1>
<p>The answer to your request is located <a href="http://example.com/cgi-bin/joe.cgi">here</a>.</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at example.com Port 80</address>
</body></html>

我在日志中没有收到任何警告。 知道mod_perl为什么决定以这种奇怪的方式处理此重定向吗?

实际上为我解决的所有问题都在使用:

my $r = Apache2::RequestUtil->request;
$r->err_headers_out->add('Set-Cookie' => $cookie);

...用于我的Cookie处理。 这样可以确保如果您以后决定重定向,cookie将会起作用。

我试图避免用mod_perl污染我的纯cgi脚本,所以我可以来回切换,但是为此受到了损害。 所有其他标头处理现在都可以正常工作。

IIRC在CGI中向mod_perl移植进行了解释 mod_perl编码准则

基本上,像您期望的,如果你使用它会工作$cgi->redirect( -cookie => $cookie, ... )混合$cgi->header和打印“头\\ n”往往没有到mod_perl下可靠地工作,挑非此即彼

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM