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 ++++---- 2 files changed, 58 insertions(+), 32 deletions(-) (limited to 'lib/Apache') 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{^/}) { -- cgit v1.2.3