Link to home
Create AccountLog in
Avatar of oleber
oleberFlag for Portugal

asked on

split the args in Apache2 mod_perl

In a request like

http://www.example.com/xpto?a=b&c=d

from the $r->args I get the "a=b&c=d"

Now I need to split the args into a hash to get the arguments in the original format, without encodings.

How do I do this?

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of oleber

ASKER

the problem isn't separating the arguments.
It is really the decoding of the parameters.

You also need to think at least in this cases:

http://www.example.com/xpto?a=b&a=c&q=c%26a
So you need to URL decode hex escapes.

Like thsi:

$param = 'c%26a';
$param =~ s/\%([a-fA-F0-9]{2})/pack('C', hex($1))/gse;

Avatar of oleber

ASKER

we are in Apach2 and mod_perl.

This is the standard way of doing this ? ? ?
Well, since you were accessing the query string directly, in scalar context, I assumed you wanted to decode the query string yourself. Otherwise, if the API did it for you even in scalar context, parsing the string would be impossible because it would have un-escaped all the & and you would not know which were separators and which were actual & in the content.

I think you should try the $r->args() in lsit context.

What happens when you do:

my %keypairs = $r->args();

This should decode for you, as it is supposed to mirror the CGI API.


I thought you asked for it in the original format, without decoding, if you want to decode the parameters, you probably want to use the standard API
Avatar of oleber

ASKER

maybe I'm missing a Package but the code:

        my %keypairs = $r->args();
        print Dumper \%keypairs;

prints

$VAR1 = {
  'a=b&c=d' => undef
};

for the linux command:

POST 'http://localhost:10000/media_server/download_request?a=b&c=d' < test_data.json
Apparently there is some inaccurate documentation for the mod_perl Apache module. Try this snippet, which came from this URL. According to docs, the Apache::args provides unescaped values, as best I can tell.

http://www.informit.com/articles/article.aspx?p=27110&seqNum=7

Try Apache::Request instead.
use Apache::Request;
 
sub handler {
 
 my $r = Apache::Request->new(shift);
 
 $r->send_http_header('text/plain');
 
 # Now, we use the param() method, which covers both GET and POST data.
 foreach my $param ($r->param) {
  print "$param => ", $r->param($param), "\n";
 }

Open in new window

I meant "Apache::args provides escaped values, as best I can tell"
Avatar of oleber

ASKER

For Apache 2 you use Apache2::* you don't use Apache::*