perl - 如何使用 ModPerl::Registry 抑制旧 CGI 脚本中的默认 mod_perl 错误页面

标签 perl cgi mod-perl mod-perl2 mod-perl-registry

我在 Perl 中有一个 CGI 脚本,它自己生成 HTTP 错误页面。我通过 ModPerl::Registry 在 mod_perl 下运行它,使用以下 Apache2 配置:

Alias /perl "/var/www/perl"
<Directory "/var/www/perl">
    SetHandler perl-script
    PerlResponseHandler ModPerl::Registry
    PerlOptions +ParseHeaders
    Options Indexes FollowSymlinks +ExecCGI
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

一切都很好,除了一个小问题:当 header 中打印的 HTTP 状态不同于 200(例如 404)时,Apache 会在我自己生成的响应中附加一个默认的 HTML 错误文档。

以下面的简单 CGI 脚本为例:

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(:standard :escapeHTML -nosticky);
use CGI::Carp qw(fatalsToBrowser);

use Apache2::Const qw(:http :common);

our $cgi = CGI->new();

print $cgi->header(-type=>'text/html', -charset => 'utf-8',
                   -status=> '404 Not Found');
our $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
print <<"EOF";
<html>
<head>
<title>die_error_minimal$mod_perl_version 
</head>
<body>
404 error
</body>
</html>
EOF

exit;

使用上面提到的 Apache 配置运行它会导致

HTTP/1.1 404 Not Found
Date: Sun, 27 Nov 2011 13:17:59 GMT
Server: Apache/2.0.54 (Fedora)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

<html>
<head>
<title>die_error_minimal mod_perl/2.0.1 
</head>
<body>
404 error
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /perl/die_error_minimal.cgi was not found on this server.</p>
<hr>
<address>Apache/2.0.54 (Fedora) Server at localhost Port 80</address>
</body></html>

请注意,将上面示例 CGI 脚本中的 exit; 替换为 return Apache2::Const::OK;return Apache2::Const::DONE;,正如关于 SO 的“How do I suppress the default apache error document in mod_perl?”问题中所建议的那样,没有帮助——结果保持不变。

我应该在我的 Apache 配置中修复什么,或者我应该在我的 CGI 脚本中添加什么以抑制 mod_perl/Apache 将错误页面附加到生成的响应中?

最佳答案

FAQ 对我有用,在您的 CGI 完成后,在发送 header 后,告诉 apache 状态正常,所以它不会发送 ErrorDocument http://search.cpan.org/~gozer/mod_perl-1.31/faq/mod_perl_faq.pod#So_how_do_I_use_mod_perl_in_conjunction_with_ErrorDocument%3F

#!/usr/bin/perl --
use strict; use warnings;
use CGI;

Main( @ARGV );
exit( 0 );

sub Main { 
    my404();
#~ my $r = CGI::Simple->new->_mod_perl_request;
    my $r = CGI->new->r;
    $r->status(200);
    return;
}
sub my404 {
    my $cgi = CGI->new;
    print $cgi->header(  -status => 404 );
    print "<html><title>print404 says tough noogies</title>
<body><h1>tough noogies</h1></body></html>";
}
__END__

GET http://localhost/perl/print404
User-Agent: lwp-request/6.03 libwww-perl/6.03

404 Not Found
Connection: close
Date: Sun, 27 Nov 2011 20:55:39 GMT
Server: Apache/2.0.54 (Win32) mod_ssl/2.0.54 OpenSSL/0.9.7g PHP/4.3.11 mod_perl/2.0.1 Perl/v5.8.9
Content-Type: text/html; charset=ISO-8859-1
Client-Date: Sun, 27 Nov 2011 20:55:39 GMT
Client-Peer: 127.0.0.1:80
Client-Response-Num: 1
Client-Transfer-Encoding: chunked
Title: print404 says tough noogies

<html><title>print404 says tough noogies</title>
<body><h1>tough noogies</h1></body></html>

关于perl - 如何使用 ModPerl::Registry 抑制旧 CGI 脚本中的默认 mod_perl 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8288425/

相关文章:

regex - 如何使用不可正则表达式的版权来处理可执行代码?

regex - 如何在perl中提取用单引号括起来的字符串?

python - Google App Engine 中的 cgi.test()

perl - 在 OSX Lion Apache 服务器上设置 mod_perl

perl - 安装 mod_perl 时出错

arrays - 如何在不使用 Perl 循环的情况下过滤数组?

perl -\x{..} 和\N{U+..} 有区别吗

perl - 有没有办法允许失败的上传使用 Perl CGI 脚本恢复?

python - 如何告诉cgi停止等待

performance - 使用 mod_perl 时,如何仅在编译时执行低效代码?