diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-04-04 10:10:13 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-04-04 10:10:13 +0200 |
commit | 0b838a6101edc810098ee0645d3bfd966096ae07 (patch) | |
tree | 284d0861a2f956e6d22c6839fdc913c41ebd11a5 | |
parent | 5e7d680c173ddbb26db4e49f7738f82e89299362 (diff) |
Definitive switch to internal's xreq
This switch allows to work on a common base for
the requests.
Change-Id: I94e7fdda80c1966af399309334c41c7c8c8259dc
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/afb-api-dbus.c | 6 | ||||
-rw-r--r-- | src/afb-api-so-v1.c | 64 | ||||
-rw-r--r-- | src/afb-api-so-v2.c | 64 | ||||
-rw-r--r-- | src/afb-api-ws.c | 6 | ||||
-rw-r--r-- | src/afb-apis.c | 29 | ||||
-rw-r--r-- | src/afb-apis.h | 6 | ||||
-rw-r--r-- | src/afb-hswitch.c | 2 | ||||
-rw-r--r-- | src/afb-subcall.c | 2 | ||||
-rw-r--r-- | src/afb-svc.c | 2 | ||||
-rw-r--r-- | src/afb-ws-json1.c | 2 |
11 files changed, 16 insertions, 168 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 919e0d59..1f465509 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,7 +73,6 @@ ADD_LIBRARY(afb-lib STATIC afb-session.c afb-svc.c afb-subcall.c - afb-thread.c afb-websock.c afb-ws-client.c afb-ws-json1.c diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index 09af983f..dc5273a8 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -311,7 +311,7 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_ } /* on call, propagate it to the dbus service */ -static void api_dbus_client_xcall(void *closure, struct afb_xreq *xreq) +static void api_dbus_client_call(void *closure, struct afb_xreq *xreq) { struct api_dbus *api = closure; size_t size; @@ -614,7 +614,7 @@ int afb_api_dbus_add_client(const char *path) /* record it as an API */ afb_api.closure = api; - afb_api.xcall = api_dbus_client_xcall; + afb_api.call = api_dbus_client_call; afb_api.service_start = api_dbus_service_start; if (afb_apis_add(api->api, afb_api) < 0) goto error2; @@ -963,7 +963,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd dreq->xreq.queryitf = &afb_api_dbus_xreq_itf; dreq->xreq.api = api->api; dreq->xreq.verb = method; - afb_apis_xcall(&dreq->xreq); + afb_apis_call(&dreq->xreq); afb_xreq_unref(&dreq->xreq); return 1; diff --git a/src/afb-api-so-v1.c b/src/afb-api-so-v1.c index a09b0f51..63ea3f8a 100644 --- a/src/afb-api-so-v1.c +++ b/src/afb-api-so-v1.c @@ -30,7 +30,6 @@ #include "afb-common.h" #include "afb-context.h" #include "afb-api-so.h" -#include "afb-thread.h" #include "afb-xreq.h" #include "verbose.h" @@ -127,53 +126,6 @@ static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, i return afb_common_rootdir_open_locale(filename, flags, locale); } -static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb) -{ - int stag = (int)verb->session; - - if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) { - if (!afb_context_check(context)) { - afb_context_close(context); - afb_req_fail(req, "failed", "invalid token's identity"); - return 0; - } - } - - if ((stag & AFB_SESSION_CREATE) != 0) { - if (afb_context_check_loa(context, 1)) { - afb_req_fail(req, "failed", "invalid creation state"); - return 0; - } - afb_context_change_loa(context, 1); - afb_context_refresh(context); - } - - if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0) - afb_context_refresh(context); - - if ((stag & AFB_SESSION_CLOSE) != 0) { - afb_context_change_loa(context, 0); - afb_context_close(context); - } - - if ((stag & AFB_SESSION_LOA_GE) != 0) { - int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK; - if (!afb_context_check_loa(context, loa)) { - afb_req_fail(req, "failed", "invalid LOA"); - return 0; - } - } - - if ((stag & AFB_SESSION_LOA_LE) != 0) { - int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK; - if (afb_context_check_loa(context, loa + 1)) { - afb_req_fail(req, "failed", "invalid LOA"); - return 0; - } - } - return 1; -} - static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name) { const struct afb_verb_desc_v1 *verb; @@ -184,20 +136,7 @@ static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char return verb->name ? verb : NULL; } -static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb) -{ - const struct afb_verb_desc_v1 *verb; - struct api_so_v1 *desc = closure; - - verb = search(desc, strverb); - if (!verb) - afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", strverb, desc->binding->v1.prefix); - else if (call_check(req, context, verb)) { - afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc); - } -} - -static void xcall_cb(void *closure, struct afb_xreq *xreq) +static void call_cb(void *closure, struct afb_xreq *xreq) { const struct afb_verb_desc_v1 *verb; struct api_so_v1 *desc = closure; @@ -326,7 +265,6 @@ int afb_api_so_v1_add(const char *path, void *handle) if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){ .closure = desc, .call = call_cb, - .xcall = xcall_cb, .service_start = service_start_cb }) < 0) { ERROR("binding [%s] can't be registered...", path); goto error2; diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c index 2770f64a..401aa5cd 100644 --- a/src/afb-api-so-v2.c +++ b/src/afb-api-so-v2.c @@ -30,7 +30,6 @@ #include "afb-common.h" #include "afb-context.h" #include "afb-api-so.h" -#include "afb-thread.h" #include "afb-xreq.h" #include "verbose.h" @@ -125,53 +124,6 @@ static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, i return afb_common_rootdir_open_locale(filename, flags, locale); } -static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_v2 *verb) -{ - int stag = (int)verb->session; - - if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) { - if (!afb_context_check(context)) { - afb_context_close(context); - afb_req_fail(req, "failed", "invalid token's identity"); - return 0; - } - } - - if ((stag & AFB_SESSION_CREATE) != 0) { - if (afb_context_check_loa(context, 1)) { - afb_req_fail(req, "failed", "invalid creation state"); - return 0; - } - afb_context_change_loa(context, 1); - afb_context_refresh(context); - } - - if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0) - afb_context_refresh(context); - - if ((stag & AFB_SESSION_CLOSE) != 0) { - afb_context_change_loa(context, 0); - afb_context_close(context); - } - - if ((stag & AFB_SESSION_LOA_GE) != 0) { - int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK; - if (!afb_context_check_loa(context, loa)) { - afb_req_fail(req, "failed", "invalid LOA"); - return 0; - } - } - - if ((stag & AFB_SESSION_LOA_LE) != 0) { - int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK; - if (afb_context_check_loa(context, loa + 1)) { - afb_req_fail(req, "failed", "invalid LOA"); - return 0; - } - } - return 1; -} - static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb) { const struct afb_verb_v2 *result; @@ -182,20 +134,7 @@ static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb return result->verb ? result : NULL; } -static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *name) -{ - struct api_so_v2 *desc = closure; - const struct afb_verb_v2 *verb; - - verb = search(desc, name); - if (!verb) - afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", name, desc->binding->api); - else if (call_check(req, context, verb)) { - afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc); - } -} - -static void xcall_cb(void *closure, struct afb_xreq *xreq) +static void call_cb(void *closure, struct afb_xreq *xreq) { struct api_so_v2 *desc = closure; const struct afb_verb_v2 *verb; @@ -317,7 +256,6 @@ int afb_api_so_v2_add(const char *path, void *handle) if (afb_apis_add(binding->api, (struct afb_api){ .closure = desc, .call = call_cb, - .xcall = xcall_cb, .service_start = service_start_cb }) < 0) { ERROR("binding [%s] can't be registered...", path); goto error2; diff --git a/src/afb-api-ws.c b/src/afb-api-ws.c index 82671875..5c3dcd47 100644 --- a/src/afb-api-ws.c +++ b/src/afb-api-ws.c @@ -765,7 +765,7 @@ static void api_ws_client_on_binary(void *closure, char *data, size_t size) } /* on call, propagate it to the ws service */ -static void api_ws_client_xcall_cb(void * closure, struct afb_xreq *xreq) +static void api_ws_client_call_cb(void * closure, struct afb_xreq *xreq) { int rc; struct api_ws_memo *memo; @@ -877,7 +877,7 @@ int afb_api_ws_add_client(const char *path) /* record it as an API */ afb_api.closure = api; - afb_api.xcall = api_ws_client_xcall_cb; + afb_api.call = api_ws_client_call_cb; afb_api.service_start = api_ws_service_start_cb; if (afb_apis_add(api->api, afb_api) < 0) goto error3; @@ -944,7 +944,7 @@ static void api_ws_server_called(struct api_ws_client *client, struct readbuf *r wreq->xreq.verb = verb; wreq->xreq.query = wreq; wreq->xreq.queryitf = &afb_api_ws_xreq_itf; - afb_apis_xcall(&wreq->xreq); + afb_apis_call(&wreq->xreq); afb_xreq_unref(&wreq->xreq); return; diff --git a/src/afb-apis.c b/src/afb-apis.c index cce21e03..de122b6b 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -181,31 +181,6 @@ static const struct api_desc *search(const char *api) } /** - * Dispatch the request 'req' with the 'context' to the - * method of 'api' and 'verb'. - * @param req the request to dispatch - * @param context the context of the request - * @param api the api of the verb - * @param verb the verb within the api - */ -void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb) -{ - const struct api_desc *a; - - /* init hooking the request */ - req = afb_hook_req_call(req, context, api, verb); - - /* search the api */ - a = search(api); - if (!a) - afb_req_fail(req, "fail", "api not found"); - else { - context->api_key = a->api.closure; - a->api.call(a->api.closure, req, context, verb); - } -} - -/** * Starts a service by its 'api' name. * @param api name of the service to start * @param share_session if true start the servic"e in a shared session @@ -253,7 +228,7 @@ int afb_apis_start_all_services(int share_session) * @param api the api of the verb * @param verb the verb within the api */ -void afb_apis_xcall(struct afb_xreq *xreq) +void afb_apis_call(struct afb_xreq *xreq) { const struct api_desc *a; @@ -266,7 +241,7 @@ void afb_apis_xcall(struct afb_xreq *xreq) afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api); else { xreq->context.api_key = a->api.closure; - a->api.xcall(a->api.closure, xreq); + a->api.call(a->api.closure, xreq); } } diff --git a/src/afb-apis.h b/src/afb-apis.h index f211bda8..540b644b 100644 --- a/src/afb-apis.h +++ b/src/afb-apis.h @@ -24,9 +24,8 @@ struct afb_xreq; struct afb_api { void *closure; - void (*call)(void *closure, struct afb_req req, struct afb_context *context, const char *verb); + void (*call)(void *closure, struct afb_xreq *xreq); int (*service_start)(void *closure, int share_session, int onneed); - void (*xcall)(void *closure, struct afb_xreq *xreq); }; @@ -37,7 +36,6 @@ extern int afb_apis_add(const char *name, struct afb_api api); extern int afb_apis_start_all_services(int share_session); extern int afb_apis_start_service(const char *name, int share_session, int onneed); -extern void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb); -extern void afb_apis_xcall(struct afb_xreq *xreq); +extern void afb_apis_call(struct afb_xreq *xreq); diff --git a/src/afb-hswitch.c b/src/afb-hswitch.c index eab5b9b7..0eae18f7 100644 --- a/src/afb-hswitch.c +++ b/src/afb-hswitch.c @@ -47,7 +47,7 @@ int afb_hswitch_apis(struct afb_hreq *hreq, void *data) if (afb_hreq_init_req_call(hreq, api, lenapi, verb, lenverb) < 0) afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); else - afb_apis_xcall(&hreq->xreq); + afb_apis_call(&hreq->xreq); return 1; } diff --git a/src/afb-subcall.c b/src/afb-subcall.c index 82128d8a..81361a08 100644 --- a/src/afb-subcall.c +++ b/src/afb-subcall.c @@ -134,7 +134,7 @@ void afb_subcall( } afb_xreq_addref(caller); - afb_apis_xcall(&subcall->xreq); + afb_apis_call(&subcall->xreq); afb_xreq_unref(&subcall->xreq); } diff --git a/src/afb-svc.c b/src/afb-svc.c index 58e2b6d3..93e85533 100644 --- a/src/afb-svc.c +++ b/src/afb-svc.c @@ -246,7 +246,7 @@ static void svc_call(void *closure, const char *api, const char *verb, struct js svcreq->svc = svc; /* terminates and frees ressources if needed */ - afb_apis_xcall(&svcreq->xreq); + afb_apis_call(&svcreq->xreq); afb_xreq_unref(&svcreq->xreq); } diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c index fdd3baed..29078e26 100644 --- a/src/afb-ws-json1.c +++ b/src/afb-ws-json1.c @@ -198,7 +198,7 @@ static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *ve wsreq->xreq.listener = wsreq->aws->listener; /* emits the call */ - afb_apis_xcall(&wsreq->xreq); + afb_apis_call(&wsreq->xreq); afb_xreq_unref(&wsreq->xreq); } |