3

私はPerlにHTTPエラーページをそれ自体で生成するCGIスクリプトを持っています。次の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スクリプトの例では、「 mod_perlのデフォルトのApacheエラードキュメントを抑制するにはどうすればよいですか? 」で推奨されているように、return Apache2::Const::OK;またはのいずれかに置き換えることに注意してください。SOに関する質問は役に立ちません。結果は同じままです。return Apache2::Const::DONE;

Apache構成で何を修正する必要がありますか、またはmod_perl / Apacheによる生成された応答へのエラーページの追加を抑制するためにCGIスクリプトに何を追加する必要がありますか?

4

3 に答える 3

5

FAQは私のために機能します。CGIが完了した後、ヘッダーが送信された後、apacheにステータスはOKであると伝えて、ErrorDocumenthttp://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>
于 2011-11-27T20:54:55.217 に答える
3

私のバージョンのコードですが、より安定して動作しています:

#!/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 ;
于 2013-02-06T18:02:34.520 に答える
2

同じ問題に直面しているようです。エラーが発生した場合にヘッダーステータスを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で説明されている方法を使用すると、実際にはエラードキュメントが抑制されますが、JakubNarębskiが指摘しているように、ステータス200OKが返されます。


しかし!これを使用して、$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>
于 2013-02-06T10:46:22.160 に答える