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_hook_xreq_end(xreq);
54 xreq->queryitf->unref(xreq);
58 /******************************************************************************/
60 extern const struct afb_req_itf xreq_itf;
61 extern const struct afb_req_itf xreq_hooked_itf;
63 static inline struct afb_req to_req(struct afb_xreq *xreq)
65 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
68 /******************************************************************************/
73 struct afb_xreq *caller;
74 void (*callback)(void*, int, struct json_object*);
78 struct jobloop *jobloop;
79 struct json_object *result;
84 void (*callback)(void*, int, struct json_object*);
85 void (*callback2)(void*, int, struct json_object*, struct afb_req);
92 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
94 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
96 return afb_xreq_subscribe(subcall->caller, event);
99 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
101 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
103 return afb_xreq_unsubscribe(subcall->caller, event);
106 static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
108 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
110 if (subcall->callback)
111 subcall->callback(subcall->closure, status, obj);
112 json_object_put(obj);
115 static void subcall_destroy(struct afb_xreq *xreq)
117 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
119 json_object_put(subcall->xreq.json);
120 afb_cred_unref(subcall->xreq.cred);
121 xreq_unref(subcall->caller);
125 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
126 .reply = subcall_reply,
127 .unref = subcall_destroy,
128 .subscribe = subcall_subscribe,
129 .unsubscribe = subcall_unsubscribe
132 static struct subcall *subcall_alloc(
133 struct afb_xreq *caller,
136 struct json_object *args
139 struct subcall *subcall;
140 size_t lenapi, lenverb;
143 lenapi = 1 + strlen(api);
144 lenverb = 1 + strlen(verb);
145 subcall = malloc(lenapi + lenverb + sizeof *subcall);
147 ERROR("out of memory");
149 copy = (char*)&subcall[1];
150 memcpy(copy, api, lenapi);
152 copy = ©[lenapi];
153 memcpy(copy, verb, lenverb);
156 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
157 afb_context_subinit(&subcall->xreq.context, &caller->context);
158 subcall->xreq.cred = afb_cred_addref(caller->cred);
159 subcall->xreq.json = args;
160 subcall->xreq.api = api;
161 subcall->xreq.verb = verb;
162 subcall->caller = caller;
168 static void subcall_process(struct subcall *subcall)
170 if (subcall->caller->queryitf->subcall) {
171 subcall->caller->queryitf->subcall(
172 subcall->caller, subcall->xreq.api, subcall->xreq.verb,
173 subcall->xreq.json, subcall->callback, subcall->closure);
174 xreq_unref(&subcall->xreq);
176 afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
179 static void subcall_sync_leave(struct subcall *subcall)
181 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
186 static void subcall_sync_reply(void *closure, int status, struct json_object *obj)
188 struct subcall *subcall = closure;
190 subcall->status = status;
191 subcall->result = json_object_get(obj);
192 subcall_sync_leave(subcall);
195 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
197 struct subcall *subcall = closure;
200 subcall->jobloop = jobloop;
201 subcall_process(subcall);
203 subcall->status = -1;
204 subcall_sync_leave(subcall);
208 /******************************************************************************/
210 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
213 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
215 fun(first, second, info);
219 /******************************************************************************/
221 static struct json_object *xreq_json_cb(void *closure)
223 struct afb_xreq *xreq = closure;
224 if (!xreq->json && xreq->queryitf->json)
225 xreq->json = xreq->queryitf->json(xreq);
229 static struct afb_arg xreq_get_cb(void *closure, const char *name)
231 struct afb_xreq *xreq = closure;
233 struct json_object *object, *value;
235 if (xreq->queryitf->get)
236 arg = xreq->queryitf->get(xreq, name);
238 object = xreq_json_cb(closure);
239 if (json_object_object_get_ex(object, name, &value)) {
241 arg.value = json_object_get_string(value);
251 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
253 struct afb_xreq *xreq = closure;
256 ERROR("reply called more than one time!!");
257 json_object_put(obj);
260 if (xreq->queryitf->success)
261 xreq->queryitf->success(xreq, obj, info);
263 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
267 static void xreq_fail_cb(void *closure, const char *status, const char *info)
269 struct afb_xreq *xreq = closure;
272 ERROR("reply called more than one time!!");
275 if (xreq->queryitf->fail)
276 xreq->queryitf->fail(xreq, status, info);
278 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
282 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
284 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
287 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
289 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
292 static void *xreq_context_get_cb(void *closure)
294 struct afb_xreq *xreq = closure;
295 return afb_context_get(&xreq->context);
298 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
300 struct afb_xreq *xreq = closure;
301 afb_context_set(&xreq->context, value, free_value);
304 static void xreq_addref_cb(void *closure)
306 struct afb_xreq *xreq = closure;
310 static void xreq_unref_cb(void *closure)
312 struct afb_xreq *xreq = closure;
316 static void xreq_session_close_cb(void *closure)
318 struct afb_xreq *xreq = closure;
319 afb_context_close(&xreq->context);
322 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
324 struct afb_xreq *xreq = closure;
325 return afb_context_change_loa(&xreq->context, level);
328 static int xreq_subscribe_cb(void *closure, struct afb_event event)
330 struct afb_xreq *xreq = closure;
331 return afb_xreq_subscribe(xreq, event);
334 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
337 return afb_evt_add_watch(xreq->listener, event);
338 if (xreq->queryitf->subscribe)
339 return xreq->queryitf->subscribe(xreq, event);
340 ERROR("no event listener, subscription impossible");
345 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
347 struct afb_xreq *xreq = closure;
348 return afb_xreq_unsubscribe(xreq, event);
351 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
354 return afb_evt_remove_watch(xreq->listener, event);
355 if (xreq->queryitf->unsubscribe)
356 return xreq->queryitf->unsubscribe(xreq, event);
357 ERROR("no event listener, unsubscription impossible");
362 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)
364 struct afb_xreq *xreq = closure;
365 struct subcall *subcall;
367 subcall = subcall_alloc(xreq, api, verb, args);
368 if (subcall == NULL) {
370 callback(cb_closure, 1, afb_msg_json_internal_error());
371 json_object_put(args);
373 subcall->callback = callback;
374 subcall->closure = cb_closure;
375 subcall_process(subcall);
379 static void xreq_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
381 struct subcall *subcall = closure;
382 subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
385 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)
387 struct afb_xreq *xreq = closure;
388 struct subcall *subcall;
390 subcall = subcall_alloc(xreq, api, verb, args);
391 if (subcall == NULL) {
393 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
394 json_object_put(args);
396 subcall->callback = xreq_subcall_req_reply_cb;
397 subcall->closure = subcall;
398 subcall->hooked.callback2 = callback;
399 subcall->hooked.closure = cb_closure;
400 subcall_process(subcall);
405 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
408 struct subcall *subcall;
409 struct afb_xreq *xreq = closure;
410 struct json_object *resu;
412 subcall = subcall_alloc(xreq, api, verb, args);
415 resu = afb_msg_json_internal_error();
416 json_object_put(args);
418 subcall->callback = subcall_sync_reply;
419 subcall->closure = subcall;
420 subcall->jobloop = NULL;
421 subcall->result = NULL;
423 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
424 resu = subcall->result;
425 if (rc < 0 || subcall->status < 0) {
426 resu = resu ?: afb_msg_json_internal_error();
433 json_object_put(resu);
437 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
439 /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
440 vverbose(level, file, line, func, fmt, args);
443 static struct afb_stored_req *xreq_store_cb(void *closure)
445 xreq_addref_cb(closure);
449 /******************************************************************************/
451 static struct json_object *xreq_hooked_json_cb(void *closure)
453 struct json_object *r = xreq_json_cb(closure);
454 struct afb_xreq *xreq = closure;
455 return afb_hook_xreq_json(xreq, r);
458 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
460 struct afb_arg r = xreq_get_cb(closure, name);
461 struct afb_xreq *xreq = closure;
462 return afb_hook_xreq_get(xreq, name, r);
465 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
467 struct afb_xreq *xreq = closure;
468 afb_hook_xreq_success(xreq, obj, info);
469 xreq_success_cb(closure, obj, info);
472 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
474 struct afb_xreq *xreq = closure;
475 afb_hook_xreq_fail(xreq, status, info);
476 xreq_fail_cb(closure, status, info);
479 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
481 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
484 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
486 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
489 static void *xreq_hooked_context_get_cb(void *closure)
491 void *r = xreq_context_get_cb(closure);
492 struct afb_xreq *xreq = closure;
493 return afb_hook_xreq_context_get(xreq, r);
496 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
498 struct afb_xreq *xreq = closure;
499 afb_hook_xreq_context_set(xreq, value, free_value);
500 xreq_context_set_cb(closure, value, free_value);
503 static void xreq_hooked_addref_cb(void *closure)
505 struct afb_xreq *xreq = closure;
506 afb_hook_xreq_addref(xreq);
507 xreq_addref_cb(closure);
510 static void xreq_hooked_unref_cb(void *closure)
512 struct afb_xreq *xreq = closure;
513 afb_hook_xreq_unref(xreq);
514 xreq_unref_cb(closure);
517 static void xreq_hooked_session_close_cb(void *closure)
519 struct afb_xreq *xreq = closure;
520 afb_hook_xreq_session_close(xreq);
521 xreq_session_close_cb(closure);
524 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
526 int r = xreq_session_set_LOA_cb(closure, level);
527 struct afb_xreq *xreq = closure;
528 return afb_hook_xreq_session_set_LOA(xreq, level, r);
531 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
533 int r = xreq_subscribe_cb(closure, event);
534 struct afb_xreq *xreq = closure;
535 return afb_hook_xreq_subscribe(xreq, event, r);
538 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
540 int r = xreq_unsubscribe_cb(closure, event);
541 struct afb_xreq *xreq = closure;
542 return afb_hook_xreq_unsubscribe(xreq, event, r);
545 static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
547 struct subcall *subcall = closure;
549 afb_hook_xreq_subcall_result(subcall->caller, status, result);
550 subcall->hooked.callback(subcall->hooked.closure, status, result);
553 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)
555 struct afb_xreq *xreq = closure;
556 struct subcall *subcall;
558 afb_hook_xreq_subcall(xreq, api, verb, args);
559 subcall = subcall_alloc(xreq, api, verb, args);
560 if (subcall == NULL) {
562 callback(cb_closure, 1, afb_msg_json_internal_error());
563 json_object_put(args);
565 subcall->callback = xreq_hooked_subcall_reply_cb;
566 subcall->closure = subcall;
567 subcall->hooked.callback = callback;
568 subcall->hooked.closure = cb_closure;
569 subcall_process(subcall);
573 static void xreq_hooked_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
575 struct subcall *subcall = closure;
577 afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
578 subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
581 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)
583 struct afb_xreq *xreq = closure;
584 struct subcall *subcall;
586 afb_hook_xreq_subcall_req(xreq, api, verb, args);
587 subcall = subcall_alloc(xreq, api, verb, args);
588 if (subcall == NULL) {
590 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
591 json_object_put(args);
593 subcall->callback = xreq_hooked_subcall_req_reply_cb;
594 subcall->closure = subcall;
595 subcall->hooked.callback2 = callback;
596 subcall->hooked.closure = cb_closure;
597 subcall_process(subcall);
601 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
604 struct afb_xreq *xreq = closure;
605 afb_hook_xreq_subcallsync(xreq, api, verb, args);
606 r = xreq_subcallsync_cb(closure, api, verb, args, result);
607 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
610 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
612 struct afb_xreq *xreq = closure;
615 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
616 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
620 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
622 struct afb_xreq *xreq = closure;
623 struct afb_stored_req *r = xreq_store_cb(closure);
624 afb_hook_xreq_store(xreq, r);
628 /******************************************************************************/
630 const struct afb_req_itf xreq_itf = {
631 .json = xreq_json_cb,
633 .success = xreq_success_cb,
634 .fail = xreq_fail_cb,
635 .vsuccess = xreq_vsuccess_cb,
636 .vfail = xreq_vfail_cb,
637 .context_get = xreq_context_get_cb,
638 .context_set = xreq_context_set_cb,
639 .addref = xreq_addref_cb,
640 .unref = xreq_unref_cb,
641 .session_close = xreq_session_close_cb,
642 .session_set_LOA = xreq_session_set_LOA_cb,
643 .subscribe = xreq_subscribe_cb,
644 .unsubscribe = xreq_unsubscribe_cb,
645 .subcall = xreq_subcall_cb,
646 .subcallsync = xreq_subcallsync_cb,
647 .vverbose = xreq_vverbose_cb,
648 .store = xreq_store_cb,
649 .subcall_req = xreq_subcall_req_cb
652 const struct afb_req_itf xreq_hooked_itf = {
653 .json = xreq_hooked_json_cb,
654 .get = xreq_hooked_get_cb,
655 .success = xreq_hooked_success_cb,
656 .fail = xreq_hooked_fail_cb,
657 .vsuccess = xreq_hooked_vsuccess_cb,
658 .vfail = xreq_hooked_vfail_cb,
659 .context_get = xreq_hooked_context_get_cb,
660 .context_set = xreq_hooked_context_set_cb,
661 .addref = xreq_hooked_addref_cb,
662 .unref = xreq_hooked_unref_cb,
663 .session_close = xreq_hooked_session_close_cb,
664 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
665 .subscribe = xreq_hooked_subscribe_cb,
666 .unsubscribe = xreq_hooked_unsubscribe_cb,
667 .subcall = xreq_hooked_subcall_cb,
668 .subcallsync = xreq_hooked_subcallsync_cb,
669 .vverbose = xreq_hooked_vverbose_cb,
670 .store = xreq_hooked_store_cb,
671 .subcall_req = xreq_hooked_subcall_req_cb
674 /******************************************************************************/
676 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
678 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
680 afb_hook_xreq_unstore(xreq);
684 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
686 return afb_req_json(to_req(xreq));
689 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
691 afb_req_success(to_req(xreq), obj, info);
694 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
698 va_start(args, info);
699 if (info == NULL || vasprintf(&message, info, args) < 0)
702 afb_xreq_success(xreq, obj, message);
706 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
708 afb_req_fail(to_req(xreq), status, info);
711 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
715 va_start(args, info);
716 if (info == NULL || vasprintf(&message, info, args) < 0)
719 afb_xreq_fail(xreq, status, message);
723 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
725 struct json_object *obj = xreq_json_cb(xreq);
726 const char *result = json_object_to_json_string(obj);
728 *size = strlen(result);
732 void afb_xreq_addref(struct afb_xreq *xreq)
734 afb_req_addref(to_req(xreq));
737 void afb_xreq_unref(struct afb_xreq *xreq)
739 afb_req_unref(to_req(xreq));
742 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)
744 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
747 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)
749 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
752 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
754 return xreq_subcallsync_cb(xreq, api, verb, args, result);
757 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
759 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
762 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
766 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
767 if (!afb_context_check(&xreq->context)) {
768 afb_context_close(&xreq->context);
769 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
775 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
776 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
777 if (!afb_context_check_loa(&xreq->context, loa)) {
778 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
784 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
785 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
786 if (afb_context_check_loa(&xreq->context, loa + 1)) {
787 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
793 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
794 afb_context_refresh(&xreq->context);
796 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
797 afb_context_change_loa(&xreq->context, 0);
798 afb_context_close(&xreq->context);
804 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
808 if (sessionflags != 0) {
809 if (!afb_context_check(&xreq->context)) {
810 afb_context_close(&xreq->context);
811 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
817 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
818 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
819 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
824 if (auth && !afb_auth_check(auth, xreq)) {
825 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
830 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
831 afb_context_refresh(&xreq->context);
833 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
834 afb_context_close(&xreq->context);
840 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
843 afb_xreq_fail_unknown_verb(xreq);
845 if (!xreq_session_check_apply_v1(xreq, verb->session))
846 verb->callback(to_req(xreq));
849 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
852 afb_xreq_fail_unknown_verb(xreq);
854 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
855 verb->callback(to_req(xreq));
858 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
860 memset(xreq, 0, sizeof *xreq);
862 xreq->queryitf = queryitf;
865 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
867 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
870 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
872 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
875 static void process_sync(struct afb_xreq *xreq)
880 afb_hook_init_xreq(xreq);
882 afb_hook_xreq_begin(xreq);
885 if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
887 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
889 afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
891 xreq->context.api_key = api.closure;
892 api.itf->call(api.closure, xreq);
896 static void process_async(int signum, void *arg)
898 struct afb_xreq *xreq = arg;
901 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
908 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
910 xreq->apiset = apiset;
913 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
914 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
915 ERROR("can't process job with threads: %m");
916 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");