From 7a7268a5697b0b988bcabc00390878edee941b4e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Fri, 22 Sep 2017 16:24:24 +0200 Subject: [PATCH] Provide API and VERB name of requests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I9a86c6314f871334231e50f9cea60b54aed434b9 Signed-off-by: José Bollo --- include/afb/afb-request-itf.h | 8 +++++++- src/afb-api-dbus.c | 6 +++--- src/afb-api-dyn.c | 2 +- src/afb-api-so-v1.c | 3 ++- src/afb-api-so-v2.c | 3 ++- src/afb-export.c | 4 ++-- src/afb-hook.c | 10 +++++----- src/afb-hreq.c | 10 +++++----- src/afb-stub-ws.c | 6 +++--- src/afb-trace.c | 4 ++-- src/afb-ws-json1.c | 4 ++-- src/afb-xreq.c | 22 +++++++++++----------- src/afb-xreq.h | 2 -- src/main.c | 4 ++-- 14 files changed, 47 insertions(+), 41 deletions(-) diff --git a/include/afb/afb-request-itf.h b/include/afb/afb-request-itf.h index eb7c5347..e542442c 100644 --- a/include/afb/afb-request-itf.h +++ b/include/afb/afb-request-itf.h @@ -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; }; /* diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index 1aad8635..806d7435 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -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; diff --git a/src/afb-api-dyn.c b/src/afb-api-dyn.c index e829f8be..571f7fac 100644 --- a/src/afb-api-dyn.c +++ b/src/afb-api-dyn.c @@ -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 */ diff --git a/src/afb-api-so-v1.c b/src/afb-api-so-v1.c index d7429efe..9a9e47a4 100644 --- a/src/afb-api-so-v1.c +++ b/src/afb-api-so-v1.c @@ -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); } diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c index edc31d53..ba53223f 100644 --- a/src/afb-api-so-v2.c +++ b/src/afb-api-so-v2.c @@ -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); } diff --git a/src/afb-export.c b/src/afb-export.c index ce48dbe0..7fd475ab 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -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 = ©[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; diff --git a/src/afb-hook.c b/src/afb-hook.c index 38ecfdcc..123a6416 100644 --- a/src/afb-hook.c +++ b/src/afb-hook.c @@ -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; diff --git a/src/afb-hreq.c b/src/afb-hreq.c index db804eb2..cdc9f239 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -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) { diff --git a/src/afb-stub-ws.c b/src/afb-stub-ws.c index 44247dd3..2b6a2328 100644 --- a/src/afb-stub-ws.c +++ b/src/afb-stub-ws.c @@ -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; diff --git a/src/afb-trace.c b/src/afb-trace.c index 4869d9a8..fb8cc19b 100644 --- a/src/afb-trace.c +++ b/src/afb-trace.c @@ -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); diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c index 774349ac..6215a684 100644 --- a/src/afb-ws-json1.c +++ b/src/afb-ws-json1.c @@ -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; diff --git a/src/afb-xreq.c b/src/afb-xreq.c index cbed87f3..5f6c7453 100644 --- a/src/afb-xreq.c +++ b/src/afb-xreq.c @@ -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; diff --git a/src/afb-xreq.h b/src/afb-xreq.h index d78d88fb..6c112130 100644 --- a/src/afb-xreq.h +++ b/src/afb-xreq.h @@ -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 */ diff --git a/src/main.c b/src/main.c index e2e8ce3e..b5b00234 100644 --- a/src/main.c +++ b/src/main.c @@ -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); -- 2.16.6