Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

trying to add header field using PerlInputFilterHandler to proxy packets

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


allgood at numerate

Jun 29, 2009, 7:07 PM

Post #1 of 4 (597 views)
Permalink
trying to add header field using PerlInputFilterHandler to proxy packets

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:

<IfModule mod_proxy.c>
ProxyRequests On
<Proxy *>
Order deny,allow
Deny from all
Allow from all
</Proxy>
</IfModule>

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

PerlModule company::AddHeader
PerlInputFilterHandler company::AddHeader

and I wrote the following example handler

package company::AddHeader;

use strict;
use warnings;

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

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

my $debug = 1;

sub handler {
my $f = shift;

# if we have already seen this do nothing
return Apache2::Const::DECLINED if $f->ctx;

# get headers
my $headers_in = $f->r->headers_in();

# add header field
$headers_in->set("Message","Hi Mom");
$f->r->headers_in($headers_in);

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

$f->ctx(1);

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?

I am pretty sure I am missing something very simple, but have spent a
day trying to figure out what it is. Any ideas?

cheers,
Brandon


torsten.foertsch at gmx

Jun 30, 2009, 12:59 AM

Post #2 of 4 (565 views)
Permalink
Re: trying to add header field using PerlInputFilterHandler to proxy packets [In reply to]

On Tue 30 Jun 2009, Brandon Allgood wrote:
> PerlInputFilterHandler company::AddHeader
>  
> and I wrote the following example handler
>  
> package company::AddHeader;
>  
> use strict;
> use warnings;
>  
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use APR::Table ();
>  
> use Apache2::Const -compile => qw(OK DECLINED);
>  
> my $debug = 1;
>  
> sub handler {
>   my $f = shift;
>  
>   # if we have already seen this do nothing
>   return Apache2::Const::DECLINED if $f->ctx;
>  
>   # get headers
>   my $headers_in = $f->r->headers_in();
>  
>   # add header field
>   $headers_in->set("Message","Hi Mom");
>   $f->r->headers_in($headers_in);
>  
>   if($debug)
>   {
>     open FILE, ">>/tmp/out.log" or die $!;
>     foreach my $key (keys %{$headers_in})
>     {
>       print FILE "$key = $headers_in->{$key}\n";
>     }
>     close FILE;
>   }
>  
>   $f->ctx(1);
>  
>   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?

A request level input filter is called when the request body is read.
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=>'DECLINED'; \
$_[0]->headers_in->{Message}='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)=@_;
# 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[at]gmx.net


dietbuddha at gmail

Jun 30, 2009, 9:09 AM

Post #3 of 4 (568 views)
Permalink
Re: trying to add header field using PerlInputFilterHandler to proxy packets [In reply to]

On Mon, Jun 29, 2009 at 8:07 PM, Brandon Allgood<allgood[at]numerate.com> 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:
>
> <IfModule mod_proxy.c>
> ProxyRequests On
> <Proxy *>
> Order deny,allow
> Deny from all
> Allow from all
> </Proxy>
> </IfModule>
>
> 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


allgood at numerate

Jul 14, 2009, 2:35 PM

Post #4 of 4 (452 views)
Permalink
RE: trying to add header field using PerlInputFilterHandler to proxy packets [In reply to]

FYI, this answered my question and everything is working.

thanks,
Brandon

-----Original Message-----
From: Torsten Foertsch [mailto:torsten.foertsch[at]gmx.net]
Sent: Tuesday, June 30, 2009 1:00 AM
To: modperl[at]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
>  
> and I wrote the following example handler
>  
> package company::AddHeader;
>  
> use strict;
> use warnings;
>  
> use Apache2::Filter ();
> use Apache2::RequestRec ();
> use APR::Table ();
>  
> use Apache2::Const -compile => qw(OK DECLINED);
>  
> my $debug = 1;
>  
> sub handler {
>   my $f = shift;
>  
>   # if we have already seen this do nothing
>   return Apache2::Const::DECLINED if $f->ctx;
>  
>   # get headers
>   my $headers_in = $f->r->headers_in();
>  
>   # add header field
>   $headers_in->set("Message","Hi Mom");
>   $f->r->headers_in($headers_in);
>  
>   if($debug)
>   {
>     open FILE, ">>/tmp/out.log" or die $!;
>     foreach my $key (keys %{$headers_in})
>     {
>       print FILE "$key = $headers_in->{$key}\n";
>     }
>     close FILE;
>   }
>  
>   $f->ctx(1);
>  
>   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?

A request level input filter is called when the request body is read.
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=>'DECLINED'; \
$_[0]->headers_in->{Message}='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)=@_;
# 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[at]gmx.net

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.