changeset 54:fe5a2f26787d

Made client api more humane wrt: urls.
author Atul Varma <avarma@mozilla.com>
date Sat, 26 Jun 2010 21:33:22 -0700
parents 18de6b362cc5
children 0a9c62c25938
files static-files/api.js static-files/example-client.html
diffstat 2 files changed, 60 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/static-files/api.js	Sat Jun 26 20:51:47 2010 -0700
+++ b/static-files/api.js	Sat Jun 26 21:33:22 2010 -0700
@@ -1,6 +1,39 @@
 (
   // Set up the public API for the Summit IDP.
   function(window) {
+    // parseUri 1.2.2
+    // (c) Steven Levithan <stevenlevithan.com>
+    // MIT License
+
+    function parseUri (str) {
+	var	o   = parseUri.options,
+		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
+		uri = {},
+		i   = 14;
+
+	while (i--) uri[o.key[i]] = m[i] || "";
+
+	uri[o.q.name] = {};
+	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
+		if ($1) uri[o.q.name][$1] = $2;
+	});
+
+	return uri;
+    };
+
+    parseUri.options = {
+	strictMode: false,
+	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
+	q:   {
+		name:   "queryKey",
+		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
+	},
+	parser: {
+		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
+		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
+	}
+    };
+
     var currId = 0;
 
     function MessageBroker(handlers, postMessage) {
@@ -75,18 +108,13 @@
       window.addEventListener("message", onMessage, false);
     }
 
-    function GenericClient(origin, path) {
+    function GenericClient(origin, url) {
       var broker = new MessageBroker({}, postMessage);
       var iframe = window.document.createElement("iframe");
       var otherWindow;
       var queuedMessages = [];
 
-      if (!path)
-        path = "/";
-
-      path += "server.html";
-
-      iframe.src = origin + path;
+      iframe.src = url;
 
       iframe.onload = function() {
         otherWindow = iframe.contentWindow;
@@ -115,9 +143,31 @@
       window.addEventListener("message", onMessage, false);
     };
 
-    function Client(origin, path) {
+    function Client(url) {
+      if (url === undefined)
+        url = ".";
+
+      var parsed = parseUri(url);
+
+      if (parsed.protocol == "") {
+        var a = window.document.createElement("a");
+        a.href = url;
+        window.document.documentElement.appendChild(a);
+        url = a.href;
+        window.document.documentElement.removeChild(a);
+        parsed = parseUri(url);
+      }
+
+      if (parsed.protocol != "http" &&
+          parsed.protocol != "https")
+        throw new Error("invalid server URL: " + url);
+
+      var origin = parsed.protocol + "://" + parsed.authority;
+
+      console.log(parsed, origin, url);
+
       var self = this;
-      var client = new GenericClient(origin, path);
+      var client = new GenericClient(origin, url + "server.html");
 
       function addMethod(name) {
         self[name] = function(options, cb) {
--- a/static-files/example-client.html	Sat Jun 26 20:51:47 2010 -0700
+++ b/static-files/example-client.html	Sat Jun 26 21:33:22 2010 -0700
@@ -21,8 +21,7 @@
 <script src="jquery-1.4.2.min.js"></script>
 <script>
 $(window).ready(function(event) {
-  var api = new Summit.Client(window.location.protocol + "//" +
-                              window.location.host);
+  var api = new Summit.Client();
   api.getAllUsers(function(response) {
     if (response.error) {
       $("#error .reason").text(response.error);