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;
76 void (*completion)(struct subcall*, 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_cb(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_cb(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_cb(struct afb_xreq *xreq, int status, struct json_object *result)
110 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
112 subcall->completion(subcall, status, result);
113 json_object_put(result);
114 afb_xreq_unref(&subcall->xreq);
117 static void subcall_destroy_cb(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_cb,
129 .unref = subcall_destroy_cb,
130 .subscribe = subcall_subscribe_cb,
131 .unsubscribe = subcall_unsubscribe_cb
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;
171 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
173 subcall->callback(subcall->closure, status, result);
176 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
178 subcall->callback2(subcall->closure, status, result, to_req(subcall->caller));
181 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
183 afb_hook_xreq_subcall_result(subcall->caller, status, result);
184 subcall_on_reply(subcall, status, result);
187 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
189 afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
190 subcall_req_on_reply(subcall, status, result);
193 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
195 struct afb_xreq *xreq = closure;
198 ERROR("subcall replied more than one time!!");
199 json_object_put(result);
202 subcall_reply_cb(xreq, status, result);
206 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
208 subcall->completion = completion;
209 if (subcall->caller->queryitf->subcall) {
210 subcall->caller->queryitf->subcall(
211 subcall->caller, subcall->xreq.api, subcall->xreq.verb,
212 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
214 afb_xreq_addref(&subcall->xreq);
215 afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
219 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
221 subcall->callback = callback;
222 subcall->closure = cb_closure;
223 subcall_process(subcall, subcall_on_reply);
226 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
228 subcall->callback2 = callback;
229 subcall->closure = cb_closure;
230 subcall_process(subcall, subcall_req_on_reply);
233 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
235 subcall->callback = callback;
236 subcall->closure = cb_closure;
237 subcall_process(subcall, subcall_hooked_on_reply);
240 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
242 subcall->callback2 = callback;
243 subcall->closure = cb_closure;
244 subcall_process(subcall, subcall_req_hooked_on_reply);
247 static void subcall_sync_leave(struct subcall *subcall)
249 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
254 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
256 subcall->status = status;
257 subcall->result = json_object_get(result);
258 subcall_sync_leave(subcall);
261 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
263 struct subcall *subcall = closure;
266 subcall->jobloop = jobloop;
267 subcall->result = NULL;
269 subcall_process(subcall, subcall_sync_reply);
271 subcall->status = -1;
272 subcall_sync_leave(subcall);
276 static int subcallsync(struct subcall *subcall, struct json_object **result)
280 afb_xreq_addref(&subcall->xreq);
281 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
282 *result = subcall->result;
283 if (rc < 0 || subcall->status < 0) {
284 *result = *result ?: afb_msg_json_internal_error();
287 afb_xreq_unref(&subcall->xreq);
291 /******************************************************************************/
293 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
296 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
298 fun(first, second, info);
302 /******************************************************************************/
304 static struct json_object *xreq_json_cb(void *closure)
306 struct afb_xreq *xreq = closure;
307 if (!xreq->json && xreq->queryitf->json)
308 xreq->json = xreq->queryitf->json(xreq);
312 static struct afb_arg xreq_get_cb(void *closure, const char *name)
314 struct afb_xreq *xreq = closure;
316 struct json_object *object, *value;
318 if (xreq->queryitf->get)
319 arg = xreq->queryitf->get(xreq, name);
321 object = xreq_json_cb(closure);
322 if (json_object_object_get_ex(object, name, &value)) {
324 arg.value = json_object_get_string(value);
334 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
336 struct afb_xreq *xreq = closure;
339 ERROR("reply called more than one time!!");
340 json_object_put(obj);
343 if (xreq->queryitf->success)
344 xreq->queryitf->success(xreq, obj, info);
346 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
350 static void xreq_fail_cb(void *closure, const char *status, const char *info)
352 struct afb_xreq *xreq = closure;
355 ERROR("reply called more than one time!!");
358 if (xreq->queryitf->fail)
359 xreq->queryitf->fail(xreq, status, info);
361 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
365 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
367 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
370 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
372 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
375 static void *xreq_context_get_cb(void *closure)
377 struct afb_xreq *xreq = closure;
378 return afb_context_get(&xreq->context);
381 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
383 struct afb_xreq *xreq = closure;
384 afb_context_set(&xreq->context, value, free_value);
387 static void xreq_addref_cb(void *closure)
389 struct afb_xreq *xreq = closure;
393 static void xreq_unref_cb(void *closure)
395 struct afb_xreq *xreq = closure;
399 static void xreq_session_close_cb(void *closure)
401 struct afb_xreq *xreq = closure;
402 afb_context_close(&xreq->context);
405 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
407 struct afb_xreq *xreq = closure;
408 return afb_context_change_loa(&xreq->context, level);
411 static int xreq_subscribe_cb(void *closure, struct afb_event event)
413 struct afb_xreq *xreq = closure;
414 return afb_xreq_subscribe(xreq, event);
417 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
420 return afb_evt_add_watch(xreq->listener, event);
421 if (xreq->queryitf->subscribe)
422 return xreq->queryitf->subscribe(xreq, event);
423 ERROR("no event listener, subscription impossible");
428 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
430 struct afb_xreq *xreq = closure;
431 return afb_xreq_unsubscribe(xreq, event);
434 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
437 return afb_evt_remove_watch(xreq->listener, event);
438 if (xreq->queryitf->unsubscribe)
439 return xreq->queryitf->unsubscribe(xreq, event);
440 ERROR("no event listener, unsubscription impossible");
445 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)
447 struct afb_xreq *xreq = closure;
450 sc = subcall_alloc(xreq, api, verb, args);
453 callback(cb_closure, 1, afb_msg_json_internal_error());
454 json_object_put(args);
456 subcall(sc, callback, cb_closure);
460 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)
462 struct afb_xreq *xreq = closure;
465 sc = subcall_alloc(xreq, api, verb, args);
468 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
469 json_object_put(args);
471 subcall_req(sc, callback, cb_closure);
476 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
480 struct afb_xreq *xreq = closure;
481 struct json_object *resu;
483 sc = subcall_alloc(xreq, api, verb, args);
486 resu = afb_msg_json_internal_error();
487 json_object_put(args);
489 rc = subcallsync(sc, &resu);
494 json_object_put(resu);
498 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
501 struct afb_xreq *xreq = closure;
503 if (!fmt || vasprintf(&p, fmt, args) < 0)
504 vverbose(level, file, line, func, fmt, args);
506 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
511 static struct afb_stored_req *xreq_store_cb(void *closure)
513 xreq_addref_cb(closure);
517 static int xreq_has_permission_cb(void*closure, const char *permission)
519 struct afb_xreq *xreq = closure;
520 return afb_auth_has_permission(xreq, permission);
523 /******************************************************************************/
525 static struct json_object *xreq_hooked_json_cb(void *closure)
527 struct json_object *r = xreq_json_cb(closure);
528 struct afb_xreq *xreq = closure;
529 return afb_hook_xreq_json(xreq, r);
532 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
534 struct afb_arg r = xreq_get_cb(closure, name);
535 struct afb_xreq *xreq = closure;
536 return afb_hook_xreq_get(xreq, name, r);
539 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
541 struct afb_xreq *xreq = closure;
542 afb_hook_xreq_success(xreq, obj, info);
543 xreq_success_cb(closure, obj, info);
546 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
548 struct afb_xreq *xreq = closure;
549 afb_hook_xreq_fail(xreq, status, info);
550 xreq_fail_cb(closure, status, info);
553 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
555 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
558 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
560 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
563 static void *xreq_hooked_context_get_cb(void *closure)
565 void *r = xreq_context_get_cb(closure);
566 struct afb_xreq *xreq = closure;
567 return afb_hook_xreq_context_get(xreq, r);
570 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
572 struct afb_xreq *xreq = closure;
573 afb_hook_xreq_context_set(xreq, value, free_value);
574 xreq_context_set_cb(closure, value, free_value);
577 static void xreq_hooked_addref_cb(void *closure)
579 struct afb_xreq *xreq = closure;
580 afb_hook_xreq_addref(xreq);
581 xreq_addref_cb(closure);
584 static void xreq_hooked_unref_cb(void *closure)
586 struct afb_xreq *xreq = closure;
587 afb_hook_xreq_unref(xreq);
588 xreq_unref_cb(closure);
591 static void xreq_hooked_session_close_cb(void *closure)
593 struct afb_xreq *xreq = closure;
594 afb_hook_xreq_session_close(xreq);
595 xreq_session_close_cb(closure);
598 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
600 int r = xreq_session_set_LOA_cb(closure, level);
601 struct afb_xreq *xreq = closure;
602 return afb_hook_xreq_session_set_LOA(xreq, level, r);
605 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
607 int r = xreq_subscribe_cb(closure, event);
608 struct afb_xreq *xreq = closure;
609 return afb_hook_xreq_subscribe(xreq, event, r);
612 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
614 int r = xreq_unsubscribe_cb(closure, event);
615 struct afb_xreq *xreq = closure;
616 return afb_hook_xreq_unsubscribe(xreq, event, r);
619 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)
621 struct afb_xreq *xreq = closure;
624 afb_hook_xreq_subcall(xreq, api, verb, args);
625 sc = subcall_alloc(xreq, api, verb, args);
628 callback(cb_closure, 1, afb_msg_json_internal_error());
629 json_object_put(args);
631 subcall_hooked(sc, callback, cb_closure);
635 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)
637 struct afb_xreq *xreq = closure;
640 afb_hook_xreq_subcall_req(xreq, api, verb, args);
641 sc = subcall_alloc(xreq, api, verb, args);
644 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
645 json_object_put(args);
647 subcall_req_hooked(sc, callback, cb_closure);
651 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
654 struct afb_xreq *xreq = closure;
655 afb_hook_xreq_subcallsync(xreq, api, verb, args);
656 r = xreq_subcallsync_cb(closure, api, verb, args, result);
657 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
660 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
662 struct afb_xreq *xreq = closure;
665 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
666 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
670 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
672 struct afb_xreq *xreq = closure;
673 struct afb_stored_req *r = xreq_store_cb(closure);
674 afb_hook_xreq_store(xreq, r);
678 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
680 struct afb_xreq *xreq = closure;
681 int r = xreq_has_permission_cb(closure, permission);
682 return afb_hook_xreq_has_permission(xreq, permission, r);
685 /******************************************************************************/
687 const struct afb_req_itf xreq_itf = {
688 .json = xreq_json_cb,
690 .success = xreq_success_cb,
691 .fail = xreq_fail_cb,
692 .vsuccess = xreq_vsuccess_cb,
693 .vfail = xreq_vfail_cb,
694 .context_get = xreq_context_get_cb,
695 .context_set = xreq_context_set_cb,
696 .addref = xreq_addref_cb,
697 .unref = xreq_unref_cb,
698 .session_close = xreq_session_close_cb,
699 .session_set_LOA = xreq_session_set_LOA_cb,
700 .subscribe = xreq_subscribe_cb,
701 .unsubscribe = xreq_unsubscribe_cb,
702 .subcall = xreq_subcall_cb,
703 .subcallsync = xreq_subcallsync_cb,
704 .vverbose = xreq_vverbose_cb,
705 .store = xreq_store_cb,
706 .subcall_req = xreq_subcall_req_cb,
707 .has_permission = xreq_has_permission_cb
710 const struct afb_req_itf xreq_hooked_itf = {
711 .json = xreq_hooked_json_cb,
712 .get = xreq_hooked_get_cb,
713 .success = xreq_hooked_success_cb,
714 .fail = xreq_hooked_fail_cb,
715 .vsuccess = xreq_hooked_vsuccess_cb,
716 .vfail = xreq_hooked_vfail_cb,
717 .context_get = xreq_hooked_context_get_cb,
718 .context_set = xreq_hooked_context_set_cb,
719 .addref = xreq_hooked_addref_cb,
720 .unref = xreq_hooked_unref_cb,
721 .session_close = xreq_hooked_session_close_cb,
722 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
723 .subscribe = xreq_hooked_subscribe_cb,
724 .unsubscribe = xreq_hooked_unsubscribe_cb,
725 .subcall = xreq_hooked_subcall_cb,
726 .subcallsync = xreq_hooked_subcallsync_cb,
727 .vverbose = xreq_hooked_vverbose_cb,
728 .store = xreq_hooked_store_cb,
729 .subcall_req = xreq_hooked_subcall_req_cb,
730 .has_permission = xreq_hooked_has_permission_cb
733 /******************************************************************************/
735 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
737 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
739 afb_hook_xreq_unstore(xreq);
743 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
745 return afb_req_json(to_req(xreq));
748 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
750 afb_req_success(to_req(xreq), obj, info);
753 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
757 va_start(args, info);
758 if (info == NULL || vasprintf(&message, info, args) < 0)
761 afb_xreq_success(xreq, obj, message);
765 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
767 afb_req_fail(to_req(xreq), status, info);
770 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
774 va_start(args, info);
775 if (info == NULL || vasprintf(&message, info, args) < 0)
778 afb_xreq_fail(xreq, status, message);
782 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
784 struct json_object *obj = xreq_json_cb(xreq);
785 const char *result = json_object_to_json_string(obj);
787 *size = strlen(result);
791 void afb_xreq_addref(struct afb_xreq *xreq)
793 afb_req_addref(to_req(xreq));
796 void afb_xreq_unref(struct afb_xreq *xreq)
798 afb_req_unref(to_req(xreq));
801 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)
803 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
806 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)
808 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
811 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
813 return xreq_subcallsync_cb(xreq, api, verb, args, result);
816 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
818 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
821 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
825 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 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 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
835 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
836 if (!afb_context_check_loa(&xreq->context, loa)) {
837 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
843 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
844 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
845 if (afb_context_check_loa(&xreq->context, loa + 1)) {
846 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
852 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
853 afb_context_refresh(&xreq->context);
855 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
856 afb_context_change_loa(&xreq->context, 0);
857 afb_context_close(&xreq->context);
863 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
867 if (sessionflags != 0) {
868 if (!afb_context_check(&xreq->context)) {
869 afb_context_close(&xreq->context);
870 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
876 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
877 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
878 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
883 if (auth && !afb_auth_check(xreq, auth)) {
884 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
889 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
890 afb_context_refresh(&xreq->context);
892 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
893 afb_context_close(&xreq->context);
899 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
902 afb_xreq_fail_unknown_verb(xreq);
904 if (!xreq_session_check_apply_v1(xreq, verb->session))
905 verb->callback(to_req(xreq));
908 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
911 afb_xreq_fail_unknown_verb(xreq);
913 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
914 verb->callback(to_req(xreq));
917 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
919 memset(xreq, 0, sizeof *xreq);
921 xreq->queryitf = queryitf;
924 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
926 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
929 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
931 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
934 static void process_sync(struct afb_xreq *xreq)
936 const struct afb_api *api;
939 afb_hook_init_xreq(xreq);
941 afb_hook_xreq_begin(xreq);
944 api = (const struct afb_api*)xreq->context.api_key;
946 api->itf->call(api->closure, xreq);
948 api = afb_apiset_lookup_started(xreq->apiset, xreq->api, 1);
950 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
952 afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
956 static void process_async(int signum, void *arg)
958 struct afb_xreq *xreq = arg;
961 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
968 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
970 const struct afb_api *api;
972 xreq->apiset = apiset;
973 api = afb_apiset_lookup_started(apiset, xreq->api, 1);
974 xreq->context.api_key = (void*)api;
977 if (jobs_queue(api && api->noconcurrency ? (void*)api : NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
978 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
979 ERROR("can't process job with threads: %m");
980 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");