Hi list,
Recently, I've been trying to do dynamic virtual hosting with mod_perl
1.29 (on Apache 1.3.33) by using a TransHandler to modify the document
root. This results in limited success; initially I can fetch the
correct document from the appropriate document root, but due to a
subrequest, the document root gets confused.
Any assistance with the following would be appreciated.
In a VirtualHost section of httpd.conf, I configure my handler like so:
PerlRequire "/home/httpd/docroot.pm"
PerlTransHandler docroot
And a cut-down/simplified version of the handler looks like this
(which is based on Recipe 4.3 from the mod_perl developer's cookbook):
package docroot;
use strict;
use Apache::Constants qw(OK DECLINED);
sub handler {
my $r = shift;
unless ($r->is_main) {
$r->log_error("not the main request; declining...");
return DECLINED;
}
my $file = $r->uri;
# Capture the old DocumentRoot setting.
my $old_docroot = $r->document_root;
$r->log_error("$file - current docroot = $old_docroot");
# Set DocumentRoot to the new value.
my $hostname = $r->hostname;
my $new_docroot = "/tmp/host/$hostname";
$r->log_error("$file - new docroot = $new_docroot");
$r->document_root($new_docroot);
# Remember to set the original DocumentRoot back.
# Here we use a closure.
$r->push_handlers(PerlCleanupHandler =>
sub {
my $r2 = shift;
my $todo_docroot = $r2->document_root;
$r2->log_error("$file - cleaning up: from $todo_docroot to
$old_docroot");
$r2->document_root($old_docroot);
return OK;
}
);
return DECLINED;
}
1;
When I use Firefox to visit the appropriate hostname (an alias for
localhost, ie: http://<hostname/), the error log receives:
[Mon Jun 6 17:57:57 2005] [error] / - current docroot = /tmp/host
[Mon Jun 6 17:57:57 2005] [error] / - new docroot = /tmp/host/<hostname>
[Mon Jun 6 17:57:57 2005] [error] not the main request; declining...
[Mon Jun 6 17:57:57 2005] [error] /index.html - current docroot =
/tmp/host/<hostname>
[Mon Jun 6 17:57:57 2005] [error] /index.html - new docroot =
/tmp/host/<hostname>
[Mon Jun 6 17:57:57 2005] [error] / - cleaning up: from
/tmp/host/aaw.com to /tmp/host
[Mon Jun 6 17:57:57 2005] [error] /index.html - cleaning up: from
/tmp/host to /tmp/host/<hostname>
[Mon Jun 6 17:57:58 2005] [error] /favicon.ico - current docroot =
/tmp/host/<hostname>
[Mon Jun 6 17:57:58 2005] [error] /favicon.ico - new docroot =
/tmp/host/<hostname>
[Mon Jun 6 17:57:58 2005] [error] [client 127.0.0.1] File does not
exist: /tmp/host/<hostname>/favicon.ico
[Mon Jun 6 17:57:58 2005] [error] /favicon.ico - cleaning up: from
/tmp/host/<hostname> to /tmp/host/<hostname>
It would appear that a subrequest is invoked to handle the
DirectoryIndex configuration in order to fetch /index.html. The
subrequest cleanup _always_ happens after the main request cleanup, so
the document_root always gets 'restored' to the modified value.
Subsequent requests start off in the wrong document root.
I have also attempted to run this handler as an InitHandler, with the
same symptoms.
Is there a solution for this?
--
Ewan Edwards, [EMAIL PROTECTED]