mrc has asked for the wisdom of the Perl Monks concerning the following question:

I have this perl filter.
package Test::Filter; use strict; use warnings; use Apache2::Filter (); use Apache2::RequestRec (); use Apache2::Const -compile => qw(OK DECLINED); sub handler { my $f = shift; my $r = $f->r; my $content_type = $r->content_type(); my $handler = $r->handler; my $encoding = $r->content_encoding(); unless (defined ($content_type)) {$content_type =0} unless (defined ($handler)) {$handler =0} unless (defined ($encoding)) {$encoding =0} open (LOGG, ">>", "/path/to/log"); print LOGG "Encoding: $encoding \nContent: $content_type \nHandler: $h +andler \n"; close LOGG; while ($f->read(my $buffer, 1024)) { $buffer =~ s!(<body>)!$1<h1>TEST</h1>!i; #if ($encoding =~ /gzip/i) {#do something} $f->print($buffer); } return Apache2::Const::OK; } 1;

http://www.gidnetwork.com/tools/gzip-test.php shows correctly content encoding for a smforum (gzip).
However my filter always return not defined value for $encoding value :(

I want to use this filter to automatically add html code for compressed content as well. Filter working without problem for uncompressed content.
Any help would be greatly appreciated. Thank you!

Replies are listed 'Best First'.
Re: Perl filter
by Khen1950fx (Canon) on Dec 20, 2009 at 08:06 UTC
    Apache2::Filter was built under 5.006, so you'd have to use the two-argument open instead of a three-argument open something like:

    open($fh, '>>').

    The three-argument open came later.

      Hmm, but open has nothing to do with Apache2::Filter. I was able to append those values without problem.
      Btw, I'm using now Compress::Zlib to uncompress the buffer, manipulate it, then compress it again. Working just fine.
      To check encoding I'm using now
      $r->headers_out->{"Content-Encoding"};

      I'm very happy for finding out the solution myself after 2 days of tests :)