René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

mod_perl

mod_perl allows to run scripts and handlers.
PerlModule loads a module (suffix .pm), while PerlRequire loads a file (suffix .pl).
PerlWarn On (-w), is not allowed within a Location directive.
PerlTaintCheck On (-T)

Script example

The following entries must be made in httpd.conf.
PerlModule Apache::Registry
<Directory /var/www/some/directory>
  SetHandler      perl-script
  PerlHandler     Apache::Registry
  Options        +ExecCGI
  PerlSendHeader  On
  allow from all
</Directory>
Options +ExecCGI is a safety check to prevent non-program files from being inadvertantly executed.
The following file must be stored under /var/www/some/directory and named for example test.pl
print "Content-type: text/html\n\n";
print "<h1>Hello</h1>";
Do not forget to restart apache to test it.

Handler example

The following entries must be made in httpd.conf.
PerlModule ModPerlHandlerTest
<Location /mod_perl_test>
  SetHandler     perl-script
  PerlHandler    ModPerlHandlerTest
  PerlSendHeader Off
</Location>
PerlHandler ModPerlHandlerTest calls ModPerlHandlerTest::handler();
It is also possible to specify a name for a handler subroutine:
PerlInitHandler           SomeModule::init
That implies that either a subroutine init exists in SomeModule.pm or there is a package named init.pm under the directory SomeModule.
The following piece of code must be stored somewhere in the @INC paths of perl:
package ModPerlHandlerTest;
use warnings;
use strict;
use Apache::Constants qw(:common);

sub handler {
  my $req = shift;

  #$req -> send_http_header('text/html');
  $req -> content_type('text/html');
  $req -> print ("<h1>mod_perl handler test</h1>");

  $req -> print ("<table>

   <tr><td>uri:             </td><td>" . $req->uri              . "</td></tr>
   <tr><td>filename         </td><td>" . $req->filename         . "</td></tr>
   <tr><td>path_info        </td><td>" . $req->path_info        . "</td></tr>
   <tr><td>args             </td><td>" . $req->args             . "</td></tr>
   <tr><td>get_remote_host  </td><td>" . $req->get_remote_host  . "</td></tr>
   <tr><td>get_basic_auth_pw</td><td>" . $req->get_basic_auth_pw. "</td></tr>

  </table>");

  my %args = $req->args;

  $req->print("<ul>");
  $req->print (map {"<li>" . $_ . " = " . $args{$_} } keys %args);
  $req->print ("</ul>");

  return OK;
}

1;
In order to add an additional path to the @INC directories, use the following construct.
my_inc.pl
use lib qw(/path/to/my/lib /path/to/my/other/lib);
Add to httpd.conf:
PerlRequire /path/to/set_my_inc.pl

Stages of a request

All of the following stages can be associated with a handler which receives a request object.

PerlChildHandler

When a new child process is forked

PerlPostReadRequestHandler

Once per transaction processing

PerlTransHandler

URI translation.
Proxies
Note: return HTTP_DECLINED if the uri has been changed.
use the request's uri method to change the uri.

PerlInitHandler

PostReadReq or HeaderParser

PerlHeaderParserHandler

URI has been mapped to a pathname.

PerlAccessHandler

Access control: anyone allowed to access?

PerlAuthenHandler

Authentication: Valid username and password?

PerlAuthzHandler

Authorization: User is allowed to access?

PerlTypeHandler

Assign a provisional MIME type

PerlFixupHandler

Any last minute changes?
mod_env

PerlHandler

Make the response

PerlLogHandler

Log the transaction

PerlCleanupHandler

Clean up. Send mail

PerlChildExitHandler

Before child dies