Re: filter->remove

Re: filter->remove

am 18.04.2008 10:35:02 von torsten.foertsch

--Boundary-00=_20FCIoRC8lddIU/
Content-Type: text/plain;
charset="iso-8859-15"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On Thu 17 Apr 2008, woinshet abdella wrote:
> I am sorry to write you directly, but did not get any response from the
> modperl user list. I would appreciate you help in advance!

Please ask on the modperl list in the future. If you don't get an answer tr=
y=20
it again. We all are sometimes too busy to answer questions on the list. So=
=20
be patient, polite but insistent.

> Last time you send me the following code. I tried it for the last serveral
> days but I did not succeed, it is not REMOVING the filter from the chain
>
> unless($f->ctx ) {
> =A0 unless( $f->r->headers_in->{'User-Agent'} eq 'Wanted' ) {
> =A0 =A0 $f->remove;
> =A0 =A0 return Apache2::Const::DECLINED;
> =A0 }
> }
>
> Environment:
>
> Red Hat Enterprise Linux
> Apache/2.0.46
> perl, v5.8.0

Those are quite old. If your mod_perl is that old as well that may be the=20
reason. I have attached 2 very simple modules that I have used for some tim=
e.=20
Apache2::PrintFilterChain prints the current output filter chain to the=20
error_log. Apache2::Remove... removes the next filter in the chain if the=20
content type of the document being delivered is not text/html. Both modules=
=20
remove themselves on the first invocation. Hence they are called only once=
=20
per document.

PerlOutputFilterHandler Apache2::PrintFilterChain
PerlOutputFilterHandler Apache2::RemoveNextFilterIfNotTextHtml
PerlSetOutputFilter INCLUDES
PerlOutputFilterHandler Apache2::PrintFilterChain

If used on a HTML document you should see in the output of the first=20
invocation of PrintFilterChain 2 times PrintFilterChain, once the filter=20
removing filter and once INCLUDES. The second PrintFilterChain will show=20
INCLUDES and one PrintFilterChain:

Here an output example for text/plain:

=46ilter Chain:
modperl_request_output <-- the first PrintFilterChain
modperl_request_output <-- RemoveNextFilterIfNotTextHtml
includes
modperl_request_output <-- PrintFilterChain again
byterange
content_length
http_header
http_outerror
log_input_output
core

=46ilter Chain:
modperl_request_output <-- the second PrintFilterChain
byterange
content_length
http_header
http_outerror
log_input_output
core

You see the 3 filters above the second PrintFilterChain have gone.

Now a text/html doc:

=46ilter Chain: <-- the 1st invocation sees the same c=
hain
modperl_request_output
modperl_request_output
includes
modperl_request_output
byterange
content_length
http_header
http_outerror
log_input_output
core

=46ilter Chain:
includes <-- but now the includes filter remains
modperl_request_output
byterange
content_length
http_header
http_outerror
log_input_output
core


Hope that helps.

Torsten

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

--Boundary-00=_20FCIoRC8lddIU/
Content-Type: application/x-perl-module;
name="PrintFilterChain.pm"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="PrintFilterChain.pm"

package Apache2::PrintFilterChain;

use strict;
use Apache2::Filter (); # $f
use Apache2::FilterRec (); # $f
use Apache2::RequestRec (); # $r
use Apache2::Const -compile => qw(OK DECLINED);

sub handler {
my $f = shift;
my $r = $f->r;

print STDERR "\n\nFilter Chain:\n";
for( my $filter=$r->output_filters; $filter; $filter=$filter->next ) {
print STDERR " ", $filter->frec->name, "\n";
}
print STDERR "\n";
$f->remove;

return Apache2::Const::DECLINED;
}

1;

--Boundary-00=_20FCIoRC8lddIU/
Content-Type: application/x-perl-module;
name="RemoveNextFilterIfNotTextHtml.pm"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="RemoveNextFilterIfNotTextHtml.pm"

package Apache2::RemoveNextFilterIfNotTextHtml;

use strict;
use Apache2::Filter ();
use Apache2::FilterRec ();
use Apache2::RequestRec ();
use Apache2::Const -compile => qw(OK DECLINED);

sub handler {
my $f = shift;
my $r = $f->r;

# we only process HTML documents
unless( $r->content_type =~ m!text/html!i ) {
eval {
$f->next->remove;
};
}
$f->remove;

return Apache2::Const::DECLINED;
}

1;

--Boundary-00=_20FCIoRC8lddIU/--

Re: filter->remove

am 18.04.2008 17:20:16 von torsten.foertsch

On Fri 18 Apr 2008, adam.prime@utoronto.ca wrote:
> This leads me to believe that i could write a filter that sat at the =C2=
=A0
> head of the chain, which looked through all the filters in the chain, =C2=
=A0
> and if appropriate removed random ones through the chain.
>
> I have a situation where i have a CGI which sometimes returns  
> text/html, and sometimes returns text/csv, but IE chokes if the csv is =
 
> gzipped by deflate.  I've had to disable deflate on ALL cgi's as a =
 
> result, but this seems like i could just create a filter that runs on =C2=
=A0
> CGI's, that looks for a pnote that indicates that deflate should be  
> taken out of the chain.
>
> Is that correct?

Yes, I think that is correct. In fact I believe to remember there is a test=
in=20
the modperl test suite that does just that.

Here it is: t/filter/both_str_native_remove.t

Torsten

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