473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

My *codeURI* Gotcha :/

forgive me for my bumbling confusion. I am learning javascript and got
caught up in a gotcha. this may be due to Mozillas spidermonkey or
most likely to my n00b mindset on how encodeURI* and decodeURI* work.

Can we all agree that this does *not* work:
decodeURI("http://domain.com%");

Can we all agree that this *does* work:
decodeURI("http://domain.com");

here is where the confusion settles in. decodeURI has no problems with
simply ignoring the second example (which is not even encoded) and
producing exactly what it was given but considers the first example a
malformed URI and returns a URIError. maybe I am the dumb one for
passing that URI in *but* this is where the confusion takes a turn for
the worse.

Why does this work?
var uri = encodeURI("http://domain.com%");
decodeURI(uri);

whats the trick usage here? I mean, why does a malformed URI passed
directly to decode choke but if I simply encode it, decode won't choke
and return exactly the URI I passed in uncoded? I expect, whatever may
be passed in to decode will simply return decoded *if* possible e.g.,
example 2.

I am certain, this is most likely on purpose but am failing to
understand how, why, when, etc :/

Can someone help clear this up for me, thanks a million!

Sep 3 '07 #1
3 3509
Because encoding an URI-like string so that it *becomes* a valid URI is what
encodeURI() is supposed to do. And decodeURI() is supposed to decode valid
URIs. If you inspect the values either method returns (I recommend Firebug
anyway), you see why it works: the "%" is percent-encoded as "%25", and then
decoded back to "%".
this sort of makes my point. decodeURI will not decode a malformed URI
but to get it to do so or to get away with doing so, I simply have to
encode it first. to me the only difference is, in raw form, I get an
error where as, if it is encoded, I don't. I'll try to show another
example of what I mean.

var a = encodeURI("http://domain.com%");
var b = decodeURI(a); // -http://domain.com%
var c = decodeURI("http://domain.com%"); // -URIError

*b* decodes correctly here because %25 decoded translates to %. *c*
springs a URIError because % by itself, well it's invalid. what I
expected is one of these too. b -URIError (technically malformed) OR
c -http://domain.com% (because I shouldn't have to encode it to
simply get it back, e.g., b).

print(decodeURI("abc")); // nothing to decode so no error!

in the above example, there is absolutely nothing to decode
(technically it is malformed, because there is nothing to decode) so,
return what I passed in, all is good, everybody have a pizza OR throw
a URIError because there is nothing to decode and so warm up the Death
star. Awesome its pizza night on the death star :)

I feel like an idiot because I feel certain this is all by design and
the only flaw is my thinking. truth is, I've completed 12 chapters on
core JavaScript and before starting on client-side, I decided to go
through the reference too while trying my hand at a few scripts. in
the core reference though, I found *codeURI* and don't remember them
explained in enough detail to understand this.
See also http://rfc-editor.org/rfc/rfc3986.txt or
http://www.rfc-editor.org/rfc/std/std66.txt
holy cow batman are you frigging kidding me? those docs are too long
so I initiated a search and was told 150 minutes (building the index)
I did find % and %25 and went over a few hits. I got some knowledge
from it and further decided to cement them by using Google for better
explanations. I found the Mozilla core JavaScript guide and they too
helped shed some light on the matter.

anyhow, I am not going to go nuts over this. I have a general idea
about them and hope I'll learn the idiomatic and intended uses for
them. only need to practice ;)

Sep 4 '07 #2
vbgunz wrote:
>Because encoding an URI-like string so that it *becomes* a valid URI is what
encodeURI() is supposed to do. And decodeURI() is supposed to decode valid
URIs. If you inspect the values either method returns (I recommend Firebug
anyway), you see why it works: the "%" is percent-encoded as "%25", and then
decoded back to "%".

this sort of makes my point.
Quite the contrary.
decodeURI will not decode a malformed URI
Correct.
but to get it to do so or to get away with doing so, I simply have to
encode it first.
No, you don't.
to me the only difference is, in raw form, I get an
error where as, if it is encoded, I don't. I'll try to show another
example of what I mean.

var a = encodeURI("http://domain.com%");
var b = decodeURI(a); // -http://domain.com%
var c = decodeURI("http://domain.com%"); // -URIError

*b* decodes correctly here because %25 decoded translates to %. *c*
springs a URIError because % by itself, well it's invalid. what I
expected is one of these too. b -URIError (technically malformed) OR
c -http://domain.com% (because I shouldn't have to encode it to
simply get it back, e.g., b).
You miss one important step in your thinking.

1. "http://domain.com%" is not a valid URI, because the "%" is not followed
by two hexadecimal digits (i.e. that is not a proper percent-encoding).
2. encodeURI() exists to make (1) valid.
3. encodeURI("http://domain.com%") yields a == "http://domain.com%25",
because that character is designated unsafe if not part of a
percent-encoding character sequence (because it is used for
percent-encoding of characters itself) and 0x25 is the ASCII code for
the "%" character.

4. a == "http://domain.com%25" is a valid URI, because the "%" is followed
by two hexadecimal digits (see above).
5. decodeURI() exists to decode it.
6. decodeURI("http://domain.com%25") yields b == "http://domain.com%",
because 0x25 is the ASCII code for the "%" character.

But:

7. "http://domain.com%" is not a valid URI, for the reasons given above.
8. However, decodeURI() exists to decode only *valid* URIs.
9. decodeURI("http://domain.com%") yields nothing but throws a URIError
exception.

That said, I find it seldom necessary to use encode/decodeURI().
encode/decodeURIComponent() is much more often used, to pass data in the
query part of a HTTP GET request or in the message body of a POST request.
>See also http://rfc-editor.org/rfc/rfc3986.txt or
http://www.rfc-editor.org/rfc/std/std66.txt

holy cow batman are you frigging kidding me? those docs are too long
so I initiated a search and was told 150 minutes (building the index)
ISTM you are using either one of a computer, CPU, operating system, or a Web
user agent that is too slow for the Web or too busy doing other things.

With Firefox 2.0.0.6, on this Pentium M 740 powered notebook (that is not
even sold anymore) on Windows XP SP2, it took me about only a second after
pressing Ctrl+F to find the relevant section. And there was no indexing needed.
I did find % and %25 and went over a few hits.
You could have searched for "percent".
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Sep 4 '07 #3
Thomas 'PointedEars' Lahn wrote:
"http://domain.com%25" is a valid URI, because the "%" is followed
by two hexadecimal digits (see above).
No, that is not a valid URI. From http://www.ietf.org/rfc/rfc3986.txt
:

| URI producing applications must not use percent-encoding in
| host unless it is used to represent a UTF-8 character sequence.
| URI producers should provide these registered names in the IDNA
| encoding, rather than a percent-encoding, if they wish to
| maximize interoperability with legacy URI resolvers.

--
Bart

Sep 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kenneth Porter | last post by:
Posting this for posterity and for googlers looking for solutions to this problem. I upgraded PHP on my Fedora Linux server to 4.3.10 due to the recent security issue and phpMyAdmin stopped...
6
by: Ed Schofield | last post by:
Hi all, I find this strange: >>> flist = >>> for i in range(3): .... f = lambda x: x+i .... flist.append(f) .... >>>
3
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error'...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
0
by: Yin99 | last post by:
Do the TableAdapters have any limitations, gotcha's, or anything that may bite ya in the rear down the road? I'm looking for lessons learned and/or past experience. I'm starting a new...
16
by: Hendrik van Rooyen | last post by:
I thought I would share this nasty little gotcha with the group. Consider the following code fragment: <start> print 'starting kbd thread' keyboard_thread = thread.start_new_thread(kbd_driver...
14
by: Hendrik van Rooyen | last post by:
Hi, I am surprised that it took me so long to bloody my nose on this one. It must be well known - and I would like to find out how well known. So here is a CLOSED BOOK multiple choice...
0
by: Just_a_fan | last post by:
I am so happy about this I had to tell someone and since no one at the house even knows how to spell VB, I just had to make a short post. I finally got my little program to correctly do a second...
5
by: cnb | last post by:
this recursive definition of sum thrumped me, is this some sort of gotcha or am I just braindead today? and yes i know this is easy a a for x in xs acc += x or just using the builtin. def...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.