c3880957e7a405ad9a34dc4ad37d1207eacbf718
[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 # mod_perl initializer: returns 0 on success, Apache error code on failure
25 sub init {
26         our $r = shift;
27
28         $PLP::print = 'PLP::Apache::print';
29         
30         $ENV{PLP_FILENAME} = my $filename = $r->filename;
31         
32         unless (-f $filename) {
33                 return MP2 ? Apache2::Const::HTTP_NOT_FOUND() : Apache::Constants::NOT_FOUND();
34         }
35         unless (-r _) {
36                 return MP2 ? Apache2::Const::HTTP_FORBIDDEN() : Apache::Constants::FORBIDDEN();
37         }
38         
39         $ENV{PLP_NAME} = $r->uri;
40
41         our $use_cache = $r->dir_config('PLPcache') !~ /^off$/i;
42 #S      our $use_safe  = $r->dir_config('PLPsafe')  =~ /^on$/i;
43         my $path = $r->filename();
44         my ($file, $dir) = File::Basename::fileparse($path);
45         chdir $dir;
46
47         $PLP::code = PLP::source($file, 0, undef, $path);
48
49         return 0; # OK
50 }
51
52 # FAST printing under mod_perl
53 sub print {
54         return unless grep length, @_;
55         PLP::sendheaders() unless $PLP::sentheaders;
56         $PLP::Apache::r->print(@_);
57 }
58
59 # This is the mod_perl handler.
60 sub handler {
61         PLP::clean();
62         if (my $ret = init($_[0])) {
63                 return $ret;
64         }
65         #S PLP::start($_[0]);
66         PLP::start();
67         no strict 'subs';
68         return MP2 ? Apache2::Const::OK() : Apache::Constants::OK();
69 }
70
71 1;
72