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

发布于 2024-12-18 07:10:34 字数 2343 浏览 1 评论 0原文

我有一个 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>

一切都很好,除了一个小问题:当标头中打印的 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;,如“如何抑制默认值mod_perl 中的 apache 错误文档?”关于 SO 的问题没有帮助——结果保持不变。

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

I have a CGI script in Perl that generates HTTP error pages by itself. I am running it under mod_perl via ModPerl::Registry, using the following Apache2 configuration:

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>

Everything is fine, except a little problem: when HTTP status printed in headers is different than 200 (for instance 404), Apache appends a default HTML error document to my own generated response.

Take for example the following simple CGI script:

#!/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;

Running it with Apache configuration mentioned above results in

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>

Note that replacing exit; in the example CGI script above with either return Apache2::Const::OK; or return Apache2::Const::DONE;, as recommended in "How do I suppress the default apache error document in mod_perl?" question on SO doesn't help -- the result stays the same.

What should I fix in my Apache configuration, or what should I add to my CGI script to suppress appending error page by mod_perl / Apache to generated response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜还是长夜 2024-12-25 07:10:34

FAQ对我有用,在你的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>

The FAQ works for me , after your CGI is done, after headers are sent, tell apache the status is ok, so it doesn't send 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>
想念有你 2024-12-25 07:10:34

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

#!/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 ;

My version of the code, but working more stable:

#!/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 ;
思念满溢 2024-12-25 07:10:34

我似乎面临着同样的问题:我将标头状态设置为 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,就像 Jakub Narę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 config:

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>

I seem to be facing the same problem: I set header status to 400 in case of an error and return JSON array to describe the actual error.

When I do:

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

With variables:

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

I will end up with this:

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>

Using the method described by faquer will in deed suppress the error document, but still returns status 200 OK, like Jakub Narębski points out.


BUT! I have found a workaround, where $r is a Apache2::RequestRec, using this:
http://perl.apache.org/docs/2.0/user/coding/coding.html#Forcing_HTTP_Response_Headers_Out
(otherwise you would use $r->send_http_header(), I guess)

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 response:

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

Apache config:

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文