move detailed setup documentation to interface modules
[perl/plp/.git] / PLP / Apache.pm
1 package PLP::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         } else {
20                 require Apache::Constants;
21         }
22 }
23
24 our $r;
25
26 # mod_perl initializer: returns 0 on success, Apache error code on failure
27 sub init {
28         $r = shift;
29
30         $PLP::print = 'PLP::Apache::print';
31         $PLP::read = \&read;
32         
33         $ENV{PLP_FILENAME} = my $filename = $r->filename;
34         
35         unless (-f $filename) {
36                 return MP2 ? Apache2::Const::HTTP_NOT_FOUND() : Apache::Constants::NOT_FOUND();
37         }
38         unless (-r _) {
39                 return MP2 ? Apache2::Const::HTTP_FORBIDDEN() : Apache::Constants::FORBIDDEN();
40         }
41         
42         $ENV{PLP_NAME} = $r->uri;
43
44         $PLP::use_cache = $r->dir_config('PLPcache') !~ /^off$/i;
45 #S      $PLP::use_safe  = $r->dir_config('PLPsafe')  =~ /^on$/i;
46         my $path = $r->filename();
47         my ($file, $dir) = File::Basename::fileparse($path);
48         chdir $dir;
49
50         $PLP::code = PLP::source($file, 0, undef, $path);
51
52         return 0; # OK
53 }
54
55 sub read ($) {
56         my ($bytes) = @_;
57         $r->read(my $data, $bytes);
58         return $data;
59 }
60
61 # FAST printing under mod_perl
62 sub print {
63         return unless grep length, @_;
64         PLP::sendheaders() unless $PLP::sentheaders;
65         $r->print(@_);
66 }
67
68 # This is the mod_perl handler.
69 sub handler {
70         PLP::clean();
71         if (my $ret = init($_[0])) {
72                 return $ret;
73         }
74         #S PLP::start($_[0]);
75         PLP::start();
76         no strict 'subs';
77         return MP2 ? Apache2::Const::OK() : Apache::Constants::OK();
78 }
79
80 1;
81
82 =head1 NAME
83
84 PLP::Apache - Apache mod_perl interface for PLP
85
86 =head1 SYNOPSIS
87
88 Naturally, you'll need to enable I<mod_perl>:
89
90     apache-modconf apache enable mod_perl
91
92 Setup F<httpd.conf> (often just create a F</etc/apache2/conf.d/plp>) with:
93
94     <IfModule mod_perl.c>
95         <Files *.plp>
96             SetHandler perl-script
97             PerlHandler PLP::Apache
98             PerlSendHeader On
99             PerlSetVar PLPcache On
100         </Files>
101     </IfModule>
102
103 =head1 DESCRIPTION
104
105 =head2 PerlSetVar configuration directives
106
107 =over 16
108
109 =item PLPcache
110
111 Sets caching B<On>/B<Off>. When caching, PLP saves your script in memory and
112 doesn't re-read and re-parse it if it hasn't changed. PLP will use more memory,
113 but will also run 50% faster.
114
115 B<On> is default, anything that isn't =~ /^off$/i is considered On.
116
117 =back
118
119 =head1 AUTHOR
120
121 Mischa POSLAWSKY <perl@shiar.org>
122
123 =head1 SEE ALSO
124
125 L<PLP|PLP>, L<PLP::FastCGI|PLP::FastCGI>, L<mod_perl|Apache>
126