aboutsummaryrefslogtreecommitdiff
path: root/lib/Apache/Inject/Handler.pm
diff options
context:
space:
mode:
authorroot <root@rbsd.ankarstrom.se>2021-04-26 23:36:30 +0000
committerroot <root@rbsd.ankarstrom.se>2021-04-26 23:36:30 +0000
commit356ace1a5f618d00f145b640acb989dd0197e8d6 (patch)
tree0d04c610d49a213d4be1403bb81954e7ef7e6677 /lib/Apache/Inject/Handler.pm
parent07b6aaa59257778fba1f805ee4051baf1917980b (diff)
downloadApache-Inject-356ace1a5f618d00f145b640acb989dd0197e8d6.tar.gz
Use filter instead of handler
This makes it work for HTML content that has already been processed, e.g., by PHP. This change was delightfully easy to make.
Diffstat (limited to 'lib/Apache/Inject/Handler.pm')
-rw-r--r--lib/Apache/Inject/Handler.pm90
1 files changed, 0 insertions, 90 deletions
diff --git a/lib/Apache/Inject/Handler.pm b/lib/Apache/Inject/Handler.pm
deleted file mode 100644
index ae81fbb..0000000
--- a/lib/Apache/Inject/Handler.pm
+++ /dev/null
@@ -1,90 +0,0 @@
-package Apache::Inject::Handler;
-
-use 5.010000;
-use strict;
-
-use mod_perl2;
-use Apache2::Const qw/OK DECLINED/;
-use Apache2::Log ();
-use Apache2::RequestRec ();
-use Apache2::RequestUtil ();
-
-my $doc = qr{
- \A
- (?<head> \s*
- ( <!-- .*? --> )? \s*
- ( <!doctype[^>]*> )? \s*
- ( <!-- .*? --> )? \s*
- ( <html[^>]*> )? \s*
- ( <!-- .*? --> )? \s*
- ( <head[^>]*> .*? </head> \s*
- | ( <meta[^>]*> \s*
- | <link[^>]*> \s*
- | <title[^>]*> .*? </title> \s*
- | <style[^>]*> .*? </style> \s*
- | <script[^>]*> .*? </script> \s*
- | <base[^>]*> \s*
- | <!-- .*? --> \s*
- )+
- )?
- ( <!-- .*? --> )? \s*
- ( <body[^>]*> )? \s*
- )?
- (?<body> .*? )
- (?<rest> </html> \s*
- ( <!-- .*? --> )? \s*
- )?
- \z
-}xmsi;
-
-sub handler {
- my $r = shift;
-
- return DECLINED if not $r->content_type eq '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');
- return DECLINED;
- }
-
- print $+{head} if $+{head};
- inject($r, "InjectHead");
- print $+{body} if $+{body};
- inject($r, "InjectFoot");
- print $+{rest} if $+{rest};
-
- return OK;
-}
-
-sub inject {
- my ($r, $var) = @_;
-
- # Retrieve value implicitly set by Inject directive
- return if not (my $val = $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");
- }
- if ($val =~ m{^../|/../|/..$}) {
- $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;
-
- # Read contents of specified file
- open my $fh, '<', "$root/$val" or do {
- $r->log_error("Inject: $var $root/$val does not exist");
- return;
- };
- print for <$fh>;
- close $fh;
-}
-
-1;