easyXDM Rpc example throws javascript error (double encoded URL?)

954 views
Skip to first unread message

Dan Parent

unread,
Aug 4, 2011, 2:11:57 PM8/4/11
to eas...@googlegroups.com
I've been trying to get the easyXDM.Rpc example setup across domains however after tracing through easyXDM.debug.js I can see that the xdm_e URL is double encoded.  This causes a javascript error to occur.  I've done a google search and searched through this newsgroup and I've come up empty-handed so I'm not sure what is unique about my situation.

This is what I see in the debugger when the javascript error occurs:
/**
* Returns a string containing the schema, domain and if present the port
* @param {String} url The url to extract the location from
* @return {String} The location part of the url
*/
function getLocation(url){
if (!url) {
throw new Error("url is undefined or empty");
}
if (/^file/.test(url)) {
throw new Error("The file:// protocol is not supported");
}
var m = url.toLowerCase().match(reURI);
var proto = m[2], domain = m[3], port = m[4] || "";
easyXDM.debug.js:294Uncaught TypeError: Cannot read property '2' of null
if ((proto == "http:" && port == ":80") || (proto == "https:" && port == ":443")) {
port = "";
}
return proto + "//" + domain + port;
}

Debugging through the code shows that url is doubly encoded. Here is how I have things setup for the provider and consumer (hopefully I get these right).

The consumer machine is a test machine hosted on rackspace and the simple html file I load contains the following:
<html> <head> <script src="http://localhost/demo/media/easyxdm/easyXDM.debug.js" type="text/javascript"></script> <script type="text/javascript"> var xhr = new easyXDM.Rpc({ remote: "http://localhost/demo/media/easyxdm/cors/", onReady: function() { alert("I am ready"); } }, { remote: { request: {} // request is exposed by /cors/ } }); </script> </head> <body> </body> </html>

The provider is localhost, hence the URL above will load easyXDM.debug.js from my local machine. The idea being that when I embed in the consumer I will not have the ability to upload javascript files to it and will dynamically load an external javascript file from the provider. The url http://localhost/demo/media/easyxdm is the default directory structure from the easyxdm distribution.

From tracing through the code I can see that xdm_e is url encoded for the first time before setting the src of the dynamically created iframe is set. After that the easyXDM code is called again and this time the url xdm_e variable is now doubly encoded. The code continues until I hit the javascript error above.

I can get around the problem by doing hacking in another decode call when I notice that xdm_e is doubly encoded but that seems like a hack to me. Here's how I get around the problem:

// build the query object either from location.query, if it contains the xdm_e argument, or from location.hash
var query = (function(input){
    input = input.substring(1).split("&");
    var data = {}, pair, i = input.length;
    while (i--) {
        pair = input[i].split("=");
if (pair[0] == 'xdm_e' && pair[1].indexOf('%253A%252F%252') >= 0)
{
// XXX Workaround, when doing the Rpc example the URL gets double URI encoded
// I've walked through the code and I see it get encoded once then set as the
// created iframe src which appears to encode yet again
pair[1] = decodeURIComponent(pair[1]);
}
        data[pair[0]] = decodeURIComponent(pair[1]);
    }
    return data;
}(/xdm_e=/.test(location.search) ? location.search : location.hash));

What am I doing wrong? Am I missing something here?

Thanks, Dan

Øyvind Sean Kinsey

unread,
Aug 4, 2011, 2:26:42 PM8/4/11
to eas...@googlegroups.com
Without seeing a real example, or getting all the code, debugging is impossible.

As a control, does http://consumer.easyxdm.net/current/example/methods.html work? Yeah, I thought so :)

The thing is, encodeURIComponent is only used one place, and that is when passing xdm_e from the consume to the parent - unless you are doing something very strange, this should never be cascaded (causing double escaping to happen).

Øyvind

Dan Parent

unread,
Aug 15, 2011, 12:32:38 PM8/15/11
to eas...@googlegroups.com
Found some more time to go through this and found out I was adding in the easyXDM.debug.js script more then once in my code.  It was causing the problem.  When I went through everything and made sure I only use easyXDM.debug.js once the problem went away.  Just in case someone else hits my problem while first using easyXDM.  Great project, glad I could find out what was wrong on my end.

Dan
Reply all
Reply to author
Forward
0 new messages