From e1c7d5442ca1d3bd67c200d2dbd94bd0e40e72bd Mon Sep 17 00:00:00 2001 From: root Date: Mon, 26 Apr 2021 12:26:06 +0000 Subject: Use single hyphen for NULL value, allow the user to specify it This lets the user inject a footer without injecting a header. --- lib/Apache/Inject.pm | 76 +++++++++++++++++++++++++++++--------------- lib/Apache/Inject/Handler.pm | 14 ++++---- 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 + + # Inject both header and footer on all pages on the server Inject head.html foot.html + + # Inject only header on pages under /blog + Inject head.html + + + + # Inject only footer on pages named index.html + Inject - foot.html + + =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 (? \s* - (]*>)? \s* - (]*>)? \s* - ( ]*>.*? \s* + ( ]*> )? \s* + ( ]*> )? \s* + ( ]*> .*? \s* | ( ]*> \s* | ]*> \s* - | ]*>.*? \s* - | ]*>.*? \s* # n.b. - | ]*>.*? \s* # n.b. + | ]*> .*? \s* + | ]*> .*? \s* + | ]*> .*? \s* | ]*> \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{^/}) { diff --git a/t/basic.t b/t/basic.t index c607548..f81c661 100644 --- a/t/basic.t +++ b/t/basic.t @@ -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', <Test\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", '-less head'; @body = ("...\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", '-ful head'; -set 't/htdocs/subdir/.htaccess', <\n", "This is a test page.\n", "\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]}", '-wrapped document'; @body = ("\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", ''; @body = ("\n\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", ' with leading newline'; +overwrite 't/htdocs/.htaccess', <