perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Justin Luster" <jus...@sawtoothsoftware.com>
Subject Mod_Perl 2.0 Header question
Date Tue, 16 Aug 2005 16:57:09 GMT
Hi, I was hoping to get a bit of help on how to send the proper HTTP
headers using Mod_Perl.  We create Perl scripts and then ship them out
to our clients for them to run on their servers.  The script should work
on regular CGI and Mod_Perl if they have it.  Things have worked fine so
far.  But now Mod_Perl 2.0 is starting to be installed on servers
throughout the world and I need to modify my script so that it will work
with it also.  I only use Mod_Perl to speed up regular CGI using
"registry".

 

I have a main.pl file that "requires" a helper.pl file.  In the
helper.pl I have the code that produces the HTTP header.  It is in the
helper.pl because both main.pl and main2.pl need it.

 

My current code looks something like this:

 

sub PrintHeader 

{

            my $strOut = "";

            my $blnModPerl = 0;

            my $intModPerlVersion = 0;

            my $strDefaultContentType = "";

 

            $strDefaultContentType .= "Content-type: text/html\r\n\r\n";

 

            if (exists($ENV{'MOD_PERL'}) && defined($ENV{'MOD_PERL'})) 

            {

                        $blnModPerl = 1;

 

                        $intModPerlVersion = $ENV{'MOD_PERL'};

 

                        # Change mod_perl/1.XX to 1.X

                        $intModPerlVersion =~
s/mod_perl\/(\d\.\d)(.*?)$/$1/i;

            }

 

            if ($ENV{'PERL_SEND_HEADER'} || ($blnModPerl == 0)) 

            {

                        $strOut = $strDefaultContentType;

            }

            else

            {

                        my $r = Apache->request;

                        

                        $r->content_type('text/html');

 

                        #Only call send_http_header for mod_perl
versions prior to (1.9) in the 1.26 series 

                        #Remember to update this in admin.pl too

                        if ($intModPerlVersion < 1.9) 

                        {

                                    $r->send_http_header;

                        }

            }

 

            $authlib::blnPrintedHeaders = 1;

 

            return $strOut;

}

 

I find that this breaks under some versions of Mod_Perl 2.0.  I'm
working with two older versions: 1.99_07-dev and 1.99_12 both on Linux
machines.

 

My confusion is how to send the proper 2.0 header.  In my in-house
version Apache->request; does not seem to work.  The 2.0 documentation
says use Apache2::RequestUtil but that does not work either.  My clients
version however works with Apache->request and I'm confused as to why.

 

The bottom line is I want to have a function like the one above that
will work for CGI, mod_perl 1.0 and mod_perl 2.0.  I'd like this
function to be in a common library (helper.pl) so that main1.pl and
main2.pl can use it.

 

>From reading the documentation I realize that there is a function called
"handler()" and that you can get a hold of $r somehow.  So I tried it
out and came up with something like this:

 

use strict;

 

my $ModPerlObj = @_[0];

 

main::main();

 

package main;

 

sub main

{

            authlib::Initialize($ModPerlObj);

}

 

This seems to work because as I understand it Mod_Perl wraps up the
contents of main.pl and places it inside a function called "handler( )".
"handler( )" then receives an Apache Object.  So I'm getting it using
@_[0] and then passing it into my function.

 

Is this bad practice?  Any "cons" to doing it this way?

 

I just want a simple way to create the header.

 

Please help.

 

Thanks,

 

Justin

 


Mime
View raw message