繁体   English   中英

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

[英]How to suppress the default mod_perl error page in legacy CGI script using ModPerl::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>

一切都很好,除了一个小问题:当标头中打印的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>

注意替换exit; 在上述示例CGI脚本中, return Apache2::Const::OK; return Apache2::Const::DONE; ,如“ 如何抑制mod_perl中的默认apache错误文档? ”中所建议的那样,SO的问题无济于事-结果保持不变。

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

常见问题解答对我有用,完成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>

我似乎正面临着同样的问题:如果发生错误,我会将标头状态设置为400,然后返回JSON数组以描述实际错误。

当我做:

print $main::cgi->header(@ret), $html;

带有变量:

@ret: {'-type' => 'application/json','-charset' => 'utf-8','-status' => '400 Bad Request'}
$html: '{"errors":{"short_name":["Missing!"]}}'

我将得出以下结论:

Status Code: 200 OK
Content-Type: application/json; charset=utf-8
Response: {"errors":{"short_name":["Missing!"]}<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>400 Bad Request</title>
  </head><body>
  <h1>Bad Request</h1>
  <p>Your browser sent a request that this server could not understand.<br />
  </p>
  <hr>
  <address>Apache/2.2.3 (CentOS) Server at localhost Port 80</address>
  </body></html>

使用faquer描述的方法实际上会抑制错误文档,但是仍然返回状态200 OK,就像JakubNarębski指出的那样。


但! 我找到了一种解决方法,其中$ r是一个Apache2 :: RequestRec,使用此方法: http : //perl.apache.org/docs/2.0/user/coding/coding.html#Forcing_HTTP_Response_Headers_Out (否则,您将使用$ r- > send_http_header(),我想)

print $main::cgi->header(@ret), $html;
my $r = $main::cgi->r;
$r->rflush; # force sending headers (with headers set by CGI)
$r->status(200); # tell Apache that everything was ok, dont send error doc.

HTTP响应:

Status Code: 400 Bad Request
Content-Type: application/json; charset=utf-8
Response: {"errors":{"short_name":["Missing!"]}}

Apache配置:

PerlModule ModPerl::PerlRun
PerlModule CGI
PerlModule Apache::DBI
PerlRequire /var/www/html/startup.pl
PerlSendHeader On

.htaccess:

<Files *.cgi>
 SetHandler  perl-script
 PerlHandler ModPerl::PerlRun
 Options ExecCGI
</Files>

我的代码版本,但工作更稳定:

#!/usr/bin/perl

use CGI qw/:standard/ ;

my $Content_of_webpage = 'Oops. 404 error ...' ;
my $status_code = 404 ;


  if( $ENV{MOD_PERL} ) { # mod_perl ON

    my $r = CGI->new->r ;
    $r->status($status_code) ;
    $r->content_type("text/html; charset=UTF-8") ;
    $r->rflush ; # send the headers out << it is the trick :)
    $r->status(200) ;      

  }
  else { # mod_perl OFF

   my $cgi = CGI->new ;
   print $cgi->header( 

     -type    => "text/html",
     -status  => $status_code,
     -charset => 'UTF-8'

   );    

  }

 print $Content_of_webpage ;

暂无
暂无

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

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