in reply to Re: Re: Dump a directory as links from CGI
in thread Dump a directory as links from CGI

Thanks for indicating the subtle difference.

I've been struggling with this very issue recently and came across a url here at perlmonks that uri_escape doesn't handle, and requires CGI::escape. This small CGI program compares uri_escape and escape by producing two links to perlmonks:

#!/usr/bin/perl use CGI qw(:all escape escapeHTML); use URI::Escape; $site = 'http://www.perlmonks.org/index.pl?node='; $user = 'Clive ;-)'; # my test case for whitespace and funny chrs print header,h1("compare"); print '<A HREF="', escapeHTML(uri_escape($site.$user)), '">', escapeHTML($user), '</A> - <tt>escapeHTML(uri_escape($site.$user))</tt><br>'; # does +n't work print '<A HREF="', escapeHTML($site.escape($user)), '">', escapeHTML($user), '</A> - <tt>escapeHTML($site.escape($user))</tt><p>'; # works
The 1st print using uri_escape on the whole url returns:
http://www.perlmonks.org/index.pl?node=Clive%20;-)

The 2nd print using CGI::escape on the param only, yields: http://www.perlmonks.org/index.pl?node=Clive%20%3B-%29

As you can see the semicolon is causing a problem in the uri_escape url.
Is this a general condition or just a peculiarity of perlmonks?

--
Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.

Replies are listed 'Best First'.
Re: Re: Re: Re: Dump a directory as links from CGI
by merlyn (Sage) on May 31, 2001 at 07:02 UTC
    The escaping of form parameters has different rules and cannot be done with a simple pass. You need to escape it by constructing a URI using URI. So, your example is bogus from the get-go.

    In particular, what you want is:

    $site = 'http://www.perlmonks.org/index.pl'; $user = 'Clive ;-)'; # my test case for whitespace and funny chrs use URI; my $uri = URI->new($site); $uri->query_form( node => $user ); print $uri->as_string;
    which prints
    http://www.perlmonks.org/index.pl?node=Clive+%3B-)

    -- Randal L. Schwartz, Perl hacker

      Ooooo, fame at last :-))

      cLive ;-)