Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
P is for Practical
 
PerlMonks  

Apache::Filter and Apache::RegistryFilter

by thraxil (Prior)
on Nov 19, 2003 at 06:07 UTC ( #308230=perlquestion: print w/ replies, xml ) Need Help??
thraxil has asked for the wisdom of the Perl Monks concerning the following question:

i'm trying to figure out how to write filters using Apache::Filter.

by modifying recipe 15.4 in the Mod_Perl Developer's Cookbook, i've got the following toy filter which just lowercases pages:

package Apache::LC; use Apache::Constants qw(OK DECLINED NOT_FOUND); use Apache::File; use Apache::Log; use strict; sub handler { my $r = shift; return DECLINED unless $r->content_type eq 'text/html'; my $fh = undef; if (lc $r->dir_config('Filter') eq 'on') { $r = $r->filter_register; ($fh,my $status) = $r->filter_input; return $status unless $status == OK; } else { $fh = Apache::File->new($r->filename); return DECLINED unless $fh; } my $dirty = do {local $/; <$fh>}; $r->send_http_header('text/html'); print lc $dirty; return OK; } 1;

with the following in my apache conf, it works just like you'd expect on static html files:

PerlModule Apache::LC PerlModule Apache::Filter <Location /test/filter/> PerlSetVar Filter On SetHandler perl-script PerlHandler Apache::LC </Location>

i'm having trouble getting it to filter the output of Registry scripts though. my understanding is that Apache::RegistryFilter should take care of it. i tried:

PerlModule Apache::LC PerlModule Apache::Filter PerlModule Apache::RegistryFilter <Location /perl/*.pl> PerlSetVar Filter On SetHandler perl-script PerlHandler Apache::RegistryFilter Apache::LC Options -Indexes ExecCGI PerlSendHeader On </Location>

this just spits out the source code for the script. if i remove the 'Apache::LC' from the end of the PerlHandler line, it works like a regular Registry script.

i tried making some other filters from the same template and i could chain them all together without any problems. i just can't seem to get any registry type action in. i also tried some other filter aware modules from CPAN like Apache::Dynagzip, and those seemed to work ok, even with RegistryFilter scripts.

at this point, i'm really not sure if it's a problem with my module, or with the apache config. can anyone spot any obvious problems with either or has anyone else done something similar and have any tips on getting it working?

(this is all with Apache 1.3.28, mod_perl 1.27, and perl 5.8.0)

Comment on Apache::Filter and Apache::RegistryFilter
Select or Download Code
Re: Apache::Filter and Apache::RegistryFilter
by Roger (Parson) on Nov 19, 2003 at 06:15 UTC
    I thought the documentation of Apache::RegistryFilter described the usage of the filter as a filter chain. A wild guess is that you probably need to swap the order of your PerlHandler line, so that the registry filter appears at the end of the chain -
    PerlHandler Apache::LC Apache::RegistryFilter

      according to the Apache::Filter documentation, the filters are listed in forward order, so putting Apache::LC first lowercases the perl script, and then hands it to RegistryFilter to execute, which isn't what i want at all (and produces blank output when i try it).

Re: Apache::Filter and Apache::RegistryFilter
by CountZero (Chancellor) on Nov 19, 2003 at 07:32 UTC
    Just a wild guess: in your script you check for "Filter" to be equal to 'on' and in your config file you set filter to be equal to 'On'. Perhaps that is why the filter doesn't work? (on second thought, then your static files wouldn't work either)

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Apache::Filter and Apache::RegistryFilter
by perrin (Chancellor) on Nov 19, 2003 at 19:37 UTC
    It sounds like you are not setting the content-type. Add a call to content-type or apache_send_header() in your Registry script.

      i think you've got it. i changed my test script from:

      #!/usr/bin/perl use CGI; my $cgi = new CGI(); print $cgi->header(); foreach my $key (keys %ENV) { print "$key: $ENV{$key}<br />\n"; }

      to:

      #!/usr/bin/perl use Apache; my $r = Apache->request; $r->content_type("text/html"); $r->send_http_header; foreach my $key (keys %ENV) { print "$key: $ENV{$key}<br />\n"; }

      and it seems to work now.

      do you know of any way to make it work with the former style. i usually take as much advantage of Registry's CGI emulation as possible so i can run scripts as either CGI or Registry. i know i can just put:

      if ($ENV{MOD_PERL}) { use Apache; my $r = Apache->request; $r->content_type("text/html"); $r->send_http_header; } else { print $cgi->header(); }

      into everything, but i'd rather not have to retrofit all my apps just to make them filterable. at least i know that if i upgrade to apache2, i can make anything filterable with the native filtering API. :)

        You could set the content-type in your httpd.conf, either by file extension or with a directive inside your Location block. A better solution would be modifying Apache::Filter so it notices when you have already sent headers.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://308230]
Approved by Roger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-04-19 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls