generalize reading post input
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 2 Oct 2007 10:08:26 +0000 (12:08 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 14 Mar 2008 23:40:21 +0000 (00:40 +0100)
Input retrieval differs per server interface, so set $PLP::read to a
function providing a read of specified number of bytes. This differs
from $PLP::print as it's not just a name, because it won't be evalled
but called directly.

PLP/Apache.pm
PLP/CGI.pm
PLP/Fields.pm

index c3880957e7a405ad9a34dc4ad37d1207eacbf718..bc788811252a533e48457aca4529033cc1325b2a 100644 (file)
@@ -26,6 +26,7 @@ sub init {
        our $r = shift;
 
        $PLP::print = 'PLP::Apache::print';
+       $PLP::read = \&read;
        
        $ENV{PLP_FILENAME} = my $filename = $r->filename;
        
@@ -49,6 +50,12 @@ sub init {
        return 0; # OK
 }
 
+sub read ($) {
+       my ($bytes) = @_;
+       $r->read(my $data, $bytes);
+       return $data;
+}
+
 # FAST printing under mod_perl
 sub print {
        return unless grep length, @_;
index 9b72355c55eafd1050d20a7ae4fab305a2814fbe..56ca4db72eece75fcd434f6ed04211545ee4bc4d 100644 (file)
@@ -9,6 +9,7 @@ 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)
@@ -57,6 +58,12 @@ sub init {
        $PLP::code = PLP::source($file, 0, undef, $ENV{PLP_FILENAME});
 }
 
+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();
index 2a4190c062dd845c4e9c1f341a157799ffa3c0c9..7f07c4e914225e8f006e86a02db99b7391ee7471 100644 (file)
@@ -27,11 +27,7 @@ sub doit {
                return \%post if $ENV{CONTENT_TYPE} !~
                        m!^(?:application/x-www-form-urlencoded|$)!;
                
-               if ($ENV{MOD_PERL}) {
-                       $post = Apache->request->content;
-               } else {
-                       read *STDIN, $post, $ENV{CONTENT_LENGTH};
-               }
+               $post = $PLP::read->($ENV{CONTENT_LENGTH}) if $ENV{CONTENT_LENGTH};
                
                return \%post unless defined $post and length $post;