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 static int xreq_has_permission_cb(void*closure, const char *permission)
453 struct afb_xreq *xreq = closure;
454 return afb_auth_has_permission(xreq, permission);
457 /******************************************************************************/
459 static struct json_object *xreq_hooked_json_cb(void *closure)
461 struct json_object *r = xreq_json_cb(closure);
462 struct afb_xreq *xreq = closure;
463 return afb_hook_xreq_json(xreq, r);
466 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
468 struct afb_arg r = xreq_get_cb(closure, name);
469 struct afb_xreq *xreq = closure;
470 return afb_hook_xreq_get(xreq, name, r);
473 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
475 struct afb_xreq *xreq = closure;
476 afb_hook_xreq_success(xreq, obj, info);
477 xreq_success_cb(closure, obj, info);
480 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
482 struct afb_xreq *xreq = closure;
483 afb_hook_xreq_fail(xreq, status, info);
484 xreq_fail_cb(closure, status, info);
487 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
489 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
492 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
494 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
497 static void *xreq_hooked_context_get_cb(void *closure)
499 void *r = xreq_context_get_cb(closure);
500 struct afb_xreq *xreq = closure;
501 return afb_hook_xreq_context_get(xreq, r);
504 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
506 struct afb_xreq *xreq = closure;
507 afb_hook_xreq_context_set(xreq, value, free_value);
508 xreq_context_set_cb(closure, value, free_value);
511 static void xreq_hooked_addref_cb(void *closure)
513 struct afb_xreq *xreq = closure;
514 afb_hook_xreq_addref(xreq);
515 xreq_addref_cb(closure);
518 static void xreq_hooked_unref_cb(void *closure)
520 struct afb_xreq *xreq = closure;
521 afb_hook_xreq_unref(xreq);
522 xreq_unref_cb(closure);
525 static void xreq_hooked_session_close_cb(void *closure)
527 struct afb_xreq *xreq = closure;
528 afb_hook_xreq_session_close(xreq);
529 xreq_session_close_cb(closure);
532 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
534 int r = xreq_session_set_LOA_cb(closure, level);
535 struct afb_xreq *xreq = closure;
536 return afb_hook_xreq_session_set_LOA(xreq, level, r);
539 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
541 int r = xreq_subscribe_cb(closure, event);
542 struct afb_xreq *xreq = closure;
543 return afb_hook_xreq_subscribe(xreq, event, r);
546 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
548 int r = xreq_unsubscribe_cb(closure, event);
549 struct afb_xreq *xreq = closure;
550 return afb_hook_xreq_unsubscribe(xreq, event, r);
553 static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
555 struct subcall *subcall = closure;
557 afb_hook_xreq_subcall_result(subcall->caller, status, result);
558 subcall->hooked.callback(subcall->hooked.closure, status, result);
561 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)
563 struct afb_xreq *xreq = closure;
564 struct subcall *subcall;
566 afb_hook_xreq_subcall(xreq, api, verb, args);
567 subcall = subcall_alloc(xreq, api, verb, args);
568 if (subcall == NULL) {
570 callback(cb_closure, 1, afb_msg_json_internal_error());
571 json_object_put(args);
573 subcall->callback = xreq_hooked_subcall_reply_cb;
574 subcall->closure = subcall;
575 subcall->hooked.callback = callback;
576 subcall->hooked.closure = cb_closure;
577 subcall_process(subcall);
581 static void xreq_hooked_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
583 struct subcall *subcall = closure;
585 afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
586 subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
589 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)
591 struct afb_xreq *xreq = closure;
592 struct subcall *subcall;
594 afb_hook_xreq_subcall_req(xreq, api, verb, args);
595 subcall = subcall_alloc(xreq, api, verb, args);
596 if (subcall == NULL) {
598 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
599 json_object_put(args);
601 subcall->callback = xreq_hooked_subcall_req_reply_cb;
602 subcall->closure = subcall;
603 subcall->hooked.callback2 = callback;
604 subcall->hooked.closure = cb_closure;
605 subcall_process(subcall);
609 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
612 struct afb_xreq *xreq = closure;
613 afb_hook_xreq_subcallsync(xreq, api, verb, args);
614 r = xreq_subcallsync_cb(closure, api, verb, args, result);
615 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
618 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
620 struct afb_xreq *xreq = closure;
623 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
624 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
628 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
630 struct afb_xreq *xreq = closure;
631 struct afb_stored_req *r = xreq_store_cb(closure);
632 afb_hook_xreq_store(xreq, r);
636 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
638 struct afb_xreq *xreq = closure;
639 int r = xreq_has_permission_cb(closure, permission);
640 return afb_hook_xreq_has_permission(xreq, permission, r);
643 /******************************************************************************/
645 const struct afb_req_itf xreq_itf = {
646 .json = xreq_json_cb,
648 .success = xreq_success_cb,
649 .fail = xreq_fail_cb,
650 .vsuccess = xreq_vsuccess_cb,
651 .vfail = xreq_vfail_cb,
652 .context_get = xreq_context_get_cb,
653 .context_set = xreq_context_set_cb,
654 .addref = xreq_addref_cb,
655 .unref = xreq_unref_cb,
656 .session_close = xreq_session_close_cb,
657 .session_set_LOA = xreq_session_set_LOA_cb,
658 .subscribe = xreq_subscribe_cb,
659 .unsubscribe = xreq_unsubscribe_cb,
660 .subcall = xreq_subcall_cb,
661 .subcallsync = xreq_subcallsync_cb,
662 .vverbose = xreq_vverbose_cb,
663 .store = xreq_store_cb,
664 .subcall_req = xreq_subcall_req_cb,
665 .has_permission = xreq_has_permission_cb
668 const struct afb_req_itf xreq_hooked_itf = {
669 .json = xreq_hooked_json_cb,
670 .get = xreq_hooked_get_cb,
671 .success = xreq_hooked_success_cb,
672 .fail = xreq_hooked_fail_cb,
673 .vsuccess = xreq_hooked_vsuccess_cb,
674 .vfail = xreq_hooked_vfail_cb,
675 .context_get = xreq_hooked_context_get_cb,
676 .context_set = xreq_hooked_context_set_cb,
677 .addref = xreq_hooked_addref_cb,
678 .unref = xreq_hooked_unref_cb,
679 .session_close = xreq_hooked_session_close_cb,
680 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
681 .subscribe = xreq_hooked_subscribe_cb,
682 .unsubscribe = xreq_hooked_unsubscribe_cb,
683 .subcall = xreq_hooked_subcall_cb,
684 .subcallsync = xreq_hooked_subcallsync_cb,
685 .vverbose = xreq_hooked_vverbose_cb,
686 .store = xreq_hooked_store_cb,
687 .subcall_req = xreq_hooked_subcall_req_cb,
688 .has_permission = xreq_hooked_has_permission_cb
691 /******************************************************************************/
693 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
695 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
697 afb_hook_xreq_unstore(xreq);
701 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
703 return afb_req_json(to_req(xreq));
706 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
708 afb_req_success(to_req(xreq), obj, info);
711 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
715 va_start(args, info);
716 if (info == NULL || vasprintf(&message, info, args) < 0)
719 afb_xreq_success(xreq, obj, message);
723 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
725 afb_req_fail(to_req(xreq), status, info);
728 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
732 va_start(args, info);
733 if (info == NULL || vasprintf(&message, info, args) < 0)
736 afb_xreq_fail(xreq, status, message);
740 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
742 struct json_object *obj = xreq_json_cb(xreq);
743 const char *result = json_object_to_json_string(obj);
745 *size = strlen(result);
749 void afb_xreq_addref(struct afb_xreq *xreq)
751 afb_req_addref(to_req(xreq));
754 void afb_xreq_unref(struct afb_xreq *xreq)
756 afb_req_unref(to_req(xreq));
759 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)
761 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
764 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)
766 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
769 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
771 return xreq_subcallsync_cb(xreq, api, verb, args, result);
774 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
776 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
779 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
783 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
784 if (!afb_context_check(&xreq->context)) {
785 afb_context_close(&xreq->context);
786 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
792 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
793 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
794 if (!afb_context_check_loa(&xreq->context, loa)) {
795 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
801 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
802 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
803 if (afb_context_check_loa(&xreq->context, loa + 1)) {
804 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
810 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
811 afb_context_refresh(&xreq->context);
813 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
814 afb_context_change_loa(&xreq->context, 0);
815 afb_context_close(&xreq->context);
821 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
825 if (sessionflags != 0) {
826 if (!afb_context_check(&xreq->context)) {
827 afb_context_close(&xreq->context);
828 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
834 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
835 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
836 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
841 if (auth && !afb_auth_check(xreq, auth)) {
842 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
847 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
848 afb_context_refresh(&xreq->context);
850 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
851 afb_context_close(&xreq->context);
857 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
860 afb_xreq_fail_unknown_verb(xreq);
862 if (!xreq_session_check_apply_v1(xreq, verb->session))
863 verb->callback(to_req(xreq));
866 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
869 afb_xreq_fail_unknown_verb(xreq);
871 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
872 verb->callback(to_req(xreq));
875 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
877 memset(xreq, 0, sizeof *xreq);
879 xreq->queryitf = queryitf;
882 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
884 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
887 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
889 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
892 static void process_sync(struct afb_xreq *xreq)
894 const struct afb_api *api;
897 afb_hook_init_xreq(xreq);
899 afb_hook_xreq_begin(xreq);
902 api = (const struct afb_api*)xreq->context.api_key;
904 api->itf->call(api->closure, xreq);
906 api = afb_apiset_lookup_started(xreq->apiset, xreq->api, 1);
908 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
910 afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
914 static void process_async(int signum, void *arg)
916 struct afb_xreq *xreq = arg;
919 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
926 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
928 const struct afb_api *api;
930 xreq->apiset = apiset;
931 api = afb_apiset_lookup_started(apiset, xreq->api, 1);
932 xreq->context.api_key = (void*)api;
935 if (jobs_queue(api && api->noconcurrency ? (void*)api : NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
936 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
937 ERROR("can't process job with threads: %m");
938 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");