accessing environment variables set by other modules

accessing environment variables set by other modules

am 12.04.2010 20:44:32 von Chris Datfung

--0016e6d99c1d2c278004840e8992
Content-Type: text/plain; charset=ISO-8859-1

I want to use mod-perl to edit server responses under certain conditions. My
plan was to use various modules, like mod-setenvif and mod-security to set
an environment variable and then have mod-perl edit the response body only
run when the environment variable is set. I tried the following test which
was supposed to append 'TEST' to my index.html page:

in the virtual host config I have:

SetEnvIf Request_URI "\.html$" TE=TEST
PerlRequire "/opt/modperl/TE/ST.pm"
PerlOutputFilterHandler TE::ST

the contents of /opt/modperl/TE/ST.pm is:
============================================================ ==========
package TE::ST;

use strict;
use warnings;

use Apache2::Filter ();
use Apache2::RequestRec ();
use APR::Table ();

BEGIN { push @INC, "/opt/modperl/"; }

use Apache2::Const -compile => qw(OK);
use constant BUFF_LEN => 1024;

sub handler
{
my $f = shift;

unless ($f->ctx)
{
while ($f->read(my $buffer, BUFF_LEN))
{
$buffer =~ s/It/Chris/g;
$buffer .= $ENV{"TE"};
$f->print($buffer);
}
return Apache2::Const::OK;
}
}
1;
============================================================ ============

The script correctly changes the 'It' in the index.html file to 'Chris' but
I don't see the value of the 'TE' variable in the response body. Can someone
point me to an example of how modperl can access environment variables set
by other apache modules?

Thanks,

- Chris

--0016e6d99c1d2c278004840e8992
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I want to use mod-perl to edit server responses under cert=
ain conditions. My plan was to use various modules, like mod-setenvif and m=
od-security to set an=A0environment=A0variable and then have mod-perl edit =
the response body only run when the=A0environment=A0variable is set. I trie=
d the following test which was supposed to append 'TEST' to my inde=
x.html page:


in the virtual host config I have:

=
   =A0 =A0 =A0SetEnvIf Request_URI "\.html$" TE=3DTEST div>
   =A0 =A0 =A0PerlRequire "/opt/modperl/TE/ST.pm" iv>
   =A0 =A0 =A0PerlOutputFilterHandler TE::ST


the contents of=A0/opt/modperl/TE/ST.pm is:
=
==================== =====3D=
==================== =====3D=
====================
>package TE::ST;

use strict;

use warnings;

use Apache2::Filter ();
us=
e Apache2::RequestRec ();
use APR::Table ();

=
BEGIN { push @INC, "/opt/modperl/"; }


use Apache2::Const -compile =3D> qw(OK);
use constant BUF=
F_LEN =3D> 1024;

sub handler
{
<=
div>   =A0 =A0 =A0my $f =3D shift;

   =A0 =
=A0 =A0unless ($f->ctx)

   =A0 =A0 =A0{
   =A0 =A0 =A0 =A0 =A0 =A0 =A0w=
hile ($f->read(my $buffer, BUFF_LEN))
   =A0 =A0 =A0 =A0 =
=A0 =A0 =A0{
   =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$b=
uffer =3D~ s/It/Chris/g;
   =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0$buffer .=3D $ENV{"TE"};

   =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$f->print($buffer=
);
   =A0 =A0 =A0 =A0 =A0 =A0 =A0}
   =A0 =A0 =
=A0 =A0 =A0 =A0 =A0return Apache2::Const::OK;
   =A0 =A0 =A0}=
}
1;
=========3D=
==================== =====3D=
==================== =====3D=
=============3D


The script correctly changes the 'It' in the in=
dex.html file to 'Chris' but I don't see the value of the '=
TE' variable in the response body. Can someone point me to an example o=
f how modperl can access=A0environment=A0variables set by other apache modu=
les?


Thanks,

- Chris
iv>

--0016e6d99c1d2c278004840e8992--

Re: accessing environment variables set by other modules

am 12.04.2010 21:15:03 von Adam Prime

you might want to take a look at subprocess_env

http://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html# C_subprocess_env_

I don't think i've every tried to use %ENV in a Filter, perhaps it's not
getting populated. %ENV can be a little strange in mp2, have a look at:

http://perl.apache.org/docs/2.0/user/troubleshooting/trouble shooting.html#C_Libraries_Don_t_See_C__ENV__Entries_Set_by_P erl_Code

though that would seem to be unrelated to your issue.

Adam


Chris Datfung wrote:
> I want to use mod-perl to edit server responses under certain
> conditions. My plan was to use various modules, like mod-setenvif and
> mod-security to set an environment variable and then have mod-perl edit
> the response body only run when the environment variable is set. I tried
> the following test which was supposed to append 'TEST' to my index.html
> page:
>
> in the virtual host config I have:
>
> SetEnvIf Request_URI "\.html$" TE=TEST
> PerlRequire "/opt/modperl/TE/ST.pm"
> PerlOutputFilterHandler TE::ST
>
> the contents of /opt/modperl/TE/ST.pm is:
> ============================================================ ==========
> package TE::ST;
>
> use strict;
> use warnings;
>
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use APR::Table ();
>
> BEGIN { push @INC, "/opt/modperl/"; }
>
> use Apache2::Const -compile => qw(OK);
> use constant BUFF_LEN => 1024;
>
> sub handler
> {
> my $f = shift;
>
> unless ($f->ctx)
> {
> while ($f->read(my $buffer, BUFF_LEN))
> {
> $buffer =~ s/It/Chris/g;
> $buffer .= $ENV{"TE"};
> $f->print($buffer);
> }
> return Apache2::Const::OK;
> }
> }
> 1;
> ============================================================ ============
>
> The script correctly changes the 'It' in the index.html file to 'Chris'
> but I don't see the value of the 'TE' variable in the response body. Can
> someone point me to an example of how modperl can
> access environment variables set by other apache modules?
>
> Thanks,
>
> - Chris

Re: accessing environment variables set by other modules

am 13.04.2010 17:29:54 von Chris Datfung

--0015175df08af4dc5d04841fee5e
Content-Type: text/plain; charset=ISO-8859-1

Thanks mate.

Unfortunately neither of those suggestions worked. Any other ideas?

Chris

On Mon, Apr 12, 2010 at 10:15 PM, Adam Prime wrote:

> you might want to take a look at subprocess_env
>
>
> http://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html# C_subprocess_env_
>
> I don't think i've every tried to use %ENV in a Filter, perhaps it's not
> getting populated. %ENV can be a little strange in mp2, have a look at:
>
>
> http://perl.apache.org/docs/2.0/user/troubleshooting/trouble shooting.html#C_Libraries_Don_t_See_C__ENV__Entries_Set_by_P erl_Code
>
> though that would seem to be unrelated to your issue.
>
> Adam
>
>
>
> Chris Datfung wrote:
>
>> I want to use mod-perl to edit server responses under certain conditions.
>> My plan was to use various modules, like mod-setenvif and mod-security to
>> set an environment variable and then have mod-perl edit the response body
>> only run when the environment variable is set. I tried the following test
>> which was supposed to append 'TEST' to my index.html page:
>>
>> in the virtual host config I have:
>>
>> SetEnvIf Request_URI "\.html$" TE=TEST
>> PerlRequire "/opt/modperl/TE/ST.pm"
>> PerlOutputFilterHandler TE::ST
>>
>> the contents of /opt/modperl/TE/ST.pm is:
>> ============================================================ ==========
>> package TE::ST;
>>
>> use strict;
>> use warnings;
>>
>> use Apache2::Filter ();
>> use Apache2::RequestRec ();
>> use APR::Table ();
>>
>> BEGIN { push @INC, "/opt/modperl/"; }
>>
>> use Apache2::Const -compile => qw(OK);
>> use constant BUFF_LEN => 1024;
>>
>> sub handler
>> {
>> my $f = shift;
>>
>> unless ($f->ctx)
>> {
>> while ($f->read(my $buffer, BUFF_LEN))
>> {
>> $buffer =~ s/It/Chris/g;
>> $buffer .= $ENV{"TE"};
>> $f->print($buffer);
>> }
>> return Apache2::Const::OK;
>> }
>> }
>> 1;
>> ============================================================ ============
>>
>> The script correctly changes the 'It' in the index.html file to 'Chris'
>> but I don't see the value of the 'TE' variable in the response body. Can
>> someone point me to an example of how modperl can access environment
>> variables set by other apache modules?
>>
>> Thanks,
>>
>> - Chris
>>
>
>

--0015175df08af4dc5d04841fee5e
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Thanks mate.

Unfortunately =
neither of those suggestions worked. Any other ideas?

iv>
Chris

On Mon, Apr 12, 2010 at 1=
0:15 PM, Adam Prime < onto.ca">adam.prime@utoronto.ca> wrote:

x #ccc solid;padding-left:1ex;">you might want to take a look at subprocess=
_env



bprocess_env_" target=3D"_blank">http://perl.apache.org/docs/2.0/api/Apache =
2/RequestRec.html#C_subprocess_env_




I don't think i've every tried to use %ENV in a Filter, perhaps it&=
#39;s not getting populated. =A0%ENV can be a little strange in mp2, have a=
look at:



ting.html#C_Libraries_Don_t_See_C__ENV__Entries_Set_by_Perl_ Code" target=3D=
"_blank">http://perl.apache.org/docs/2.0/user/troubleshootin g/troubleshooti=
ng.html#C_Libraries_Don_t_See_C__ENV__Entries_Set_by_Perl_Co de





though that would seem to be unrelated to your issue.
8888">


Adam






Chris Datfung wrote:

x #ccc solid;padding-left:1ex">
I want to use mod-perl to edit server responses under certain conditions. M=
y plan was to use various modules, like mod-setenvif and mod-security to se=
t an environment variable and then have mod-perl edit the response body onl=
y run when the environment variable is set. I tried the following test whic=
h was supposed to append 'TEST' to my index.html page:




in the virtual host config I have:



=A0 =A0 =A0 =A0SetEnvIf Request_URI "\.html$" TE=3DTEST

=A0 =A0 =A0 =A0PerlRequire "/opt/modperl/TE/ST.pm"

=A0 =A0 =A0 =A0PerlOutputFilterHandler TE::ST



the contents of /opt/modperl/TE/ST.pm is:

==================== =====3D=
==================== =====3D=
====================

package TE::ST;



use strict;

use warnings;



use Apache2::Filter ();

use Apache2::RequestRec ();

use APR::Table ();



BEGIN { push @INC, "/opt/modperl/"; }



use Apache2::Const -compile =3D> qw(OK);

use constant BUFF_LEN =3D> 1024;



sub handler

{

=A0 =A0 =A0 =A0my $f =3D shift;



=A0 =A0 =A0 =A0unless ($f->ctx)

=A0 =A0 =A0 =A0{

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0while ($f->read(my $buffer, BUFF_LEN)) r>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$buffer =3D~ s/It/Chris/g;<=
br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$buffer .=3D $ENV{"TE&=
quot;};

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$f->print($buffer);

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return Apache2::Const::OK;

=A0 =A0 =A0 =A0}

}

1;

==================== =====3D=
==================== =====3D=
==================== ==



The script correctly changes the 'It' in the index.html file to =
9;Chris' but I don't see the value of the 'TE' variable in =
the response body. Can someone point me to an example of how modperl can ac=
cess environment variables set by other apache modules?




Thanks,



- Chris







--0015175df08af4dc5d04841fee5e--

Re: accessing environment variables set by other modules

am 13.04.2010 17:34:17 von Fred Moyer

Correct me if I'm wrong, but don't you need to do this:

PerlPassEnv TE

in your httpd.conf?

On Mon, Apr 12, 2010 at 11:44 AM, Chris Datfung w=
rote:
> I want to use mod-perl to edit server responses under certain conditions.=
My
> plan was to use various modules, like mod-setenvif and mod-security to se=
t
> an=A0environment=A0variable and then have mod-perl edit the response body=
only
> run when the=A0environment=A0variable is set. I tried the following test =
which
> was supposed to append 'TEST' to my index.html page:
> in the virtual host config I have:
>    =A0 =A0 =A0SetEnvIf Request_URI "\.html$" TE=3DTEST
>    =A0 =A0 =A0PerlRequire "/opt/modperl/TE/ST.pm"
>    =A0 =A0 =A0PerlOutputFilterHandler TE::ST
> the contents of=A0/opt/modperl/TE/ST.pm is:
> ==================== =====
==================== =====3D=
==================== =3D
> package TE::ST;
> use strict;
> use warnings;
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use APR::Table ();
> BEGIN { push @INC, "/opt/modperl/"; }
> use Apache2::Const -compile =3D> qw(OK);
> use constant BUFF_LEN =3D> 1024;
> sub handler
> {
>    =A0 =A0 =A0my $f =3D shift;
>    =A0 =A0 =A0unless ($f->ctx)
>    =A0 =A0 =A0{
>    =A0 =A0 =A0 =A0 =A0 =A0 =A0while ($f->read(my $buffer, BUFF_LEN))
>    =A0 =A0 =A0 =A0 =A0 =A0 =A0{
>    =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$buffer =3D~ s/It/Chris=
/g;
>    =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$buffer .=3D $ENV{"TE"}=
;
>    =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$f->print($buffer);
>    =A0 =A0 =A0 =A0 =A0 =A0 =A0}
>    =A0 =A0 =A0 =A0 =A0 =A0 =A0return Apache2::Const::OK;
>    =A0 =A0 =A0}
> }
> 1;
> ==================== =====
==================== =====3D=
==================== ===3D
> The script correctly changes the 'It' in the index.html file to 'Chris' b=
ut
> I don't see the value of the 'TE' variable in the response body. Can some=
one
> point me to an example of how modperl can access=A0environment=A0variable=
s set
> by other apache modules?
> Thanks,
> - Chris

Re: accessing environment variables set by other modules

am 13.04.2010 20:57:10 von Chris Datfung

--00151747bd942cc02c048422d473
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Apr 13, 2010 at 6:34 PM, Fred Moyer wrote:

> Correct me if I'm wrong, but don't you need to do this:
>
> PerlPassEnv TE
>
> in your httpd.conf?
>
>
Hey Fred,

That looks correct and I added PerlPassEnv to the virtual host config, but I
still don't see the word 'TEST' when accessing
http://localhost/index.htmlwhile using the TE::ST package shown below.
Am I using $ENV{"TE"}
incorrectly? FWIW, I tried using $ENV{TE} with identical results. I also
added PerlPassEnv HOME to the virtual host config and a '$buffer .=
$ENV{HOME}' to the TE::ST package but did not see the path in the
/index.html output either. Any ideas what I'm missing?

Thanks,

Chris


> On Mon, Apr 12, 2010 at 11:44 AM, Chris Datfung
> wrote:
> > I want to use mod-perl to edit server responses under certain conditions.
> My
> > plan was to use various modules, like mod-setenvif and mod-security to
> set
> > an environment variable and then have mod-perl edit the response body
> only
> > run when the environment variable is set. I tried the following test
> which
> > was supposed to append 'TEST' to my index.html page:
> > in the virtual host config I have:
> > SetEnvIf Request_URI "\.html$" TE=TEST
> > PerlRequire "/opt/modperl/TE/ST.pm"
> > PerlOutputFilterHandler TE::ST
> > the contents of /opt/modperl/TE/ST.pm is:
> > ============================================================ ==========
> > package TE::ST;
> > use strict;
> > use warnings;
> > use Apache2::Filter ();
> > use Apache2::RequestRec ();
> > use APR::Table ();
> > BEGIN { push @INC, "/opt/modperl/"; }
> > use Apache2::Const -compile => qw(OK);
> > use constant BUFF_LEN => 1024;
> > sub handler
> > {
> > my $f = shift;
> > unless ($f->ctx)
> > {
> > while ($f->read(my $buffer, BUFF_LEN))
> > {
> > $buffer =~ s/It/Chris/g;
> > $buffer .= $ENV{"TE"};
> > $f->print($buffer);
> > }
> > return Apache2::Const::OK;
> > }
> > }
> > 1;
> > ============================================================ ============
> > The script correctly changes the 'It' in the index.html file to 'Chris'
> but
> > I don't see the value of the 'TE' variable in the response body. Can
> someone
> > point me to an example of how modperl can access environment variables
> set
> > by other apache modules?
> > Thanks,
> > - Chris
>

--00151747bd942cc02c048422d473
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Tue, Apr 13, 2010 at 6:34 PM, Fred Moyer tr"><fred@redhotpenguin.com >> wrote:
0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Correct me if I'm wrong, but don't you need to do this:



PerlPassEnv TE



in your httpd.conf?



Hey=
Fred,

That looks correct and I added PerlPassEnv =
to the virtual host config, but I still don't see the word 'TEST=
9; when accessing
http://localhost/=
index.html
while using the TE::ST package shown below. Am I using $ENV{=
"TE"} incorrectly? FWIW, I tried using $ENV{TE} with identical re=
sults. I also added PerlPassEnv HOME to the virtual host config and a '=
$buffer .=3D $ENV{HOME}' to the TE::ST package but did not see the path=
in the /index.html output either. Any ideas what I'm missing?


Thanks,

Chris
=A0 iv>
:1px #ccc solid;padding-left:1ex;">

On Mon, Apr 12, 2010 at 11:44 AM, Chris Datfung < ..datfung@gmail.com">chris.datfung@gmail.com> wrote:

> I want to use mod-perl to edit server responses under certain conditio=
ns. My

> plan was to use various modules, like mod-setenvif and mod-security to=
set

> an=A0environment=A0variable and then have mod-perl edit the response b=
ody only

> run when the=A0environment=A0variable is set. I tried the following te=
st which

> was supposed to append 'TEST' to my index.html page:

> in the virtual host config I have:

>    =A0 =A0 =A0SetEnvIf Request_URI "\.html$" TE=3DTEST r>
>    =A0 =A0 =A0PerlRequire "/opt/modperl/TE/ST.pm"

>    =A0 =A0 =A0PerlOutputFilterHandler TE::ST

> the contents of=A0/opt/modperl/TE/ST.pm is:

> ==================== ===3D=
==================== =====3D=
==================== ==

> package TE::ST;

> use strict;

> use warnings;

> use Apache2::Filter ();

> use Apache2::RequestRec ();

> use APR::Table ();

> BEGIN { push @INC, "/opt/modperl/"; }

> use Apache2::Const -compile =3D> qw(OK);

> use constant BUFF_LEN =3D> 1024;

> sub handler

> {

>    =A0 =A0 =A0my $f =3D shift;

>    =A0 =A0 =A0unless ($f->ctx)

>    =A0 =A0 =A0{

>    =A0 =A0 =A0 =A0 =A0 =A0 =A0while ($f->read(my $buffer, BUFF_=
LEN))

>    =A0 =A0 =A0 =A0 =A0 =A0 =A0{

>    =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$buffer =3D~ s/It/Ch=
ris/g;

>    =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$buffer .=3D $ENV{&q=
uot;TE"};

>    =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$f->print($buffer=
);

>    =A0 =A0 =A0 =A0 =A0 =A0 =A0}

>    =A0 =A0 =A0 =A0 =A0 =A0 =A0return Apache2::Const::OK;

>    =A0 =A0 =A0}

> }

> 1;

> ==================== ===3D=
==================== =====3D=
==================== ==== >
> The script correctly changes the 'It' in the index.html file t=
o 'Chris' but

> I don't see the value of the 'TE' variable in the response=
body. Can someone

> point me to an example of how modperl can access=A0environment=A0varia=
bles set

> by other apache modules?

> Thanks,

> - Chris




--00151747bd942cc02c048422d473--

Re: accessing environment variables set by other modules

am 14.04.2010 23:17:53 von Chris Datfung

--0003255549fa4b9f39048438e9dd
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Apr 13, 2010 at 9:57 PM, Chris Datfung wrote:

> On Tue, Apr 13, 2010 at 6:34 PM, Fred Moyer wrote:
>
>> Correct me if I'm wrong, but don't you need to do this:
>>
>> PerlPassEnv TE
>>
>
Hi Fred,

After a bit more research, It seems that PerlPassEnv is just for passing
shell environment variables, Apache maintains a separate environment, as
documented here:

http://httpd.apache.org/docs/2.1/env.html

What I'm looking for is a way to access the environment variables stored in
the internal Apache structure. Any leads of how to access these environment
variables is much appreciated.

Thanks,
Chris

--0003255549fa4b9f39048438e9dd
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable



On Tue, Apr 13, 2010 at=
9:57 PM, Chris Datfung < ng@gmail.com">chris.datfung@gmail.com> wrote:
class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid=
;padding-left:1ex;">
On Tue, Apr 13, 2010 at 6:34 PM, Fred Mo=
yer < =3D"_blank">fred@redhotpenguin.com> wrote:
ss=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pa=
dding-left:1ex">

Correct me if I'm wrong, but don't you need to do this:



PerlPassEnv TE

>Hi Fred,

After a bit more research, It seems that=
PerlPassEnv is just for passing shell environment variables, Apache mainta=
ins a=A0separate=A0environment, as documented here:


h=
ttp://httpd.apache.org/docs/2.1/env.html

What =
I'm looking for is a way to access the environment variables stored in =
the internal Apache structure. Any leads of how to access these environment=
variables is much appreciated.


Thanks,
Chris


--0003255549fa4b9f39048438e9dd--

Re: accessing environment variables set by other modules

am 14.04.2010 23:23:38 von Perrin Harkins

On Wed, Apr 14, 2010 at 5:17 PM, Chris Datfung wrote:
> What I'm looking for is a way to access the environment variables stored in
> the internal Apache structure. Any leads of how to access these environment
> variables is much appreciated.

The subprocess_env info that Adam sent you should have worked. Can
you show us what you tried? Can you try it in a response handler to
make sure it's not an odd bug with filters?

- Perrin

Re: accessing environment variables set by other modules

am 15.04.2010 11:10:49 von Chris Datfung

--00151758ac54e85720048442de25
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Apr 15, 2010 at 12:23 AM, Perrin Harkins wrote:

> The subprocess_env info that Adam sent you should have worked. Can
> you show us what you tried? Can you try it in a response handler to
> make sure it's not an odd bug with filters?
>
>
Hi Perrin,

Thanks, subprocess_env works when just using a response handler. When used
with a filter I get the following error message:

============================================================ ======
[Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't locate object
method "subprocess_env" via package "Apache2::Filter" at
/opt/modperl//TE/ST.pm line 40.\n
[Thu Apr 15 11:47:34 2010] [debug] core.c(3765): [client 127.0.0.1]
(500)Unknown error 500: default_handler: ap_pass_brigade returned 500
[Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't locate object
method "subprocess_env" via package "Apache2::Filter" at
/opt/modperl//TE/ST.pm line 40.\n
============================================================ ======

Can I test the script to use Apache2:RequestRec for the subprocess_env?

The source of the package is as follows:
============================================================ ======
package TE::ST;

use strict;
use warnings;
use Apache2::Filter ();
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use APR::Table ();

BEGIN { push @INC, "/opt/modperl/"; }

use Apache2::Const -compile => qw(OK);
use constant BUFF_LEN => 1024;

sub handler
{
my $f = shift;
$f->subprocess_env;
my $envar = $ENV{TE};


unless ($f->ctx)
{
while ($f->read(my $buffer, BUFF_LEN))
{
$buffer =~ s/It/Chris/g;
$buffer .= "[" . $envar . "]";
$f->print($buffer);
}
return Apache2::Const::OK;
}
}
1;
============================================================ ======

Finally, the TE Apache environment variable is set in the virtual host
config via:

SetEnv TE TEST

Thanks,
- Chris

--00151758ac54e85720048442de25
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Thu, Apr 15, 2010 at 12:23 A=
M, Perrin Harkins < m">pharkins@gmail.com> wrote:
uote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1e=
x;">
The subprocess_env info that Adam sent you should have wo=
rked. =A0Can

you show us what you tried? =A0Can you try it in a response handler to

make sure it's not an odd bug with filters?



Hi Perr=
in,

Thanks, subprocess_env works when just using a=
response handler. When used with a filter I get the following error messag=
e:


===================
==================== =====3D=
==================== ===3D
=
[Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't l=
ocate object method "subprocess_env" via package "Apache2::F=
ilter" at /opt/modperl//TE/ST.pm line 40.\n

[Thu Apr 15 11:47:34 2010] [debug] core.c(3765): [client 127.0.0.1] (5=
00)Unknown error 500: default_handler: ap_pass_brigade returned 500
iv>[Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't locate o=
bject method "subprocess_env" via package "Apache2::Filter&q=
uot; at /opt/modperl//TE/ST.pm line 40.\n

==================== ===3D=
==================== =====3D=
==================

=
Can I test the script to use Apache2:RequestRec for the subprocess_env=
?

The source of the package is as follows:

==================== ===3D=
==================== =====3D=
==================
packa=
ge TE::ST;

use strict;
use warnings; v>
use Apache2::Filter ();
use Apache2::RequestRec ();

use Apache2::RequestIO ();
use APR::Table ();

=
BEGIN { push @INC, "/opt/modperl/"; }

div>
use Apache2::Const -compile =3D> qw(OK);
use constant=
BUFF_LEN =3D> 1024;


sub handler=A0
{
   =A0my $f =
=3D shift;
   =A0$f->subprocess_env;
   =A0m=
y $envar =3D $ENV{TE};


   =A0un=
less ($f->ctx)

   =A0{   
   =A0 =A0 =A0while ($f->read(my $=
buffer, BUFF_LEN))
   =A0 =A0 =A0{
   =A0 =A0 =
=A0 =A0 =A0$buffer =3D~ s/It/Chris/g;
   =A0 =A0 =A0 =A0 =A0$=
buffer .=3D "[" . $envar . "]";

   =A0 =A0 =A0 =A0 =A0$f->print($buffer);
   =A0 =A0 =
=A0}
   =A0 =A0 =A0return Apache2::Const::OK;
=A0=
=A0 =A0}
}
1;
===========
==================== =====3D=
==================== =====3D=
======


Finally, the TE Apache environment va=
riable is set in the virtual host config via:

SetE=
nv TE TEST
=A0
Thanks,
- Chris
>


--00151758ac54e85720048442de25--

Re: accessing environment variables set by other modules

am 15.04.2010 12:22:58 von torsten.foertsch

On Thursday 15 April 2010 11:10:49 Chris Datfung wrote:
> Thanks, subprocess_env works when just using a response handler. When used
> with a filter I get the following error message:
>=20
> ==================== =====
==================== =====3D=
=================3D
> [Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't locate object
> method "subprocess_env" via package "Apache2::Filter" at
> /opt/modperl//TE/ST.pm line 40.\n
> [Thu Apr 15 11:47:34 2010] [debug] core.c(3765): [client 127.0.0.1]
> (500)Unknown error 500: default_handler: ap_pass_brigade returned 500
> [Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't locate object
> method "subprocess_env" via package "Apache2::Filter" at
> /opt/modperl//TE/ST.pm line 40.\n
> ==================== =====
==================== =====3D=
=================3D
>=20
> Can I test the script to use Apache2:RequestRec for the subprocess_env?

This is a RTFM question, you know?

http://perl.apache.org/docs/2.0/api/Apache2/Filter.html#C_r_
>=20
> The source of the package is as follows:
> ==================== =====
==================== =====3D=
=================3D
> package TE::ST;
>=20
> use strict;
> use warnings;
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use Apache2::RequestIO ();
> use APR::Table ();
>=20
> BEGIN { push @INC, "/opt/modperl/"; }
>=20
> use Apache2::Const -compile =3D> qw(OK);
> use constant BUFF_LEN =3D> 1024;
>=20
> sub handler
> {
> my $f =3D shift;
> $f->subprocess_env;
> my $envar =3D $ENV{TE};
>=20
>=20
> unless ($f->ctx)
> {
> while ($f->read(my $buffer, BUFF_LEN))
> {
> $buffer =3D~ s/It/Chris/g;
> $buffer .=3D "[" . $envar . "]";
> $f->print($buffer);
> }
> return Apache2::Const::OK;
> }

This will probably not do what you want.

1) It will not change It into Chris at block boundaries. That means

nth block | (n+1) block
...I t...

will not be changed into

nth block | (n+1) block
...C hris...

2) While it may look like a pull-model (due to $f->read) Apache actually us=
es=20
a push-model for output. A filter is passed one chunk of data (called a buc=
ket=20
brigade) at a time. $f->read reads only from the current brigade. When you =
are=20
writing a filter think you are implementing something like the C functions=
=20
write(2) or better writev(2). For a particular output stream your function =
is=20
called many times. Each time it is passed a certain chunk of data. In case =
of=20
writev it gets a list of pointers and lengths and should write all these da=
ta=20
blocks to some file. In case of an Apache filter it is passed a brigade whi=
ch=20
is something similar to a list of pointer, length pairs. What $f->read does=
,=20
it simplifies the reading of data from one of these lists.

Now, with that in mind reread the following chapter, especially the bottom=
=20
half.
http://perl.apache.org/docs/2.0/user/handlers/filters.html#S tream_oriented_=
Output_Filters

Another useful resource may be http://www.apachetutor.org/dev/brigades

Torsten Förtsch

=2D-=20
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net

Re: accessing environment variables set by other modules

am 15.04.2010 20:19:10 von Adam Prime

You need to use $f->r->subprocess_env in the filter. subprocess_env is
a RequestRec method, not a Filter method.

Adam

Chris Datfung wrote:
> On Thu, Apr 15, 2010 at 12:23 AM, Perrin Harkins > > wrote:
>
> The subprocess_env info that Adam sent you should have worked. Can
> you show us what you tried? Can you try it in a response handler to
> make sure it's not an odd bug with filters?
>
>
> Hi Perrin,
>
> Thanks, subprocess_env works when just using a response handler. When
> used with a filter I get the following error message:
>
> ============================================================ ======
> [Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't locate
> object method "subprocess_env" via package "Apache2::Filter" at
> /opt/modperl//TE/ST.pm line 40.\n
> [Thu Apr 15 11:47:34 2010] [debug] core.c(3765): [client 127.0.0.1]
> (500)Unknown error 500: default_handler: ap_pass_brigade returned 500
> [Thu Apr 15 11:47:34 2010] [error] [client 127.0.0.1] Can't locate
> object method "subprocess_env" via package "Apache2::Filter" at
> /opt/modperl//TE/ST.pm line 40.\n
> ============================================================ ======
>
> Can I test the script to use Apache2:RequestRec for the subprocess_env?
>
> The source of the package is as follows:
> ============================================================ ======
> package TE::ST;
>
> use strict;
> use warnings;
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use Apache2::RequestIO ();
> use APR::Table ();
>
> BEGIN { push @INC, "/opt/modperl/"; }
>
> use Apache2::Const -compile => qw(OK);
> use constant BUFF_LEN => 1024;
>
> sub handler
> {
> my $f = shift;
> $f->subprocess_env;
> my $envar = $ENV{TE};
>
>
> unless ($f->ctx)
> {
> while ($f->read(my $buffer, BUFF_LEN))
> {
> $buffer =~ s/It/Chris/g;
> $buffer .= "[" . $envar . "]";
> $f->print($buffer);
> }
> return Apache2::Const::OK;
> }
> }
> 1;
> ============================================================ ======
>
> Finally, the TE Apache environment variable is set in the virtual host
> config via:
>
> SetEnv TE TEST
>
> Thanks,
> - Chris
>

Re: accessing environment variables set by other modules

am 15.04.2010 20:34:31 von aw

Chris Datfung wrote:
> I want to use mod-perl to edit server responses under certain conditions. My
> plan was to use various modules, like mod-setenvif and mod-security to set
> an environment variable and then have mod-perl edit the response body only
> run when the environment variable is set. I tried the following test which
> was supposed to append 'TEST' to my index.html page:
>
> in the virtual host config I have:
>
> SetEnvIf Request_URI "\.html$" TE=TEST
> PerlRequire "/opt/modperl/TE/ST.pm"
> PerlOutputFilterHandler TE::ST
>
....
Hi.
Without entering into the details, and without contradiction with your
original line of enquiry and other responses, you may want to have a
look at the alternatives consisting of :

A)
- conditionally adding a HTTP header to the request at an early stage
(using a mod_perl module instead of mod_setenvif e.g.)
- later checking for the presence of that header and deciding to filter
or not
Adding and retrieving headers is easy with mod_perl.

B)
Or use an early-running mod_perl module to conditionally set a "pnote",
and later checking on that pnote.

The first alternative has the advantage (contrary to Apache internal
variables or pnotes) that even if you proxy the request to another
server, the value you set in the additional HTTP request header is
visible to that other server.
The second is probably more efficient, since you are running mod_perl
anyway, as long as this is within the same server.