package Example::RewriteStuff;
use strict;
use Apache2::Filter ();
use Apache2::Const -compile => qw(OK OR_ALL TAKE2);
use Apache2::Module ();
use Apache2::CmdParms;
use Apache2::RequestRec ();
use APR::Table;
use constant BUFF_LEN => 1024;
# There is perldoc at the end of this file.
my @directives = (
{
name => 'RewriteWhatToWhat',
func => __PACKAGE__ . '::RewriteWhatToWhat',
req_override => Apache2::Const::OR_ALL,
args_how => Apache2::Const::TAKE2,
errmsg => 'RewriteWhatToWhat FROM-THING TO-THING',
},
);
Apache2::Module::add(__PACKAGE__, \@directives);
sub RewriteWhatToWhat {
my ($self, $parms, @args) = @_;
my $srv_cfg = Apache2::Module::get_config($self, $parms->server);
($srv_cfg->{from},
$srv_cfg->{to}) = map quotemeta, @args;
}
sub handler {
my $f = shift;
# unset Content-Length but only on the first bucket per response.
unless ($f->ctx) {
$f->r->headers_out->unset('Content-Length');
$f->ctx(1);
}
# read from the incoming brigade
while ($f->read(my $buffer, BUFF_LEN)) {
# get the config for this module,
# so we can access the directive's arguments
my $cfg = Apache2::Module::get_config(__PACKAGE__, $f->r->server);
# do the switch!
$buffer =~ s/$cfg->{from}/$cfg->{to}/g;
$f->print($buffer); # write to the outgoing brigade
}
return Apache2::Const::OK;
}
###
1;#
###
=head1 NAME
Example::RewriteStuff
=head1 SYNOPSIS
The following is not Perl; it goes in your Apache vhost configuration.
(for example, /etc/apache2/sites-enabled/000-default)
PerlLoadModule Example::RewriteStuff
PerlOutputFilterHandler Example::RewriteStuff
RewriteWhatToWhat foo bar
=head1 DESCRIPTION
This filter rewrites files as Apache serves them, substituting one string
for another (for example, "foo" becomes "bar").
This is an output filter for Apache2. It will NOT work with apache 1.3.
You do not have to write a perl program to use this module, but you
do need to futz with your apache configuration. See the L for
a somewhat complete example.
To use this module as shown, you will need:
* Apache version 2
* Mod_perl version 2
=head1 Directives
=over 12
=item C
Takes two arguments, the string to find, and the string to replace it with.
RewriteWhatToWhat foo bar
All instances of "foo" in the affected pages will be changed to "bar".
=back
=head1 LICENSE
Public domain. You are free to use this example module in any way you like.
=head1 AUTHOR
Beth Skwarecki
=head1 SEE ALSO
Apache2::Filter
mod_perl
The excellent tutorial at http://perl.apache.org/docs/2.0/user/handlers/filters.html
=cut