Hi,
I'm reworking our login framework and trying to move big chunks of
code from mason components into perl modules.
Two of our utility routines are "check_auth" and "force_auth". The
former tries to authenticate a user and returns undef to the caller on
failure. The latter makes the same authentication attempt, but shows a
login page on failure.
In module-land, I'm having trouble getting the "redirect" to the login
page to work as I've imagined it. What I've tried to do is use a mason
subrequest as a kind of internal redirect, taking the following steps:
1) clear the output buffer
2) make and exec the subrequest
3) flush the output buffer
4) abort
Here's my code:
sub force_authn {
my ( $self, $m ) = @_;
my $user = $self->check_authn ( $m, @_ );
unless ( $user ) {
$m->clear_buffer();
my $req = $m->make_subrequest
( comp => '/index.html',
args => [ request => $m->apache_req->the_request] );
$req->exec();
$m->flush_buffer();
$m->abort();
return;
}
return $user;
}
This doesn't work as I'd hoped: calling this method from the %init
block of a top-level component, I get the headers back, but no bytes
in the body.
Commenting out the abort call does produce the results I expected: the
content of the subrequest appears, immediately followed by the content
generated by the original request.
I'm sure I'm just misreading the docs, and there's an easy way to
accomplish what I'm trying to do. Has anyone tackled a similar
problem? Any suggestions?
By the way, following the code in the Devel manual I originally wrote
the make_subrequest/exec lines as a single subexec call. With no
changes to the arguments that are being passed, the subexec call
generates an error. This is true under both Mason 1.18 and 1.19,
mod_perl compiled against perl 5.8:
----
error: Component path given to Interp->load must be absolute (was
given comp)
context:
...
149: die "Cannot make alias without caller"
150: unless defined $Exception::Class::Caller;
151:
152: no strict 'refs';
153: *{"$Exception::Class::Caller\::$alias"} = sub { $subclass->throw(@_) };
154: }
155:
156: eval $code;
157:
...
code stack: /usr/lib/perl5/site_perl/5.8.0/Exception/Class.pm:153
/usr/lib/perl5/site_perl/5.8.0/HTML/Mason/Interp.pm:254
/usr/lib/perl5/site_perl/5.8.0/HTML/Mason/Request.pm:201
/usr/lib/perl5/site_perl/5.8.0/HTML/Mason/Request.pm:166
/usr/lib/perl5/site_perl/5.8.0/HTML/Mason/ApacheHandler.pm:60
/usr/lib/perl5/site_perl/5.8.0/Class/Container.pm:256
/usr/lib/perl5/site_perl/5.8.0/Class/Container.pm:334
/usr/lib/perl5/site_perl/5.8.0/HTML/Mason/Interp.pm:232
/usr/lib/perl5/site_perl/5.8.0/HTML/Mason/Request.pm:412
/usr/lib/perl5/site_perl/5.8.0/HTML/Mason/Request.pm:395
/usr/lib/perl5/site_perl/5.8.0/CommaX/Mason/AuthManager.pm:59
/usr/local/apache/htdocs/protected.html:21
/usr/local/apache/htdocs/autohandler:9
raw error
----
|