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