[the-board/wip/http-api: 1/4] [util] Add simple mechanism to export object via HTTP



commit d10e0ebf72206343165c1b17f58a4ecd07faeda2
Author: Lucas Rocha <lucasr gnome org>
Date:   Tue Nov 23 01:24:50 2010 +0000

    [util] Add simple mechanism to export object via HTTP
    
    Similar to DBus' exportObject. Only added very initial code to export
    json methods.

 configure.ac        |    1 +
 src/Makefile-js.am  |    1 +
 src/js/util/http.js |   65 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 3709548..b395a73 100644
--- a/configure.ac
+++ b/configure.ac
@@ -51,6 +51,7 @@ PKG_CHECK_MODULES(THE_BOARD,
                   clutter-1.0
                   mx-1.0
                   gtk+-3.0
+                  libsoup-2.4
                   clutter-gtk-1.0
                   clutter-gst-1.0)
 
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index b0aa4ab..8811449 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -44,6 +44,7 @@ js/util/path.js: js/util/path.js.in
 	$(AM_V_GEN) $(do_subst) $< > $@
 
 dist_jsutil_DATA = \
+    js/util/http.js \
     js/util/json.js \
     js/util/mathUtil.js \
     js/util/path.js
diff --git a/src/js/util/http.js b/src/js/util/http.js
new file mode 100644
index 0000000..92b1eb6
--- /dev/null
+++ b/src/js/util/http.js
@@ -0,0 +1,65 @@
+// standard imports
+const Lang = imports.lang;
+
+// gi imports
+const Soup = imports.gi.Soup;
+
+// util imports
+const JSON = imports.util.json;
+
+function exportObject(port, iface, object) {
+    log('Http: exporting object on port ' + port);
+
+    object._httpServer = new Soup.Server({ port: port });
+
+    let handleRequest = function(server, msg, path, query, client, method) {
+        let args = {};
+
+        // Only accept local http requests
+        if (client.get_host() != "127.0.0.1") {
+            return;
+        }
+
+        if (query) {
+            Lang.copyProperties(query, args);
+        }
+
+        let result = object[method.name].apply(object, [args]);
+
+        if (result) {
+            let resultStr = JSON.JSON.stringify(result, null, 4);
+
+            msg.set_response("text/plain",
+                             Soup.MemoryUse.COPY,
+                             resultStr,
+                             resultStr.length);
+        }
+
+        msg.set_status(Soup.KnownStatusCode.OK);
+    }
+
+    if ('methods' in iface) {
+        let methods = iface.methods;
+
+        for (let i = 0; i < methods.length; ++i) {
+            let method = methods[i];
+
+            if (!('name' in method))
+                throw new Error("Method definition must have a name");
+
+            // Only export json methods
+            if (('inSignature' in method && method.inSignature != "a{sv}") ||
+                ('outSignature' in method && method.outSignature != "a{sv}")) {
+                continue;
+            }
+
+            log('Http: exporting method ' + method.name);
+
+            object._httpServer.add_handler("/" + method.name,
+                                           Lang.bind(object, handleRequest, method),
+                                           null, null);
+        }
+    }
+
+    object._httpServer.run_async();
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]