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>
28 #include <afb/afb-request.h>
30 #include "afb-context.h"
33 #include "afb-msg-json.h"
37 #include "afb-apiset.h"
42 /******************************************************************************/
44 static void xreq_finalize(struct afb_xreq *xreq)
47 afb_xreq_fail(xreq, "error", "no reply");
49 afb_hook_xreq_end(xreq);
51 afb_xreq_unhooked_unref(xreq->caller);
52 xreq->queryitf->unref(xreq);
55 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
57 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
60 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
62 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
66 /******************************************************************************/
68 static inline struct afb_request *to_request(struct afb_xreq *xreq)
70 return &xreq->request;
73 static inline struct afb_req to_req(struct afb_xreq *xreq)
75 return (struct afb_req){ .itf = xreq->request.itf, .closure = &xreq->request };
78 static inline struct afb_xreq *from_request(struct afb_request *request)
80 return CONTAINER_OF(struct afb_xreq, request, request);
83 /******************************************************************************/
89 void (*completion)(struct subcall*, int, struct json_object*);
93 struct jobloop *jobloop;
94 struct json_object *result;
99 void (*callback)(void*, int, struct json_object*);
100 void (*callback2)(void*, int, struct json_object*, struct afb_req);
107 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
109 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
111 return afb_xreq_subscribe(subcall->xreq.caller, event);
114 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
116 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
118 return afb_xreq_unsubscribe(subcall->xreq.caller, event);
121 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
123 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
125 subcall->completion(subcall, status, result);
126 json_object_put(result);
127 afb_xreq_unhooked_unref(&subcall->xreq);
130 static void subcall_destroy_cb(struct afb_xreq *xreq)
132 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
134 json_object_put(subcall->xreq.json);
135 afb_cred_unref(subcall->xreq.cred);
139 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
140 .reply = subcall_reply_cb,
141 .unref = subcall_destroy_cb,
142 .subscribe = subcall_subscribe_cb,
143 .unsubscribe = subcall_unsubscribe_cb
146 static struct subcall *subcall_alloc(
147 struct afb_xreq *caller,
150 struct json_object *args
153 struct subcall *subcall;
154 size_t lenapi, lenverb;
157 lenapi = 1 + strlen(api);
158 lenverb = 1 + strlen(verb);
159 subcall = malloc(lenapi + lenverb + sizeof *subcall);
161 ERROR("out of memory");
163 copy = (char*)&subcall[1];
164 memcpy(copy, api, lenapi);
166 copy = ©[lenapi];
167 memcpy(copy, verb, lenverb);
170 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
171 afb_context_subinit(&subcall->xreq.context, &caller->context);
172 subcall->xreq.cred = afb_cred_addref(caller->cred);
173 subcall->xreq.json = args;
174 subcall->xreq.api = api;
175 subcall->xreq.verb = verb;
176 subcall->xreq.caller = caller;
177 afb_xreq_unhooked_addref(caller);
183 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
185 subcall->callback(subcall->closure, status, result);
188 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
190 subcall->callback2(subcall->closure, status, result, to_req(subcall->xreq.caller));
193 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
195 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
196 subcall_on_reply(subcall, status, result);
199 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
201 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
202 subcall_req_on_reply(subcall, status, result);
205 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
207 struct afb_xreq *xreq = closure;
210 ERROR("subcall replied more than one time!!");
211 json_object_put(result);
214 subcall_reply_cb(xreq, status, result);
218 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
220 subcall->completion = completion;
221 if (subcall->xreq.caller->queryitf->subcall) {
222 subcall->xreq.caller->queryitf->subcall(
223 subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb,
224 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
226 afb_xreq_unhooked_addref(&subcall->xreq);
227 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
231 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
233 subcall->callback = callback;
234 subcall->closure = cb_closure;
235 subcall_process(subcall, subcall_on_reply);
238 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
240 subcall->callback2 = callback;
241 subcall->closure = cb_closure;
242 subcall_process(subcall, subcall_req_on_reply);
245 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
247 subcall->callback = callback;
248 subcall->closure = cb_closure;
249 subcall_process(subcall, subcall_hooked_on_reply);
252 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
254 subcall->callback2 = callback;
255 subcall->closure = cb_closure;
256 subcall_process(subcall, subcall_req_hooked_on_reply);
259 static void subcall_sync_leave(struct subcall *subcall)
261 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
266 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
268 subcall->status = status;
269 subcall->result = json_object_get(result);
270 subcall_sync_leave(subcall);
273 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
275 struct subcall *subcall = closure;
278 subcall->jobloop = jobloop;
279 subcall->result = NULL;
281 subcall_process(subcall, subcall_sync_reply);
283 subcall->status = -1;
284 subcall_sync_leave(subcall);
288 static int subcallsync(struct subcall *subcall, struct json_object **result)
292 afb_xreq_unhooked_addref(&subcall->xreq);
293 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
294 *result = subcall->result;
295 if (rc < 0 || subcall->status < 0) {
296 *result = *result ?: afb_msg_json_internal_error();
299 afb_xreq_unhooked_unref(&subcall->xreq);
303 /******************************************************************************/
305 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
308 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
310 fun(first, second, info);
314 /******************************************************************************/
316 static struct json_object *xreq_json_cb(struct afb_request *closure)
318 struct afb_xreq *xreq = from_request(closure);
319 if (!xreq->json && xreq->queryitf->json)
320 xreq->json = xreq->queryitf->json(xreq);
324 static struct afb_arg xreq_get_cb(struct afb_request *closure, const char *name)
326 struct afb_xreq *xreq = from_request(closure);
328 struct json_object *object, *value;
330 if (xreq->queryitf->get)
331 arg = xreq->queryitf->get(xreq, name);
333 object = xreq_json_cb(closure);
334 if (json_object_object_get_ex(object, name, &value)) {
336 arg.value = json_object_get_string(value);
346 static void xreq_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
348 struct afb_xreq *xreq = from_request(closure);
351 ERROR("reply called more than one time!!");
352 json_object_put(obj);
355 if (xreq->queryitf->success)
356 xreq->queryitf->success(xreq, obj, info);
358 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
362 static void xreq_fail_cb(struct afb_request *closure, const char *status, const char *info)
364 struct afb_xreq *xreq = from_request(closure);
367 ERROR("reply called more than one time!!");
370 if (xreq->queryitf->fail)
371 xreq->queryitf->fail(xreq, status, info);
373 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
377 static void xreq_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
379 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
382 static void xreq_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
384 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
387 static void *xreq_context_get_cb(struct afb_request *closure)
389 struct afb_xreq *xreq = from_request(closure);
390 return afb_context_get(&xreq->context);
393 static void xreq_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
395 struct afb_xreq *xreq = from_request(closure);
396 afb_context_set(&xreq->context, value, free_value);
399 static void xreq_addref_cb(struct afb_request *closure)
401 struct afb_xreq *xreq = from_request(closure);
402 afb_xreq_unhooked_addref(xreq);
405 static void xreq_unref_cb(struct afb_request *closure)
407 struct afb_xreq *xreq = from_request(closure);
408 afb_xreq_unhooked_unref(xreq);
411 static void xreq_session_close_cb(struct afb_request *closure)
413 struct afb_xreq *xreq = from_request(closure);
414 afb_context_close(&xreq->context);
417 static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
419 struct afb_xreq *xreq = from_request(closure);
420 return afb_context_change_loa(&xreq->context, level);
423 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
425 struct afb_xreq *xreq = from_request(closure);
426 return afb_xreq_subscribe(xreq, event);
429 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
432 return afb_evt_add_watch(xreq->listener, event);
433 if (xreq->queryitf->subscribe)
434 return xreq->queryitf->subscribe(xreq, event);
435 ERROR("no event listener, subscription impossible");
440 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
442 struct afb_xreq *xreq = from_request(closure);
443 return afb_xreq_unsubscribe(xreq, event);
446 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
449 return afb_evt_remove_watch(xreq->listener, event);
450 if (xreq->queryitf->unsubscribe)
451 return xreq->queryitf->unsubscribe(xreq, event);
452 ERROR("no event listener, unsubscription impossible");
457 static void xreq_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
459 struct afb_xreq *xreq = from_request(closure);
462 sc = subcall_alloc(xreq, api, verb, args);
465 callback(cb_closure, 1, afb_msg_json_internal_error());
466 json_object_put(args);
468 subcall(sc, callback, cb_closure);
472 static void xreq_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
474 struct afb_xreq *xreq = from_request(closure);
477 sc = subcall_alloc(xreq, api, verb, args);
480 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
481 json_object_put(args);
483 subcall_req(sc, callback, cb_closure);
488 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
492 struct afb_xreq *xreq = from_request(closure);
493 struct json_object *resu;
495 sc = subcall_alloc(xreq, api, verb, args);
498 resu = afb_msg_json_internal_error();
499 json_object_put(args);
501 rc = subcallsync(sc, &resu);
506 json_object_put(resu);
510 static void xreq_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
513 struct afb_xreq *xreq = from_request(closure);
515 if (!fmt || vasprintf(&p, fmt, args) < 0)
516 vverbose(level, file, line, func, fmt, args);
518 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
523 static struct afb_stored_req *xreq_store_cb(struct afb_request *closure)
525 xreq_addref_cb(closure);
526 return (struct afb_stored_req*)closure;
529 static int xreq_has_permission_cb(struct afb_request *closure, const char *permission)
531 struct afb_xreq *xreq = from_request(closure);
532 return afb_auth_has_permission(xreq, permission);
535 static char *xreq_get_application_id_cb(struct afb_request *closure)
537 struct afb_xreq *xreq = from_request(closure);
538 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
541 static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
543 struct afb_xreq *xreq = from_request(closure);
544 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
547 /******************************************************************************/
549 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
551 struct json_object *r = xreq_json_cb(closure);
552 struct afb_xreq *xreq = from_request(closure);
553 return afb_hook_xreq_json(xreq, r);
556 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
558 struct afb_arg r = xreq_get_cb(closure, name);
559 struct afb_xreq *xreq = from_request(closure);
560 return afb_hook_xreq_get(xreq, name, r);
563 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
565 struct afb_xreq *xreq = from_request(closure);
566 afb_hook_xreq_success(xreq, obj, info);
567 xreq_success_cb(closure, obj, info);
570 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
572 struct afb_xreq *xreq = from_request(closure);
573 afb_hook_xreq_fail(xreq, status, info);
574 xreq_fail_cb(closure, status, info);
577 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
579 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
582 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
584 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
587 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
589 void *r = xreq_context_get_cb(closure);
590 struct afb_xreq *xreq = from_request(closure);
591 return afb_hook_xreq_context_get(xreq, r);
594 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
596 struct afb_xreq *xreq = from_request(closure);
597 afb_hook_xreq_context_set(xreq, value, free_value);
598 xreq_context_set_cb(closure, value, free_value);
601 static void xreq_hooked_addref_cb(struct afb_request *closure)
603 struct afb_xreq *xreq = from_request(closure);
604 afb_hook_xreq_addref(xreq);
605 xreq_addref_cb(closure);
608 static void xreq_hooked_unref_cb(struct afb_request *closure)
610 struct afb_xreq *xreq = from_request(closure);
611 afb_hook_xreq_unref(xreq);
612 xreq_unref_cb(closure);
615 static void xreq_hooked_session_close_cb(struct afb_request *closure)
617 struct afb_xreq *xreq = from_request(closure);
618 afb_hook_xreq_session_close(xreq);
619 xreq_session_close_cb(closure);
622 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
624 int r = xreq_session_set_LOA_cb(closure, level);
625 struct afb_xreq *xreq = from_request(closure);
626 return afb_hook_xreq_session_set_LOA(xreq, level, r);
629 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
631 int r = xreq_subscribe_cb(closure, event);
632 struct afb_xreq *xreq = from_request(closure);
633 return afb_hook_xreq_subscribe(xreq, event, r);
636 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
638 int r = xreq_unsubscribe_cb(closure, event);
639 struct afb_xreq *xreq = from_request(closure);
640 return afb_hook_xreq_unsubscribe(xreq, event, r);
643 static void xreq_hooked_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
645 struct afb_xreq *xreq = from_request(closure);
648 afb_hook_xreq_subcall(xreq, api, verb, args);
649 sc = subcall_alloc(xreq, api, verb, args);
652 callback(cb_closure, 1, afb_msg_json_internal_error());
653 json_object_put(args);
655 subcall_hooked(sc, callback, cb_closure);
659 static void xreq_hooked_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
661 struct afb_xreq *xreq = from_request(closure);
664 afb_hook_xreq_subcall_req(xreq, api, verb, args);
665 sc = subcall_alloc(xreq, api, verb, args);
668 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
669 json_object_put(args);
671 subcall_req_hooked(sc, callback, cb_closure);
675 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
678 struct afb_xreq *xreq = from_request(closure);
679 afb_hook_xreq_subcallsync(xreq, api, verb, args);
680 r = xreq_subcallsync_cb(closure, api, verb, args, result);
681 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
684 static void xreq_hooked_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
686 struct afb_xreq *xreq = from_request(closure);
689 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
690 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
694 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
696 struct afb_xreq *xreq = from_request(closure);
697 struct afb_stored_req *r = xreq_store_cb(closure);
698 afb_hook_xreq_store(xreq, r);
702 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
704 struct afb_xreq *xreq = from_request(closure);
705 int r = xreq_has_permission_cb(closure, permission);
706 return afb_hook_xreq_has_permission(xreq, permission, r);
709 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
711 struct afb_xreq *xreq = from_request(closure);
712 char *r = xreq_get_application_id_cb(closure);
713 return afb_hook_xreq_get_application_id(xreq, r);
716 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
718 struct afb_xreq *xreq = from_request(closure);
719 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
720 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
723 /******************************************************************************/
725 const struct afb_request_itf xreq_itf = {
726 .json = xreq_json_cb,
728 .success = xreq_success_cb,
729 .fail = xreq_fail_cb,
730 .vsuccess = xreq_vsuccess_cb,
731 .vfail = xreq_vfail_cb,
732 .context_get = xreq_context_get_cb,
733 .context_set = xreq_context_set_cb,
734 .addref = xreq_addref_cb,
735 .unref = xreq_unref_cb,
736 .session_close = xreq_session_close_cb,
737 .session_set_LOA = xreq_session_set_LOA_cb,
738 .subscribe = xreq_subscribe_cb,
739 .unsubscribe = xreq_unsubscribe_cb,
740 .subcall = xreq_subcall_cb,
741 .subcallsync = xreq_subcallsync_cb,
742 .vverbose = xreq_vverbose_cb,
743 .store = xreq_store_cb,
744 .subcall_req = xreq_subcall_req_cb,
745 .has_permission = xreq_has_permission_cb,
746 .get_application_id = xreq_get_application_id_cb,
747 .context_make = xreq_context_make_cb
750 const struct afb_request_itf xreq_hooked_itf = {
751 .json = xreq_hooked_json_cb,
752 .get = xreq_hooked_get_cb,
753 .success = xreq_hooked_success_cb,
754 .fail = xreq_hooked_fail_cb,
755 .vsuccess = xreq_hooked_vsuccess_cb,
756 .vfail = xreq_hooked_vfail_cb,
757 .context_get = xreq_hooked_context_get_cb,
758 .context_set = xreq_hooked_context_set_cb,
759 .addref = xreq_hooked_addref_cb,
760 .unref = xreq_hooked_unref_cb,
761 .session_close = xreq_hooked_session_close_cb,
762 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
763 .subscribe = xreq_hooked_subscribe_cb,
764 .unsubscribe = xreq_hooked_unsubscribe_cb,
765 .subcall = xreq_hooked_subcall_cb,
766 .subcallsync = xreq_hooked_subcallsync_cb,
767 .vverbose = xreq_hooked_vverbose_cb,
768 .store = xreq_hooked_store_cb,
769 .subcall_req = xreq_hooked_subcall_req_cb,
770 .has_permission = xreq_hooked_has_permission_cb,
771 .get_application_id = xreq_hooked_get_application_id_cb,
772 .context_make = xreq_hooked_context_make_cb
775 /******************************************************************************/
777 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
779 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
781 afb_hook_xreq_unstore(xreq);
785 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
787 return afb_request_json(to_request(xreq));
790 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
792 afb_request_success(to_request(xreq), obj, info);
795 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
799 va_start(args, info);
800 afb_request_success_v(to_request(xreq), obj, info, args);
804 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
806 afb_request_fail(to_request(xreq), status, info);
809 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
813 va_start(args, info);
814 afb_request_fail_v(to_request(xreq), status, info, args);
819 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
821 struct json_object *obj = xreq_json_cb(to_request(xreq));
822 const char *result = json_object_to_json_string(obj);
824 *size = strlen(result);
828 void afb_xreq_addref(struct afb_xreq *xreq)
830 afb_request_addref(to_request(xreq));
833 void afb_xreq_unref(struct afb_xreq *xreq)
835 afb_request_unref(to_request(xreq));
838 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)
840 xreq_subcall_cb(to_request(xreq), api, verb, args, callback, cb_closure);
843 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)
845 afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure);
848 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
850 return xreq_subcallsync_cb(to_request(xreq), api, verb, args, result);
853 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
855 return afb_request_subcall_sync(to_request(xreq), api, verb, args, result);
858 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
862 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
863 if (!afb_context_check(&xreq->context)) {
864 afb_context_close(&xreq->context);
865 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
871 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
872 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
873 if (!afb_context_check_loa(&xreq->context, loa)) {
874 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
880 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
881 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
882 if (afb_context_check_loa(&xreq->context, loa + 1)) {
883 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
889 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
890 afb_context_refresh(&xreq->context);
892 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
893 afb_context_change_loa(&xreq->context, 0);
894 afb_context_close(&xreq->context);
900 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
904 if (sessionflags != 0) {
905 if (!afb_context_check(&xreq->context)) {
906 afb_context_close(&xreq->context);
907 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
913 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
914 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
915 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
920 if (auth && !afb_auth_check(xreq, auth)) {
921 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
926 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
927 afb_context_refresh(&xreq->context);
929 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
930 afb_context_close(&xreq->context);
936 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
939 afb_xreq_fail_unknown_verb(xreq);
941 if (!xreq_session_check_apply_v1(xreq, verb->session))
942 verb->callback(to_req(xreq));
945 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
948 afb_xreq_fail_unknown_verb(xreq);
950 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
951 verb->callback(to_req(xreq));
954 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
956 memset(xreq, 0, sizeof *xreq);
957 xreq->request.itf = &xreq_hooked_itf; /* hook by default */
959 xreq->queryitf = queryitf;
962 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
964 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
967 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
969 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
972 static void init_hooking(struct afb_xreq *xreq)
974 afb_hook_init_xreq(xreq);
976 afb_hook_xreq_begin(xreq);
978 xreq->request.itf = &xreq_itf; /* unhook the interface */
982 * job callback for asynchronous and secured processing of the request.
984 static void process_async(int signum, void *arg)
986 struct afb_xreq *xreq = arg;
987 const struct afb_api *api;
990 /* emit the error (assumes that hooking is initialised) */
991 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
995 /* invoke api call method to process the reqiest */
996 api = (const struct afb_api*)xreq->context.api_key;
997 api->itf->call(api->closure, xreq);
999 /* release the request */
1000 afb_xreq_unhooked_unref(xreq);
1004 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1005 * The early failure occurs only in function 'afb_xreq_process' where normally,
1006 * the hooking is not initialised. So this "early" failure takes care to initialise
1007 * the hooking in first.
1009 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1017 va_start(args, info);
1018 afb_request_fail_v(to_request(xreq), status, info, args);
1023 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1024 * Errors are reported as request failures.
1026 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1028 const struct afb_api *api;
1029 struct afb_xreq *caller;
1031 /* lookup at the api */
1032 xreq->apiset = apiset;
1033 api = afb_apiset_lookup_started(apiset, xreq->api, 1);
1035 if (errno == ENOENT)
1036 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
1038 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
1041 xreq->context.api_key = api;
1043 /* check self locking */
1045 caller = xreq->caller;
1047 if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1048 /* noconcurrency lock detected */
1049 ERROR("self-lock detected in call stack for API %s", xreq->api);
1050 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
1053 caller = caller->caller;
1057 /* queue the request job */
1058 afb_xreq_unhooked_addref(xreq);
1059 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1060 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1061 ERROR("can't process job with threads: %m");
1062 early_failure(xreq, "cancelled", "not able to create a job for the task");
1063 afb_xreq_unhooked_unref(xreq);
1066 afb_xreq_unhooked_unref(xreq);