trying to add header field using PerlInputFilterHandler to proxy packets

trying to add header field using PerlInputFilterHandler to proxy packets

am 30.06.2009 04:07:15 von Brandon Allgood

This is a multi-part message in MIME format.

------_=_NextPart_001_01C9F927.7E157A1A
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

I am running an apache server 2.2.3 on CentOS 5.2. I have turned on the
proxy with the following lines from my apache.conf:
=20

ProxyRequests On

Order deny,allow
Deny from all
Allow from all


=20
I would like to add a header field to all requests going through the
proxy. After doing a bunch of reading it seemed that setting up a
PerlInputFilterHandler was the right thing to do. I added the following
lines to the apache.conf
=20
PerlModule company::AddHeader
PerlInputFilterHandler company::AddHeader=20
=20
and I wrote the following example handler
=20
package company::AddHeader;
=20
use strict;
use warnings;
=20
use Apache2::Filter ();
use Apache2::RequestRec ();
use APR::Table ();
=20
use Apache2::Const -compile =3D> qw(OK DECLINED);
=20
my $debug =3D 1;
=20
sub handler {
my $f =3D shift;
=20
# if we have already seen this do nothing
return Apache2::Const::DECLINED if $f->ctx;
=20
# get headers
my $headers_in =3D $f->r->headers_in();
=20
# add header field
$headers_in->set("Message","Hi Mom");
$f->r->headers_in($headers_in);
=20
if($debug)
{
open FILE, ">>/tmp/out.log" or die $!;
foreach my $key (keys %{$headers_in})
{
print FILE "$key =3D $headers_in->{$key}\n";
}
close FILE;
}
=20
$f->ctx(1);
=20
return Apache2::Const::OK;
}
1;

As you can see, if debugging is turned on the headers are written to the
file /tmp/out.log. The contents of out.log contains the new header, but
the requests being forwarded by the proxy don't seem to be altered. Why
is the new header not being sent?
=20
I am pretty sure I am missing something very simple, but have spent a
day trying to figure out what it is. Any ideas?
=20
cheers,
Brandon
=20



------_=_NextPart_001_01C9F927.7E157A1A
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable



charset=3Dus-ascii">


class=3D910094901-30062009>I am=20
running an apache server 2.2.3 on CentOS 5.2.  I have turned on the =
proxy=20
with the following lines from my apache.conf:

class=3D910094901-30062009> face=3DArial size=3D2> 

class=3D910094901-30062009><IfModule mod_proxy.c>
ProxyRequests =

On

class=3D910094901-30062009><Proxy *>
    Order=20
deny,allow
    Deny from all
    =
Allow from=20
all
</Proxy>

class=3D910094901-30062009></IfModule>

class=3D910094901-30062009> face=3DArial size=3D2> 

class=3D910094901-30062009>I would=20
like to add a header field to all requests =
going through the=20
proxy.  After doing a bunch of reading it seemed that setting=20
up a PerlInputFilterHandler was the right thing to do.  I =
added the=20
following lines to the apache.conf

class=3D910094901-30062009> face=3DArial size=3D2> 

class=3D910094901-30062009> face=3DArial>PerlModule company::AddHeader
PerlInputFilterHandler=20
company::AddHeader 

 

size=3D2>a class=3D910094901-30062009>nd I wrote the following example=20
handler

class=3D910094901-30062009> 

class=3D910094901-30062009>package=20
company::AddHeader;

 

class=3D910094901-30062009>use=20
strict;
use warnings;

 

class=3D910094901-30062009>use=20
Apache2::Filter ();
use Apache2::RequestRec ();
use APR::Table=20
();

 

class=3D910094901-30062009>use=20
Apache2::Const -compile =3D> qw(OK =
DECLINED);

 

class=3D910094901-30062009>my $debug =
1;

 

class=3D910094901-30062009>sub handler=20
{
  my $f =3D shift;

 

class=3D910094901-30062009>  # if we=20
have already seen this do nothing
  return =
Apache2::Const::DECLINED if=20
$f->ctx;

 

class=3D910094901-30062009>  # get=20
headers
  my $headers_in =
$f->r->headers_in();

 

class=3D910094901-30062009>  # add=20
header field
  $headers_in->set("Message","Hi =
Mom");
 =20
$f->r->headers_in($headers_in);

 

class=3D910094901-30062009> =20
if($debug)
  {
    open FILE, =
">>/tmp/out.log"=20
or die $!;
    foreach my $key (keys=20
%{$headers_in})
    =
{
      print=20
FILE "$key =3D $headers_in->{$key}\n";
   =20
}
    close FILE;
  =
}

 

class=3D910094901-30062009> =20
$f->ctx(1);

 

class=3D910094901-30062009>  return=20
Apache2::Const::OK;
}
1;

class=3D910094901-30062009>As you can=20
see, if debugging is turned on the headers are written to the file=20
/tmp/out.log.  The contents of out.log contains the new header, but =
the=20
requests being forwarded by the proxy don't seem to be altered.  =
Why is the=20
new header not being sent?

class=3D910094901-30062009> 

class=3D910094901-30062009>I am pretty=20
sure I am missing something very simple, but have spent a day trying to =
figure=20
out what it is.  Any ideas?

class=3D910094901-30062009> 

class=3D910094901-30062009>cheers,

class=3D910094901-30062009>Brandon

class=3D910094901-30062009> 

size=3D2>


------_=_NextPart_001_01C9F927.7E157A1A--

Re: trying to add header field using PerlInputFilterHandler to proxy packets

am 30.06.2009 09:59:53 von torsten.foertsch

On Tue 30 Jun 2009, Brandon Allgood wrote:
> PerlInputFilterHandler company::AddHeader
> =A0
> and I wrote the following example handler
> =A0
> package company::AddHeader;
> =A0
> use strict;
> use warnings;
> =A0
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use APR::Table ();
> =A0
> use Apache2::Const -compile =3D> qw(OK DECLINED);
> =A0
> my $debug =3D 1;
> =A0
> sub handler {
> =A0 my $f =3D shift;
> =A0
> =A0 # if we have already seen this do nothing
> =A0 return Apache2::Const::DECLINED if $f->ctx;
> =A0
> =A0 # get headers
> =A0 my $headers_in =3D $f->r->headers_in();
> =A0
> =A0 # add header field
> =A0 $headers_in->set("Message","Hi Mom");
> =A0 $f->r->headers_in($headers_in);
> =A0
> =A0 if($debug)
> =A0 {
> =A0 =A0 open FILE, ">>/tmp/out.log" or die $!;
> =A0 =A0 foreach my $key (keys %{$headers_in})
> =A0 =A0 {
> =A0 =A0 =A0 print FILE "$key =3D $headers_in->{$key}\n";
> =A0 =A0 }
> =A0 =A0 close FILE;
> =A0 }
> =A0
> =A0 $f->ctx(1);
> =A0
> =A0 return Apache2::Const::OK;
> }
> 1;
>
> As you can see, if debugging is turned on the headers are written to
> the file /tmp/out.log. =A0The contents of out.log contains the new
> header, but the requests being forwarded by the proxy don't seem to
> be altered. =A0Why is the new header not being sent?

A request level input filter is called when the request body is read.=20
mod_proxy does this obviously *after* sending the request header to the=20
backend server. Hence, adding an input header at this time is too late.

Why do you want to do that in a filter? Why don't you add it in a=20
request phase prior to response. Fixup would be a good place for=20
example.

PerlFixupHandler "sub { \
use Apache2::RequestRec; \
use Apache2::Const -compile=3D>'DECLINED'; \
$_[0]->headers_in->{Message}=3D'Hi, Mom'; \
return Apache2::Const::DECLINED; \
}"

Another word to your filter, did you know you can remove a filter on=20
first invocation? This way you can avoid the useless

return Apache2::Const::DECLINED if $f->ctx;

sub filter {
my ($f)=3D@_;
# do something
$f->remove;
return Apache2::Const::DECLINED;
}

I haven't benchmarked it but I believe this is faster than return if=20
$f->ctx. But it only makes a difference of course if the filter is=20
called more than once.

Torsten

=2D-=20
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net

Re: trying to add header field using PerlInputFilterHandler to proxy

am 30.06.2009 18:09:37 von William T

On Mon, Jun 29, 2009 at 8:07 PM, Brandon Allgood wrote:
> I am running an apache server 2.2.3 on CentOS 5.2. I have turned on the
> proxy with the following lines from my apache.conf:
>
>
> ProxyRequests On
>
> Order deny,allow
> Deny from all
> Allow from all
>

>

>
> I would like to add a header field to all requests going through the proxy.

If your adding a static header you can just use the Apache directive
'Header'. Using mod_perl to add a static header is somewhat overkil.

-wjt

RE: trying to add header field using PerlInputFilterHandler to proxy packets

am 14.07.2009 23:35:10 von Brandon Allgood

FYI, this answered my question and everything is working.

thanks,
Brandon

-----Original Message-----
From: Torsten Foertsch [mailto:torsten.foertsch@gmx.net]=20
Sent: Tuesday, June 30, 2009 1:00 AM
To: modperl@perl.apache.org
Cc: Brandon Allgood
Subject: Re: trying to add header field using PerlInputFilterHandler to =
proxy packets

On Tue 30 Jun 2009, Brandon Allgood wrote:
> PerlInputFilterHandler company::AddHeader
> =A0
> and I wrote the following example handler
> =A0
> package company::AddHeader;
> =A0
> use strict;
> use warnings;
> =A0
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use APR::Table ();
> =A0
> use Apache2::Const -compile =3D> qw(OK DECLINED);
> =A0
> my $debug =3D 1;
> =A0
> sub handler {
> =A0 my $f =3D shift;
> =A0
> =A0 # if we have already seen this do nothing
> =A0 return Apache2::Const::DECLINED if $f->ctx;
> =A0
> =A0 # get headers
> =A0 my $headers_in =3D $f->r->headers_in();
> =A0
> =A0 # add header field
> =A0 $headers_in->set("Message","Hi Mom");
> =A0 $f->r->headers_in($headers_in);
> =A0
> =A0 if($debug)
> =A0 {
> =A0 =A0 open FILE, ">>/tmp/out.log" or die $!;
> =A0 =A0 foreach my $key (keys %{$headers_in})
> =A0 =A0 {
> =A0 =A0 =A0 print FILE "$key =3D $headers_in->{$key}\n";
> =A0 =A0 }
> =A0 =A0 close FILE;
> =A0 }
> =A0
> =A0 $f->ctx(1);
> =A0
> =A0 return Apache2::Const::OK;
> }
> 1;
>
> As you can see, if debugging is turned on the headers are written to=20
> the file /tmp/out.log. =A0The contents of out.log contains the new=20
> header, but the requests being forwarded by the proxy don't seem to be =

> altered. =A0Why is the new header not being sent?

A request level input filter is called when the request body is read.=20
mod_proxy does this obviously *after* sending the request header to the =
backend server. Hence, adding an input header at this time is too late.

Why do you want to do that in a filter? Why don't you add it in a =
request phase prior to response. Fixup would be a good place for =
example.

PerlFixupHandler "sub { \
use Apache2::RequestRec; \
use Apache2::Const -compile=3D>'DECLINED'; \
$_[0]->headers_in->{Message}=3D'Hi, Mom'; \
return Apache2::Const::DECLINED; \
}"

Another word to your filter, did you know you can remove a filter on =
first invocation? This way you can avoid the useless

return Apache2::Const::DECLINED if $f->ctx;

sub filter {
my ($f)=3D@_;
# do something
$f->remove;
return Apache2::Const::DECLINED;
}

I haven't benchmarked it but I believe this is faster than return if =
$f->ctx. But it only makes a difference of course if the filter is =
called more than once.

Torsten

--
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net