fix setting PLPcache from apache configuration
[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