如何使用ModPerl :: Registry禁用旧版CGI脚本中的默认mod_perl错误页面

Jak*_*ski 3 perl cgi mod-perl mod-perl2 mod-perl-registry

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

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>
Run Code Online (Sandbox Code Playgroud)

一切都很好,除了一个小问题:当标题中打印的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;
Run Code Online (Sandbox Code Playgroud)

使用上面提到的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>
Run Code Online (Sandbox Code Playgroud)

请注意,exit;在上面的示例CGI脚本中替换为return Apache2::Const::OK;或者return Apache2::Const::DONE;,如" 我如何在mod_perl中抑制默认的apache错误文档? "中所建议的那样,问题无效 - 结果保持不变.

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

faq*_*uer 5

常见问题解答适用于我,在CGI完成后,在发送标题后,告诉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>
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是在某些情况下(读取:对于其他人)这导致发送HTTP状态为"200 OK"的页面而不是正确的错误信息. (2认同)