blib/lib/PLP/Backend/Apache.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 116 CC 11 SCAR 26.3
CC
11
Cov
0%
init
42.0
print
12.0
handler
12.0
CRAP 13.8
1 package PLP::Backend::Apache;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '1.01';
7  
8 use PLP;
9  
10 use constant MP2 => (
11 defined $ENV{MOD_PERL_API_VERSION} and $ENV{MOD_PERL_API_VERSION} >= 2
12 );
13  
14 BEGIN {
15 if (MP2) {
16 require Apache2::Const;
17 require Apache2::RequestRec;
18 require Apache2::RequestUtil;
19 require Apache2::RequestIO;
20 require Apache2::Log;
21 Apache2::Const->import(-compile => qw(
22 HTTP_NOT_FOUND HTTP_FORBIDDEN OK
23 ));
24 } else {
25 require Apache::Constants;
26 }
27 }
28  
29 our $r;
30  
31 # mod_perl initializer: returns 0 on success, Apache error code on failure
32 sub init {
33 $r = shift;
34  
35 $PLP::print = 'PLP::Backend::Apache::print';
36 $PLP::read = \&read;
37 *CORE::GLOBAL::warn = $SIG{__WARN__} = \&Apache2::ServerRec::warn if MP2;
38
39 $ENV{PLP_FILENAME} = my $filename = $r->filename;
40
41 unless (-f $filename) {
42 return MP2 ? Apache2::Const::HTTP_NOT_FOUND() : Apache::Constants::NOT_FOUND();
43 }
44 unless (-r _) {
45 return MP2 ? Apache2::Const::HTTP_FORBIDDEN() : Apache::Constants::FORBIDDEN();
46 }
47
48 $ENV{PLP_NAME} = $r->uri;
49  
50 $PLP::use_cache = ($r->dir_config('PLPcache') || 'on') !~ /^off$/i;
51 #S $PLP::use_safe = $r->dir_config('PLPsafe') =~ /^on$/i;
52 my $path = $r->filename();
53 my ($file, $dir) = File::Basename::fileparse($path);
54 chdir $dir;
55  
56 $PLP::code = PLP::source($file, 0, undef, $path);
57  
58 return 0; # OK
59 }
60  
61 sub read ($) {
62 my ($bytes) = @_;
63 $r->read(my $data, $bytes);
64 return $data;
65 }
66  
67 # FAST printing under mod_perl
68 sub print {
69 return unless grep length, @_;
70 PLP::sendheaders() unless $PLP::sentheaders;
71 $r->print(@_);
72 }
73  
74 # This is the mod_perl handler.
75 sub handler {
76 PLP::clean();
77 $PLP::interface = __PACKAGE__;
78 if (my $ret = init($_[0])) {
79 return $ret;
80 }
81 #S PLP::start($_[0]);
82 PLP::start();
83 no strict 'subs';
84 return MP2 ? Apache2::Const::OK() : Apache::Constants::OK();
85 }
86  
87 1;
88  
89 =head1 NAME
90  
91 PLP::Backend::Apache - Apache mod_perl interface for PLP
92  
93 =head1 SYNOPSIS
94  
95 Naturally, you'll need to enable I<mod_perl>:
96  
97 apache-modconf apache enable mod_perl
98  
99 Setup F<httpd.conf> (in new installs just create F</etc/apache/conf.d/plp>) with:
100  
101 <IfModule mod_perl.c>
102 <Files *.plp>
103 SetHandler perl-script
104 PerlHandler PLP::Backend::Apache
105 PerlSendHeader On
106 </Files>
107 </IfModule>
108  
109 =head1 DESCRIPTION
110  
111 =head2 Configuration directives
112  
113 PLP behaviour can be configured by B<PerlSetVar> rules.
114 These can be added to a F<.htaccess> file or most any scope of server
115 configuration. For example, to disable caching for a specific site:
116  
117 <Directory /var/www/somesite/>
118 PerlSetVar PLPcache Off
119 </Directory>
120  
121 =over 16
122  
123 =item PLPcache
124  
125 Sets caching B<On>/B<Off>.
126 When caching, PLP saves your script in memory and doesn't re-read
127 and re-parse it if it hasn't changed. PLP will use more memory,
128 but will also run 50% faster.
129  
130 B<On> is default, anything that isn't =~ /^off$/i is considered On.
131  
132 =back
133  
134 =head1 BUGS
135  
136 With mod_perlB<2>, any new request will change the cwd for all processes.
137 This means that if you're running files from multiple directories,
138 you I<should not use the current path> for it may change at any time.
139  
140 The bug has been confirmed with at least mod_perl 2.0.2/3/4 on Apache 2.2.3/8.
141 Using this backend on Apache2 is extremely discouraged until this is fixed.
142 Instead, L<the FastCGI backend|PLP::Backend::FastCGI> is recommended.
143  
144 Apache1 does not show any problems.
145  
146 =head1 AUTHOR
147  
148 Mischa POSLAWSKY <perl@shiar.org>
149  
150 =head1 SEE ALSO
151  
152 L<PLP>, L<PLP::Backend::FastCGI>, L<mod_perl>
153