OutputChain.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SSIChain.pm ↑ Summary
1  
2 =head1 NAME
3  
4 Apache::OutputChain - chain stacked Perl handlers
5  
6 =cut
7  
8 package Apache::OutputChain;
9 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw( $VERSION $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 $VERSION = '0.11';
13  
14 0 ▶use Apache::Constants ':common';
Subroutine BEGIN: called CC=1 SCAR=2.6
15 $DEBUG = 0;
16 sub DEBUG() { $DEBUG; }
17 sub handler
18 {
19 my $r = shift;
20 my $class = shift;
21 $class = __PACKAGE__ unless defined $class;
22  
23 my $tied = tied *STDOUT;
24 my $reftied = ref $tied;
25 print STDERR " Apache::OutputChain tied $class -> ",
26 $reftied ? $reftied : 'STDOUT', "\n" if DEBUG;
27  
28 local $^W = 0;
29 ### undef *STDOUT;
30 untie *STDOUT;
31 tie *STDOUT, $class, $r;
32  
33 if ($reftied eq 'Apache') { tie *STDOUT, $class, $r; }
34 else { tie *STDOUT, $class, $r, $tied; }
35 return DECLINED;
36 }
37 sub TIEHANDLE
38 {
39 my ($class, @opt) = @_;
40 my $self = [ @opt ];
41 # @opt should be set up to $r (request structure
42 # reference) and optionally the next handler in the row
43 print STDERR " Apache::OutputChain::TIEHANDLE $self\n"
44 if DEBUG;
45 bless $self, $class;
46 }
47 sub PRINT
48 {
49 my $self = shift;
50 print STDERR " Apache::OutputChain::PRINT $self\n"
51 if DEBUG;
52  
53 if (defined $self->[1]) { $self->[1]->PRINT(@_); }
54 elsif (defined $self->[0]) { $self->[0]->print(@_); }
55 }
56  
57 1;
58  
59 =head1 SYNOPSIS
60  
61 You need reasonably new version of Apache httpd, compiled with
62 mod_perl with PERL_STACKED_HANDLERS enabled. In the conf/access.conf
63 file of your Apache installation, add lines like
64  
65 <Files *.html>
66 SetHandler perl-script
67 PerlHandler Apache::OutputChain Apache::SSIChain Apache::PassHtml
68 </Files>
69  
70 Also, this module can be used as a base for other Apache::*Chain
71 handlers -- inherit from it to get the chaining features.
72  
73 =head1 DESCRIPTION
74  
75 This module allows chaining perl of handlers in Apache, which allows you
76 to make filter modules that take output from previous handlers, make
77 some modifications, and pass the output to the next handler or out to
78 browser.
79  
80 Normal handler modules specify functions I<handler> that do the job --
81 authenticate, log, or send back the response. With chaining modules,
82 the I<handler> function only registers the handler into chain. It is
83 done by tie of STDOUT. The module then catches output of other
84 handlers in its function I<PRINT>, that gets called whenever something
85 is printed to tied handle, can modify or otherwise process the output
86 and send it further on.
87  
88 The C<PerlHandler> above shows the typical configuration: first,
89 B<Apache::OutputChain> starts the chaining feature. Then, there is
90 a list of chaining modules, in reverse order. Here shown is
91 B<Apache::SSIChain>; if you would also want to gzip the output, you
92 would write something like
93  
94 Apache::OutputChain Apache::GzipChain Apache::SSIChain
95  
96 Note that you probably want to do SSI first and gzip the result,
97 that's why the modules are written in that (reverse) order in the
98 configuration file.
99  
100 As the last in the chaining chain, there should be some module that
101 actually produces the data: B<Apache::PassHtml>, B<Apache::PassFile>,
102 B<Apache::PassExec>, or even B<Apache::Registry>.
103  
104 =head1 INTERNALS
105  
106 I will try to explain how this feature is achieved, because I hope
107 you could help me to make it better and mature.
108  
109 When the I<handler> function is called, it checks if it gets
110 a reference to a class. If this is true, then this function was called
111 from some other handler that wants to be put into the chain. If not,
112 it's probably an initialization (first call) of this package
113 (B<Apache::OutputChain>) and we will supply name of this package.
114 Note that other chaining modules should call inherited I<handler>
115 from their own I<handler>s.
116  
117 Now we check, where is STDOUT tied. If it is Apache, we are the first
118 one trying to be put into the chain. If it is not, there is somebody
119 in the chain already. We call tie on the STDOUT, steal it from anybody
120 who had it before -- either Apache or the other class.
121  
122 When later anybody prints into STDOUT, it will call function I<PRINT>
123 of the first class in the chain (the last one that registered). If
124 there is not other class behind, the I<print> method of Apache will be
125 called. If this is not the last user defined handler in the chain, we
126 will call I<PRINT> method of the next class.
127  
128 =head1 VERSION
129  
130 0.11
131  
132 =head1 AUTHOR
133  
134 (c) 1997--2002 Jan Pazdziora, adelton@fi.muni.cz,
135 http://www.fi.muni.cz/~adelton/ at Faculty of Informatics, Masaryk
136 University, Brno, Czech Republic
137  
138 =head1 SEE ALSO
139  
140 Apache::GzipChain(3) by Andreas Koenig for solution that gzips the output
141 on the fly; Apache::SSIChain(3) for SSI parsing module; mod_perl(1)
142 by Doug MacEachern for the great Perl in Apache project.
143  
144 Apache::MakeCapital(3) for a simple example of inheriting from this
145 module.
146  
147 www.apache.org, www.perl.com.
148  
149 =cut
« SSIChain.pm ↑ Summary