HomePage RecentChanges ModPerl Filters

Writing mod_perl filters for Apache2

Apache 2 as a Pipeline

Handler - Server life cycle

Server startup, stop, and log handling

Handler - Protocols

Lets say you want to write SMTP... See Apache2::Protocol::EMSTP

Handler - Fllters

Filters... In, Out and all about

Handler - HTTP Handlers

This set is specific to HTTP Handlers.

Handler - HTTP Handlers

HTTP continued...

My First Filter

A basic filter to strip CR and LF from HTML. Start with the usual setup.

#!Perl
package MyApache2::FilterObfuscate;
use strict;
use warnings;
use Apache2::Filter ();
use Apache2::RequestRec ();
use APR::Table ();
use Apache2::Const -compile => qw(OK DECLINED);

My First Filter

#!Perl
sub handler {
	my $f = shift;

	unless ($f->ctx) {
		$f->r->headers_out->unset('Content-Length');
		$f->ctx(1);
	}

	while ($f->read(my $buffer, 1024)) {
		$buffer =~ s/[\r\n]//g;
		$f->print($buffer);
	}

	return Apache2::Const::OK;
}
1;

My First Filter

Restrict your filter to html files only. This is a good way to allow non HTML file. E.g. CSS or XML.

NOTE: This could break those files.

<Files *.html>
	PerlOutputFilterHandler MyApache2::FilterObfuscate
</Files>

My First Filter

Another way to restrict...

#!Perl
...
        my $ctype = $r->content_type;
        $ctype =~ s{;.*}{};
        unless ($ctype eq "text/html") {
                $log->info( "skipping request to ", $r->uri, " (not a text/html)" );
                return DECLINED;
        }
...

What do you mean only once...

So how do we buffer

"$f->ctx" represents a data storage point.

#!Perl
	# Get wht we have now
        my $ctx = $f->ctx;
        while ($f->read(my $buffer, 4096)) {
                $ctx .= $buffer;
        }

	# Seen the end? Not yet, Save CTX and return OK
        unless ($f->seen_eos) {
                $f->ctx( $ctx );
                return OK
        }

So how do we buffer

Finally process what you have, output it and return OK

#!Perl
        if ($ctx) {
                $ctx =~ s|OSDC2007|OSDC2008|g;
                $r->headers_out->unset( 'Content-Length' );
                $f->print( $ctx );
        }
	return OK;

The better way

In this case it would be better to only store what is not translated. So you only need to keep, in this case, up to 7 characters - in case they match O or OS or OSD or OSDC ...

Lack of understanding...

Talk History

Software error:

Can't locate object method "endform" via package "CGI" at /data/scott.dd.com.au/wiki/modules/search.pl line 15.

For help, please send mail to the webmaster (webmaster@dd.com.au), giving this error message and the time and date of the error.