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