Infinite Pig Theorem

DirectoryIndex and the "perl-script" handler

without comments

Wouter Verhelst blogs about combining ScriptAlias (or a way to run CGIs) and DirectoryIndex in Apache.

Recently, developing a TinyURL clone I had a similar (not identical, though) issue.

Basically, I set SetHandler to perl-script on the "/" location, which basically means, everything accessed on a given virtual host, is affected. Because you are changing the handler for the entire location, DirectoryIndex will have no effect on it because mod_dir is the one dealing with the DirectoryIndex function, that is, using the DIR_MAGIC_TYPE handler.

To fix this you can use a mod_perl (2, I never really used 1) Fixup phase handler:

<VirtualHost *:80>
	...
	# some stuff
	...

	# PerlSections rule.
	<Perl>
	$Location{"/"} = {
		SetHandler => 'perl-script',
		# some stuff
		PerlFixupHandler => 'Axiombox::Awbox::Fixup',
		# some other stuff
		DirectoryIndex => 'whatever.html',
	};
	</Perl>

</VirtualHost>

If it looks incomplete is because some other information, out of the scope of this sample, like DocumentRoot or other mod_perl directives, are hidden as the "other stuff". My Fixup.pm handler looks like this:

package Axiombox::Awbox::Fixup;

use strict;
use warnings FATAL => qw(all);

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

sub handler {
	my $r = shift;

	if ($r->handler eq 'perl-script' &&
		-d $r->filename              &&
		$r->is_initial_req)
	{
		$r->handler(Apache2::Const::DIR_MAGIC_TYPE);

		return Apache2::Const::OK;
	}

	return Apache2::Const::DECLINED;
}

1;

Which is very straight-forward: If the request is set to perl-script, the requested file is a directory and if the current request is the main one, then change the handler and return to the normal flow of phases. Otherwise, decline the Fixup phase handler.

Related posts

Written by David Moreno

December 2nd, 2008 at 12:12 pm

Categorized in: apache, in-english, perl, planeta linux

Tagged with , ,

Leave a Reply