move detailed setup documentation to interface modules
[perl/plp/.git] / PLP / CGI.pm
index 1e4d7de63b33b433244b48a3bfc0de022c6cc9d6..698c34b1120d531e7dd1cbeb9e9c45e87a2f7933 100644 (file)
@@ -2,51 +2,125 @@ package PLP::CGI;
 
 use strict;
 
-our $VERSION = '1.00';
+our $VERSION = '1.02';
 
-# CGI initializer: parses PATH_TRANSLATED
+use PLP;
+
+# CGI initializer: opens SCRIPT_FILENAME
 sub init {
        $PLP::print = 'print';
-       
-       my $path = $ENV{PATH_TRANSLATED};
-       $ENV{PLP_NAME} = $ENV{PATH_INFO};
-       my $path_info;
-       while (not -f $path) {
-               if (not $path =~ s/(\/+[^\/]*)$//) {
-                       print STDERR "PLP: Not found: $ENV{PATH_TRANSLATED} ($ENV{REQUEST_URI})\n";
-                       PLP::error(undef, 404);
-                       exit;
+       $PLP::read = \&read;
+
+       if (defined $ENV{PATH_TRANSLATED}) {
+               # SCRIPT_* points to handler script (Apache CGI)
+               # Run backwards through PATH_TRANSLATED to find target filename,
+               # then get file (relative) by stripping PATH_INFO.
+               my ($path, $rel) = (delete $ENV{PATH_TRANSLATED}, delete $ENV{PATH_INFO});
+               my $path_info;
+               while (not -f $path) {
+                       if (not $path =~ s/(\/+[^\/]*)$//) {
+                               printf STDERR "PLP: Not found: $path$path_info ($ENV{REQUEST_URI})\n";
+                               PLP::error(undef, 404);
+                               return;
+                       }
+                       # move last path element onto PATH_INFO
+                       $path_info = $1 . $path_info;
                }
-               my $pi = $1;
-               $ENV{PLP_NAME} =~ s/\Q$pi\E$//;
-               $path_info = $pi . $path_info;
+               if (defined $path_info) {
+                       $rel =~ s/\Q$path_info\E$//;
+                       $ENV{PATH_INFO} = $path_info;
+               }
+               $ENV{SCRIPT_FILENAME} = $path;
+               $ENV{SCRIPT_NAME} = $rel;
+       }
+       elsif (not -f $ENV{SCRIPT_FILENAME}) {
+               print STDERR "PLP: Not found: $ENV{SCRIPT_FILENAME} ($ENV{REQUEST_URI})\n";
+               PLP::error(undef, 404);
+               return;
        }
-       
-       if (not -r $path) {
-               print STDERR "PLP: Can't read: $ENV{PATH_TRANSLATED} ($ENV{REQUEST_URI})\n";
+
+       $ENV{"PLP_$_"} = $ENV{"SCRIPT_$_"} for qw/NAME FILENAME/;
+
+       if (not -r $ENV{PLP_FILENAME}) {
+               print STDERR "PLP: Can't read: $ENV{PLP_FILENAME} ($ENV{REQUEST_URI})\n";
                PLP::error(undef, 403);
-               exit;
+               return;
        }
 
        delete @ENV{
-               qw(PATH_TRANSLATED SCRIPT_NAME SCRIPT_FILENAME PATH_INFO),
+               qw(SCRIPT_NAME SCRIPT_FILENAME),
                grep /^REDIRECT_/, keys %ENV
        };
 
-       $ENV{PATH_INFO} = $path_info if defined $path_info;
-       $ENV{PLP_FILENAME} = $path;
-       my ($file, $dir) = File::Basename::fileparse($path);
+       my ($file, $dir) = File::Basename::fileparse($ENV{PLP_FILENAME});
        chdir $dir;
 
-       $PLP::code = PLP::source($file, 0, undef, $path);
+       $PLP::code = PLP::source($file, 0, undef, $ENV{PLP_FILENAME});
+       return 1;
+}
+
+sub read ($) {
+       my ($bytes) = @_;
+       read *STDIN, my $data, $bytes;
+       return $data;
+}
+
+sub everything {
+       PLP::clean();
+       $_[0]->init() and PLP::start();
 }
 
-# This is run by the CGI script. (#!perl \n use PLP; PLP::everything;)
-#sub everything {
-#      clean();
-#      cgi_init();
-#      start();
-#}
+# This is run by the CGI script. (#!perl \n use PLP::CGI;)
+sub import {
+       $_[0]->everything();
+}
 
 1;
 
+=head1 NAME
+
+PLP::CGI - CGI interface for PLP
+
+=head1 SYNOPSIS
+
+For most servers you'll need a script executable.
+Example F</foo/bar/plp.cgi>:
+
+    #!/usr/bin/perl
+    use PLP::CGI;
+
+Or install the C<plp.cgi> included with PLP.
+
+=head2 Lighttpd
+
+Usually in F</etc/lighttpd/lighttpd.conf>:
+enable I<mod_cgi> (add/outcomment in server.modules), and add:
+
+    cgi.assign = (
+        ".plp" => "/foo/bar/plp.cgi",
+    )
+
+=head2 Apache
+
+Enable I<mod_actions> and setup F<httpd.conf>
+(often just create a F</etc/apache2/conf.d/plp>) with:
+
+    <IfModule mod_actions.c>
+        ScriptAlias /PLP_COMMON/ /foo/bar/
+        <Directory /foo/bar/>
+            Options +ExecCGI
+            Order allow,deny
+            Allow from all
+        </Directory>
+        AddHandler plp-document plp
+        Action plp-document /PLP_COMMON/plp.cgi
+    </IfModule>
+
+=head1 AUTHOR
+
+Mischa POSLAWSKY <perl@shiar.org>
+
+=head1 SEE ALSO
+
+L<PLP|PLP>, L<PLP::FastCGI|PLP::FastCGI>
+