Hello – I’m new to the list and new to mod_perl.

 

I’m having a problem getting a perl module to do exactly what I want it to – perhaps I haven’t yet gotten the magic formula just right, but perhaps it’s not possible. Rather than beat my head against the wall, I thought I would take a step back and see what the experts have to say.

 

Long story short, I started out looking for a way to have Apache automatically append a standard footer to the bottom of pages whether html, php, cgi or other files/locations that I matched. Searching around, I found that Apache::Sandwich was probably a decent way to accomplish this. One problem: I’m running Apache 2.0.54 and mod_perl2: no Apache::Sandwich for me.

 

Okay, now back to the long story. J So I found a tutorial on writing mod_perl modules and made a simple “Hello World” type deal that worked. I then decided to hack the important bits of Sandwich.pm into my script in an approach that was half migration of the Sandwich.pm to mod_perl2 compatability, and half a customized version that did exactly what I wanted and nothing more.

 

The resulting code is close. Very close. In fact, the only thing, so far as I can tell, that is missing is the fact that if my module handles a PHP file, the PHP content is simply piped through to the output, not processed by PHP. Now I have PHP 5.0.5 installed and working just fine on this server, so there should be no problem at that level.

 

Before I copy the code here, I’ll synopsize it’s functionality:

1)     Receive control from Apache based on some conf directive that matches files/location and invokes the module

2)     Form a subRequest to capture information about the originally requested URI

3)     Set a “handler” by name to use for the subRequest

4)     Run the subRequest with the request’s args to get the “normal” output of the URI

5)     Form another subRequest for the footer file we want to append

6)     Set a “handler” such as ‘server-parsed’ for the footer subRequest

7)     Run the footer subRequest

8)     Return the combined page result with an OK status

 

That’s how it’s supposed to work, anyway. My problem appears to be at step 3 where I can’t seem to identify a “handler” by name which will process the subRequest through the PHP interpreter. Running that subRequest has so far returned only the PHP code itself having experimented with handlers named “default-handler”, “server-parsed”, and “php-script”. “php-script” was just a guess based on something I found on Google about an Apache configuration directive to “AddHandler php-script php” – that had no effect.

 

The source of all my woes is the fact that the original Apache::Sandwich (sandwich.pm) would have (as far as I can tell) had the same problem: it required whoever was configuring it to specifically know what handler should be handling the originally requested file. It just so happens that I have no idea what the name of the handler should be! The original sample code for sandwich called for ‘server-parsed’, but that doesn’t do it.

 

So without further ado, the code that I’m experimenting with and the relevant apache configuration directives as well as the PHP source:

 

 

 

/usr/local/source/apache2/ModPerl/smk_test.php:

--- begin ---

package ModPerl::smk_test;

 

use warnings;

 

use Apache2::RequestRec ();

use Apache2::RequestUtil ();

use Apache2::SubRequest ();     # For lookup_uri()

use Apache2::RequestIO ();

use Apache2::Const -compile => qw(OK);

 

sub handler {

        my $r = shift;

 

        # BEGIN New1

        my $subr = $r->lookup_uri($r->uri);

        my $fileName = $subr->filename;

 

        return NOT_FOUND unless -f $fileName; # file not found

        return DECLINED unless -T _; # not a text file

        # END New1

 

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

 

        # BEGIN New2

        return OK if $r->header_only(); # HEAD request, so skip out early!

 

        # run subrequest using the specified handler (or default handler)

        # for the main document.

        my $shandler = $r->dir_config('OriginalHandler') || 'default-handler';

        #my $shandler = 'php-script';

        $subr->handler($shandler);

        $subr->args($r->args);  # pass along query string if there is one.

        $subr->run;

        # END New2

 

        print "\n<!--ModPerl::smk_test (/usr/local/apache2/ModPerl/smk_test.pm) -->\n";

        print "subr handler = [$shandler]<br/>\n";

 

        $footerfile = $r->dir_config('FOOTER');

 

        if (-f $footerfile) {

                print "\n<!-- ModPerl::smk_test : BEGIN FOOTER -->\n";

 

                # Now actually bring in the footer file

                my $footsubr = $r->lookup_file($footerfile);

                $footsubr->handler('server-parsed');

                $footsubr->run;

 

                print "\n<!-- ModPerl::smk_test : END FOOTER -->\n";

        }

        else {

                print "\n<!-- ModPerl::smk_test : Error; FOOTER file ($footerfile) is missing -->\n";

        }

 

        return(Apache2::OK);

}

1;

--- end ---

 

 

 

Relevant httpd.conf configuration directives:

--- begin ---

<FilesMatch "smk_page.php">

                SetHandler perl-script

                PerlHandler ModPerl::smk_test

                PerlSetVar OriginalHandler php-script

                PerlSetVar FOOTER /home/htdocs/network_footer.htm

                SetOutputFilter php # this was an experimental addition, no change in result

</FilesMatch>

--- end ---

 

/home/htdocs/smk_page.php

--- begin ---

<?php

 

phpinfo();

 

?>

--- end ---

 

 

 

Resulting from requesting the page http://localhost/smk_page.php:

--- begin ---

<?php
 
phpinfo();
 
?>
<!--ModPerl::smk_test (/usr/local/apache2/ModPerl/smk_test.pm) -->
subr handler = [php-script]<br/>
<!-- ModPerl::smk_test : BEGIN FOOTER -->
<!-- BEGIN: Global Footer -->
<!-- END: Global Footer -->
<!-- ModPerl::smk_test : END FOOTER -->

--- end ---

 

 

 

As you can see, the module successfully grabs the contents of the originally requested file and successfully appends the results of the footer file that was called out in httpd.conf. The only thing left is to get the PHP interpreter to handle the PHP code.

 

Any hot tips would be greatly appreciated at this point!

 

 

Kind thanks,

- SK

 

Sean Kelly - The “other half” of Keen Kelly

[EMAIL PROTECTED]

http://www.keenkelly.com/

Office: 1 (888) KEEN-KELLY

P.O. Box 18202; San Jose, Ca; 95158

 

Reply via email to