Apache Hacks: Beth Skwarecki
Apache Hacks: Beth Skwarecki
Beth Skwarecki
Pittsburgh Perl Workshop
2007-10-13
PerlLoadModule Example::RewriteStuff
<Files *.html>
PerlOutputFilterHandler Example::RewriteStuff
RewriteWhatToWhat foo bar
</Files>
How Filters Work
Essential Parts of this Filter
package Example::RewriteStuff;
Apache2::Const - constants
Apache2::CmdParms – $parms
Apache2::RequestRec – request object $r
APR::Table – unset Content-Length
Define your Directives and Add
your Module
my @directives = (
{
name => 'RewriteWhatToWhat',
args_how => Apache2::Const::TAKE2,
errmsg => 'Args: FROM-STRING TO-STRING',
},
);
Apache2::Module::add(__PACKAGE__, \@directives);
my $srv_cfg = Apache2::Module::get_config
($self, $parms->server);
}
(This goes in Example::RewriteStuff)
The Handler
sub handler{
my $f = shift;
# [unset Content-length here]
$f->ctx()
$f->ctx({ somedata => “thing to
save” });
What are Filters Good For?
PerlModule Example::MyRegistry
PerlHandler Example::MyRegistry
compile($eval);
$r->stash_rgy_endav($script_name);
if ($@) {
# your code here
xlog_error($r, $@);
return SERVER_ERROR unless $Debug && $Debug &
2;
return Apache::Debug::dump($r, SERVER_ERROR);
}
# ...
(This is in Example::MyRegistry)
Catch a Runtime Error
use Example::Purgatory; # catches $SIG{__DIE__}
# ...
if($errsv) {
# your code here
xlog_error($r, $errsv);
return SERVER_ERROR unless $Debug && $Debug
& 2;
return Apache::Debug::dump($r, SERVER_ERROR);
}
# ...
(This is in Example::MyRegistry)
Email the Backtrace
use Carp::Heavy;
use Mail::Sendmail;
$SIG{__DIE__} = sub {
sendmail (
To => 'developers@yourcompany',
Subject => “$page died”,
Body => Carp::longmess_heavy()
);
} (This is in Example::Purgatory)
The End!
http://bethskwarecki.com/ppw2007
Apache Hacks
Beth Skwarecki
Pittsburgh Perl Workshop
2007-10-13
PerlLoadModule Example::RewriteStuff
<Files *.html>
PerlOutputFilterHandler Example::RewriteStuff
RewriteWhatToWhat foo bar
</Files>
Apache2::Const - constants
Apache2::CmdParms – $parms
Apache2::RequestRec – request object $r
APR::Table – unset Content-Length
6
Apache2::Module::add(__PACKAGE__, \@directives);
my $srv_cfg = Apache2::Module::get_config
($self, $parms->server);
}
(This goes in Example::RewriteStuff) 8
The Handler
sub handler{
my $f = shift;
# [unset Content-length here]
$f->ctx()
$f->ctx({ somedata => “thing to
save” });
10
What are Filters Good For?
11
12
Hacking Apache::Registry
( Major Surgery)
This next example uses Apache 1.3
13
Actually, It's Really Easy
PerlModule Example::MyRegistry
PerlHandler Example::MyRegistry
14
Example: mv /usr/lib/perl5/Apache/Registry.pm
/usr/local/lib/site_perl/Example/MyRegistry.pm
compile($eval);
$r->stash_rgy_endav($script_name);
if ($@) {
# your code here
xlog_error($r, $@);
return SERVER_ERROR unless $Debug && $Debug &
2;
return Apache::Debug::dump($r, SERVER_ERROR);
}
# ...
(This is in Example::MyRegistry)
15
Catch a Runtime Error
use Example::Purgatory; # catches $SIG{__DIE__}
# ...
if($errsv) {
# your code here
xlog_error($r, $errsv);
return SERVER_ERROR unless $Debug && $Debug
& 2;
return Apache::Debug::dump($r, SERVER_ERROR);
}
# ...
(This is in Example::MyRegistry)
16
$SIG{__DIE__} = sub {
sendmail (
To => 'developers@yourcompany',
Subject => “$page died”,
Body => Carp::longmess_heavy()
);
} (This is in Example::Purgatory) 17
The End!
http://bethskwarecki.com/ppw2007
18