Programming :  Student Freelance Forum For Work Experience Builders' (CertificationPoint) The fastest message board... ever.
 
PERL-Reusing Data from POST request
Posted by: adcertpoint (Moderator)
Date: April 29, 2020 04:35PM

What happens if you need to access the POSTed data more than once, say to reuse it in subsequent handlers of the same request? POSTed data comes directly from the socket, and at the low level data can only be read from a socket once. So you have to store it to make it available for reuse.

There is an experimental option for Makefile.PL called PERL_STASH_POST_DATA. If you turn it on, you can get at it again with $r->subprocess_env("POST_DATA"winking smiley. This is not enabled by default because it adds a processing overhead for each POST request.

But what do we do with large multipart file uploads? Because POST data is not all read in one clump, it's a problem that's not easy to solve in a general way. A transparent way to do this is to switch the request method from POST to GET, and store the POST data in the query string. This handler does exactly this:
Apache/POST2GET.pm
------------------
package Apache:tongue sticking out smileyOST2GET;
use Apache::Constants qw(M_GET OK DECLINED);

sub handler {
my $r = shift;
return DECLINED unless $r->method eq "POST";
$r->args(scalar $r->content);
$r->method('GET');
$r->method_number(M_GET);
$r->headers_in->unset('Content-length');
return OK;
}
1;
__END__

In httpd.conf add:
PerlInitHandler Apache:tongue sticking out smileyOST2GET

or even this:
<Limit POST>
PerlInitHandler Apache:tongue sticking out smileyOST2GET
</Limit>

To save a few more cycles, so the handler will be called only for POST requests.

Effectively, this trick turns the POST request into a GET request internally. Now when CGI.pm, Apache::Request or whatever module parses the client data, it can do so more than once since $r->args doesn't go away (unless you make it go away by resetting it).

If you are using Apache::Request, it solves this problem for you with its instance() class method, which allows Apache::Request to be a singleton. This means that whenever you call Apache::Request->instance() within a single request you always get the same Apache::Request object back.

Options: ReplyQuote


Sorry, only registered users may post in this forum.
This forum powered by Phorum.