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 inline void xreq_addref(struct afb_xreq *xreq)
45 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
48 static inline void xreq_unref(struct afb_xreq *xreq)
50 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
52 afb_xreq_fail(xreq, "error", "no reply");
54 afb_hook_xreq_end(xreq);
55 xreq->queryitf->unref(xreq);
59 /******************************************************************************/
61 extern const struct afb_req_itf xreq_itf;
62 extern const struct afb_req_itf xreq_hooked_itf;
64 static inline struct afb_req to_req(struct afb_xreq *xreq)
66 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
69 /******************************************************************************/
74 struct afb_xreq *caller;
75 void (*callback)(void*, int, struct json_object*);
79 struct jobloop *jobloop;
80 struct json_object *result;
85 void (*callback)(void*, int, struct json_object*);
86 void (*callback2)(void*, int, struct json_object*, struct afb_req);
93 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
95 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
97 return afb_xreq_subscribe(subcall->caller, event);
100 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
102 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
104 return afb_xreq_unsubscribe(subcall->caller, event);
107 static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
109 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
111 if (subcall->callback)
112 subcall->callback(subcall->closure, status, obj);
113 json_object_put(obj);
116 static void subcall_destroy(struct afb_xreq *xreq)
118 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
120 json_object_put(subcall->xreq.json);
121 afb_cred_unref(subcall->xreq.cred);
122 xreq_unref(subcall->caller);
126 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
127 .reply = subcall_reply,
128 .unref = subcall_destroy,
129 .subscribe = subcall_subscribe,
130 .unsubscribe = subcall_unsubscribe
133 static struct subcall *subcall_alloc(
134 struct afb_xreq *caller,
137 struct json_object *args
140 struct subcall *subcall;
141 size_t lenapi, lenverb;
144 lenapi = 1 + strlen(api);
145 lenverb = 1 + strlen(verb);
146 subcall = malloc(lenapi + lenverb + sizeof *subcall);
148 ERROR("out of memory");
150 copy = (char*)&subcall[1];
151 memcpy(copy, api, lenapi);
153 copy = ©[lenapi];
154 memcpy(copy, verb, lenverb);
157 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
158 afb_context_subinit(&subcall->xreq.context, &caller->context);
159 subcall->xreq.cred = afb_cred_addref(caller->cred);
160 subcall->xreq.json = args;
161 subcall->xreq.api = api;
162 subcall->xreq.verb = verb;
163 subcall->caller = caller;
169 static void subcall_process(struct subcall *subcall)
171 if (subcall->caller->queryitf->subcall) {
172 subcall->caller->queryitf->subcall(
173 subcall->caller, subcall->xreq.api, subcall->xreq.verb,
174 subcall->xreq.json, subcall->callback, subcall->closure);
175 xreq_unref(&subcall->xreq);
177 afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
180 static void subcall_sync_leave(struct subcall *subcall)
182 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
187 static void subcall_sync_reply(void *closure, int status, struct json_object *obj)
189 struct subcall *subcall = closure;
191 subcall->status = status;
192 subcall->result = json_object_get(obj);
193 subcall_sync_leave(subcall);
196 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
198 struct subcall *subcall = closure;
201 subcall->jobloop = jobloop;
202 subcall_process(subcall);
204 subcall->status = -1;
205 subcall_sync_leave(subcall);
209 /******************************************************************************/
211 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
214 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
216 fun(first, second, info);
220 /******************************************************************************/
222 static struct json_object *xreq_json_cb(void *closure)
224 struct afb_xreq *xreq = closure;
225 if (!xreq->json && xreq->queryitf->json)
226 xreq->json = xreq->queryitf->json(xreq);
230 static struct afb_arg xreq_get_cb(void *closure, const char *name)
232 struct afb_xreq *xreq = closure;
234 struct json_object *object, *value;
236 if (xreq->queryitf->get)
237 arg = xreq->queryitf->get(xreq, name);
239 object = xreq_json_cb(closure);
240 if (json_object_object_get_ex(object, name, &value)) {
242 arg.value = json_object_get_string(value);
252 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
254 struct afb_xreq *xreq = closure;
257 ERROR("reply called more than one time!!");
258 json_object_put(obj);
261 if (xreq->queryitf->success)
262 xreq->queryitf->success(xreq, obj, info);
264 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
268 static void xreq_fail_cb(void *closure, const char *status, const char *info)
270 struct afb_xreq *xreq = closure;
273 ERROR("reply called more than one time!!");
276 if (xreq->queryitf->fail)
277 xreq->queryitf->fail(xreq, status, info);
279 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
283 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
285 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
288 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
290 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
293 static void *xreq_context_get_cb(void *closure)
295 struct afb_xreq *xreq = closure;
296 return afb_context_get(&xreq->context);
299 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
301 struct afb_xreq *xreq = closure;
302 afb_context_set(&xreq->context, value, free_value);
305 static void xreq_addref_cb(void *closure)
307 struct afb_xreq *xreq = closure;
311 static void xreq_unref_cb(void *closure)
313 struct afb_xreq *xreq = closure;
317 static void xreq_session_close_cb(void *closure)
319 struct afb_xreq *xreq = closure;
320 afb_context_close(&xreq->context);
323 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
325 struct afb_xreq *xreq = closure;
326 return afb_context_change_loa(&xreq->context, level);
329 static int xreq_subscribe_cb(void *closure, struct afb_event event)
331 struct afb_xreq *xreq = closure;
332 return afb_xreq_subscribe(xreq, event);
335 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
338 return afb_evt_add_watch(xreq->listener, event);
339 if (xreq->queryitf->subscribe)
340 return xreq->queryitf->subscribe(xreq, event);
341 ERROR("no event listener, subscription impossible");
346 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
348 struct afb_xreq *xreq = closure;
349 return afb_xreq_unsubscribe(xreq, event);
352 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
355 return afb_evt_remove_watch(xreq->listener, event);
356 if (xreq->queryitf->unsubscribe)
357 return xreq->queryitf->unsubscribe(xreq, event);
358 ERROR("no event listener, unsubscription impossible");
363 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)
365 struct afb_xreq *xreq = closure;
366 struct subcall *subcall;
368 subcall = subcall_alloc(xreq, api, verb, args);
369 if (subcall == NULL) {
371 callback(cb_closure, 1, afb_msg_json_internal_error());
372 json_object_put(args);
374 subcall->callback = callback;
375 subcall->closure = cb_closure;
376 subcall_process(subcall);
380 static void xreq_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
382 struct subcall *subcall = closure;
383 subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
386 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)
388 struct afb_xreq *xreq = closure;
389 struct subcall *subcall;
391 subcall = subcall_alloc(xreq, api, verb, args);
392 if (subcall == NULL) {
394 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
395 json_object_put(args);
397 subcall->callback = xreq_subcall_req_reply_cb;
398 subcall->closure = subcall;
399 subcall->hooked.callback2 = callback;
400 subcall->hooked.closure = cb_closure;
401 subcall_process(subcall);
406 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
409 struct subcall *subcall;
410 struct afb_xreq *xreq = closure;
411 struct json_object *resu;
413 subcall = subcall_alloc(xreq, api, verb, args);
416 resu = afb_msg_json_internal_error();
417 json_object_put(args);
419 subcall->callback = subcall_sync_reply;
420 subcall->closure = subcall;
421 subcall->jobloop = NULL;
422 subcall->result = NULL;
424 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
425 resu = subcall->result;
426 if (rc < 0 || subcall->status < 0) {
427 resu = resu ?: afb_msg_json_internal_error();
434 json_object_put(resu);
438 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
441 struct afb_xreq *xreq = closure;
443 if (!fmt || vasprintf(&p, fmt, args) < 0)
444 vverbose(level, file, line, func, fmt, args);
446 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
451 static struct afb_stored_req *xreq_store_cb(void *closure)
453 xreq_addref_cb(closure);
457 static int xreq_has_permission_cb(void*closure, const char *permission)
459 struct afb_xreq *xreq = closure;
460 return afb_auth_has_permission(xreq, permission);
463 /******************************************************************************/
465 static struct json_object *xreq_hooked_json_cb(void *closure)
467 struct json_object *r = xreq_json_cb(closure);
468 struct afb_xreq *xreq = closure;
469 return afb_hook_xreq_json(xreq, r);
472 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
474 struct afb_arg r = xreq_get_cb(closure, name);
475 struct afb_xreq *xreq = closure;
476 return afb_hook_xreq_get(xreq, name, r);
479 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
481 struct afb_xreq *xreq = closure;
482 afb_hook_xreq_success(xreq, obj, info);
483 xreq_success_cb(closure, obj, info);
486 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
488 struct afb_xreq *xreq = closure;
489 afb_hook_xreq_fail(xreq, status, info);
490 xreq_fail_cb(closure, status, info);
493 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
495 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
498 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
500 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
503 static void *xreq_hooked_context_get_cb(void *closure)
505 void *r = xreq_context_get_cb(closure);
506 struct afb_xreq *xreq = closure;
507 return afb_hook_xreq_context_get(xreq, r);
510 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
512 struct afb_xreq *xreq = closure;
513 afb_hook_xreq_context_set(xreq, value, free_value);
514 xreq_context_set_cb(closure, value, free_value);
517 static void xreq_hooked_addref_cb(void *closure)
519 struct afb_xreq *xreq = closure;
520 afb_hook_xreq_addref(xreq);
521 xreq_addref_cb(closure);
524 static void xreq_hooked_unref_cb(void *closure)
526 struct afb_xreq *xreq = closure;
527 afb_hook_xreq_unref(xreq);
528 xreq_unref_cb(closure);
531 static void xreq_hooked_session_close_cb(void *closure)
533 struct afb_xreq *xreq = closure;
534 afb_hook_xreq_session_close(xreq);
535 xreq_session_close_cb(closure);
538 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
540 int r = xreq_session_set_LOA_cb(closure, level);
541 struct afb_xreq *xreq = closure;
542 return afb_hook_xreq_session_set_LOA(xreq, level, r);
545 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
547 int r = xreq_subscribe_cb(closure, event);
548 struct afb_xreq *xreq = closure;
549 return afb_hook_xreq_subscribe(xreq, event, r);
552 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
554 int r = xreq_unsubscribe_cb(closure, event);
555 struct afb_xreq *xreq = closure;
556 return afb_hook_xreq_unsubscribe(xreq, event, r);
559 static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
561 struct subcall *subcall = closure;
563 afb_hook_xreq_subcall_result(subcall->caller, status, result);
564 subcall->hooked.callback(subcall->hooked.closure, status, result);
567 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)
569 struct afb_xreq *xreq = closure;
570 struct subcall *subcall;
572 afb_hook_xreq_subcall(xreq, api, verb, args);
573 subcall = subcall_alloc(xreq, api, verb, args);
574 if (subcall == NULL) {
576 callback(cb_closure, 1, afb_msg_json_internal_error());
577 json_object_put(args);
579 subcall->callback = xreq_hooked_subcall_reply_cb;
580 subcall->closure = subcall;
581 subcall->hooked.callback = callback;
582 subcall->hooked.closure = cb_closure;
583 subcall_process(subcall);
587 static void xreq_hooked_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
589 struct subcall *subcall = closure;
591 afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
592 subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
595 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)
597 struct afb_xreq *xreq = closure;
598 struct subcall *subcall;
600 afb_hook_xreq_subcall_req(xreq, api, verb, args);
601 subcall = subcall_alloc(xreq, api, verb, args);
602 if (subcall == NULL) {
604 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
605 json_object_put(args);
607 subcall->callback = xreq_hooked_subcall_req_reply_cb;
608 subcall->closure = subcall;
609 subcall->hooked.callback2 = callback;
610 subcall->hooked.closure = cb_closure;
611 subcall_process(subcall);
615 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
618 struct afb_xreq *xreq = closure;
619 afb_hook_xreq_subcallsync(xreq, api, verb, args);
620 r = xreq_subcallsync_cb(closure, api, verb, args, result);
621 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
624 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
626 struct afb_xreq *xreq = closure;
629 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
630 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
634 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
636 struct afb_xreq *xreq = closure;
637 struct afb_stored_req *r = xreq_store_cb(closure);
638 afb_hook_xreq_store(xreq, r);
642 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
644 struct afb_xreq *xreq = closure;
645 int r = xreq_has_permission_cb(closure, permission);
646 return afb_hook_xreq_has_permission(xreq, permission, r);
649 /******************************************************************************/
651 const struct afb_req_itf xreq_itf = {
652 .json = xreq_json_cb,
654 .success = xreq_success_cb,
655 .fail = xreq_fail_cb,
656 .vsuccess = xreq_vsuccess_cb,
657 .vfail = xreq_vfail_cb,
658 .context_get = xreq_context_get_cb,
659 .context_set = xreq_context_set_cb,
660 .addref = xreq_addref_cb,
661 .unref = xreq_unref_cb,
662 .session_close = xreq_session_close_cb,
663 .session_set_LOA = xreq_session_set_LOA_cb,
664 .subscribe = xreq_subscribe_cb,
665 .unsubscribe = xreq_unsubscribe_cb,
666 .subcall = xreq_subcall_cb,
667 .subcallsync = xreq_subcallsync_cb,
668 .vverbose = xreq_vverbose_cb,
669 .store = xreq_store_cb,
670 .subcall_req = xreq_subcall_req_cb,
671 .has_permission = xreq_has_permission_cb
674 const struct afb_req_itf xreq_hooked_itf = {
675 .json = xreq_hooked_json_cb,
676 .get = xreq_hooked_get_cb,
677 .success = xreq_hooked_success_cb,
678 .fail = xreq_hooked_fail_cb,
679 .vsuccess = xreq_hooked_vsuccess_cb,
680 .vfail = xreq_hooked_vfail_cb,
681 .context_get = xreq_hooked_context_get_cb,
682 .context_set = xreq_hooked_context_set_cb,
683 .addref = xreq_hooked_addref_cb,
684 .unref = xreq_hooked_unref_cb,
685 .session_close = xreq_hooked_session_close_cb,
686 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
687 .subscribe = xreq_hooked_subscribe_cb,
688 .unsubscribe = xreq_hooked_unsubscribe_cb,
689 .subcall = xreq_hooked_subcall_cb,
690 .subcallsync = xreq_hooked_subcallsync_cb,
691 .vverbose = xreq_hooked_vverbose_cb,
692 .store = xreq_hooked_store_cb,
693 .subcall_req = xreq_hooked_subcall_req_cb,
694 .has_permission = xreq_hooked_has_permission_cb
697 /******************************************************************************/
699 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
701 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
703 afb_hook_xreq_unstore(xreq);
707 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
709 return afb_req_json(to_req(xreq));
712 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
714 afb_req_success(to_req(xreq), obj, info);
717 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
721 va_start(args, info);
722 if (info == NULL || vasprintf(&message, info, args) < 0)
725 afb_xreq_success(xreq, obj, message);
729 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
731 afb_req_fail(to_req(xreq), status, info);
734 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
738 va_start(args, info);
739 if (info == NULL || vasprintf(&message, info, args) < 0)
742 afb_xreq_fail(xreq, status, message);
746 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
748 struct json_object *obj = xreq_json_cb(xreq);
749 const char *result = json_object_to_json_string(obj);
751 *size = strlen(result);
755 void afb_xreq_addref(struct afb_xreq *xreq)
757 afb_req_addref(to_req(xreq));
760 void afb_xreq_unref(struct afb_xreq *xreq)
762 afb_req_unref(to_req(xreq));
765 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)
767 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
770 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)
772 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
775 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
777 return xreq_subcallsync_cb(xreq, api, verb, args, result);
780 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
782 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
785 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
789 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
790 if (!afb_context_check(&xreq->context)) {
791 afb_context_close(&xreq->context);
792 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
798 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
799 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
800 if (!afb_context_check_loa(&xreq->context, loa)) {
801 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
807 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
808 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
809 if (afb_context_check_loa(&xreq->context, loa + 1)) {
810 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
816 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
817 afb_context_refresh(&xreq->context);
819 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
820 afb_context_change_loa(&xreq->context, 0);
821 afb_context_close(&xreq->context);
827 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
831 if (sessionflags != 0) {
832 if (!afb_context_check(&xreq->context)) {
833 afb_context_close(&xreq->context);
834 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
840 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
841 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
842 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
847 if (auth && !afb_auth_check(xreq, auth)) {
848 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
853 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
854 afb_context_refresh(&xreq->context);
856 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
857 afb_context_close(&xreq->context);
863 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
866 afb_xreq_fail_unknown_verb(xreq);
868 if (!xreq_session_check_apply_v1(xreq, verb->session))
869 verb->callback(to_req(xreq));
872 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
875 afb_xreq_fail_unknown_verb(xreq);
877 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
878 verb->callback(to_req(xreq));
881 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
883 memset(xreq, 0, sizeof *xreq);
885 xreq->queryitf = queryitf;
888 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
890 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
893 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
895 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
898 static void process_sync(struct afb_xreq *xreq)
900 const struct afb_api *api;
903 afb_hook_init_xreq(xreq);
905 afb_hook_xreq_begin(xreq);
908 api = (const struct afb_api*)xreq->context.api_key;
910 api->itf->call(api->closure, xreq);
912 api = afb_apiset_lookup_started(xreq->apiset, xreq->api, 1);
914 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
916 afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
920 static void process_async(int signum, void *arg)
922 struct afb_xreq *xreq = arg;
925 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
932 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
934 const struct afb_api *api;
936 xreq->apiset = apiset;
937 api = afb_apiset_lookup_started(apiset, xreq->api, 1);
938 xreq->context.api_key = (void*)api;
941 if (jobs_queue(api && api->noconcurrency ? (void*)api : NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
942 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
943 ERROR("can't process job with threads: %m");
944 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");