2 * Copyright (C) 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 #include <json-c/json.h>
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
29 #include "afb-context.h"
32 #include "afb-msg-json.h"
36 #include "afb-apiset.h"
41 /******************************************************************************/
43 static void xreq_finalize(struct afb_xreq *xreq)
46 afb_xreq_fail(xreq, "error", "no reply");
48 afb_hook_xreq_end(xreq);
50 afb_xreq_unhooked_unref(xreq->caller);
51 xreq->queryitf->unref(xreq);
54 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
56 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
59 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
61 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
65 /******************************************************************************/
67 static inline struct afb_req to_req(struct afb_xreq *xreq)
69 return (struct afb_req){ .itf = xreq->itf, .closure = xreq };
72 /******************************************************************************/
78 void (*completion)(struct subcall*, int, struct json_object*);
82 struct jobloop *jobloop;
83 struct json_object *result;
88 void (*callback)(void*, int, struct json_object*);
89 void (*callback2)(void*, int, struct json_object*, struct afb_req);
96 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
98 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
100 return afb_xreq_subscribe(subcall->xreq.caller, event);
103 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
105 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
107 return afb_xreq_unsubscribe(subcall->xreq.caller, event);
110 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
112 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
114 subcall->completion(subcall, status, result);
115 json_object_put(result);
116 afb_xreq_unhooked_unref(&subcall->xreq);
119 static void subcall_destroy_cb(struct afb_xreq *xreq)
121 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
123 json_object_put(subcall->xreq.json);
124 afb_cred_unref(subcall->xreq.cred);
128 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
129 .reply = subcall_reply_cb,
130 .unref = subcall_destroy_cb,
131 .subscribe = subcall_subscribe_cb,
132 .unsubscribe = subcall_unsubscribe_cb
135 static struct subcall *subcall_alloc(
136 struct afb_xreq *caller,
139 struct json_object *args
142 struct subcall *subcall;
143 size_t lenapi, lenverb;
146 lenapi = 1 + strlen(api);
147 lenverb = 1 + strlen(verb);
148 subcall = malloc(lenapi + lenverb + sizeof *subcall);
150 ERROR("out of memory");
152 copy = (char*)&subcall[1];
153 memcpy(copy, api, lenapi);
155 copy = ©[lenapi];
156 memcpy(copy, verb, lenverb);
159 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
160 afb_context_subinit(&subcall->xreq.context, &caller->context);
161 subcall->xreq.cred = afb_cred_addref(caller->cred);
162 subcall->xreq.json = args;
163 subcall->xreq.api = api;
164 subcall->xreq.verb = verb;
165 subcall->xreq.caller = caller;
166 afb_xreq_unhooked_addref(caller);
172 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
174 subcall->callback(subcall->closure, status, result);
177 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
179 subcall->callback2(subcall->closure, status, result, to_req(subcall->xreq.caller));
182 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
184 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
185 subcall_on_reply(subcall, status, result);
188 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
190 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
191 subcall_req_on_reply(subcall, status, result);
194 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
196 struct afb_xreq *xreq = closure;
199 ERROR("subcall replied more than one time!!");
200 json_object_put(result);
203 subcall_reply_cb(xreq, status, result);
207 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
209 subcall->completion = completion;
210 if (subcall->xreq.caller->queryitf->subcall) {
211 subcall->xreq.caller->queryitf->subcall(
212 subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb,
213 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
215 afb_xreq_unhooked_addref(&subcall->xreq);
216 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
220 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
222 subcall->callback = callback;
223 subcall->closure = cb_closure;
224 subcall_process(subcall, subcall_on_reply);
227 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
229 subcall->callback2 = callback;
230 subcall->closure = cb_closure;
231 subcall_process(subcall, subcall_req_on_reply);
234 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
236 subcall->callback = callback;
237 subcall->closure = cb_closure;
238 subcall_process(subcall, subcall_hooked_on_reply);
241 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
243 subcall->callback2 = callback;
244 subcall->closure = cb_closure;
245 subcall_process(subcall, subcall_req_hooked_on_reply);
248 static void subcall_sync_leave(struct subcall *subcall)
250 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
255 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
257 subcall->status = status;
258 subcall->result = json_object_get(result);
259 subcall_sync_leave(subcall);
262 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
264 struct subcall *subcall = closure;
267 subcall->jobloop = jobloop;
268 subcall->result = NULL;
270 subcall_process(subcall, subcall_sync_reply);
272 subcall->status = -1;
273 subcall_sync_leave(subcall);
277 static int subcallsync(struct subcall *subcall, struct json_object **result)
281 afb_xreq_unhooked_addref(&subcall->xreq);
282 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
283 *result = subcall->result;
284 if (rc < 0 || subcall->status < 0) {
285 *result = *result ?: afb_msg_json_internal_error();
288 afb_xreq_unhooked_unref(&subcall->xreq);
292 /******************************************************************************/
294 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
297 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
299 fun(first, second, info);
303 /******************************************************************************/
305 static struct json_object *xreq_json_cb(void *closure)
307 struct afb_xreq *xreq = closure;
308 if (!xreq->json && xreq->queryitf->json)
309 xreq->json = xreq->queryitf->json(xreq);
313 static struct afb_arg xreq_get_cb(void *closure, const char *name)
315 struct afb_xreq *xreq = closure;
317 struct json_object *object, *value;
319 if (xreq->queryitf->get)
320 arg = xreq->queryitf->get(xreq, name);
322 object = xreq_json_cb(closure);
323 if (json_object_object_get_ex(object, name, &value)) {
325 arg.value = json_object_get_string(value);
335 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
337 struct afb_xreq *xreq = closure;
340 ERROR("reply called more than one time!!");
341 json_object_put(obj);
344 if (xreq->queryitf->success)
345 xreq->queryitf->success(xreq, obj, info);
347 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
351 static void xreq_fail_cb(void *closure, const char *status, const char *info)
353 struct afb_xreq *xreq = closure;
356 ERROR("reply called more than one time!!");
359 if (xreq->queryitf->fail)
360 xreq->queryitf->fail(xreq, status, info);
362 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
366 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
368 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
371 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
373 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
376 static void *xreq_context_get_cb(void *closure)
378 struct afb_xreq *xreq = closure;
379 return afb_context_get(&xreq->context);
382 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
384 struct afb_xreq *xreq = closure;
385 afb_context_set(&xreq->context, value, free_value);
388 static void xreq_addref_cb(void *closure)
390 struct afb_xreq *xreq = closure;
391 afb_xreq_unhooked_addref(xreq);
394 static void xreq_unref_cb(void *closure)
396 struct afb_xreq *xreq = closure;
397 afb_xreq_unhooked_unref(xreq);
400 static void xreq_session_close_cb(void *closure)
402 struct afb_xreq *xreq = closure;
403 afb_context_close(&xreq->context);
406 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
408 struct afb_xreq *xreq = closure;
409 return afb_context_change_loa(&xreq->context, level);
412 static int xreq_subscribe_cb(void *closure, struct afb_event event)
414 struct afb_xreq *xreq = closure;
415 return afb_xreq_subscribe(xreq, event);
418 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
421 return afb_evt_add_watch(xreq->listener, event);
422 if (xreq->queryitf->subscribe)
423 return xreq->queryitf->subscribe(xreq, event);
424 ERROR("no event listener, subscription impossible");
429 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
431 struct afb_xreq *xreq = closure;
432 return afb_xreq_unsubscribe(xreq, event);
435 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
438 return afb_evt_remove_watch(xreq->listener, event);
439 if (xreq->queryitf->unsubscribe)
440 return xreq->queryitf->unsubscribe(xreq, event);
441 ERROR("no event listener, unsubscription impossible");
446 static void xreq_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
448 struct afb_xreq *xreq = closure;
451 sc = subcall_alloc(xreq, api, verb, args);
454 callback(cb_closure, 1, afb_msg_json_internal_error());
455 json_object_put(args);
457 subcall(sc, callback, cb_closure);
461 static void xreq_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
463 struct afb_xreq *xreq = closure;
466 sc = subcall_alloc(xreq, api, verb, args);
469 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
470 json_object_put(args);
472 subcall_req(sc, callback, cb_closure);
477 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
481 struct afb_xreq *xreq = closure;
482 struct json_object *resu;
484 sc = subcall_alloc(xreq, api, verb, args);
487 resu = afb_msg_json_internal_error();
488 json_object_put(args);
490 rc = subcallsync(sc, &resu);
495 json_object_put(resu);
499 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
502 struct afb_xreq *xreq = closure;
504 if (!fmt || vasprintf(&p, fmt, args) < 0)
505 vverbose(level, file, line, func, fmt, args);
507 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
512 static struct afb_stored_req *xreq_store_cb(void *closure)
514 xreq_addref_cb(closure);
518 static int xreq_has_permission_cb(void*closure, const char *permission)
520 struct afb_xreq *xreq = closure;
521 return afb_auth_has_permission(xreq, permission);
524 static char *xreq_get_application_id_cb(void*closure)
526 struct afb_xreq *xreq = closure;
527 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
530 static void *xreq_context_make_cb(void *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
532 struct afb_xreq *xreq = closure;
533 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
536 /******************************************************************************/
538 static struct json_object *xreq_hooked_json_cb(void *closure)
540 struct json_object *r = xreq_json_cb(closure);
541 struct afb_xreq *xreq = closure;
542 return afb_hook_xreq_json(xreq, r);
545 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
547 struct afb_arg r = xreq_get_cb(closure, name);
548 struct afb_xreq *xreq = closure;
549 return afb_hook_xreq_get(xreq, name, r);
552 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
554 struct afb_xreq *xreq = closure;
555 afb_hook_xreq_success(xreq, obj, info);
556 xreq_success_cb(closure, obj, info);
559 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
561 struct afb_xreq *xreq = closure;
562 afb_hook_xreq_fail(xreq, status, info);
563 xreq_fail_cb(closure, status, info);
566 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
568 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
571 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
573 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
576 static void *xreq_hooked_context_get_cb(void *closure)
578 void *r = xreq_context_get_cb(closure);
579 struct afb_xreq *xreq = closure;
580 return afb_hook_xreq_context_get(xreq, r);
583 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
585 struct afb_xreq *xreq = closure;
586 afb_hook_xreq_context_set(xreq, value, free_value);
587 xreq_context_set_cb(closure, value, free_value);
590 static void xreq_hooked_addref_cb(void *closure)
592 struct afb_xreq *xreq = closure;
593 afb_hook_xreq_addref(xreq);
594 xreq_addref_cb(closure);
597 static void xreq_hooked_unref_cb(void *closure)
599 struct afb_xreq *xreq = closure;
600 afb_hook_xreq_unref(xreq);
601 xreq_unref_cb(closure);
604 static void xreq_hooked_session_close_cb(void *closure)
606 struct afb_xreq *xreq = closure;
607 afb_hook_xreq_session_close(xreq);
608 xreq_session_close_cb(closure);
611 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
613 int r = xreq_session_set_LOA_cb(closure, level);
614 struct afb_xreq *xreq = closure;
615 return afb_hook_xreq_session_set_LOA(xreq, level, r);
618 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
620 int r = xreq_subscribe_cb(closure, event);
621 struct afb_xreq *xreq = closure;
622 return afb_hook_xreq_subscribe(xreq, event, r);
625 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
627 int r = xreq_unsubscribe_cb(closure, event);
628 struct afb_xreq *xreq = closure;
629 return afb_hook_xreq_unsubscribe(xreq, event, r);
632 static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
634 struct afb_xreq *xreq = closure;
637 afb_hook_xreq_subcall(xreq, api, verb, args);
638 sc = subcall_alloc(xreq, api, verb, args);
641 callback(cb_closure, 1, afb_msg_json_internal_error());
642 json_object_put(args);
644 subcall_hooked(sc, callback, cb_closure);
648 static void xreq_hooked_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
650 struct afb_xreq *xreq = closure;
653 afb_hook_xreq_subcall_req(xreq, api, verb, args);
654 sc = subcall_alloc(xreq, api, verb, args);
657 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
658 json_object_put(args);
660 subcall_req_hooked(sc, callback, cb_closure);
664 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
667 struct afb_xreq *xreq = closure;
668 afb_hook_xreq_subcallsync(xreq, api, verb, args);
669 r = xreq_subcallsync_cb(closure, api, verb, args, result);
670 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
673 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
675 struct afb_xreq *xreq = closure;
678 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
679 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
683 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
685 struct afb_xreq *xreq = closure;
686 struct afb_stored_req *r = xreq_store_cb(closure);
687 afb_hook_xreq_store(xreq, r);
691 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
693 struct afb_xreq *xreq = closure;
694 int r = xreq_has_permission_cb(closure, permission);
695 return afb_hook_xreq_has_permission(xreq, permission, r);
698 static char *xreq_hooked_get_application_id_cb(void*closure)
700 struct afb_xreq *xreq = closure;
701 char *r = xreq_get_application_id_cb(closure);
702 return afb_hook_xreq_get_application_id(xreq, r);
705 static void *xreq_hooked_context_make_cb(void *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
707 struct afb_xreq *xreq = closure;
708 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
709 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
712 /******************************************************************************/
714 const struct afb_req_itf xreq_itf = {
715 .json = xreq_json_cb,
717 .success = xreq_success_cb,
718 .fail = xreq_fail_cb,
719 .vsuccess = xreq_vsuccess_cb,
720 .vfail = xreq_vfail_cb,
721 .context_get = xreq_context_get_cb,
722 .context_set = xreq_context_set_cb,
723 .addref = xreq_addref_cb,
724 .unref = xreq_unref_cb,
725 .session_close = xreq_session_close_cb,
726 .session_set_LOA = xreq_session_set_LOA_cb,
727 .subscribe = xreq_subscribe_cb,
728 .unsubscribe = xreq_unsubscribe_cb,
729 .subcall = xreq_subcall_cb,
730 .subcallsync = xreq_subcallsync_cb,
731 .vverbose = xreq_vverbose_cb,
732 .store = xreq_store_cb,
733 .subcall_req = xreq_subcall_req_cb,
734 .has_permission = xreq_has_permission_cb,
735 .get_application_id = xreq_get_application_id_cb,
736 .context_make = xreq_context_make_cb
739 const struct afb_req_itf xreq_hooked_itf = {
740 .json = xreq_hooked_json_cb,
741 .get = xreq_hooked_get_cb,
742 .success = xreq_hooked_success_cb,
743 .fail = xreq_hooked_fail_cb,
744 .vsuccess = xreq_hooked_vsuccess_cb,
745 .vfail = xreq_hooked_vfail_cb,
746 .context_get = xreq_hooked_context_get_cb,
747 .context_set = xreq_hooked_context_set_cb,
748 .addref = xreq_hooked_addref_cb,
749 .unref = xreq_hooked_unref_cb,
750 .session_close = xreq_hooked_session_close_cb,
751 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
752 .subscribe = xreq_hooked_subscribe_cb,
753 .unsubscribe = xreq_hooked_unsubscribe_cb,
754 .subcall = xreq_hooked_subcall_cb,
755 .subcallsync = xreq_hooked_subcallsync_cb,
756 .vverbose = xreq_hooked_vverbose_cb,
757 .store = xreq_hooked_store_cb,
758 .subcall_req = xreq_hooked_subcall_req_cb,
759 .has_permission = xreq_hooked_has_permission_cb,
760 .get_application_id = xreq_hooked_get_application_id_cb,
761 .context_make = xreq_hooked_context_make_cb
764 /******************************************************************************/
766 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
768 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
770 afb_hook_xreq_unstore(xreq);
774 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
776 return afb_req_json(to_req(xreq));
779 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
781 afb_req_success(to_req(xreq), obj, info);
784 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
788 va_start(args, info);
789 afb_req_success_v(to_req(xreq), obj, info, args);
793 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
795 afb_req_fail(to_req(xreq), status, info);
798 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
802 va_start(args, info);
803 afb_req_fail_v(to_req(xreq), status, info, args);
808 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
810 struct json_object *obj = xreq_json_cb(xreq);
811 const char *result = json_object_to_json_string(obj);
813 *size = strlen(result);
817 void afb_xreq_addref(struct afb_xreq *xreq)
819 afb_req_addref(to_req(xreq));
822 void afb_xreq_unref(struct afb_xreq *xreq)
824 afb_req_unref(to_req(xreq));
827 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
829 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
832 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
834 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
837 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
839 return xreq_subcallsync_cb(xreq, api, verb, args, result);
842 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
844 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
847 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
851 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
852 if (!afb_context_check(&xreq->context)) {
853 afb_context_close(&xreq->context);
854 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
860 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
861 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
862 if (!afb_context_check_loa(&xreq->context, loa)) {
863 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
869 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
870 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
871 if (afb_context_check_loa(&xreq->context, loa + 1)) {
872 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
878 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
879 afb_context_refresh(&xreq->context);
881 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
882 afb_context_change_loa(&xreq->context, 0);
883 afb_context_close(&xreq->context);
889 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
893 if (sessionflags != 0) {
894 if (!afb_context_check(&xreq->context)) {
895 afb_context_close(&xreq->context);
896 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
902 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
903 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
904 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
909 if (auth && !afb_auth_check(xreq, auth)) {
910 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
915 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
916 afb_context_refresh(&xreq->context);
918 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
919 afb_context_close(&xreq->context);
925 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
928 afb_xreq_fail_unknown_verb(xreq);
930 if (!xreq_session_check_apply_v1(xreq, verb->session))
931 verb->callback(to_req(xreq));
934 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
937 afb_xreq_fail_unknown_verb(xreq);
939 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
940 verb->callback(to_req(xreq));
943 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
945 memset(xreq, 0, sizeof *xreq);
946 xreq->itf = &xreq_hooked_itf; /* hook by default */
948 xreq->queryitf = queryitf;
951 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
953 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
956 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
958 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
961 static void init_hooking(struct afb_xreq *xreq)
963 afb_hook_init_xreq(xreq);
965 afb_hook_xreq_begin(xreq);
967 xreq->itf = &xreq_itf; /* unhook the interface */
971 * job callback for asynchronous and secured processing of the request.
973 static void process_async(int signum, void *arg)
975 struct afb_xreq *xreq = arg;
976 const struct afb_api *api;
979 /* emit the error (assumes that hooking is initialised) */
980 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
984 /* invoke api call method to process the reqiest */
985 api = (const struct afb_api*)xreq->context.api_key;
986 api->itf->call(api->closure, xreq);
988 /* release the request */
989 afb_xreq_unhooked_unref(xreq);
993 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
994 * The early failure occurs only in function 'afb_xreq_process' where normally,
995 * the hooking is not initialised. So this "early" failure takes care to initialise
996 * the hooking in first.
998 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1006 va_start(args, info);
1007 afb_req_fail_v(to_req(xreq), status, info, args);
1012 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1013 * Errors are reported as request failures.
1015 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1017 const struct afb_api *api;
1018 struct afb_xreq *caller;
1020 /* lookup at the api */
1021 xreq->apiset = apiset;
1022 api = afb_apiset_lookup_started(apiset, xreq->api, 1);
1024 if (errno == ENOENT)
1025 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
1027 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
1030 xreq->context.api_key = api;
1032 /* check self locking */
1034 caller = xreq->caller;
1036 if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1037 /* noconcurrency lock detected */
1038 ERROR("self-lock detected in call stack for API %s", xreq->api);
1039 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
1042 caller = caller->caller;
1046 /* queue the request job */
1047 afb_xreq_unhooked_addref(xreq);
1048 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1049 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1050 ERROR("can't process job with threads: %m");
1051 early_failure(xreq, "cancelled", "not able to create a job for the task");
1052 afb_xreq_unhooked_unref(xreq);
1055 afb_xreq_unhooked_unref(xreq);