[the-board] [util] Add simple mechanism to export object via HTTP



commit 827c588bc44443a746683240bb8cdf8fbefde5c3
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 very initial code to export json
    methods. i.e. inSignature and outSignature are "a{sv}".

 configure.ac               |   21 ++++++++++++++
 src/Makefile-js.am         |    1 +
 src/Makefile.am            |    3 +-
 src/js/ui/application.js   |    4 +++
 src/js/util/features.js.in |    1 +
 src/js/util/http.js        |   65 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 94 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 1f8778b..f5374c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -76,6 +76,26 @@ else
     HAVE_LIBNOTIFY_RESULT="no"
 fi
 
+AC_ARG_ENABLE([libsoup],
+              AC_HELP_STRING([--enable-libsoup],[enable libsoup support]),,
+              [enable_libsoup=yes])
+
+HAVE_LIBSOUP=0
+
+if test x$enable_libsoup = xyes ; then
+   PKG_CHECK_MODULES([LIBSOUP],
+                     [libsoup-2.4],
+                     [HAVE_LIBSOUP=1],[HAVE_LIBSOUP=0])
+fi
+
+AC_SUBST(HAVE_LIBSOUP)
+
+if test x$HAVE_LIBSOUP = x1 ; then
+    HAVE_LIBSOUP_RESULT="yes"
+else
+    HAVE_LIBSOUP_RESULT="no"
+fi
+
 AC_ARG_ENABLE([nautilus],
               AC_HELP_STRING([--enable-nautilus],[enable nautilus support]),,
               [enable_nautilus=yes])
@@ -131,6 +151,7 @@ echo "
 
         nautilus:  $HAVE_NAUTILUS_RESULT
         libnotify: $HAVE_LIBNOTIFY_RESULT
+        libsoup:   $HAVE_LIBSOUP_RESULT
 
         Now type 'make' to build $PACKAGE
 "
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 6fc99db..9c13a82 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -47,6 +47,7 @@ js/util/features.js: js/util/features.js.in
 	$(AM_V_GEN) $(do_subst) $< > $@
 
 dist_jsutil_DATA = \
+    js/util/http.js \
     js/util/features.js \
     js/util/json.js \
     js/util/mathUtil.js \
diff --git a/src/Makefile.am b/src/Makefile.am
index a4c3aa6..b1a0b7e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -39,7 +39,8 @@ do_subst = sed -e 's|@abs_top_srcdir[ ]|$(abs_top_srcdir)|g' \
                -e 's|@libexecdir[ ]|$(libexecdir)|g' \
 	       -e 's|@pkglibdir[ ]|$(pkglibdir)|g' \
 	       -e 's|@pkgdatadir[ ]|$(pkgdatadir)|g' \
-	       -e 's|@have_libnotify[ ]|$(HAVE_LIBNOTIFY)|g'
+	       -e 's|@have_libnotify[ ]|$(HAVE_LIBNOTIFY)|g' \
+	       -e 's|@have_libsoup[ ]|$(HAVE_LIBSOUP)|g'
 
 # gobject-introspection rules
 include $(INTROSPECTION_MAKEFILE)
diff --git a/src/js/ui/application.js b/src/js/ui/application.js
index c52738c..996438c 100644
--- a/src/js/ui/application.js
+++ b/src/js/ui/application.js
@@ -7,6 +7,10 @@ const Lang = imports.lang;
 const Features = imports.util.features;
 const Path = imports.util.path;
 
+if (Features.HAVE_LIBSOUP) {
+    const Http = imports.util.http;
+}
+
 // gi imports
 const Gtk = imports.gi.Gtk;
 const Mx = imports.gi.Mx;
diff --git a/src/js/util/features.js.in b/src/js/util/features.js.in
index 4cb2efa..5362929 100644
--- a/src/js/util/features.js.in
+++ b/src/js/util/features.js.in
@@ -1 +1,2 @@
 let HAVE_LIBNOTIFY = @have_libnotify@; 
+let HAVE_LIBSOUP = @have_libsoup@;
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]