handle request automatically on PLP::CGI import
[perl/plp/.git] / PLP / CGI.pm
index ee81b37d0d0522001954b377540dc15bc1d595a5..1f46a3c9852954a25d1fc279da38aa56dbb42b37 100644 (file)
@@ -2,13 +2,14 @@ package PLP::CGI;
 
 use strict;
 
-our $VERSION = '1.01';
+our $VERSION = '1.02';
 
 use PLP;
 
 # CGI initializer: opens SCRIPT_FILENAME
 sub init {
        $PLP::print = 'print';
+       $PLP::read = \&read;
 
        if (defined $ENV{PATH_TRANSLATED}) {
                # SCRIPT_* points to handler script (Apache CGI)
@@ -20,7 +21,7 @@ sub init {
                        if (not $path =~ s/(\/+[^\/]*)$//) {
                                printf STDERR "PLP: Not found: $path$path_info ($ENV{REQUEST_URI})\n";
                                PLP::error(undef, 404);
-                               exit;
+                               return;
                        }
                        # move last path element onto PATH_INFO
                        $path_info = $1 . $path_info;
@@ -32,13 +33,18 @@ sub init {
                $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;
+       }
 
        $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{
@@ -50,13 +56,23 @@ sub init {
        chdir $dir;
 
        $PLP::code = PLP::source($file, 0, undef, $ENV{PLP_FILENAME});
+       return 1;
+}
+
+sub read ($) {
+       my ($bytes) = @_;
+       read *STDIN, my $data, $bytes;
+       return $data;
 }
 
-# This is run by the CGI script. (#!perl \n use PLP::CGI; PLP::CGI::everything;)
 sub everything {
        PLP::clean();
-       init();
-       PLP::start();
+       $_[0]->init() and PLP::start();
+}
+
+# This is run by the CGI script. (#!perl \n use PLP::CGI;)
+sub import {
+       $_[0]->everything();
 }
 
 1;