2011-11-27 5 views
3

HTTP 오류 페이지를 자체적으로 생성하는 Perl의 CGI 스크립트가 있습니다. 헤더에 인쇄 된 HTTP 상태가 200과 다른 경우 (예 : 404), 아파치가를 추가 : 약간의 문제를 제외하고,ModPerl :: Registry를 사용하여 레거시 CGI 스크립트의 기본 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> 

모든 것이 괜찮 : 나는 다음과 같은 아파치 구성을 사용하여, ModPerl::Registry를 통해 mod_perl을 아래를 실행하고 기본 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; 

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;, "How do I suppress the default apache error document in mod_perl?"에서 권장 한대로 도움이되지 않습니다. 결과는 동일하게 유지됩니다.

Apache 구성에서 무엇을 수정해야합니까? 또는 mod_perl/Apache가 추가 응답 페이지를 생성하지 못하도록 CGI 스크립트에 추가해야하는 항목은 무엇입니까?

답변

5
당신의 CGI 완료 후 자주 묻는 질문, 나를 위해 작동

, 헤더가 전송 된 후, 상태가 괜찮 아파치 이야기, 그래서 보내지 않는 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> 
+0

. 어떤 ** 아파치 설정 **을 사용합니까? 내 것과 다른 경우 응답에 추가 할 수 있습니까? TIA. –

+0

감사합니다. 작동합니다. –

+2

불행히도 어떤 경우에는 (다른 사용자의 경우 읽기) 올바른 오류 정보 대신 HTTP 상태가 "200 OK"인 페이지를 전송하게됩니다. –

3

는 내가 직면 할 것 동일한 문제 : 오류 발생시 헤더 상태를 400으로 설정하고 JSON 배열을 반환하여 실제 오류를 설명합니다.

I가 수행 할 때

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

변수 :

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 의해 기술 된 방법을 사용하여 :

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

I이 끝낼 것이다 오류 문서를 반환하지만 Jakub Narębski가 지적한 것처럼 여전히 200 OK를 반환합니다.


그러나! http://perl.apache.org/docs/2.0/user/coding/coding.html#Forcing_HTTP_Response_Headers_Out

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 응답을 (그렇지 않으면 당신은, 내가 생각) $ R -> send_http_header를 (사용합니다) : :이 사용 $의 R은 아파치 :: RequestRec 인 해결 방법을 발견했다

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

아파치 설정 :

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> 
+0

나는 이것을 한 번 이상 upvote 할 수만 있다면! 이 일로 나와 팀이 벽에 걸리게했습니다. – StrikeForceZero

2

내 버전하지만, 더 안정적인 작업 : BTW

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