I'm using mod_perl + Template Toolkit, and I'm having trouble getting Apache
to pass the DirectoryIndex to my handler. The handler is supposed to take
the path_info and pull a template of the same name from a subdirectory
called 'html'.

This works fine for file names which do exist in the subdirectory. And, if
they don't exist, an error is correctly generated. However, if the path is
empty or '/', I expect Apache to include the DirectoryIndex (index.html) in
path_info or somewhere else that I can get to it. Unfortunately, Apache is
passing empty paths and '/' straight through to the handler, causing a file
not found error.

Maybe I'm just not looking in the right place, but I've spent a lot of time
Googling and looking at the mod_perl guide, and I can't seem to find an
exact answer to my problem. This would seem to be a common issue. Can anyone
offer a solution?

Admittedly, I'm new to Apache, mod_perl and Template Toolkit, and I'm
getting my feet wet trying to learn all three using my Windows 2000
workstation as a test bed, although the eventual target server will be
running Linux. Below is some sample code (mostly copied/modified from
various online sources) that I boiled down in an attempt to solve the
problem, but to no avail.

Thanks in advance.

-Tim



httpd.conf
----------

# default win32 config up to here ...

# mod_perl config
Include conf/perl.conf

----------------------------------------------------------------------

perl.conf
---------

LoadModule perl_module modules/mod_perl.so
AddModule mod_perl.c

PerlRequire conf/startup.pl

PerlWarn On
PerlTaintCheck On
PerlFreshRestart On

PerlSetVar websrc /usr/local/apache/tt

<Location /tt>
 SetHandler perl-script
 PerlHandler Test::Mod
</Location>

----------------------------------------------------------------------

startup.pl
----------

#!/usr/bin/perl -w

use strict;

BEGIN {
 # modify include path
 use Apache ();
 use lib Apache->server_root_relative('lib/perl');
}

# are we under mod_perl?
$ENV{MOD_PERL}
 or die("not running under mod_perl");

# detailed warnings
use Carp ();
$SIG{__WARN__} = \&Carp::cluck;

# load common modules
use Apache::Constants ();
use Template ();

# load custom modules
use Test::Mod ();

1;

----------------------------------------------------------------------

Test/Mod.pm
-----------

#!/usr/bin/perl -wT

package Test::Mod;

use strict;
use Apache::Constants qw( :common );
use Template qw( :template );

our $VERSION = 0.01;

sub handler {
 my $r = shift;
 my $websrc = $r->dir_config('websrc')
  or return fail( $r, SERVER_ERROR, "'websrc' not specified" );
 my $template = Template->new({
  INCLUDE_PATH => "$websrc:$websrc/html:$websrc/html/include",
  OUTPUT   => $r,
 });

 # use the path_info to determine which template file to process
 my $file = $r->path_info();
 $file =~ s[^/][];

 my $vars = {
  uri  => $r->uri(),
  file => $r->filename(),
  path => $r->path_info(),
  tt  => $file,
 };

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

 $template->process($file, $vars)
  or return fail( $r, SERVER_ERROR, $template->error() );

 return OK;
}

sub fail {
 my ($r, $status, $message) = @_;
 $r->log_reason( $message, $r->filename() );
 return $status;
}

----------------------------------------------------------------------

index.html
----------

[% PROCESS header.html
 title = 'template test'
%]

<p>uri: [% uri %]</p>
<p>file: [% file %]</p>
<p>path: [% path %]</p>
<p>template: [% tt %]</p>

[% PROCESS footer.html %]


Reply via email to