1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package Apache::Inject;
use 5.008000;
use strict;
use warnings;
our $VERSION = '0.01';
use Apache2::CmdParms ();
use Apache2::Module ();
use Apache2::Const qw/OR_LIMIT OR_AUTHCFG TAKE12/;
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) = @_;
# Construct directives for passing arguments to handler
my @vars;
my @names = qw/InjectHead InjectFoot/;
for (@args) {
s/\\/\\\\/; s/"/\\"/;
push @vars, 'PerlSetVar ' . (shift @names) . ' "' . $_ . '"';
}
# Add relevant directives to current configuration
$parms->add_config(['SetHandler perl-script',
'PerlResponseHandler Apache::Inject::Handler',
@vars]);
}
1;
__END__
=head1 NAME
Apache::Inject - Apache directive for injecting HTML headers and footers
=head1 SYNOPSIS
DocumentRoot /uar/local/www/apache24/data
PerlModule Apache::Inject
<Directory /usr/local/www/apache24/data>
Inject head.html foot.html
</Directory>
=head1 DESCRIPTION
Apache::Inject is a mod_perl module that adds the Inject directive.
It injects a header before the body and (optionally) a footer after the body
of any requested HTML file.
The directive is smart enough to place the inserted contents
in the proper places.
The contents of the first file is inserted after any elements
belonging to E<lt>headE<gt> and before any elements belonging to E<lt>bodyE<gt>,
regardless of whether any explicit E<lt>headE<gt> or E<lt>bodyE<gt> tag is present
in the source of the requested HTML page.
Likewise, the contents of the second file is placed
before any potential final E<lt>/htmlE<gt>.
Note:
=over
=item *
The Inject directive is valid only inside directory sections,
such as E<lt>DirectoryE<gt>, E<lt>LocationE<gt> and E<lt>FilesMatchE<gt> blocks.
It is valid in .htaccess files if AllowOverride Limit/AuthConfig/All is enabled.
=item *
The file paths given to Inject are relative to the document root
of the current server or virtual server.
=back
=head1 DIAGNOSTICS
Apache::Inject::Handler, which is the actual handler of the requests,
logs errors to the Apache log file.
Below is a list of all issued errors and warnings.
All of them result in Apache::Inject::Handler declining the request,
letting Apache handle it as it would if the Inject directive were not used.
=over
=item Error: InjectHead/InjectFoot should not begin with slash, as it is already always relative to document root
The paths given to Inject are always relative to the document root,
even if the Inject directive is located within a directory section
that applies to another path.
Beginning any of the paths with a slash implies that there would be
some difference in behavior compared to omitting the slash,
which is false.
=item Error: InjectHead/InjectFoot cannot extend past document root
This error is issued if any of the paths given to Inject tries to
go above the document root by using C<../>.
=item Error: InjectHead/InjectFoot I<path/to/file> does not exist
This error is issued if any of the paths given to Inject doesn't exist.
=item Warning: Declining request due to empty document root
This warning is issued if Apache::Inject::Handler for some reason
cannot retrieve the current document root from Apache.
=back
=head1 CAVEATS
On FreeBSD, you need to enable the accf_http kernel module
in order for the tests to work.
Note that Apache::Inject works fine without the module;
it is only the tests that require it.
=head1 AUTHOR
John Ankarström, E<lt>john [at] ankarstrom.se<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2021 by John Ankarström
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.32.1 or,
at your option, any later version of Perl 5 you may have available.
=cut
|