You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Alex Menendez <am...@hotbacon.com> on 2000/08/16 23:07:45 UTC

new cgi mod_perl question

does anyone know how to get the output of a standalone cgi script from a
mod_perl module

I have tried all the subrequest stuff but I can't get it to work.

Apache seems to parse the cgi file once a handler has been called. simply
writing a handler that returns DECLINED every time it is called like so:

package LameModule;
use Apache::Constants qw(:common);
sub handler {
    my $r    = shift;
    return DECLINED;
}

with a config entry like so:

    <Files ~ "\.cgi$">
       SetHandler perl-script
       PerlHandler LameModule
    </Files>

will always parse the cgi file as text instead of just declining to handle
it and letting the cgi do its thing. Any ideas on how to get this cgi to
write some output to STDOUT even when a Handler has been called for its
file extension?

thanx,
-amen


Re: new cgi mod_perl question

Posted by darren chamberlain <da...@boston.com>.
Alex Menendez (amen@hotbacon.com) said something to this effect:
> 
> does anyone know how to get the output of a standalone cgi script from a
> mod_perl module
> 
> I have tried all the subrequest stuff but I can't get it to work.

If all you are trying to do is get the raw output, use LWP and a virtual
server running on localhost:

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
package Apache::CGIGetter;

use vars qw($agent);
use constant HOST => 'http://localhost';

use Apache::Constants 'OK';
use LWP::UserAgent;
use HTTP::Request;

BGEIN { $agent = LWP::UserAgent->new(); }

sub handler {
    my($r) = @_;
    my $url = join '/',HOST,$r->uri;
    if(my $args = $r->args) { $url .= "?$args"; }

    # These lines are taken from pp. 376-377 of the Eagle book
    # http://www.modperl.com/book/chapters/ch7.html#Handling_the_Proxy_Process_Ourse

    my $request = HTTP::Request->new($r->method, $url);
    $r->headers_in->do(sub { $request->header(@_) });
    if($r->method eq 'POST') {
        my $len = $r->header_in('Content-length');
        my $buf;
        $r->read($buf,$len);
        $request->content($buf);
    }

    my $result = $agent->request($request);

    # $cgi_content will contain the body of the resulting page, i.e.,
    # the HTML page.
    my $cgi_content = $result->content;

    # Do stuff here with $cgi_content.

    return OK;
}
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In the httpd.conf:

<FilesMatch "\.cgi$">
    SetHandler  perl-script
    PerlHandler Apache::CGIGetter
</FilesMatch>

And a virtual host on 127.0.0.1, running mod_cgi as usual.

Good luck.

(darren)

-- 
He who would trade liberty for safety deserves neither.