Example A-5. Book/MimeTypeDispatch.pm
package Book::MimeTypeDispatch;
use Apache::Constants qw(DECLINED);
my %mime_types = (
'text/html' => \&HTML::Template::handler,
'text/plain' => \&Book::Text::handler,
);
sub handler {
my $r = shift;
if (my $h = $mime_types{$r->content_type}) {
$r->push_handlers(PerlHandler => $h);
$r->handler('perl-script');
}
return DECLINED;
}
1;
_ _END_ _
This should be done with PerlFixupHandler, so we
add this line in httpd.conf:
PerlFixupHandler Book::MimeTypeDispatch
After declaring the package name and importing constants, we set a
translation table of MIME types and the corresponding handlers to be
called. Then comes the handler, where the request object is
retrieved. If the request object's MIME type is
found in our translation table, we set the handler that should handle
this request; otherwise, we do nothing. At the end we return
DECLINEDso another fixup handler can take over.
| | |
A.10. mod_rewrite in Perl | | A.12. Singleton Database Handles |