diff options
author | root <root@rbsd.ankarstrom.se> | 2021-04-26 12:26:06 +0000 |
---|---|---|
committer | root <root@rbsd.ankarstrom.se> | 2021-04-26 12:26:06 +0000 |
commit | e1c7d5442ca1d3bd67c200d2dbd94bd0e40e72bd (patch) | |
tree | fe2b3bb1c7e3612f05311d591f3704deb912fcd4 | |
parent | e455862603e1ae1842b26bc9cbe685e5c4ee2d90 (diff) | |
download | Apache-Inject-e1c7d5442ca1d3bd67c200d2dbd94bd0e40e72bd.tar.gz |
Use single hyphen for NULL value, allow the user to specify it
This lets the user inject a footer without injecting a header.
-rw-r--r-- | lib/Apache/Inject.pm | 76 | ||||
-rw-r--r-- | lib/Apache/Inject/Handler.pm | 14 | ||||
-rw-r--r-- | t/basic.t | 40 |
3 files changed, 78 insertions, 52 deletions
diff --git a/lib/Apache/Inject.pm b/lib/Apache/Inject.pm index 2c05b14..09bf18a 100644 --- a/lib/Apache/Inject.pm +++ b/lib/Apache/Inject.pm @@ -11,35 +11,31 @@ use Apache2::Const qw/OR_LIMIT OR_AUTHCFG TAKE12/; use Apache2::Log (); use Apache2::Module (); -my @directives = ( - { name => 'Inject', - func => __PACKAGE__.'::Inject', - req_override => OR_LIMIT|OR_AUTHCFG, - args_how => TAKE12, - errmsg => 'Inject HeadFile[!] FootFile[!]' } -); +my @directives = ({ + name => 'Inject', + func => __PACKAGE__.'::Inject', + req_override => OR_LIMIT|OR_AUTHCFG, + args_how => TAKE12, + errmsg => 'Inject HeadFile[!] FootFile[!]', +}); Apache2::Module::add(__PACKAGE__, \@directives); sub Inject { my ($self, $parms, @args) = @_; - for (@args) { - if ($_ eq ' ') { - $parms->server->log_error('Inject: Argument cannot be a single space'); - } - } - # Construct directives for passing arguments to handler my $head = $args[0]; - my $foot = $args[1] || ' '; # single space signifies absence of argument + my $foot = $args[1] || '-'; # single hyphen signifies absence of argument $head =~ s/\\/\\\\/; s/"/\\"/; $foot =~ s/\\/\\\\/; s/"/\\"/; # Add relevant directives to current configuration - $parms->add_config(['SetHandler perl-script', - 'PerlResponseHandler Apache::Inject::Handler', - qq{PerlSetVar InjectHead "$head"}, - qq{PerlSetVar InjectFoot "$foot"}]); + $parms->add_config([ + 'SetHandler perl-script', + 'PerlResponseHandler Apache::Inject::Handler', + qq{PerlSetVar InjectHead "$head"}, + qq{PerlSetVar InjectFoot "$foot"}, + ]); } 1; @@ -54,11 +50,23 @@ Apache::Inject - Apache directive for injecting HTML headers and footers LoadModule perl_module libexec/apache24/mod_perl.so PerlLoadModule Apache::Inject - DocumentRoot /uar/local/www/apache24/data + DocumentRoot /usr/local/www/apache24/data + <Directory /usr/local/www/apache24/data> + # Inject both header and footer on all pages on the server Inject head.html foot.html </Directory> + <Location /blog> + # Inject only header on pages under /blog + Inject head.html + </Location> + + <Files index.html> + # Inject only footer on pages named index.html + Inject - foot.html + </Files> + =head1 DESCRIPTION Apache::Inject is a mod_perl module that adds an Apache directive @@ -129,6 +137,30 @@ an unprivileged user and will be skipped if they are run as root. This is relevant if you install Apache::Inject via App::Cpan, which normally runs as root. +=head1 SYNTAX + +The Inject directive takes one or two arguments: + + Inject HEADER_FILE [FOOTER_FILE] + +Each argument can consist of one of two things: + +=over + +=item 1. +the path to a file relative to the document root, or + +=item 2. +a single hyphen (C<->), signifying the absence of an argument. + +=back + +Passing a hyphen as the first argument disables the header, and +passing a hyphen as the second argument disables the footer. + +If you leave out the second argument, then it is implicitly equivalent +to a hyphen. + =head1 OPERATION Behind the scenes, the Inject directive works as an alias for @@ -160,12 +192,6 @@ Apache handle it as it would if the Inject directive were not used. =over -=item Error: Argument cannot be a single space - -In the current implementation, the file names given to Inject are -not allowed to consist solely of a single space, as this is a special -value signifying the absence of an argument. - =item Error: InjectHead/InjectFoot should not begin with slash, as it is already always relative to document root diff --git a/lib/Apache/Inject/Handler.pm b/lib/Apache/Inject/Handler.pm index f9254fd..0e054e7 100644 --- a/lib/Apache/Inject/Handler.pm +++ b/lib/Apache/Inject/Handler.pm @@ -12,14 +12,14 @@ use Apache2::RequestUtil (); my $doc = qr{ \A (?<head> \s* - (<!doctype[^>]*>)? \s* - (<html[^>]*>)? \s* - ( <head[^>]*>.*?</head> \s* + ( <!doctype[^>]*> )? \s* + ( <html[^>]*> )? \s* + ( <head[^>]*> .*? </head> \s* | ( <meta[^>]*> \s* | <link[^>]*> \s* - | <title[^>]*>.*?</title> \s* - | <style[^>]*>.*?</style> \s* # n.b. - | <script[^>]*>.*?</script> \s* # n.b. + | <title[^>]*> .*? </title> \s* + | <style[^>]*> .*? </style> \s* + | <script[^>]*> .*? </script> \s* | <base[^>]*> \s* )+ )? @@ -57,7 +57,7 @@ sub inject { # 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 + return if $val eq '-'; # special value signifying absence of argument # Validate path if ($val =~ m{^/}) { @@ -7,6 +7,8 @@ use Apache::TestRequest qw/GET_BODY/; BEGIN { plan tests => 7; } +# Prepare environment + my $head; # expected page header my $foot; # expected page footer my @body; # sections of page body @@ -25,7 +27,7 @@ close $h; close $f; # Helper for replacing file contents -sub set { +sub overwrite { my $file = shift; open my $fh, '>', $file or die "Could not open > $file: $!"; print $fh join('', @_); @@ -34,56 +36,54 @@ sub set { # Run tests -set 't/htdocs/.htaccess', <<CONF; -Inject " " -CONF - -@body = ("This is a test page.\n"); -set 't/htdocs/test.html', @body; -set 't/htdocs/ ', 'whatever'; -ok GET_BODY('/test.html'), ${body[0]}, # should not include 'whatever' - 'single-space argument should fail'; - -set 't/htdocs/.htaccess', <<CONF; +overwrite 't/htdocs/.htaccess', <<CONF; Inject head.html foot.html CONF @body = ("<title>Test</title>\n", "This is a test page.\n"); -set 't/htdocs/test.html', @body; +overwrite 't/htdocs/test.html', @body; ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot", '<head>-less head'; @body = ("<head>...</head>\n", "This is a test page.\n"); -set 't/htdocs/test.html', @body; +overwrite 't/htdocs/test.html', @body; ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot", '<head>-ful head'; -set 't/htdocs/subdir/.htaccess', <<CONF; +overwrite 't/htdocs/subdir/.htaccess', <<CONF; Inject head.html CONF @body = ("This is a test page.\n"); -set 't/htdocs/subdir/test.html', @body; +overwrite 't/htdocs/subdir/test.html', @body; ok GET_BODY('/subdir/test.html'), "$head${body[0]}", 'different injection in subdirectory'; @body = ("<html>\n", "This is a test page.\n", "</html>\n"); -set 't/htdocs/test.html', @body; +overwrite 't/htdocs/test.html', @body; ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot${body[2]}", '<html>-wrapped document'; @body = ("<!doctype html>\n", "This is a test page.\n"); -set 't/htdocs/test.html', @body; +overwrite 't/htdocs/test.html', @body; ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot", '<!doctype>'; @body = ("\n<!doctype html>\n", "This is a test page.\n"); -set 't/htdocs/test.html', @body; +overwrite 't/htdocs/test.html', @body; ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot", '<!doctype> with leading newline'; +overwrite 't/htdocs/.htaccess', <<CONF; +Inject - head.html +CONF + +@body = ("This is a test page.\n"); +overwrite 't/htdocs/test.html', @body; +ok GET_BODY('/test.html'), "${body[0]}$head", + 'only footer'; + unlink 't/htdocs/.htaccess'; unlink 't/htdocs/test.html'; unlink 't/htdocs/subdir/.htaccess'; unlink 't/htdocs/subdir/test.html'; -unlink 't/htdocs/ '; |