perlcgimod-perlmod-perl2mod-perl-registry

How to suppress the default mod_perl error page in legacy CGI script using ModPerl::Registry


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?


Solution

  • 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>