improve lighttpd configuration samples
[perl/plp/.git] / lib / PLP / Backend / CGI.pm
1 package PLP::Backend::CGI;
2
3 use strict;
4
5 our $VERSION = '1.02';
6
7 use PLP;
8
9 # CGI initializer: opens SCRIPT_FILENAME
10 sub init {
11         $PLP::print = 'print';
12         $PLP::read = \&read;
13
14         if (defined $ENV{PATH_TRANSLATED}) {
15                 # SCRIPT_* points to handler script (Apache CGI)
16                 # Run backwards through PATH_TRANSLATED to find target filename,
17                 # then get file (relative) by stripping PATH_INFO.
18                 my ($path, $rel) = (delete $ENV{PATH_TRANSLATED}, delete $ENV{PATH_INFO});
19                 my $path_info;
20                 while (not -f $path) {
21                         if (not $path =~ s/(\/+[^\/]*)$//) {
22                                 printf STDERR "PLP: Not found: $path$path_info ($ENV{REQUEST_URI})\n";
23                                 PLP::error(undef, 404);
24                                 return;
25                         }
26                         # move last path element onto PATH_INFO
27                         $path_info = $1 . $path_info;
28                 }
29                 if (defined $path_info) {
30                         $rel =~ s/\Q$path_info\E$//;
31                         $ENV{PATH_INFO} = $path_info;
32                 }
33                 $ENV{SCRIPT_FILENAME} = $path;
34                 $ENV{SCRIPT_NAME} = $rel;
35         }
36         elsif (not -f $ENV{SCRIPT_FILENAME}) {
37                 print STDERR "PLP: Not found: $ENV{SCRIPT_FILENAME} ($ENV{REQUEST_URI})\n";
38                 PLP::error(undef, 404);
39                 return;
40         }
41
42         $ENV{"PLP_$_"} = $ENV{"SCRIPT_$_"} for qw/NAME FILENAME/;
43
44         if (not -r $ENV{PLP_FILENAME}) {
45                 print STDERR "PLP: Can't read: $ENV{PLP_FILENAME} ($ENV{REQUEST_URI})\n";
46                 PLP::error(undef, 403);
47                 return;
48         }
49
50         delete @ENV{
51                 grep /^REDIRECT_/, keys %ENV
52         };
53
54         my ($file, $dir) = File::Basename::fileparse($ENV{PLP_FILENAME});
55         chdir $dir;
56
57         $PLP::code = PLP::source($file, 0, undef, $ENV{PLP_FILENAME});
58         return 1;
59 }
60
61 sub read ($) {
62         my ($bytes) = @_;
63         read *STDIN, my ($data), $bytes;
64         return $data;
65 }
66
67 sub everything {
68         PLP::clean();
69         $_[0]->init() and PLP::start();
70 }
71
72 # This is run by the CGI script. (#!perl \n use PLP::Backend::CGI;)
73 sub import {
74         $PLP::interface = $_[0];
75         $_[0]->everything();
76 }
77
78 1;
79
80 =head1 NAME
81
82 PLP::Backend::CGI - CGI interface for PLP
83
84 =head1 SYNOPSIS
85
86 For most servers you'll need a script executable.
87 Example F</foo/bar/plp.cgi>:
88
89     #!/usr/bin/perl
90     use PLP::Backend::CGI;
91
92 Or install the C<plp.cgi> included with PLP.
93
94 =head2 Lighttpd
95
96 Add this to your configuration file (usually F</etc/lighttpd/lighttpd.conf>):
97
98     server.modules += ("mod_cgi")
99     cgi.assign += (".plp" => "/foo/bar/plp.cgi")
100     server.indexfiles += ("index.plp")
101     static-file.exclude-extensions += (".plp")
102
103 =head2 Apache
104
105 Enable I<mod_actions> and
106 setup F<httpd.conf> (in new installs just create F</etc/apache/conf.d/plp>) with:
107
108     <IfModule mod_actions.c>
109         ScriptAlias /PLP_COMMON/ /foo/bar/
110         <Directory /foo/bar/>
111             Options +ExecCGI
112             Order allow,deny
113             Allow from all
114         </Directory>
115         AddHandler plp-document plp
116         Action plp-document /PLP_COMMON/plp.cgi
117     </IfModule>
118
119 =head1 AUTHOR
120
121 Mischa POSLAWSKY <perl@shiar.org>
122
123 =head1 SEE ALSO
124
125 L<PLP>, L<PLP::Backend::FastCGI>
126