Provide API and VERB name of requests
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 22 Sep 2017 14:24:24 +0000 (16:24 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Mon, 9 Oct 2017 12:08:33 +0000 (14:08 +0200)
Change-Id: I9a86c6314f871334231e50f9cea60b54aed434b9
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
14 files changed:
include/afb/afb-request-itf.h
src/afb-api-dbus.c
src/afb-api-dyn.c
src/afb-api-so-v1.c
src/afb-api-so-v2.c
src/afb-export.c
src/afb-hook.c
src/afb-hreq.c
src/afb-stub-ws.c
src/afb-trace.c
src/afb-ws-json1.c
src/afb-xreq.c
src/afb-xreq.h
src/main.c

index eb7c534..e542442 100644 (file)
@@ -51,12 +51,18 @@ struct afb_request
        /* interface for the request */
        const struct afb_request_itf *itf;
 
-       /* current dynapi if dynapi (is NULL for bindings v1 and v2) */
+       /* current dynapi (if any) */
        struct afb_dynapi *dynapi;
 
        /* closure associated with the callback processing the verb of the request
         * as given at its declaration */
        void *vcbdata;
+
+       /* the name of the called verb */
+       const char *api;
+
+       /* the name of the called verb */
+       const char *verb;
 };
 
 /*
index 1aad863..806d743 100644 (file)
@@ -332,7 +332,7 @@ static void api_dbus_client_call(void *closure, struct afb_xreq *xreq)
 
        /* creates the message */
        msg = NULL;
-       rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, xreq->verb);
+       rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, xreq->request.verb);
        if (rc < 0)
                goto error;
 
@@ -985,8 +985,8 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
                dreq->json = json_object_new_string(dreq->request);
        }
        dreq->listener = listener;
-       dreq->xreq.api = api->api;
-       dreq->xreq.verb = method;
+       dreq->xreq.request.api = api->api;
+       dreq->xreq.request.verb = method;
        afb_xreq_process(&dreq->xreq, api->server.apiset);
        return 1;
 
index e829f8b..571f7fa 100644 (file)
@@ -123,7 +123,7 @@ static void call_cb(void *closure, struct afb_xreq *xreq)
        int i;
        const char *name;
 
-       name = xreq->verb;
+       name = xreq->request.verb;
        xreq->request.dynapi = (void*)dynapi->export; /* hack: this avoids to export afb_export structure */
 
        /* look first in dyna mic verbs */
index d7429ef..9a9e47a 100644 (file)
@@ -67,7 +67,8 @@ static void call_cb(void *closure, struct afb_xreq *xreq)
        const struct afb_verb_desc_v1 *verb;
        struct api_so_v1 *desc = closure;
 
-       verb = search(desc, xreq->verb);
+       xreq->request.dynapi = (void*)desc->export; /* hack: this avoids to export afb_export structure */
+       verb = search(desc, xreq->request.verb);
        afb_xreq_call_verb_v1(xreq, verb);
 }
 
index edc31d5..ba53223 100644 (file)
@@ -67,7 +67,8 @@ static void call_cb(void *closure, struct afb_xreq *xreq)
        struct api_so_v2 *desc = closure;
        const struct afb_verb_v2 *verb;
 
-       verb = search(desc, xreq->verb);
+       xreq->request.dynapi = (void*)desc->export; /* hack: this avoids to export afb_export structure */
+       verb = search(desc, xreq->request.verb);
        afb_xreq_call_verb_v2(xreq, verb);
 }
 
index ce48dbe..7fd475a 100644 (file)
@@ -568,10 +568,10 @@ static struct call_req *callreq_create(
                callreq->xreq.context.validated = 1;
                copy = (char*)&callreq[1];
                memcpy(copy, api, lenapi);
-               callreq->xreq.api = copy;
+               callreq->xreq.request.api = copy;
                copy = &copy[lenapi];
                memcpy(copy, verb, lenverb);
-               callreq->xreq.verb = copy;
+               callreq->xreq.request.verb = copy;
                callreq->xreq.listener = export->listener;
                callreq->xreq.json = args;
                callreq->export = export;
index 38ecfdc..123a641 100644 (file)
@@ -207,7 +207,7 @@ static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
 {
        va_list ap;
        va_start(ap, format);
-       _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->api, xreq->verb);
+       _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.api, xreq->request.verb);
        va_end(ap);
 }
 
@@ -407,8 +407,8 @@ static struct afb_hook_xreq_itf hook_xreq_default_itf = {
                if (hook->itf->hook_xreq_##what \
                 && (hook->flags & afb_hook_flag_req_##what) != 0 \
                 && (!hook->session || hook->session == xreq->context.session) \
-                && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
-                && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
+                && (!hook->api || !strcasecmp(hook->api, xreq->request.api)) \
+                && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb))) { \
                        hook->itf->hook_xreq_##what(hook->closure, &hookid, __VA_ARGS__); \
                } \
                hook = hook->next; \
@@ -576,8 +576,8 @@ void afb_hook_init_xreq(struct afb_xreq *xreq)
                f = hook->flags & afb_hook_flags_req_all;
                add = f != 0
                   && (!hook->session || hook->session == xreq->context.session)
-                  && (!hook->api || !strcasecmp(hook->api, xreq->api))
-                  && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
+                  && (!hook->api || !strcasecmp(hook->api, xreq->request.api))
+                  && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb));
                if (add)
                        flags |= f;
                hook = hook->next;
index db804eb..cdc9f23 100644 (file)
@@ -316,8 +316,8 @@ static void req_destroy(struct afb_xreq *xreq)
        }
        afb_context_disconnect(&hreq->xreq.context);
        json_object_put(hreq->json);
-       free((char*)hreq->xreq.api);
-       free((char*)hreq->xreq.verb);
+       free((char*)hreq->xreq.request.api);
+       free((char*)hreq->xreq.request.verb);
        afb_cred_unref(hreq->xreq.cred);
        free(hreq);
 }
@@ -914,9 +914,9 @@ static void req_success(struct afb_xreq *xreq, json_object *obj, const char *inf
 
 void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
 {
-       hreq->xreq.api = strndup(api, lenapi);
-       hreq->xreq.verb = strndup(verb, lenverb);
-       if (hreq->xreq.api == NULL || hreq->xreq.verb == NULL) {
+       hreq->xreq.request.api = strndup(api, lenapi);
+       hreq->xreq.request.verb = strndup(verb, lenverb);
+       if (hreq->xreq.request.api == NULL || hreq->xreq.request.verb == NULL) {
                ERROR("Out of memory");
                afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
        } else if (afb_hreq_init_context(hreq) < 0) {
index 44247dd..2b6a232 100644 (file)
@@ -259,7 +259,7 @@ static void client_call_cb(void * closure, struct afb_xreq *xreq)
 {
        struct afb_stub_ws *stubws = closure;
 
-       afb_proto_ws_client_call(stubws->proto, xreq->verb, afb_xreq_json(xreq), afb_session_uuid(xreq->context.session), xreq);
+       afb_proto_ws_client_call(stubws->proto, xreq->request.verb, afb_xreq_json(xreq), afb_session_uuid(xreq->context.session), xreq);
        afb_xreq_unhooked_addref(xreq);
 }
 
@@ -483,8 +483,8 @@ static void on_call(void *closure, struct afb_proto_ws_call *call, const char *v
 
        /* makes the call */
        wreq->xreq.cred = afb_cred_addref(stubws->cred);
-       wreq->xreq.api = stubws->apiname;
-       wreq->xreq.verb = verb;
+       wreq->xreq.request.api = stubws->apiname;
+       wreq->xreq.request.verb = verb;
        wreq->xreq.json = args;
        afb_xreq_process(&wreq->xreq, stubws->apiset);
        return;
index 4869d9a..fb8cc19 100644 (file)
@@ -283,8 +283,8 @@ static void hook_xreq(void *closure, const struct afb_hookid *hookid, const stru
        va_start(ap, format);
        emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
                                        "index", xreq->hookindex,
-                                       "api", xreq->api,
-                                       "verb", xreq->verb,
+                                       "api", xreq->request.api,
+                                       "verb", xreq->request.verb,
                                        "action", action,
                                        "credentials", cred,
                                        "session", session);
index 774349a..6215a68 100644 (file)
@@ -193,8 +193,8 @@ static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *ve
        afb_wsj1_msg_addref(msg);
        wsreq->msgj1 = msg;
        wsreq->xreq.cred = afb_cred_addref(ws->cred);
-       wsreq->xreq.api = api;
-       wsreq->xreq.verb = verb;
+       wsreq->xreq.request.api = api;
+       wsreq->xreq.request.verb = verb;
        wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
        wsreq->aws = afb_ws_json1_addref(ws);
        wsreq->xreq.listener = wsreq->aws->listener;
index cbed87f..5f6c745 100644 (file)
@@ -173,8 +173,8 @@ static struct subcall *subcall_alloc(
                afb_context_subinit(&subcall->xreq.context, &caller->context);
                subcall->xreq.cred = afb_cred_addref(caller->cred);
                subcall->xreq.json = args;
-               subcall->xreq.api = api;
-               subcall->xreq.verb = verb;
+               subcall->xreq.request.api = api;
+               subcall->xreq.request.verb = verb;
                subcall->xreq.caller = caller;
                afb_xreq_unhooked_addref(caller);
        }
@@ -233,7 +233,7 @@ static void subcall_process(struct subcall *subcall, void (*completion)(struct s
        subcall->completion = completion;
        if (subcall->xreq.caller->queryitf->subcall) {
                subcall->xreq.caller->queryitf->subcall(
-                       subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb,
+                       subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb,
                        subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
        } else {
                afb_xreq_unhooked_addref(&subcall->xreq);
@@ -570,7 +570,7 @@ static void xreq_vverbose_cb(struct afb_request *closure, int level, const char
        if (!fmt || vasprintf(&p, fmt, args) < 0)
                vverbose(level, file, line, func, fmt, args);
        else {
-               verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
+               verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p);
                free(p);
        }
 }
@@ -1059,12 +1059,12 @@ void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *query
 
 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
 {
-       afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
+       afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
 }
 
 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
 {
-       afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
+       afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
 }
 
 static void init_hooking(struct afb_xreq *xreq)
@@ -1128,12 +1128,12 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
 
        /* lookup at the api */
        xreq->apiset = apiset;
-       api = afb_apiset_lookup_started(apiset, xreq->api, 1);
+       api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
        if (!api) {
                if (errno == ENOENT)
-                       early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
+                       early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
                else
-                       early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
+                       early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
                goto end;
        }
        xreq->context.api_key = api;
@@ -1144,8 +1144,8 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
                while (caller) {
                        if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
                                /* noconcurrency lock detected */
-                               ERROR("self-lock detected in call stack for API %s", xreq->api);
-                               early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
+                               ERROR("self-lock detected in call stack for API %s", xreq->request.api);
+                               early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
                                goto end;
                        }
                        caller = caller->caller;
index d78d88f..6c11213 100644 (file)
@@ -60,8 +60,6 @@ struct afb_xreq
        struct afb_request request;     /**< exported request */
        struct afb_context context;     /**< context of the request */
        struct afb_apiset *apiset;      /**< apiset of the xreq */
-       const char *api;                /**< the requested API */
-       const char *verb;               /**< the requested VERB */
        struct json_object *json;       /**< the json object (or NULL) */
        const struct afb_xreq_query_itf *queryitf; /**< interface of xreq implmentation functions */
        int refcount;                   /**< current ref count */
index e2e8ce3..b5b0023 100644 (file)
@@ -504,8 +504,8 @@ static void startup_call_current(struct startup_req *sreq)
                        sreq->xreq.context.validated = 1;
                        sreq->api = strndup(api, verb - api);
                        sreq->verb = strndup(verb + 1, json - verb - 1);
-                       sreq->xreq.api = sreq->api;
-                       sreq->xreq.verb = sreq->verb;
+                       sreq->xreq.request.api = sreq->api;
+                       sreq->xreq.request.verb = sreq->verb;
                        sreq->xreq.json = json_tokener_parse(json + 1);
                        if (sreq->api && sreq->verb && sreq->xreq.json) {
                                afb_xreq_process(&sreq->xreq, main_apiset);