perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Geoffrey Young <ge...@modperlcookbook.org>
Subject Re: submit-data and chained handlers
Date Mon, 17 Dec 2001 15:54:53 GMT
Perrin Harkins wrote:
> 
> > Apache::RequestNotes don't work because Apache::Registry expect to
> read the
> > POST/PUT-data from STDIN.
> >
> > It's important that the cgi-scripts run unmodified and without any
> notice of
> > their "unnaturally" environment.
> 
> I don't think there's any way around the fact that you can only read the
> content once.  That means you need to read and store it for other
> handlers to use, which is what Apache::RequestNotes does.
> Alternatively, you could add something to your Registry script that
> stuffs the parsed values into pnotes yourself after using them.  If you
> put that inside a block that checks for $ENV{'MOD_PERL'}, you'll still
> be able to run the script safely under standard CGI.
> 
> It also looks like you're re-inventing Apache::Filter or
> Apache::OutputChain.  Have you tried them?

I agree - Apache::Filter is really the defacto standard here.  no
reason to reinvent the wheel...

anyway, I'm really loving Apache::RegistryNG and ties these days... go
figure, I had to search for weeks for a decent Apache::RegistryNG
subclassing example for the book, but in two weeks the list provides
two examples that are probably better (though not as illustrative and
harder to explain).

anyway, I'm sure this is incomplete, but someone else around here can
hack it up more - the READ interface is pretty much stolen from
Apache::Filter, so I assume it works...

package My::CachePOSTRegistry;

use Apache::Constants qw(DONE);
use Apache::RegistryNG;
use Apache::Request;
use strict;

@My::CachePOSTRegistry::ISA = qw(Apache::RegistryNG);

sub new {

  my ($class, $r) = @_;

  $r = Apache::Request->instance($r || Apache->request);

  tie *STDIN, $class, $r;

  return tied *STDIN;
}

sub READ {

  my $self = shift;
  my $buf = \($_[0]); shift;
  my $len = shift;
  my $offset = shift || 0;

  my @args = ();

  $self->{r}->param->do(sub {
    push @args, join '=', @_;
    1;
  });

  my $input = join '&', @args;

  substr($$buf, $offset) = substr($input, 0, $len);
  substr($input, 0, $len) = '';
  return length substr($$buf, $offset);
}

sub TIEHANDLE {

  my ($class, $r) = @_;

  return bless { r => $r }, $class;
}
1;


I did a quick test with

#!/usr/bin/perl

my $posted;
read(STDIN, $posted, $ENV{'CONTENT_LENGTH'});
my $again;
read(STDIN, $again, $ENV{'CONTENT_LENGTH'});
print "Content-type: text/plain\n\n";
print "posted: $posted\nagain: $again\n";

--Geoff

Mime
View raw message