How to decode javascript encodeURI / encodeURIComponent ?

C

Cloink

Simply: How ?

I can't believe no-one's already done it, but I can't find any exact
mention of it on the tinterweb.

Closest I got was someone suggesting a custom javascript function to
encode a URI in the style that Perl expects - no way José - what's the
point in the W3C standardising this stuff for all our benefits if we go
and write custom functions instead.

So come on - I need a Perl function to decode a URL that has been
encoded with a javascript encodeURI() or encodeURIComponent() call.

Alternatively, I can write the Perl function myself if someone can
explain how the encoding works in the js funcs?

e.g.
é (e-acute) is encoded via encodeURIComponent to %C3%A9, which doesn't
obviously tie in with the Unicode specification 00E9. But the js
references I've read claim that it is Unicode compliant, so what am I
missing?

Cheers all.
Cloink
 
R

Reinhard Pagitsch

Cloink said:
Simply: How ?

I can't believe no-one's already done it, but I can't find any exact
mention of it on the tinterweb.

Closest I got was someone suggesting a custom javascript function to
encode a URI in the style that Perl expects - no way José - what's the
point in the W3C standardising this stuff for all our benefits if we go
and write custom functions instead.

So come on - I need a Perl function to decode a URL that has been
encoded with a javascript encodeURI() or encodeURIComponent() call.

Alternatively, I can write the Perl function myself if someone can
explain how the encoding works in the js funcs?

e.g.
é (e-acute) is encoded via encodeURIComponent to %C3%A9, which doesn't
obviously tie in with the Unicode specification 00E9. But the js
references I've read claim that it is Unicode compliant, so what am I
missing?

Cheers all.
Cloink

Maybe the following modules will help you:
CGI::Deurl and CGI::Enurl

regards
Reinhard
 
B

Brian McCauley

URI::Escape would seem to be a good starting point...

The docs thereof point out that "The JavaScript encodeURI() function is
similar to uri_escape_utf8()".

Oddly, however there's no uri_unescape_utf8 provided by URI::Escape.
However combining URI::Escape::uri_unescape() and Encode::decode_utf8()
in one statement is not overly taxing.

use Encode;
use URI::Escape qw(uri_unescape);
my $decoded = decode_utf8 uri_unescape $encoded;
 
B

Brian McCauley

Simply: How ?

This is (almost) FAQ: "How do I decode or create those %-encodings on
the web?"
I can't believe no-one's already done it, but I can't find any exact
mention of it on the tinterweb.

But there is (almost) an exact mention in the FAQ.
Closest I got was someone suggesting a custom javascript function to
encode a URI in the style that Perl expects - no way José - what's the
point in the W3C standardising this stuff for all our benefits if we go
and write custom functions instead.

If you are going to mention a standard it's couresy to provide a link.
So come on - I need a Perl function to decode a URL that has been
encoded with a javascript encodeURI() or encodeURIComponent() call.

Alternatively, I can write the Perl function myself if someone can
explain how the encoding works in the js funcs?

Hang on, didn't you just say there was a W3C standard?
e.g.
é (e-acute) is encoded via encodeURIComponent to %C3%A9, which doesn't
obviously tie in with the Unicode specification 00E9. But the js
references I've read claim that it is Unicode compliant, so what am I
missing?

Unicode gives each character a "code point". That is a pure number. The
way that number is encoded as a byte sequence is another thing. Unicode
defines a number of such encodings. The way U+E9 is represented in the
most common Unicode encoding (utf8) is the byte seqence 0xC3,0xA9. So
it looks like encodeURIComponent is encoding the utf8 byte seqence.
 
C

Cloink

Look Brian clever clogs McCauley, I spent hours trawling the web, don't
come your "You haven't tried hard enough," sneering down your nose at
me. The FAQ. THE faq. Which one? I frequently ask myself "Why o
why?" but I don't think you meant that FAQ. I am encoding using
standardised javascript functions. I searched on those functions. I
found nothing useful. I don't enjoy wasting my time, and I certainly
ain't taking any gip off you.

Do you really need a link to the ECMAscript v3 definition of
encodeURI/encodeURIComponent/decodeblahblah when you can look it up in
the Rhino book? Sorry if I used the wrong terminolgy when I said W3C,
I'm not a geek.

Nevertheless, I'm sure many people have sat round a table and decided
that the best way to encode a URL (sorry, URI, mustn't get the jargon
wrong) is by the method that encodeURI/Component does it. So if that's
the way that javascript encodes it, bearing in mind that javascript
communicates http requests with Perl a zillion times a day on the
tinterweb, how come I'm finding it so hard to find a reference to a
Perl function that decodes via the reverse algorithm?

Talking of algorithms, the only almost-useful bit of your reply doesn't
actually explain the algorithm by which one reaches xC3xA9 from u00E9.
Which would've been nice.

Thanks for your time.
 
C

Cloink

Thanks Reinhard, but does that definitely decode a URL encoded with
JavaScript's encodeURI/ encodeURIComponent ? The copyright says 1997
and I don't think the js functions have been around that long. Plus,
I've already parsed my query string into key/value pairs, I just need
to decode the %xx[%xx[%xx]] characters either into their unicode uxxxx
representations or genuine characters.
 
P

Paul Lalli

Cloink said:
Look Brian clever clogs McCauley, I spent hours trawling the web, don't
come your "You haven't tried hard enough,"

Trying long is no where near the same as trying hard.
sneering down your nose at
me. The FAQ. THE faq. Which one?

Are you really that dense? You posted a question to a *Perl*
newsgroup, and someone tells you to look at the FAQ, and you can't
figure out that means "The Perl FAQ"? Cripes, no wonder you're having
so much difficulty.

Paul Lalli
 
C

Cloink

Thanks - I don't think that copes though, the regexp mentioned in the
doc'n is the same regexp I'm already using.

I have emailed the author of the package though.

Thanks again.

Michele said:
I can't believe no-one's already done it, but I can't find any exact
mention of it on the tinterweb.

What is the tinterweb?!?
So come on - I need a Perl function to decode a URL that has been
encoded with a javascript encodeURI() or encodeURIComponent() call.

URI::Escape would seem to be a good starting point...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
J

Jürgen Exner

[Unfortunately you choose a poor answering style, therefore I have to guess
what you are referring to]
Look Brian clever clogs McCauley, I spent hours trawling the web,

Well, that's your problem. Why didn't you look at the readily available
documentation on your own harddrive first?
don't come your "You haven't tried hard enough," sneering down your
nose at me. The FAQ. THE faq. Which one?

See below

Maybe the one to which Brian quoted the _exact_ title?
The one that pops up if you query the FAQ for any of the words 'decode' or
'create' or 'encoding'?

jue
 
I

Ian

Seriously people, is it any wonder that Perl is so niche!

With "helpers" like those in this group, there's no wonder that PHP's
so popular - the beginning Perler must take one look at this group of
self-appointed a**holes and run away shrieking!

To quote "Flower", the skunk in Bambi "If ya aint got nothing nice to
say, don't say nothing at all"

Why do you pill*cks even bother replying - oh wait, that's rhetorical
so I'll leave off the question mark and supply the answer myself...
because you have a pent-up need to show how superior you are (without
actually showing any real superior knowledge of the subject - NONE of
you actually came up with the answer, did you?).

Get a life you pr*cks! This newsgroup posting is yet another that will
be indexed by the search engines and give zero information to the
searcher.

*sheesh*

(And don't bother with your "clever" replies, I won't be coming back to
read them)
 
I

Ian

Brian said:
But there is (almost) an exact mention in the FAQ.

I gather "it's courtesy to provide a link"... that would have avoided
confusion over what was meant by "the FAQ"

8;o)
 
J

Jürgen Exner

[see subject]
[long rant snipped]
NONE of
you actually came up with the answer, did you?).

Do you want us to READ the answer in the FAQ to you?
(And don't bother with your "clever" replies, I won't be coming back
to read them)

Is that a promise?

jue
 
J

Jürgen Exner

Ian said:
I gather "it's courtesy to provide a link"... that would have avoided
confusion over what was meant by "the FAQ"

How can there be any confusion? As I mentioned before you have been given
the _EXACT_ title of the relevant FAQ entry.
Is is too much to ask you to run "perldoc -q <keyword>" for any of the key
words in the title yourself? Or do you really believe that people should
quote the answer of the FAQ every time someone is asking for it?

jue
 
B

Brian McCauley

Look Brian clever clogs McCauley, I spent hours trawling the web, don't
come your "You haven't tried hard enough," sneering down your nose at
me. The FAQ. THE faq. Which one? I frequently ask myself "Why o
why?" but I don't think you meant that FAQ.

Sorry about that. As others have pointed out "The FAQ" (without
further qualification) in a Perl newsgroup means "The Perl FAQ".
Sometimes we forget that this is not as blinding obvious to everyone as
it seems to us.
I am encoding using
standardised javascript functions. I searched on those functions. I
found nothing useful.

I think I've missed something here? What was it you were looking for? A
description of what those functions do? That's what you appeared to be
asking. You were asking a question about Javascript in a Perl
newsgroup.

You also asked for a Perl function that was the reverse - and that you
were promptly given in another branch of this thread by Michele Dondi
(with further clarification from me).
Sorry if I used the wrong terminolgy when I said W3C,

Er? Who said you were wrong?

http://www.w3.org/International/O-URL-code.html
Do you really need a link to the ECMAscript v3 definition of
encodeURI/encodeURIComponent/decodeblahblah when you can look it up in
the Rhino book?

What makes you think I have the Rhino book? Anyhow I can look them up
on the ECMA website and that does appear to answer your question. I'm a
little bemused that you are criticising me for not bothering to make
the trivial effort to look up the exact same information that you were
seeking. If it's trivial for me why should it be non-trivial for you?
So if that's
the way that javascript encodes it, bearing in mind that javascript
communicates http requests with Perl a zillion times a day on the
tinterweb, how come I'm finding it so hard to find a reference to a
Perl function that decodes via the reverse algorithm?

Dunno, why you found it hard so find. Like I said it's a FAQ.
Talking of algorithms, the only almost-useful bit of your reply doesn't
actually explain the algorithm by which one reaches xC3xA9 from u00E9.
Which would've been nice.

Sorry, yes, guilty as charged. I only gave you the _name_ of the
algorithm "utf-8" (as, of course, does the description
encodeURIComponent on the ECMA site and the description of
international URI encoding on the W3C site).

I accept that it would have been a good idea to have included a pointer
to an explaination of utf-8. (It would of course have been wasteful to
have pasted the whole explaination into usenet).

http://en.wikipedia.org/wiki/Utf8

However I only feel very sighly guilty about this. Putting "utf8" or
"utf-8" into google and hitting "I feel lucky" takes me directly to the
Wikipedia entry.
 
B

Brian McCauley

[ rudeness ]
NONE of you actually came up with the answer, did you?).
[..] don't bother with [...] replies, I won't be coming back to
read them)

Well the OP isn't comming back (and we won't loose sleep over that) but
just for my own sanity would any on-lookers like to verify that where
weren't, in fact, any questions that the OP asked in this thread that
weren't answered by several people.

I'm fairly sure my first two posts in this thread answered everything
the OP asked and more. Can anyone imagine how the OP could justify the
assertion "NONE of you actually came up with the answer". Sometimes
people get like this because they expect to be spoon-fed the answer,
but in this case even that doesn't explain it because I _did_
spoon-feed him the answer:

use Encode;
use URI::Escape qw(uri_unescape);
my $decoded = decode_utf8 uri_unescape $encoded;
 
C

Charlton Wilbur

BMcC> I'm fairly sure my first two posts in this thread answered
BMcC> everything the OP asked and more. Can anyone imagine how the
BMcC> OP could justify the assertion "NONE of you actually came up
BMcC> with the answer". Sometimes people get like this because
BMcC> they expect to be spoon-fed the answer, but in this case
BMcC> even that doesn't explain it because I _did_ spoon-feed him
BMcC> the answer: [snip answer]

I've seen this happen often; someone asks a basic question, and one
person spoon-feeds an answer while others admonish him for not
checking the FAQ; the person focuses on the admonishment and
completely misses the answer.

Alternately, the OP was a clueless twit, incapable of recognizing an
answer even when spoon-fed to him. Given how he responded, either
theory works.

Charlton
 
M

Mark Donovan

BMcC> I'm fairly sure my first two posts in this thread answered
BMcC> everything the OP asked and more. Can anyone imagine how the
BMcC> OP could justify the assertion "NONE of you actually came up
BMcC> with the answer". Sometimes people get like this because
BMcC> they expect to be spoon-fed the answer, but in this case
BMcC> even that doesn't explain it because I _did_ spoon-feed him
BMcC> the answer: [snip answer]

I've seen this happen often; someone asks a basic question, and one
person spoon-feeds an answer while others admonish him for not
checking the FAQ; the person focuses on the admonishment and
completely misses the answer.

Alternately, the OP was a clueless twit, incapable of recognizing an
answer even when spoon-fed to him. Given how he responded, either
theory works.

Charlton and others,

I followed-up on this problem by e-mail and your first guess is close to
what happened. Cloink had a perl code problem with URI-escaped, UTF-8
encoded Unicode, which included a tie-in with JavaScript. He asked his
friend, Ian, about it. Ian knows JavaScript, but doesn't know perl. Ian
suggested clpmisc.

Brian answered Cloink's question twice. One answer was a reply to Cloink's
post; and a second answer (actually posted first), which included a clear
three-line solution, was a reply to Michele's post. Brian's best answer was
two levels down from Cloink's question. Brian's direct reply to Cloink's
question never did get around to the three-line answer.

What Cloink and Ian saw was Brian's answer that replied directly to Cloink's
first post. It told Cloink that his answer was in the "FAQ," and then said
that Cloink should provide a link to any quoted standard, etc.

As you guessed, Charlton, Cloink and Ian had no idea which "FAQ" they should
check. It might be a newsgroup FAQ, or something related to... well, they
were both confused.

Cloink felt put-off because he was told to post a link to a standard.
However, when he got a reply, which was posted directly to his question, he
was referred to a mysterious "FAQ." Brian's reply had no link and no
indication of where Cloink should look for for the FAQ. Cloink replied and
the discussion went downhill from there.

After a few rounds of reply and counter-reply, Ian posted an indignant
remark of his own. Cloink had been asked, "Are you really that dense?", etc.
Ian felt he was responding in the same tone as the replies that Cloink had
received.

It's quite possible Cloink will never return to clpmisc; it's almost certain
that Ian won't be back... but mostly because Ian doesn't use perl in any
case. His remark about perl being "niche" simply confirmed his prejudice
against perl based on what happened to Cloink's question. Remarks that Ian's
departure will improve the newsgroup have missed the point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top