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



commit 5b09f41350b965276abff96de5476d828c0cb8cf
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 |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 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..66ed373
--- /dev/null
+++ b/src/js/util/http.js
@@ -0,0 +1,53 @@
+// standard imports
+const Lang = imports.lang;
+
+// gi imports
+const Soup = imports.gi.Soup;
+
+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);
+        }
+
+        object[method.name].apply(object, [args]);
+
+        msg.set_status(200);
+    }
+
+    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]