You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by titetluc titetluc <ti...@gmail.com> on 2008/04/29 10:50:29 UTC

[MP2][QUESTION]Adding handlers when defining a new directive

Hello,

I am writing a new mod_perl Apache (mod_perl2) to manage session tracking
and SSO
This module defines a new Apache directive (MyNewDirective), which is usable
in a <location>, <files><directory> block.

For example
<Location /a_test>
    Set-Handler perl-script
    MyNewDirective a_test arg1 arg2
    PerlResponseHandler ResponseHandlerToTestTheNewDirective
</Location>
<Location /another_test>
    Set-Handler perl-script
    PerlResponseHandler ResponseHandlerToTestTheNewDirective
</Location>


When this directive is used, my module should a PerlLogHandler automatically
to obtain the following configuration
<Location /a_test>
    Set-Handler perl-script
    MyNewDirective a_test arg1 arg2
    PerlResponseHandler ResponseHandlerToTestTheNewDirective
    PerlLogHandler TestPerlLogHandler
</Location>
<Location /another_test>
    Set-Handler perl-script
    PerlResponseHandler ResponseHandlerToTestTheNewDirective
</Location>

I tried to use the push_handler method when the 'MyNewDirective' is defined.

my @directives = ({name => 'MyNewDirective ', func =>
__PACKAGE__.'::MyNewDirective'});

Apache2::Module::add(__PACKAGE__, \@directives);

sub MyNewDirective {
    my ($self, $parms, $arg) = @_;

    # blablabla

    $parms->server->push_handlers(PerlLogHandler => sub {my ($r) _ @_;
$r->server->error_log('hello world'); return Apache2::Const::OK;});

    # blablabla
    return;
}

This code works ... but for any blocks.
For example, if I access the URI '/a_test', the PerlLogHandler will be
called BUT if I access the URI '/another_test', the PerlLogHandler will also
be called.

Do I use the mod_perl API correctly ?
What is wrong in my code ?

Thanks.

Gaetan

Re: [MP2][QUESTION]Adding handlers when defining a new directive

Posted by titetluc titetluc <ti...@gmail.com>.
It works !
Thanks a lot.

One additionnal question: does the hook ordering work (according to the
mod_perl documentation, it does not !) ?

Gaetan

2008/4/29, Philippe M. Chiasson <go...@ectoplasm.org>:
>
> titetluc titetluc wrote:
>
> > Hello,
> >
> > I am writing a new mod_perl Apache (mod_perl2) to manage session
> > tracking and SSO
> > This module defines a new Apache directive (MyNewDirective), which is
> > usable in a <location>, <files><directory> block.
> >
> > For example
> > <Location /a_test>
> >    Set-Handler perl-script
> >    MyNewDirective a_test arg1 arg2
> >    PerlResponseHandler ResponseHandlerToTestTheNewDirective
> > </Location>
> > <Location /another_test>
> >    Set-Handler perl-script
> >    PerlResponseHandler ResponseHandlerToTestTheNewDirective
> > </Location>
> >
> >
> > When this directive is used, my module should a PerlLogHandler
> > automatically to obtain the following configuration
> > <Location /a_test>
> >    Set-Handler perl-script
> >    MyNewDirective a_test arg1 arg2
> >    PerlResponseHandler ResponseHandlerToTestTheNewDirective
> >    PerlLogHandler TestPerlLogHandler
> > </Location>
> > <Location /another_test>
> >    Set-Handler perl-script
> >    PerlResponseHandler ResponseHandlerToTestTheNewDirective
> > </Location>
> >
> > I tried to use the push_handler method when the 'MyNewDirective' is
> > defined.
> >
> > my @directives = ({name => 'MyNewDirective ', func =>
> > __PACKAGE__.'::MyNewDirective'});
> >
> > Apache2::Module::add(__PACKAGE__, \@directives);
> >
> > sub MyNewDirective {
> >    my ($self, $parms, $arg) = @_;
> >
> >    # blablabla
> >
> >    $parms->server->push_handlers(PerlLogHandler => sub {my ($r) _ @_;
> > $r->server->error_log('hello world'); return Apache2::Const::OK;});
> >
>
>
> Right here, you are adding your handler to the current *server*
> configuration
> object, effectively enabling this handler for eery requests to that
> server/vhost
>
>     # blablabla
> >    return;
> > }
> >
> > This code works ... but for any blocks.
> > For example, if I access the URI '/a_test', the PerlLogHandler will be
> > called BUT if I access the URI '/another_test', the PerlLogHandler will also
> > be called.
> >
>
> See above.
>
>  Do I use the mod_perl API correctly ?
> >
>
> Correctly, yes. Unfortunately, it's not what you are trying to do.
>
>  What is wrong in my code ?
> >
>
> If you want to push your loghandler only for requests for your configured
> module, I would just delay the loghandler registration until runtime and
> do it in your content handler with
>
> $r->push_handlerrs(...)
>
> Or you can do it in your command handler, but like so
>
> sub MyLogHandler {
> [...]
> }
>
> sub MyNewDirective {
>  my ($self, $param, $arg) = @_;
>
>  $parms->add_config(["PerlLogHandler MyLogHandler"]);
>  [...]
>
> --
> Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
> http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/
>
>
>

Re: [MP2][QUESTION]Adding handlers when defining a new directive

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
titetluc titetluc wrote:
> Hello,
> 
> I am writing a new mod_perl Apache (mod_perl2) to manage session 
> tracking and SSO
> This module defines a new Apache directive (MyNewDirective), which is 
> usable in a <location>, <files><directory> block.
> 
> For example
> <Location /a_test>
>     Set-Handler perl-script
>     MyNewDirective a_test arg1 arg2
>     PerlResponseHandler ResponseHandlerToTestTheNewDirective
> </Location>
> <Location /another_test>
>     Set-Handler perl-script
>     PerlResponseHandler ResponseHandlerToTestTheNewDirective
> </Location>
> 
> 
> When this directive is used, my module should a PerlLogHandler 
> automatically to obtain the following configuration
> <Location /a_test>
>     Set-Handler perl-script
>     MyNewDirective a_test arg1 arg2
>     PerlResponseHandler ResponseHandlerToTestTheNewDirective
>     PerlLogHandler TestPerlLogHandler
> </Location>
> <Location /another_test>
>     Set-Handler perl-script
>     PerlResponseHandler ResponseHandlerToTestTheNewDirective
> </Location>
> 
> I tried to use the push_handler method when the 'MyNewDirective' is defined.
> 
> my @directives = ({name => 'MyNewDirective ', func => 
> __PACKAGE__.'::MyNewDirective'});
> 
> Apache2::Module::add(__PACKAGE__, \@directives);
> 
> sub MyNewDirective {
>     my ($self, $parms, $arg) = @_;
> 
>     # blablabla
> 
>     $parms->server->push_handlers(PerlLogHandler => sub {my ($r) _ @_; 
> $r->server->error_log('hello world'); return Apache2::Const::OK;});


Right here, you are adding your handler to the current *server* configuration
object, effectively enabling this handler for eery requests to that server/vhost

>     # blablabla
>     return;
> }
> 
> This code works ... but for any blocks.
> For example, if I access the URI '/a_test', the PerlLogHandler will be 
> called BUT if I access the URI '/another_test', the PerlLogHandler will 
> also be called.

See above.

> Do I use the mod_perl API correctly ?

Correctly, yes. Unfortunately, it's not what you are trying to do.

> What is wrong in my code ?

If you want to push your loghandler only for requests for your configured
module, I would just delay the loghandler registration until runtime and
do it in your content handler with

$r->push_handlerrs(...)

Or you can do it in your command handler, but like so

sub MyLogHandler {
[...]
}

sub MyNewDirective {
   my ($self, $param, $arg) = @_;

   $parms->add_config(["PerlLogHandler MyLogHandler"]);
   [...]

-- 
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/