r19051: JSON-RPC server work-in-progress. It's almost working.
authorDerrell Lipman <derrell@samba.org>
Mon, 2 Oct 2006 20:39:31 +0000 (20:39 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 19:20:31 +0000 (14:20 -0500)
jsonrpc/json.esp
jsonrpc/jsondate.esp
jsonrpc/request.esp
source/scripting/ejs/smbcalls.c
source/scripting/ejs/smbcalls_sys.c
source/web_server/http.c

index ad0e13a6c339ab3a7c82ec7dc1b7a763d5cf86a1..32f0fa776a6467232ac2bde131b678b759418f91 100644 (file)
@@ -37,6 +37,11 @@ function _escape(s)
         }
     }
 
         }
     }
 
+    if (arr.length == 0)
+    {
+        return "";
+    }
+    
     return join("", arr);
 }
 
     return join("", arr);
 }
 
@@ -72,10 +77,14 @@ function _encode(o)
     }
     else if (type == "float" ||
              type == "integer" ||
     }
     else if (type == "float" ||
              type == "integer" ||
-             type == "integer64" ||
-             type == "pointer")
+             type == "integer64")
+    {
+        return o + 0;
+    }
+    else if (type == "pointer")
     {
     {
-        return (o + 0);
+        var x = "" + o;
+        return '"' + substr(x, 16, strlen(x) - 16 - 1) + '"';
     }
     else if (type == "object")
     {
     }
     else if (type == "object")
     {
@@ -128,6 +137,10 @@ function _encode(o)
     {
         return '"' + this._internal.escape(o) + '"';
     }
     {
         return '"' + this._internal.escape(o) + '"';
     }
+    else
+    {
+        return '{ "unknown_object":"' + type + '"}';
+    }
 }
 
 /* Allocate the public Json access object */
 }
 
 /* Allocate the public Json access object */
@@ -193,6 +206,7 @@ Json._internal.convert['\x1f'] = '\\u001f';
 
 
 /* Test it */
 
 
 /* Test it */
+/*
 libinclude("base.js");
 function testFormat()
 {
 libinclude("base.js");
 function testFormat()
 {
@@ -211,7 +225,11 @@ function testFormat()
     test.obj.array[1] = 223;
     printf("%s\n", Json.encode(test));
 }
     test.obj.array[1] = 223;
     printf("%s\n", Json.encode(test));
 }
+testFormat();
+*/
 
 
+/*
+libinclude("base.js");
 function testParse()
 {
     var s;
 function testParse()
 {
     var s;
@@ -236,8 +254,6 @@ function testParse()
     obj = Json.decode(s);
     printf("Decode/encode of\n\t%s\nyielded\n\t%s\n\n", s, Json.encode(obj));
 }
     obj = Json.decode(s);
     printf("Decode/encode of\n\t%s\nyielded\n\t%s\n\n", s, Json.encode(obj));
 }
-
-//testFormat();
 testParse();
 testParse();
-
+*/
 %>
 %>
index af2c7e2e3f9895ef739c466e047b4d53bfe44c45..b2f2b9ec11ce1d2e7c7a76865c473cb0591f46c9 100644 (file)
@@ -78,7 +78,7 @@ function _JSON_Date_create(secondsSinceEpoch)
 
         if (typeof(secondsSinceEpoch) != "number")
         {
 
         if (typeof(secondsSinceEpoch) != "number")
         {
-            var currentTime = getTimeOfDay();
+            var currentTime = gettimeofday();
             secondsSinceEpoch = currentTime.sec;
             microseconds = currentTime.usec;
         }
             secondsSinceEpoch = currentTime.sec;
             microseconds = currentTime.usec;
         }
@@ -105,7 +105,7 @@ function _JSON_Date_create(secondsSinceEpoch)
     {
         return this.month;
     }
     {
         return this.month;
     }
-    o.getUtcMonth = getUtcMonth;
+    o.getUtcMonth = _getUtcMonth;
 
     function _getUtcDay()
     {
 
     function _getUtcDay()
     {
@@ -167,7 +167,7 @@ function _JSON_Date_create(secondsSinceEpoch)
 
     if (! secondsSinceEpoch)
     {
 
     if (! secondsSinceEpoch)
     {
-        var now = getTimeOfDay();
+        var now = gettimeofday();
         o.setEpochTime(now.sec);
     }
     else
         o.setEpochTime(now.sec);
     }
     else
index a8080d9dc717d5ea1fe9218419ed9913ed900906..d58a00069c054c27d12576181b03fa76ead0fca5 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 /* Bring in the date class */
  */
 
 /* Bring in the date class */
+jsonrpc_include("json.esp");
 jsonrpc_include("jsondate.esp");
 
 /* bring the string functions into the global frame */
 jsonrpc_include("jsondate.esp");
 
 /* bring the string functions into the global frame */
@@ -28,12 +29,6 @@ function printf()
 }
 
 
 }
 
 
-/* KLUDGE... */
-form = new Array();
-server = new Array();
-request = new Array();
-/* ...KLUDGE */
-
 /*
  * All of our manipulation of JSON RPC methods will be through this object.
  * Each class of methods will assign to here, and all of the constants will
 /*
  * All of our manipulation of JSON RPC methods will be through this object.
  * Each class of methods will assign to here, and all of the constants will
@@ -147,7 +142,7 @@ function sendReply(reply, scriptTransportId)
     if (scriptTransportId == jsonrpc.Constant.ScriptTransport.NotInUse)
     {
         /* ... then just output the reply. */
     if (scriptTransportId == jsonrpc.Constant.ScriptTransport.NotInUse)
     {
         /* ... then just output the reply. */
-        printf(reply);
+        write(reply);
     }
     else
     {
     }
     else
     {
@@ -156,43 +151,11 @@ function sendReply(reply, scriptTransportId)
             "qx.io.remote.ScriptTransport._requestFinished(" +
             scriptTransportId + ", " + reply +
             ");";
             "qx.io.remote.ScriptTransport._requestFinished(" +
             scriptTransportId + ", " + reply +
             ");";
-        printf(reply);
+        write(reply);
     }
 }
 
 
     }
 }
 
 
-/*
- * class Json
- *
- * This class provides the JSON encoder and decoder, and some utility
- * functions.
- */
-Json = new Object();
-
-/* KLUDGE... */
-function _jsonDecode(s)
-{
-    var o = new Object();
-    o.id = 23;
-    o.service = "qooxdoo.test";
-    o.method = "echo";
-    o.params = new Array(1);
-    o.params[0] = "hello world";
-    return o;
-}
-/* ...KLUDGE */
-
-Json.decode = _jsonDecode;
-
-/* KLUDGE... */
-function _jsonEncode(o)
-{
-    return "{ result: \"hello world\" }"
-}
-/* ...KLUDGE */
-
-Json.encode = _jsonEncode;
-
 function _jsonValidRequest(req)
 {
     if (req == undefined)
 function _jsonValidRequest(req)
 {
     if (req == undefined)
@@ -300,14 +263,14 @@ error = jsonrpc.createError(jsonrpc.Constant.ErrorOrigin.Server,
 scriptTransportId = jsonrpc.Constant.ScriptTransport.NotInUse;
 
 /* What type of request did we receive? */
 scriptTransportId = jsonrpc.Constant.ScriptTransport.NotInUse;
 
 /* What type of request did we receive? */
-if (server["REQUEST_METHOD"] == "POST" &&
-    server["CONENT_TYPE"] == "text/json")
+if (request["REQUEST_METHOD"] == "POST" &&
+    request["CONTENT_TYPE"] == "text/json")
 {
     /* We found literal POSTed json-rpc data (we hope) */
     input = request["POST_DATA"];
     jsonInput = Json.decode(input);
 }
 {
     /* We found literal POSTed json-rpc data (we hope) */
     input = request["POST_DATA"];
     jsonInput = Json.decode(input);
 }
-else if (server["REQUEST_METHOD"] == "GET" &&
+else if (request["REQUEST_METHOD"] == "GET" &&
          form["_ScriptTransport_id"] != undefined &&
          form["_ScriptTransport_data"] != undefined)
 {
          form["_ScriptTransport_id"] != undefined &&
          form["_ScriptTransport_data"] != undefined)
 {
@@ -318,10 +281,6 @@ else if (server["REQUEST_METHOD"] == "GET" &&
     jsonInput = Json.decode(input);
 }
 
     jsonInput = Json.decode(input);
 }
 
-/* KLUDGE... */
-jsonInput = Json.decode(input);
-/* ...KLUDGE */
-
 /* Ensure that this was a JSON-RPC service request */
 if (! jsonrpc.validRequest(jsonInput))
 {
 /* Ensure that this was a JSON-RPC service request */
 if (! jsonrpc.validRequest(jsonInput))
 {
@@ -329,7 +288,7 @@ if (! jsonrpc.validRequest(jsonInput))
      * This request was not issued with JSON-RPC so echo the error rather than
      * issuing a JsonRpcError response.
      */
      * This request was not issued with JSON-RPC so echo the error rather than
      * issuing a JsonRpcError response.
      */
-    printf("JSON-RPC request expected; service, method or params missing<br>");
+    write("JSON-RPC request expected; service, method or params missing<br>");
     return;
 }
 
     return;
 }
 
index 839ec7b634857490aae220cd8fa4e832f7c722d4..815b3e2b5d8f1cd5246bbb2620cb9df1e57803a5 100644 (file)
@@ -203,6 +203,38 @@ static int jsonrpc_include(int eid, int argc, char **argv)
 }
 
 
 }
 
 
+static int ejs_debug(int eid, int argc, char **argv)
+{
+        int i;
+        int level;
+        void *ctx = mprMemCtx();
+        char *msg;
+
+
+        if (argc < 2) {
+               return -1;
+        }
+
+        level = atoi(argv[0]);
+
+        msg = talloc_zero_size(ctx, 1);
+        if (msg == NULL) {
+                DEBUG(0, ("out of memory in debug()\n"));
+                return 0;
+        }
+
+        for (i = 1; i < argc; i++) {
+                msg = talloc_append_string(ctx, msg, argv[i]);
+                if (msg == NULL) {
+                        DEBUG(0, ("out of memory in debug()\n"));
+                        return 0;
+                }
+        }
+
+        DEBUG(level, ("%s", msg));
+        talloc_free(msg);
+       return 0;
+}
 
 static void (*ejs_exception_handler) (const char *) = NULL;
 
 
 static void (*ejs_exception_handler) (const char *) = NULL;
 
@@ -241,5 +273,6 @@ void smb_setup_ejs_functions(void (*exception_handler)(const char *))
        ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
        ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
        ejsDefineStringCFunction(-1, "jsonrpc_include", jsonrpc_include, NULL, MPR_VAR_SCRIPT_HANDLE);
        ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
        ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
        ejsDefineStringCFunction(-1, "jsonrpc_include", jsonrpc_include, NULL, MPR_VAR_SCRIPT_HANDLE);
+       ejsDefineStringCFunction(-1, "debug", ejs_debug, NULL, MPR_VAR_SCRIPT_HANDLE);
 }
 
 }
 
index 42990f49c0ed8f0bc202ccb72d9c8865cc17a165..97fcc19cd1f85c04f256b22227a54da80fa8e7a3 100644 (file)
@@ -413,7 +413,7 @@ static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
        mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
        mprSetCFunction(obj, "hostname", ejs_sys_hostname);
        mprSetCFunction(obj, "nttime", ejs_sys_nttime);
        mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
        mprSetCFunction(obj, "hostname", ejs_sys_hostname);
        mprSetCFunction(obj, "nttime", ejs_sys_nttime);
-       mprSetCFunction(obj, "getTimeOfDay", ejs_sys_gettimeofday);
+       mprSetCFunction(obj, "gettimeofday", ejs_sys_gettimeofday);
        mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime);
        mprSetCFunction(obj, "gmmktime", ejs_sys_gmmktime);
        mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
        mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime);
        mprSetCFunction(obj, "gmmktime", ejs_sys_gmmktime);
        mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
index 6e42c8a39cd95576b1d13c4ab59dd626b52d71e6..057acc70b30d9cb1e2677425baf675b7f3532b19 100644 (file)
@@ -36,6 +36,7 @@
 #define SWAT_SESSION_KEY "SwatSessionId"
 #define HTTP_PREAUTH_URI "/scripting/preauth.esp"
 #define JSONRPC_REQUEST "/services"
 #define SWAT_SESSION_KEY "SwatSessionId"
 #define HTTP_PREAUTH_URI "/scripting/preauth.esp"
 #define JSONRPC_REQUEST "/services"
+#define JSONRPC_SERVER "/services/request.esp"
 
 /* state of the esp subsystem for a specific request */
 struct esp_state {
 
 /* state of the esp subsystem for a specific request */
 struct esp_state {
@@ -415,9 +416,10 @@ static void http_setup_arrays(struct esp_state *esp)
        SETVAR(ESP_REQUEST_OBJ, "CONTENT_LENGTH", 
               talloc_asprintf(esp, "%u", web->input.content_length));
        SETVAR(ESP_REQUEST_OBJ, "QUERY_STRING", web->input.query_string);
        SETVAR(ESP_REQUEST_OBJ, "CONTENT_LENGTH", 
               talloc_asprintf(esp, "%u", web->input.content_length));
        SETVAR(ESP_REQUEST_OBJ, "QUERY_STRING", web->input.query_string);
-#if 0 /* djl -- not yet.  need to track down the compiler warning */
-       SETVAR(ESP_REQUEST_OBJ, "POST_DATA", web->input.partial);
-#endif
+       SETVAR(ESP_REQUEST_OBJ, "POST_DATA",
+               talloc_strndup(esp,
+                              web->input.partial.data,
+                              web->input.partial.length));
        SETVAR(ESP_REQUEST_OBJ, "REQUEST_METHOD", web->input.post_request?"POST":"GET");
        SETVAR(ESP_REQUEST_OBJ, "REQUEST_URI", web->input.url);
        p = strrchr(web->input.url, '/');
        SETVAR(ESP_REQUEST_OBJ, "REQUEST_METHOD", web->input.post_request?"POST":"GET");
        SETVAR(ESP_REQUEST_OBJ, "REQUEST_URI", web->input.url);
        p = strrchr(web->input.url, '/');
@@ -427,7 +429,6 @@ static void http_setup_arrays(struct esp_state *esp)
                struct MprVar mpv = mprObject("socket_address");
                mprSetPtrChild(&mpv, "socket_address", peer_address);
                espSetVar(req, ESP_REQUEST_OBJ, "REMOTE_SOCKET_ADDRESS", mpv);
                struct MprVar mpv = mprObject("socket_address");
                mprSetPtrChild(&mpv, "socket_address", peer_address);
                espSetVar(req, ESP_REQUEST_OBJ, "REMOTE_SOCKET_ADDRESS", mpv);
-
                SETVAR(ESP_REQUEST_OBJ, "REMOTE_ADDR", peer_address->addr);
        }
        p = socket_get_peer_name(web->conn->socket, esp);
                SETVAR(ESP_REQUEST_OBJ, "REMOTE_ADDR", peer_address->addr);
        }
        p = socket_get_peer_name(web->conn->socket, esp);
@@ -522,6 +523,30 @@ static void esp_request(struct esp_state *esp, const char *url)
        talloc_free(buf);
 }
 
        talloc_free(buf);
 }
 
+/*
+  process a JSON RPC request
+*/
+static void jsonrpc_request(struct esp_state *esp)
+{
+       const char *path = http_local_path(esp->web, JSONRPC_SERVER);
+
+        /* Ensure we got a valid path. */
+       if (path == NULL) {
+                /* should never occur */
+               http_error(esp->web, 500, "Internal server error");
+               return;
+       }
+
+        /* Ensure that the JSON-RPC server request script exists */
+       if (!file_exist(path)) {
+               http_error_unix(esp->web, path);
+               return;
+       }
+
+        /* Call the server request script */
+       esp_request(esp, JSONRPC_SERVER);
+}
+
 /*
   perform pre-authentication on every page is /scripting/preauth.esp
   exists.  If this script generates any non-whitepace output at all,
 /*
   perform pre-authentication on every page is /scripting/preauth.esp
   exists.  If this script generates any non-whitepace output at all,
@@ -859,7 +884,11 @@ void http_process_input(struct websrv_context *web)
          * Work out the mime type.  First, we see if the request is a JSON-RPC
          * service request.  If not, we look at the extension.
          */
          * Work out the mime type.  First, we see if the request is a JSON-RPC
          * service request.  If not, we look at the extension.
          */
-        if (strcmp(web->input.url, JSONRPC_REQUEST) == 0) {
+        if (strncmp(web->input.url,
+                    JSONRPC_REQUEST,
+                    sizeof(JSONRPC_REQUEST) - 1) == 0 &&
+            (web->input.url[sizeof(JSONRPC_REQUEST) - 1] == '\0' ||
+             web->input.url[sizeof(JSONRPC_REQUEST) - 1] == '/')) {
             page_type = page_type_jsonrpc;
             file_type = "text/json";
             
             page_type = page_type_jsonrpc;
             file_type = "text/json";
             
@@ -876,6 +905,7 @@ void http_process_input(struct websrv_context *web)
             }
             if (file_type == NULL) {
                file_type = "text/html";
             }
             if (file_type == NULL) {
                file_type = "text/html";
+                page_type = page_type_simple;
             }
         }
 
             }
         }
 
@@ -908,11 +938,7 @@ void http_process_input(struct websrv_context *web)
                 break;
 
         case page_type_jsonrpc:
                 break;
 
         case page_type_jsonrpc:
-#if 0 /* djl -- not yet */
-                if (! jsonrpc_request(esp)) {
-                        http_error(web, 500, "Out of memory");
-                }
-#endif
+                jsonrpc_request(esp);
                 break;
         }
 
                 break;
         }