Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > CGI.pm Escaping query strings - ampersand issue

Reply
Thread Tools

CGI.pm Escaping query strings - ampersand issue

 
 
Matthew Salerno
Guest
Posts: n/a
 
      04-30-2004

I have a cgi/mod_perl script that at one point it generates url's based on
directories:

foreach (@dirs){
my $lnk = qq|<A HREF="index.cgi?List=$_&TestID=$testid" TARGET="$testid"
ONCLICK="window.open('index.cgi?List=$_&testid=$te stid',
'$testid','toolbar=no,location=no,directories=no,s tatus=no,menubar=no,scroll
bars=yes,resizable=yes, width=450,height=230,left=100,top=100'); return
false">|;
print "$lnk1 here </a><br>";
}

The problem is that some of the directories contain ampersands "&".

If there is an ampersand in the directory name, the the rest of the query
string gets all messed up.

ex.
If the directory is titled:
Paperwork_&_Cover
The URL becomes
http://testserv/index.cgi?List=Paper...r&testID=70821

Before the print statement, I have tried the following:

escape($lnk);
Gives me:
Software error:
/TestDocs/70822/Paperwork_ No Documents in this system No such file or
directory at /docs/index.cgi line 345.

$_ =~ s/\&/\&amp\;/g;
Gives me:
Software error:
/TestDocs/70822/Paperwork_ No Documents in this system No such file or
directory at /docs/index.cgi line 345.

$_ =~ s/\&/%26/g;
Gives me:
Software error:
/TestDocs/70822/Paperwork_%26_Cover No Documents in this system No such file
or directory at /docs/index.cgi line 345.

I am going nuts trying to figure out how to get this to work. Can anyone
offer up a bit of wisdom.

Thanks,

Matt


 
Reply With Quote
 
 
 
 
Matthew Salerno
Guest
Posts: n/a
 
      04-30-2004
"Matthew Salerno" <msalerno_rmhere_@bellsouth.net> wrote in message
news:2Kykc.98000$. ..
>
> I have a cgi/mod_perl script that at one point it generates url's based on
> directories:
>
> foreach (@dirs){
> my $lnk = qq|<A HREF="index.cgi?List=$_&TestID=$testid"

TARGET="$testid"
> ONCLICK="window.open('index.cgi?List=$_&testid=$te stid',
>

'$testid','toolbar=no,location=no,directories=no,s tatus=no,menubar=no,scroll
> bars=yes,resizable=yes, width=450,height=230,left=100,top=100'); return
> false">|;
> print "$lnk1 here </a><br>";
> }
>


My apologiex, obvious typo:

print "$lnk1 here </a><br>";

Should be:
print "$lnk here </a><br>";


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-30-2004
Matthew Salerno wrote:
> I have a cgi/mod_perl script that at one point it generates url's
> based on directories:


<snip>

> The problem is that some of the directories contain ampersands "&".


<snip>

> $_ =~ s/\&/%26/g;
> Gives me:
> Software error:
> /TestDocs/70822/Paperwork_%26_Cover No Documents in this system No
> such file or directory at /docs/index.cgi line 345.


URI-escaping the directory name, i.e. converting the '&' character to
'%26', should do the trick. I don't understand what kind of test you
are doing to conclude that that does not work.

It is a URL, right? If you submit

http://testserv/index.cgi?List=Paper...r&testID=70821

with the browser, doesn't CGI.pm unescape the directory name back to
Paperwork_&_Cover?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      04-30-2004
"Matthew Salerno" <msalerno_rmhere_@bellsouth.net> wrote in message
news:2Kykc.98000$. ..
>

[ query string ]
>
> The problem is that some of the directories contain ampersands "&".
>
> If there is an ampersand in the directory name, the the rest of the query
> string gets all messed up.
>
> ...
> $_ =~ s/\&/%26/g;
> Gives me:
> Software error:
> /TestDocs/70822/Paperwork_%26_Cover No Documents in this system No such

file
> or directory at /docs/index.cgi line 345.


you are using CGI.pm are you not?

by the way, you should properly urlencode your querystring parameters,
as there are more characters that may appear in filenames but are
not valid in querystrings.

gnari




 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      05-01-2004
Matthew Salerno wrote:

> I have a cgi/mod_perl script that at one point it generates url's based on
> directories:
>
> foreach (@dirs){
> my $lnk = qq|<A HREF="index.cgi?List=$_&TestID=$testid" TARGET="$testid"
> ONCLICK="window.open('index.cgi?List=$_&testid=$te stid',
> '$testid','toolbar=no,location=no,directories=no,s tatus=no,menubar=no,scroll
> bars=yes,resizable=yes, width=450,height=230,left=100,top=100'); return
> false">|;
> print "$lnk1 here </a><br>";
> }
>
> The problem is that some of the directories contain ampersands "&".
>
> If there is an ampersand in the directory name, the the rest of the query
> string gets all messed up.
>
> ex.
> If the directory is titled:
> Paperwork_&_Cover
> The URL becomes
> http://testserv/index.cgi?List=Paper...r&testID=70821
>
> Before the print statement, I have tried the following:
>
> escape($lnk);
> Gives me:
> Software error:
> /TestDocs/70822/Paperwork_ No Documents in this system No such file or
> directory at /docs/index.cgi line 345.
>
> $_ =~ s/\&/\&amp\;/g;
> Gives me:
> Software error:
> /TestDocs/70822/Paperwork_ No Documents in this system No such file or
> directory at /docs/index.cgi line 345.
>
> $_ =~ s/\&/%26/g;
> Gives me:
> Software error:
> /TestDocs/70822/Paperwork_%26_Cover No Documents in this system No such file
> or directory at /docs/index.cgi line 345.
>
> I am going nuts trying to figure out how to get this to work. Can anyone
> offer up a bit of wisdom.

....
> Matt


Just a WAG: Try something like:

for(@dirs){
my $i=$_;
$i=s/&/%26/g;
... #using $i instead of $_
}

It looks like the errors might possibly be coming from something you are
doing with array @dir later on, and the code you have is placing the
escape codes into @dir, which then means @dir won't work when used for
other stuff later. I say this mostly because the errors you are getting
don't appear to be from a browser, but from Perl.

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
pkent
Guest
Posts: n/a
 
      05-01-2004
In article <2Kykc.98000$>,
"Matthew Salerno" <msalerno_rmhere_@bellsouth.net> wrote:

> I have a cgi/mod_perl script that at one point it generates url's based on
> directories:
>
> foreach (@dirs){
> my $lnk = qq|<A HREF="index.cgi?List=$_&TestID=$testid" TARGET="$testid"


You can't just put any old string into a query string value, or key, and
expect it to work. Only certain characters are safe - in particular you
noticed that & is special, because that's the thing used to separate
key-value pairs! [There are other specials, of course, but you can read
the RFC for them]

So, you need to url-escape $_ _before_ you whack it into a query string
value or key. Look at the URI::Escape module.

And then, don't forget that '&' itself is a special character in HTML
and needs to be escaped there too.

P

--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Escaping strings Krishna Rokhale Ruby 3 03-07-2010 10:09 PM
strings without escaping magnus.moraberg@gmail.com C++ 3 02-03-2009 03:04 PM
Raw strings and escaping Matthew Warren Python 7 10-03-2006 05:11 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Newbie Question: Escaping special characters in array of strings Gene Kahn Ruby 5 11-22-2004 03:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57