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).