diff options
Diffstat (limited to 'lib/Apache/Inject')
-rw-r--r-- | lib/Apache/Inject/Filter.pm (renamed from lib/Apache/Inject/Handler.pm) | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/lib/Apache/Inject/Handler.pm b/lib/Apache/Inject/Filter.pm index ae81fbb..f87cdc1 100644 --- a/lib/Apache/Inject/Handler.pm +++ b/lib/Apache/Inject/Filter.pm @@ -1,9 +1,10 @@ -package Apache::Inject::Handler; +package Apache::Inject::Filter; use 5.010000; use strict; use mod_perl2; +use base qw/Apache2::Filter/; use Apache2::Const qw/OK DECLINED/; use Apache2::Log (); use Apache2::RequestRec (); @@ -37,53 +38,55 @@ my $doc = qr{ \z }xmsi; -sub handler { - my $r = shift; +sub handler : FilterRequestHandler { + my $f = shift; - return DECLINED if not $r->content_type eq 'text/html'; + return DECLINED if not $f->r->content_type; + return DECLINED if not $f->r->content_type =~ m{^text/html($|;.*)}; - my $content = ${$r->slurp_filename}; - return DECLINED if not $content =~ /$doc/; - - if (not $r->document_root) { - $r->warn('Inject: Declining request due to empty document root'); + if (not $f->r->document_root) { + $f->r->warn('Inject: Declining due to empty document root'); return DECLINED; } - print $+{head} if $+{head}; - inject($r, "InjectHead"); - print $+{body} if $+{body}; - inject($r, "InjectFoot"); - print $+{rest} if $+{rest}; + my ($buf, $content); + $content .= $buf while $f->read($buf); + return DECLINED if not $content =~ /$doc/; + + $f->print($+{head}) if $+{head}; + inject($f, "InjectHead"); + $f->print($+{body}) if $+{body}; + inject($f, "InjectFoot"); + $f->print($+{rest}) if $+{rest}; return OK; } sub inject { - my ($r, $var) = @_; + my ($f, $var) = @_; # Retrieve value implicitly set by Inject directive - return if not (my $val = $r->dir_config($var)); + return if not (my $val = $f->r->dir_config($var)); return if $val eq '-'; # special value signifying absence of argument # Validate path if ($val =~ m{^/}) { - $r->log_error("Inject: $var should not begin with slash, as it is already always relative to document root"); + $f->r->log_error("Inject: $var should not begin with slash, as it is already always relative to document root"); } if ($val =~ m{^../|/../|/..$}) { - $r->log_error("Inject: $var cannot extend past document root"); + $f->r->log_error("Inject: $var cannot extend past document root"); return; } # note: document root has been confirmed not to be empty - my $root = $r->document_root; + my $root = $f->r->document_root; # Read contents of specified file open my $fh, '<', "$root/$val" or do { - $r->log_error("Inject: $var $root/$val does not exist"); + $f->r->log_error("Inject: $var $root/$val does not exist"); return; }; - print for <$fh>; + $f->print($_) for <$fh>; close $fh; } |