PDA

View Full Version : Hex codes


spaze
09-13-2002, 05:09 AM
Hello!

Just wanted to know, that when I get values of input texts from a form, is there a way to change the data to correspond the true value without a huge amount of replacing? Here are the examples:

I'm getting the data from the form. If the data contains special characters like !"#¤%&/()= they are transferred by Perl into Hexcodes \n would be %0D%0A

Now I have to do the following lines for each data I receive from the form to replace the hexcodes with real value:


$data =~ s/%0D%0A/\n/g;
$data =~ s/%7C/|/g;
$data =~ s/%252C/,/g;
$data =~ s/%2C/,/g;
$data =~ s/%2F/\//g;
$data =~ s/%3A/:/g;
$data =~ s/%3B/;/g;
$data =~ s/%253B/,/g;
$data =~ s/%21/!/g;
$data =~ s/%27/'/g;
$data =~ s/%3F/?/g;
$data =~ s/%2B/-/g;
$data =~ s/%28/(/g;
$data =~ s/%29/)/g;
$data =~ s/%E5/å/g;
$data =~ s/%E4/ä/g;
$data =~ s/%F6/ö/g;
$data =~ s/%C5/Å/g;
$data =~ s/%C4/Ä/g;
$data =~ s/%D6/Ö/g;
$data =~ s/%22/"/g;
$data =~ s/%23/#/g;
$data =~ s/%A4/¤/g;
$data =~ s/%25/%/g;
$data =~ s/%26/&/g;
$data =~ s/%3D/=/g;
$data =~ s/%60/`/g;

You can imagine if I get 30 entries from the form, the script gets way too long and it is frustrating to copy/paste/edit.

So my question is, is there a way in Perl to get the TRUE data without this replacing hassle?

This is how I get the formdata:

read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
$buffer =~ tr/+/ /;
$buffer =~ s/\r/ /g;
$buffer =~ s/\n/ /g;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}
$data=$formdata{'username'};

nex
09-13-2002, 05:29 AM
$data =~ s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg;
if you use it in more than one place you might want to put it in a sub called urldecode.

by the way, it's the web browser that converts them into the funny codes, not Perl. it's so that request bodies are all plaintext. PHP has a urldecode function built-in.

spaze
09-13-2002, 07:37 AM
Originally posted by nex
$data =~ s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg;
if you use it in more than one place you might want to put it in a sub called urldecode.

by the way, it's the web browser that converts them into the funny codes, not Perl. it's so that request bodies are all plaintext. PHP has a urldecode function built-in.

Thanks a lot Nex!! That sure saved a lot of script space and also time :rolleyes:

TheLinuxDuck
09-13-2002, 09:52 AM
If you're doing CGI work in perl, why are you not using the CGI module?? You will save yourself a TON of headaches. Here is what you are doing using CGI:

use CGI qw(:param);

my($cgi) = new CGI();
my(%formdata);

if($cgi->param()) {
for my $param($cgi->param()) {
$formdata{$param} .= $cgi->param($param);
}
}


And that's it! No character replacement, no splitting or adjusting.. the CGI module does all that for you. It also has a tremenoud amount of other things that make CGI writing much MUCH easier.. here's a resource page for you:
http://stein.cshl.org/WWW/CGI/

You will find this module to be an invaluable resource. (=