work in progress (tbf)
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 1 Apr 2016 15:00:47 +0000 (17:00 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 1 Apr 2016 15:00:47 +0000 (17:00 +0200)
Change-Id: I01f72892530bb4ef14a7216a112812026a367bfa
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/afb-req-itf.h [moved from src/afb-req-itf.h with 100% similarity]
include/local-def.h
plugins/samples/HelloWorld.c
src/afb-apis.c
src/afb-hreq.c
src/helper-api.c

similarity index 100%
rename from src/afb-req-itf.h
rename to include/afb-req-itf.h
index 605c05a..f46dd97 100644 (file)
@@ -124,60 +124,6 @@ typedef struct AFB_plugin AFB_plugin;
 typedef enum  {AFB_MODE_LOCAL=0, AFB_MODE_REMOTE, AFB_MODE_GLOBAL} AFB_Mode;
 
 
-#if 0
-
-typedef enum  {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType;
-
-
-
-// Post Upload File Handle
-typedef struct {
-   int   fd; 
-   char *path; 
-   int  errcode;
-   struct json_object* jresp;
-} AFB_PostCtx;
-
-typedef  struct {
-    int  len;   // post element size
-    char *data; // post data in raw format
-    AFB_PostType type; // Json type
-} AFB_PostRequest;
-  
-// Post handler
-typedef struct {
-  void*  ctx;               // Application context
-  int    len;               // current len for post
-  int    uid;               // post uid for debug
-  AFB_PostType type;        // JSON or FORM
-  AFB_apiCB  completeCB;    // callback when post is completed
-  char   *privatebuf;       // use internally to keep track or partial buffer
-  struct MHD_PostProcessor *pp; // iterator handle
-} AFB_PostHandle;
-
-typedef struct {
-    enum MHD_ValueKind kind; // kind type of the value
-    const char *key;         // key 0-terminated key for the value
-    const char *filename;    // filename of the uploaded file, NULL if not known
-    const char *mimetype;    // content_type mime-type of the data, NULL if not known
-    const char *encoding;    // transfer_encoding encoding of the data, NULL if not known
-    const char *data;        // data pointer to size bytes of data at the specified offset
-    uint64_t   offset;       // offset of data in the overall value
-    size_t     len;          // number of bytes in data available
-} AFB_PostItem;
-
-typedef struct {
-  char  path[512];
-  int   fd;
-} AFB_staticfile;
-
-typedef struct {
-     char    *msg;
-     size_t  len;
-} AFB_redirect_msg;
-
-#endif
-
 typedef struct {
   char  *url;
   char  *path;
index 85e92f4..01275aa 100644 (file)
@@ -15,6 +15,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+#define _GNU_SOURCE
 
 #include <stdio.h>
 #include <string.h>
@@ -22,9 +23,8 @@
 #include "local-def.h"
 #include "afb-req-itf.h"
 
-static json_object* pingSample (AFB_request *request) {
+static json_object* ping (AFB_request *request, json_object *jresp) {
     static int pingcount = 0;
-    json_object *response;
     char query [512];
     size_t len;
 
@@ -34,14 +34,18 @@ static json_object* pingSample (AFB_request *request) {
     
     // return response to caller
 //    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon %d query={%s}", pingcount++, query);
-    afb_req_success_f(*request->areq, NULL, "Ping Binder Daemon %d query={%s}", pingcount++, query);
+    afb_req_success_f(*request->areq, jresp, "Ping Binder Daemon %d query={%s}", pingcount++, query);
     
     if (verbose) fprintf(stderr, "%d: \n", pingcount);
-    return NULL; //(response);
+    return jresp;
+}
+
+static json_object* pingSample (AFB_request *request) {
+       return ping(request, json_object_new_string ("Some String"));
 }
 
 static json_object* pingFail (AFB_request *request) {
-    return NULL;
+       return ping(request, NULL);
 }
 
 static json_object* pingBug (AFB_request *request) {
@@ -70,8 +74,8 @@ static json_object* pingJson (AFB_request *request) {
     json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
     
     json_object_object_add(jresp,"eobj", embed);
-    
-    return jresp;
+
+    return ping(request, jresp);
 }
 
 // NOTE: this sample does not use session to keep test a basic as possible
index effef8a..8fbd9fc 100644 (file)
@@ -37,7 +37,7 @@
 #include <sys/syscall.h>
 #include <setjmp.h>
 
-#include "../include/local-def.h"
+#include "local-def.h"
 
 #include "afb-req-itf.h"
 #include "afb-apis.h"
@@ -49,6 +49,7 @@ struct api_desc {
        void *handle;           /* context of dlopen */
 };
 
+static int api_timeout = 15;
 static struct api_desc *apis_array = NULL;
 static int apis_count = 0;
 
@@ -285,9 +286,9 @@ int afb_apis_add_pathset(const char *pathset)
 
 // Check of apiurl is declare in this plugin and call it
 extern __thread sigjmp_buf *error_handler;
-static int callPluginApi(AFB_request * request)
+static void trapping_handle(AFB_request * request, struct json_object *(*cb)(AFB_request *,void*))
 {
-       volatile int status, timerset;
+       volatile int signum, timerset;
        timer_t timerid;
        sigjmp_buf jmpbuf, *older;
        struct sigevent sevp;
@@ -296,43 +297,38 @@ static int callPluginApi(AFB_request * request)
        // save context before calling the API
        timerset = 0;
        older = error_handler;
-       status = setjmp(jmpbuf);
-       if (status != 0) {
-               status = 0;
+       signum = setjmp(jmpbuf);
+       if (signum != 0) {
+               afb_req_fail_f(*request->areq, "aborted", "signal %d caught", signum);
        }
        else {
                error_handler = &jmpbuf;
-               if (request->config->apiTimeout > 0) {
+               if (api_timeout > 0) {
                        timerset = 1; /* TODO: check statuses */
                        sevp.sigev_notify = SIGEV_THREAD_ID;
                        sevp.sigev_signo = SIGALRM;
 #if defined(sigev_notify_thread_id)
-                       sevp.sigev_notify_thread_id = syscall(SYS_gettid);
+                       sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
 #else
-                       sevp._sigev_un._tid = syscall(SYS_gettid);
+                       sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
 #endif
                        timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
                        its.it_interval.tv_sec = 0;
                        its.it_interval.tv_nsec = 0;
-                       its.it_value.tv_sec = 15;
+                       its.it_value.tv_sec = api_timeout;
                        its.it_value.tv_nsec = 0;
                        timer_settime(timerid, 0, &its, NULL);
                }
 
-               //doCallPluginApi(request, apiidx, verbidx, context);
-               status = 1;
+               cb(request, NULL);
        }
        if (timerset)
                timer_delete(timerid);
        error_handler = older;
-
-       return status;
 }
 
 static void handle(struct afb_req req, const struct api_desc *api, const struct AFB_restapi *verb)
 {
-       json_object *jresp, *jcall, *jreqt;
-
        AFB_request request;
 
        request.uuid = request.url = "fake";
@@ -357,7 +353,7 @@ static void handle(struct afb_req req, const struct api_desc *api, const struct
        default:
                break;
        }
-       verb->callback(&request, NULL);
+       trapping_handle(&request, verb->callback);
 
        if (verb->session == AFB_SESSION_CLOSE)
                /*close*/;
@@ -369,20 +365,18 @@ int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const ch
        const struct api_desc *a;
        const struct AFB_restapi *v;
 
-//fprintf(stderr,"afb_apis_handle prefix:%.*s verb:%.*s\n",(int)lenapi,api,(int)lenverb,verb);
        a = apis_array;
        for (i = 0 ; i < apis_count ; i++, a++) {
                if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) {
-//fprintf(stderr,"afb_apis_handle found prefix:%.*s -> %s\n",(int)lenapi,api,a->prefix);
                        v = a->plugin->apis;
                        for (j = 0 ; v->name ; j++, v++) {
                                if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) {
-//fprintf(stderr,"afb_apis_handle found prefix:%.*s verb:%.*s -> %s/%s\n",(int)lenapi,api,(int)lenverb,verb,a->prefix,v->name);
                                        handle(req, a, v);
                                        return 1;
                                }
                        }
-                       break;
+                       afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix);
+                       return 1;
                }
        }
        return 0;
index 58dbb04..e146bcb 100644 (file)
@@ -32,6 +32,8 @@
 #include "afb-req-itf.h"
 #include "afb-hreq.h"
 
+#define SIZE_RESPONSE_BUFFER   8000
+
 static char empty_string[] = "";
 
 struct hreq_data {
@@ -460,7 +462,7 @@ static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *statu
        if (resp)
                json_object_object_add(root, "response", resp);
 
-       response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 65500, (void*)send_json_cb, root, (void*)json_object_put);
+       response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, SIZE_RESPONSE_BUFFER, (void*)send_json_cb, root, (void*)json_object_put);
        MHD_queue_response(hreq->connection, retcode, response);
        MHD_destroy_response(response);
 }
index 1e3d882..9d1ec0b 100644 (file)
@@ -54,7 +54,6 @@ PUBLIC int verbose;
 static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success", "done", "unauth"};
 
 
-
 // Helper to retrieve argument from  connection
 const char* getQueryValue(const AFB_request * request, const char *name) {
     return afb_req_argument(*request->areq, name);