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.
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
28 #include <afb/afb-binding-v2.h>
30 #include "afb-context.h"
33 #include "afb-msg-json.h"
37 #include "afb-apiset.h"
42 /******************************************************************************/
44 static inline void xreq_addref(struct afb_xreq *xreq)
46 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
49 static inline void xreq_unref(struct afb_xreq *xreq)
51 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
53 afb_xreq_fail(xreq, "error", "no reply");
55 afb_hook_xreq_end(xreq);
56 xreq->queryitf->unref(xreq);
60 /******************************************************************************/
62 extern const struct afb_req_itf xreq_itf;
63 extern const struct afb_req_itf xreq_hooked_itf;
65 static inline struct afb_req to_req(struct afb_xreq *xreq)
67 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
70 /******************************************************************************/
75 struct afb_xreq *caller;
76 void (*callback)(void*, int, struct json_object*);
80 struct jobloop *jobloop;
81 struct json_object *result;
86 void (*callback)(void*, int, struct json_object*);
87 void (*callback2)(void*, int, struct json_object*, struct afb_req);
94 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
96 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
98 return afb_xreq_subscribe(subcall->caller, event);
101 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
103 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
105 return afb_xreq_unsubscribe(subcall->caller, event);
108 static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
110 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
112 if (subcall->callback)
113 subcall->callback(subcall->closure, status, obj);
114 json_object_put(obj);
117 static void subcall_destroy(struct afb_xreq *xreq)
119 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
121 json_object_put(subcall->xreq.json);
122 afb_cred_unref(subcall->xreq.cred);
123 xreq_unref(subcall->caller);
127 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
128 .reply = subcall_reply,
129 .unref = subcall_destroy,
130 .subscribe = subcall_subscribe,
131 .unsubscribe = subcall_unsubscribe
134 static struct subcall *subcall_alloc(
135 struct afb_xreq *caller,
138 struct json_object *args
141 struct subcall *subcall;
142 size_t lenapi, lenverb;
145 lenapi = 1 + strlen(api);
146 lenverb = 1 + strlen(verb);
147 subcall = malloc(lenapi + lenverb + sizeof *subcall);
149 ERROR("out of memory");
151 copy = (char*)&subcall[1];
152 memcpy(copy, api, lenapi);
154 copy = ©[lenapi];
155 memcpy(copy, verb, lenverb);
158 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
159 afb_context_subinit(&subcall->xreq.context, &caller->context);
160 subcall->xreq.cred = afb_cred_addref(caller->cred);
161 subcall->xreq.json = args;
162 subcall->xreq.api = api;
163 subcall->xreq.verb = verb;
164 subcall->caller = caller;
170 static void subcall_process(struct subcall *subcall)
172 if (subcall->caller->queryitf->subcall) {
173 subcall->caller->queryitf->subcall(
174 subcall->caller, subcall->xreq.api, subcall->xreq.verb,
175 subcall->xreq.json, subcall->callback, subcall->closure);
176 xreq_unref(&subcall->xreq);
178 afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
181 static void subcall_sync_leave(struct subcall *subcall)
183 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
188 static void subcall_sync_reply(void *closure, int status, struct json_object *obj)
190 struct subcall *subcall = closure;
192 subcall->status = status;
193 subcall->result = json_object_get(obj);
194 subcall_sync_leave(subcall);
197 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
199 struct subcall *subcall = closure;
202 subcall->jobloop = jobloop;
203 subcall_process(subcall);
205 subcall->status = -1;
206 subcall_sync_leave(subcall);
210 /******************************************************************************/
212 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
215 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
217 fun(first, second, info);
221 /******************************************************************************/
223 static struct json_object *xreq_json_cb(void *closure)
225 struct afb_xreq *xreq = closure;
226 if (!xreq->json && xreq->queryitf->json)
227 xreq->json = xreq->queryitf->json(xreq);
231 static struct afb_arg xreq_get_cb(void *closure, const char *name)
233 struct afb_xreq *xreq = closure;
235 struct json_object *object, *value;
237 if (xreq->queryitf->get)
238 arg = xreq->queryitf->get(xreq, name);
240 object = xreq_json_cb(closure);
241 if (json_object_object_get_ex(object, name, &value)) {
243 arg.value = json_object_get_string(value);
253 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
255 struct afb_xreq *xreq = closure;
258 ERROR("reply called more than one time!!");
259 json_object_put(obj);
262 if (xreq->queryitf->success)
263 xreq->queryitf->success(xreq, obj, info);
265 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
269 static void xreq_fail_cb(void *closure, const char *status, const char *info)
271 struct afb_xreq *xreq = closure;
274 ERROR("reply called more than one time!!");
277 if (xreq->queryitf->fail)
278 xreq->queryitf->fail(xreq, status, info);
280 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
284 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
286 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
289 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
291 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
294 static void *xreq_context_get_cb(void *closure)
296 struct afb_xreq *xreq = closure;
297 return afb_context_get(&xreq->context);
300 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
302 struct afb_xreq *xreq = closure;
303 afb_context_set(&xreq->context, value, free_value);
306 static void xreq_addref_cb(void *closure)
308 struct afb_xreq *xreq = closure;
312 static void xreq_unref_cb(void *closure)
314 struct afb_xreq *xreq = closure;
318 static void xreq_session_close_cb(void *closure)
320 struct afb_xreq *xreq = closure;
321 afb_context_close(&xreq->context);
324 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
326 struct afb_xreq *xreq = closure;
327 return afb_context_change_loa(&xreq->context, level);
330 static int xreq_subscribe_cb(void *closure, struct afb_event event)
332 struct afb_xreq *xreq = closure;
333 return afb_xreq_subscribe(xreq, event);
336 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
339 return afb_evt_add_watch(xreq->listener, event);
340 if (xreq->queryitf->subscribe)
341 return xreq->queryitf->subscribe(xreq, event);
342 ERROR("no event listener, subscription impossible");
347 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
349 struct afb_xreq *xreq = closure;
350 return afb_xreq_unsubscribe(xreq, event);
353 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
356 return afb_evt_remove_watch(xreq->listener, event);
357 if (xreq->queryitf->unsubscribe)
358 return xreq->queryitf->unsubscribe(xreq, event);
359 ERROR("no event listener, unsubscription impossible");
364 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)
366 struct afb_xreq *xreq = closure;
367 struct subcall *subcall;
369 subcall = subcall_alloc(xreq, api, verb, args);
370 if (subcall == NULL) {
372 callback(cb_closure, 1, afb_msg_json_internal_error());
373 json_object_put(args);
375 subcall->callback = callback;
376 subcall->closure = cb_closure;
377 subcall_process(subcall);
381 static void xreq_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
383 struct subcall *subcall = closure;
384 subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
387 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)
389 struct afb_xreq *xreq = closure;
390 struct subcall *subcall;
392 subcall = subcall_alloc(xreq, api, verb, args);
393 if (subcall == NULL) {
395 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
396 json_object_put(args);
398 subcall->callback = xreq_subcall_req_reply_cb;
399 subcall->closure = subcall;
400 subcall->hooked.callback2 = callback;
401 subcall->hooked.closure = cb_closure;
402 subcall_process(subcall);
407 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
410 struct subcall *subcall;
411 struct afb_xreq *xreq = closure;
412 struct json_object *resu;
414 subcall = subcall_alloc(xreq, api, verb, args);
417 resu = afb_msg_json_internal_error();
418 json_object_put(args);
420 subcall->callback = subcall_sync_reply;
421 subcall->closure = subcall;
422 subcall->jobloop = NULL;
423 subcall->result = NULL;
425 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
426 resu = subcall->result;
427 if (rc < 0 || subcall->status < 0) {
428 resu = resu ?: afb_msg_json_internal_error();
435 json_object_put(resu);
439 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
441 /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
442 vverbose(level, file, line, func, fmt, args);
445 static struct afb_stored_req *xreq_store_cb(void *closure)
447 xreq_addref_cb(closure);
451 /******************************************************************************/
453 static struct json_object *xreq_hooked_json_cb(void *closure)
455 struct json_object *r = xreq_json_cb(closure);
456 struct afb_xreq *xreq = closure;
457 return afb_hook_xreq_json(xreq, r);
460 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
462 struct afb_arg r = xreq_get_cb(closure, name);
463 struct afb_xreq *xreq = closure;
464 return afb_hook_xreq_get(xreq, name, r);
467 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
469 struct afb_xreq *xreq = closure;
470 afb_hook_xreq_success(xreq, obj, info);
471 xreq_success_cb(closure, obj, info);
474 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
476 struct afb_xreq *xreq = closure;
477 afb_hook_xreq_fail(xreq, status, info);
478 xreq_fail_cb(closure, status, info);
481 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
483 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
486 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
488 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
491 static void *xreq_hooked_context_get_cb(void *closure)
493 void *r = xreq_context_get_cb(closure);
494 struct afb_xreq *xreq = closure;
495 return afb_hook_xreq_context_get(xreq, r);
498 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
500 struct afb_xreq *xreq = closure;
501 afb_hook_xreq_context_set(xreq, value, free_value);
502 xreq_context_set_cb(closure, value, free_value);
505 static void xreq_hooked_addref_cb(void *closure)
507 struct afb_xreq *xreq = closure;
508 afb_hook_xreq_addref(xreq);
509 xreq_addref_cb(closure);
512 static void xreq_hooked_unref_cb(void *closure)
514 struct afb_xreq *xreq = closure;
515 afb_hook_xreq_unref(xreq);
516 xreq_unref_cb(closure);
519 static void xreq_hooked_session_close_cb(void *closure)
521 struct afb_xreq *xreq = closure;
522 afb_hook_xreq_session_close(xreq);
523 xreq_session_close_cb(closure);
526 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
528 int r = xreq_session_set_LOA_cb(closure, level);
529 struct afb_xreq *xreq = closure;
530 return afb_hook_xreq_session_set_LOA(xreq, level, r);
533 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
535 int r = xreq_subscribe_cb(closure, event);
536 struct afb_xreq *xreq = closure;
537 return afb_hook_xreq_subscribe(xreq, event, r);
540 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
542 int r = xreq_unsubscribe_cb(closure, event);
543 struct afb_xreq *xreq = closure;
544 return afb_hook_xreq_unsubscribe(xreq, event, r);
547 static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
549 struct subcall *subcall = closure;
551 afb_hook_xreq_subcall_result(subcall->caller, status, result);
552 subcall->hooked.callback(subcall->hooked.closure, status, result);
555 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)
557 struct afb_xreq *xreq = closure;
558 struct subcall *subcall;
560 afb_hook_xreq_subcall(xreq, api, verb, args);
561 subcall = subcall_alloc(xreq, api, verb, args);
562 if (subcall == NULL) {
564 callback(cb_closure, 1, afb_msg_json_internal_error());
565 json_object_put(args);
567 subcall->callback = xreq_hooked_subcall_reply_cb;
568 subcall->closure = subcall;
569 subcall->hooked.callback = callback;
570 subcall->hooked.closure = cb_closure;
571 subcall_process(subcall);
575 static void xreq_hooked_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
577 struct subcall *subcall = closure;
579 afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
580 subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
583 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)
585 struct afb_xreq *xreq = closure;
586 struct subcall *subcall;
588 afb_hook_xreq_subcall_req(xreq, api, verb, args);
589 subcall = subcall_alloc(xreq, api, verb, args);
590 if (subcall == NULL) {
592 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
593 json_object_put(args);
595 subcall->callback = xreq_hooked_subcall_req_reply_cb;
596 subcall->closure = subcall;
597 subcall->hooked.callback2 = callback;
598 subcall->hooked.closure = cb_closure;
599 subcall_process(subcall);
603 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
606 struct afb_xreq *xreq = closure;
607 afb_hook_xreq_subcallsync(xreq, api, verb, args);
608 r = xreq_subcallsync_cb(closure, api, verb, args, result);
609 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
612 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
614 struct afb_xreq *xreq = closure;
617 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
618 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
622 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
624 struct afb_xreq *xreq = closure;
625 struct afb_stored_req *r = xreq_store_cb(closure);
626 afb_hook_xreq_store(xreq, r);
630 /******************************************************************************/
632 const struct afb_req_itf xreq_itf = {
633 .json = xreq_json_cb,
635 .success = xreq_success_cb,
636 .fail = xreq_fail_cb,
637 .vsuccess = xreq_vsuccess_cb,
638 .vfail = xreq_vfail_cb,
639 .context_get = xreq_context_get_cb,
640 .context_set = xreq_context_set_cb,
641 .addref = xreq_addref_cb,
642 .unref = xreq_unref_cb,
643 .session_close = xreq_session_close_cb,
644 .session_set_LOA = xreq_session_set_LOA_cb,
645 .subscribe = xreq_subscribe_cb,
646 .unsubscribe = xreq_unsubscribe_cb,
647 .subcall = xreq_subcall_cb,
648 .subcallsync = xreq_subcallsync_cb,
649 .vverbose = xreq_vverbose_cb,
650 .store = xreq_store_cb,
651 .subcall_req = xreq_subcall_req_cb
654 const struct afb_req_itf xreq_hooked_itf = {
655 .json = xreq_hooked_json_cb,
656 .get = xreq_hooked_get_cb,
657 .success = xreq_hooked_success_cb,
658 .fail = xreq_hooked_fail_cb,
659 .vsuccess = xreq_hooked_vsuccess_cb,
660 .vfail = xreq_hooked_vfail_cb,
661 .context_get = xreq_hooked_context_get_cb,
662 .context_set = xreq_hooked_context_set_cb,
663 .addref = xreq_hooked_addref_cb,
664 .unref = xreq_hooked_unref_cb,
665 .session_close = xreq_hooked_session_close_cb,
666 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
667 .subscribe = xreq_hooked_subscribe_cb,
668 .unsubscribe = xreq_hooked_unsubscribe_cb,
669 .subcall = xreq_hooked_subcall_cb,
670 .subcallsync = xreq_hooked_subcallsync_cb,
671 .vverbose = xreq_hooked_vverbose_cb,
672 .store = xreq_hooked_store_cb,
673 .subcall_req = xreq_hooked_subcall_req_cb
676 /******************************************************************************/
678 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
680 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
682 afb_hook_xreq_unstore(xreq);
686 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
688 return afb_req_json(to_req(xreq));
691 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
693 afb_req_success(to_req(xreq), obj, info);
696 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
700 va_start(args, info);
701 if (info == NULL || vasprintf(&message, info, args) < 0)
704 afb_xreq_success(xreq, obj, message);
708 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
710 afb_req_fail(to_req(xreq), status, info);
713 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
717 va_start(args, info);
718 if (info == NULL || vasprintf(&message, info, args) < 0)
721 afb_xreq_fail(xreq, status, message);
725 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
727 struct json_object *obj = xreq_json_cb(xreq);
728 const char *result = json_object_to_json_string(obj);
730 *size = strlen(result);
734 void afb_xreq_addref(struct afb_xreq *xreq)
736 afb_req_addref(to_req(xreq));
739 void afb_xreq_unref(struct afb_xreq *xreq)
741 afb_req_unref(to_req(xreq));
744 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)
746 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
749 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)
751 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
754 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
756 return xreq_subcallsync_cb(xreq, api, verb, args, result);
759 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
761 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
764 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
768 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
769 if (!afb_context_check(&xreq->context)) {
770 afb_context_close(&xreq->context);
771 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
777 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
778 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
779 if (!afb_context_check_loa(&xreq->context, loa)) {
780 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
786 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
787 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
788 if (afb_context_check_loa(&xreq->context, loa + 1)) {
789 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
795 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
796 afb_context_refresh(&xreq->context);
798 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
799 afb_context_change_loa(&xreq->context, 0);
800 afb_context_close(&xreq->context);
806 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
810 if (sessionflags != 0) {
811 if (!afb_context_check(&xreq->context)) {
812 afb_context_close(&xreq->context);
813 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
819 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
820 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
821 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
826 if (auth && !afb_auth_check(auth, xreq)) {
827 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
832 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
833 afb_context_refresh(&xreq->context);
835 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
836 afb_context_close(&xreq->context);
842 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
845 afb_xreq_fail_unknown_verb(xreq);
847 if (!xreq_session_check_apply_v1(xreq, verb->session))
848 verb->callback(to_req(xreq));
851 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
854 afb_xreq_fail_unknown_verb(xreq);
856 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
857 verb->callback(to_req(xreq));
860 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
862 memset(xreq, 0, sizeof *xreq);
864 xreq->queryitf = queryitf;
867 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
869 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
872 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
874 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
877 static void process_sync(struct afb_xreq *xreq)
882 afb_hook_init_xreq(xreq);
884 afb_hook_xreq_begin(xreq);
887 if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
889 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
891 afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
893 xreq->context.api_key = api.closure;
894 api.itf->call(api.closure, xreq);
898 static void process_async(int signum, void *arg)
900 struct afb_xreq *xreq = arg;
903 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
910 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
912 xreq->apiset = apiset;
915 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
916 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
917 ERROR("can't process job with threads: %m");
918 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");