Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

Cannot retrieve empty keys from request using Apache2::Request.param

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


tommaso.torti at xpeppers

Jan 3, 2012, 7:36 AM

Post #1 of 7 (992 views)
Permalink
Cannot retrieve empty keys from request using Apache2::Request.param

Hi all,

i'm using mod_perl in order to retrieve all keys from a POST request with:

my $request = Apache2::Request->new($r);
my $data = $request->param();

When i receive something like
key1=value1&key2=&key3=value3
$data contains all 3 keys

but with
key1=value1&key2&key3=value3
$data contains only 2 keys: key1 and key2&key3

Is there a way to retrieve all the 3 keys like the first case? Any suggestions?
Thank you,

Tommaso


aw at ice-sa

Jan 3, 2012, 7:51 AM

Post #2 of 7 (943 views)
Permalink
Re: Cannot retrieve empty keys from request using Apache2::Request.param [In reply to]

Tommaso Torti wrote:
> Hi all,
>
> i'm using mod_perl in order to retrieve all keys from a POST request with:
>
> my $request = Apache2::Request->new($r);
> my $data = $request->param();
>
> When i receive something like
> key1=value1&key2=&key3=value3
> $data contains all 3 keys
>
> but with
> key1=value1&key2&key3=value3
> $data contains only 2 keys: key1 and key2&key3
>
> Is there a way to retrieve all the 3 keys like the first case? Any suggestions?
> Thank you,
>
As far as I know, there isn't a way, because the browser will not send any data for an
input field that is empty in the <form>.

If you want to do that, then you would need some javascript inside the form, which fills
the empty fields with some "dummy" sequence (like "--empty--"), before posting the form.
Then your module/script would need to catch such fields, and consider them as empty.


tommaso.torti at xpeppers

Jan 3, 2012, 8:33 AM

Post #3 of 7 (942 views)
Permalink
Re: Cannot retrieve empty keys from request using Apache2::Request.param [In reply to]

Hi Andrč

the input i'm receiving is not directly invoked from an html form, but
it could be built from java code or php or other sources i don't even
know.
So i can't use javascript to fix the problem ..

Thank you,


randolf at modperl

Jan 3, 2012, 10:12 AM

Post #4 of 7 (940 views)
Permalink
Re: Cannot retrieve empty keys from request using Apache2::Request.param [In reply to]

> Tommaso Torti wrote:
> > Hi all,
> >
> > i'm using mod_perl in order to retrieve all keys from a POST request with:
> >
> > my $request = Apache2::Request->new($r);
> > my $data = $request->param();
> >
> > When i receive something like
> > key1=value1&key2=&key3=value3
> > $data contains all 3 keys
> >
> > but with
> > key1=value1&key2&key3=value3
> > $data contains only 2 keys: key1 and key2&key3
> >
> > Is there a way to retrieve all the 3 keys like the first case? Any suggestions?
> > Thank you,
>
> As far as I know, there isn't a way, because the browser will not send any data for an
> input field that is empty in the <form>.
>
> If you want to do that, then you would need some javascript inside the form, which fills
> the empty fields with some "dummy" sequence (like "--empty--"), before posting the form.
> Then your module/script would need to catch such fields, and consider them as empty.

Interesting. I suppose one could use the GET method to verify
whether $request->param() shoots blanks or undefs.

Filling in fields seems like a lot of work though, and using
JavaScript to fill in those fields with dummy data won't be
compatible with anything. A better approach would be to just include
a hidden input element with the same name at some point after the
user's input field:

<input type=hidden name=key2 value=--empty-->

But, I'd rather use this approach instead, if possible:

<input type=hidden name=key2 value="">

The beauty of this solution is that calling $request->param() in
scalar context returns the first value, but if the POST method really
isn't passing along blanks then the hidden input element's value will
be in first place instead.

However, I strongly suspect that this really is not the right
solution.

Randolf Richardson - randolf [at] inter-corporate
Inter-Corporate Computer & Network Services, Inc.
Beautiful British Columbia, Canada
http://www.inter-corporate.com/


jnarins at seniorbridge

Jan 3, 2012, 11:46 AM

Post #5 of 7 (943 views)
Permalink
RE: Cannot retrieve empty keys from request using Apache2::Request.param [In reply to]

> How about a little perl string manipulation?
>
> #my $a = 'key1=value1&key2=&key3=value3';
> my $a = 'key1=value1&key2&key3=value3';
>
> my $b = join "&", map { my $c = ($_ =~ m/(\S+)=(\S*)?/o) ? $_ : "$_=";
> $c; } split "&", $a;
>
> print "a= $a\nb= $b\n";

This is not a good idea because of HTML entities and other legal uses of & in the text returned by ->param();


Josh Narins
Director of Application Development
SeniorBridge

845 Third Ave
7th Floor
New York, NY 10022
Tel: (212) 994-6194
Mobile: (917) 488-6248
Fax: (212) 994-4260
jnarins [at] seniorbridge

SeniorBridge
Managing Complex Chronic Care
http://www.seniorbridge.com



SeniorBridge Statement of Confidentiality: The contents of this email message are intended for the exclusive use of the addressee(s) and may contain confidential or privileged information. Any dissemination, distribution or copying of this email by an unintended or mistaken recipient is strictly prohibited. In said event, kindly reply to the sender and destroy all entries of this message and any attachments from your system. Thank you.


aw at ice-sa

Jan 3, 2012, 1:22 PM

Post #6 of 7 (949 views)
Permalink
Re: Cannot retrieve empty keys from request using Apache2::Request.param [In reply to]

Josh Narins wrote:
>> How about a little perl string manipulation?
>>
>> #my $a = 'key1=value1&key2=&key3=value3';
>> my $a = 'key1=value1&key2&key3=value3';
>>
>> my $b = join "&", map { my $c = ($_ =~ m/(\S+)=(\S*)?/o) ? $_ : "$_=";
>> $c; } split "&", $a;
>>
>> print "a= $a\nb= $b\n";
>
> This is not a good idea because of HTML entities and other legal uses of & in the text returned by ->param();
>
>

In any case, aren't we all getting a bit beside the point here ?
In the documentation of Apache2::Request, what I see is this :

param

$req->param()
$req->param($name)

Get the request parameters (using case-insensitive keys) by mimicing the OO interface of
CGI::param.

# similar to CGI.pm

my $foo_value = $req->param('foo');
my @foo_values = $req->param('foo');
my @param_names = $req->param;

# the following differ slightly from CGI.pm

# returns ref to APR::Request::Param::Table object representing
# all (args + body) params
my $table = $req->param;
@table_keys = keys %$table;


I tend to use CGI.pm myself rather than Apache2::Request, but if I get the above
correctly, then I do not really understand the original post :

quote:

my $request = Apache2::Request->new($r);
my $data = $request->param();

When i receive something like
key1=value1&key2=&key3=value3
$data contains all 3 keys

but with
key1=value1&key2&key3=value3
$data contains only 2 keys: key1 and key2&key3

unquote

in the sense that
my $data = $request->param();
should return a ref to an APR::Request::Param::Table object,
and not a string as seems to be implied above.

But assuming that this is not what the OP meant, and that he really means that he is
getting an APR::Request::Param::Table, I would anyway add that a POST request where the
POST body looks like :

key1=value1&key2&key3=value3

(as in the OP's 2d example) seems invalid to me.

I refer to http://www.w3.org/TR/html4/interact/forms.html#form-data-set,
in particular section 17.13.4 Form content types,
which explicitly specifies (in the case of a POST using the
"application/x-www-form-urlencoded" format) that the succesful inputs of the form must be
sent as "key=value" pairs. In other words a key alone, without a "=" sign following it,
is invalid.
And since it is invalid, what Apache2::Request::param() makes with it, is rather
unpredictable.

So I would also say that in my view, the first step should be to go back to these unruly
clients who send such things, and tell them to change their evil ways.

To which (just to save a round-trip) the answer is probably going to be "But I cannot
change the clients..".

To which the answer would be : then you're out of luck, and you'll have to parse the body
yourself. Or try CGI.pm, maybe it does this differently.
(By the way, as far as I know, in a mod_perl context, CGI.pm is smart enough to know that
it runs in a mod_perl context, and it uses the internal Apache request object for
optimisation).

See : http://search.cpan.org/~markstos/CGI.pm-3.59/lib/CGI.pm
and check the $query->param method (which returns just the names of the parameters).


tommaso.torti at xpeppers

Jan 4, 2012, 7:16 AM

Post #7 of 7 (932 views)
Permalink
Re: Cannot retrieve empty keys from request using Apache2::Request.param [In reply to]

Thank you André for the clear explanation!

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.