From 67180c4ccdab56fe1f41b293f6bbea1f955e3d91 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 5 Aug 2005 19:02:01 +0000 Subject: r9131: started adding the server side code for "AJAJ" (asynchronous javascript and javascript) This is rather like AJAX, but passes around javascript objects between the client and server, taking advantage of the fact that we have the same language on both client and server. (This used to be commit 5f9e00fb7f36f3351f40da93acfe47c911f6f65f) --- source4/scripting/libjs/encoder.js | 100 +++++++++++++++++++++++++++++++++ source4/scripting/libjs/server_call.js | 95 +++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 source4/scripting/libjs/encoder.js create mode 100644 source4/scripting/libjs/server_call.js (limited to 'source4/scripting') diff --git a/source4/scripting/libjs/encoder.js b/source4/scripting/libjs/encoder.js new file mode 100644 index 0000000000..65b2854d30 --- /dev/null +++ b/source4/scripting/libjs/encoder.js @@ -0,0 +1,100 @@ +/* + server side js functions for encoding/decoding objects into linear strings + + Copyright Andrew Tridgell 2005 + released under the GNU GPL Version 2 or later +*/ +/* + usage: + + enc = encodeObject(obj); + obj = decodeObject(enc); + + The encoded format of the object is a string that is safe to + use in URLs + + Note that only data elements are encoded, not functions +*/ + +function __count_members(o) { + var i, count = 0; + for (i in o) { + count++; + } + return count; +} + +function __replace(str, old, rep) { + var s = string_init(); + var a = s.split(old, str); + var j = s.join(rep, a); + return s.join(rep, a); +} + +function encodeObject(o) { + var s = string_init(); + var i, r = s.sprintf("%u:", __count_members(o)); + for (i in o) { + var t = typeof(o[i]); + if (t == 'object' && o[i] == null) { + t = 'null'; + } + if (t == 'object') { + r = s.sprintf("%s%s:%s:%s", r, i, t, encodeObject(o[i])); + } else if (t == "string") { + var enc = s.encodeURIComponent(o[i]); + var rep = __replace(enc, '%', '#'); + r = s.sprintf("%s%s:%s:%s:", + r, i, t, __replace(s.encodeURIComponent(o[i]),'%','#')); + } else if (t == "boolean" || t == "number") { + r = s.sprintf("%s%s:%s:%s:", r, i, t, "" + o[i]); + } else if (t == "undefined" || t == "null") { + r = s.sprintf("%s%s:%s:", r, i, t); + } else { + println("Unable to linearise type " + t); + } + } + return r; +} + +function decodeObjectArray(a) { + var s = string_init(); + var o = new Object(); + var i, count = a[a.i]; a.i++; + for (i=0;i