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-api-dyn.h"
38 #include "afb-apiset.h"
43 /******************************************************************************/
45 static void xreq_finalize(struct afb_xreq *xreq)
48 afb_xreq_fail(xreq, "error", "no reply");
50 afb_hook_xreq_end(xreq);
52 afb_xreq_unhooked_unref(xreq->caller);
53 xreq->queryitf->unref(xreq);
56 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
58 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
61 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
63 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
67 /******************************************************************************/
69 static inline struct afb_req to_req(struct afb_xreq *xreq)
71 return (struct afb_req){ .itf = xreq->request.itf, .closure = &xreq->request };
74 /******************************************************************************/
80 void (*completion)(struct subcall*, int, struct json_object*);
84 struct jobloop *jobloop;
85 struct json_object *result;
90 void (*callback)(void*, int, struct json_object*);
91 void (*callback_req)(void*, int, struct json_object*, struct afb_req);
92 void (*callback_request)(void*, int, struct json_object*, struct afb_request*);
99 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
101 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
103 return afb_xreq_subscribe(subcall->xreq.caller, eventid);
106 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
108 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
110 return afb_xreq_unsubscribe(subcall->xreq.caller, eventid);
113 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
115 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
117 subcall->completion(subcall, status, result);
118 json_object_put(result);
119 afb_xreq_unhooked_unref(&subcall->xreq);
122 static void subcall_destroy_cb(struct afb_xreq *xreq)
124 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
126 json_object_put(subcall->xreq.json);
127 afb_cred_unref(subcall->xreq.cred);
131 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
132 .reply = subcall_reply_cb,
133 .unref = subcall_destroy_cb,
134 .subscribe = subcall_subscribe_cb,
135 .unsubscribe = subcall_unsubscribe_cb
138 static struct subcall *subcall_alloc(
139 struct afb_xreq *caller,
142 struct json_object *args
145 struct subcall *subcall;
146 size_t lenapi, lenverb;
149 lenapi = 1 + strlen(api);
150 lenverb = 1 + strlen(verb);
151 subcall = malloc(lenapi + lenverb + sizeof *subcall);
153 ERROR("out of memory");
155 copy = (char*)&subcall[1];
156 memcpy(copy, api, lenapi);
158 copy = ©[lenapi];
159 memcpy(copy, verb, lenverb);
162 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
163 afb_context_subinit(&subcall->xreq.context, &caller->context);
164 subcall->xreq.cred = afb_cred_addref(caller->cred);
165 subcall->xreq.json = args;
166 subcall->xreq.request.api = api;
167 subcall->xreq.request.verb = verb;
168 subcall->xreq.caller = caller;
169 afb_xreq_unhooked_addref(caller);
175 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
177 subcall->callback(subcall->closure, status, result);
180 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
182 subcall->callback_req(subcall->closure, status, result, to_req(subcall->xreq.caller));
185 static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result)
187 subcall->callback_request(subcall->closure, status, result, xreq_to_request(subcall->xreq.caller));
190 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
192 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
193 subcall_on_reply(subcall, status, result);
196 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
198 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
199 subcall_req_on_reply(subcall, status, result);
202 static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
204 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
205 subcall_request_on_reply(subcall, status, result);
208 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
210 struct afb_xreq *xreq = closure;
213 ERROR("subcall replied more than one time!!");
214 json_object_put(result);
217 subcall_reply_cb(xreq, status, result);
221 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
223 subcall->completion = completion;
224 if (subcall->xreq.caller->queryitf->subcall) {
225 subcall->xreq.caller->queryitf->subcall(
226 subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb,
227 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
229 afb_xreq_unhooked_addref(&subcall->xreq);
230 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
234 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
236 subcall->callback = callback;
237 subcall->closure = cb_closure;
238 subcall_process(subcall, subcall_on_reply);
241 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
243 subcall->callback_req = callback;
244 subcall->closure = cb_closure;
245 subcall_process(subcall, subcall_req_on_reply);
248 static void subcall_request(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
250 subcall->callback_request = callback;
251 subcall->closure = cb_closure;
252 subcall_process(subcall, subcall_request_on_reply);
255 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
257 subcall->callback = callback;
258 subcall->closure = cb_closure;
259 subcall_process(subcall, subcall_hooked_on_reply);
262 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
264 subcall->callback_req = callback;
265 subcall->closure = cb_closure;
266 subcall_process(subcall, subcall_req_hooked_on_reply);
269 static void subcall_request_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
271 subcall->callback_request = callback;
272 subcall->closure = cb_closure;
273 subcall_process(subcall, subcall_request_hooked_on_reply);
276 static void subcall_sync_leave(struct subcall *subcall)
278 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
283 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
285 subcall->status = status;
286 subcall->result = json_object_get(result);
287 subcall_sync_leave(subcall);
290 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
292 struct subcall *subcall = closure;
295 subcall->jobloop = jobloop;
296 subcall->result = NULL;
298 subcall_process(subcall, subcall_sync_reply);
300 subcall->status = -1;
301 subcall_sync_leave(subcall);
305 static int subcallsync(struct subcall *subcall, struct json_object **result)
309 afb_xreq_unhooked_addref(&subcall->xreq);
310 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
311 *result = subcall->result;
312 if (rc < 0 || subcall->status < 0) {
313 *result = *result ?: afb_msg_json_internal_error();
316 afb_xreq_unhooked_unref(&subcall->xreq);
320 /******************************************************************************/
322 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
325 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
327 fun(first, second, info);
331 /******************************************************************************/
333 static struct json_object *xreq_json_cb(struct afb_request *closure)
335 struct afb_xreq *xreq = xreq_from_request(closure);
336 if (!xreq->json && xreq->queryitf->json)
337 xreq->json = xreq->queryitf->json(xreq);
341 static struct afb_arg xreq_get_cb(struct afb_request *closure, const char *name)
343 struct afb_xreq *xreq = xreq_from_request(closure);
345 struct json_object *object, *value;
347 if (xreq->queryitf->get)
348 arg = xreq->queryitf->get(xreq, name);
350 object = xreq_json_cb(closure);
351 if (json_object_object_get_ex(object, name, &value)) {
353 arg.value = json_object_get_string(value);
363 static void xreq_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
365 struct afb_xreq *xreq = xreq_from_request(closure);
368 ERROR("reply called more than one time!!");
369 json_object_put(obj);
372 if (xreq->queryitf->success)
373 xreq->queryitf->success(xreq, obj, info);
375 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
379 static void xreq_fail_cb(struct afb_request *closure, const char *status, const char *info)
381 struct afb_xreq *xreq = xreq_from_request(closure);
384 ERROR("reply called more than one time!!");
387 if (xreq->queryitf->fail)
388 xreq->queryitf->fail(xreq, status, info);
390 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
394 static void xreq_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
396 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
399 static void xreq_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
401 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
404 static void *xreq_context_get_cb(struct afb_request *closure)
406 struct afb_xreq *xreq = xreq_from_request(closure);
407 return afb_context_get(&xreq->context);
410 static void xreq_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
412 struct afb_xreq *xreq = xreq_from_request(closure);
413 afb_context_set(&xreq->context, value, free_value);
416 static struct afb_request *xreq_addref_cb(struct afb_request *closure)
418 struct afb_xreq *xreq = xreq_from_request(closure);
419 afb_xreq_unhooked_addref(xreq);
423 static void xreq_unref_cb(struct afb_request *closure)
425 struct afb_xreq *xreq = xreq_from_request(closure);
426 afb_xreq_unhooked_unref(xreq);
429 static void xreq_session_close_cb(struct afb_request *closure)
431 struct afb_xreq *xreq = xreq_from_request(closure);
432 afb_context_close(&xreq->context);
435 static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
437 struct afb_xreq *xreq = xreq_from_request(closure);
438 return afb_context_change_loa(&xreq->context, level);
441 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
442 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
444 return xreq_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
447 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
449 struct afb_xreq *xreq = xreq_from_request(closure);
450 return afb_xreq_subscribe(xreq, eventid);
453 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
456 return afb_evt_eventid_add_watch(xreq->listener, eventid);
457 if (xreq->queryitf->subscribe)
458 return xreq->queryitf->subscribe(xreq, eventid);
459 ERROR("no event listener, subscription impossible");
464 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
465 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
467 return xreq_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
470 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
472 struct afb_xreq *xreq = xreq_from_request(closure);
473 return afb_xreq_unsubscribe(xreq, eventid);
476 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
479 return afb_evt_eventid_remove_watch(xreq->listener, eventid);
480 if (xreq->queryitf->unsubscribe)
481 return xreq->queryitf->unsubscribe(xreq, eventid);
482 ERROR("no event listener, unsubscription impossible");
487 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)
489 struct afb_xreq *xreq = xreq_from_request(closure);
492 sc = subcall_alloc(xreq, api, verb, args);
495 callback(cb_closure, 1, afb_msg_json_internal_error());
496 json_object_put(args);
498 subcall(sc, callback, cb_closure);
502 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)
504 struct afb_xreq *xreq = xreq_from_request(closure);
507 sc = subcall_alloc(xreq, api, verb, args);
510 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
511 json_object_put(args);
513 subcall_req(sc, callback, cb_closure);
517 static void xreq_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
519 struct afb_xreq *xreq = xreq_from_request(closure);
522 sc = subcall_alloc(xreq, api, verb, args);
525 callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_request(xreq));
526 json_object_put(args);
528 subcall_request(sc, callback, cb_closure);
533 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
537 struct afb_xreq *xreq = xreq_from_request(closure);
538 struct json_object *resu;
540 sc = subcall_alloc(xreq, api, verb, args);
543 resu = afb_msg_json_internal_error();
544 json_object_put(args);
546 rc = subcallsync(sc, &resu);
551 json_object_put(resu);
555 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)
558 struct afb_xreq *xreq = xreq_from_request(closure);
560 if (!fmt || vasprintf(&p, fmt, args) < 0)
561 vverbose(level, file, line, func, fmt, args);
563 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p);
568 static struct afb_stored_req *xreq_store_cb(struct afb_request *closure)
570 xreq_addref_cb(closure);
571 return (struct afb_stored_req*)closure;
574 static int xreq_has_permission_cb(struct afb_request *closure, const char *permission)
576 struct afb_xreq *xreq = xreq_from_request(closure);
577 return afb_auth_has_permission(xreq, permission);
580 static char *xreq_get_application_id_cb(struct afb_request *closure)
582 struct afb_xreq *xreq = xreq_from_request(closure);
583 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
586 static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
588 struct afb_xreq *xreq = xreq_from_request(closure);
589 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
592 static int xreq_get_uid_cb(struct afb_request *closure)
594 struct afb_xreq *xreq = xreq_from_request(closure);
595 return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
598 /******************************************************************************/
600 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
602 struct json_object *r = xreq_json_cb(closure);
603 struct afb_xreq *xreq = xreq_from_request(closure);
604 return afb_hook_xreq_json(xreq, r);
607 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
609 struct afb_arg r = xreq_get_cb(closure, name);
610 struct afb_xreq *xreq = xreq_from_request(closure);
611 return afb_hook_xreq_get(xreq, name, r);
614 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
616 struct afb_xreq *xreq = xreq_from_request(closure);
617 afb_hook_xreq_success(xreq, obj, info);
618 xreq_success_cb(closure, obj, info);
621 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
623 struct afb_xreq *xreq = xreq_from_request(closure);
624 afb_hook_xreq_fail(xreq, status, info);
625 xreq_fail_cb(closure, status, info);
628 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
630 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
633 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
635 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
638 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
640 void *r = xreq_context_get_cb(closure);
641 struct afb_xreq *xreq = xreq_from_request(closure);
642 return afb_hook_xreq_context_get(xreq, r);
645 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
647 struct afb_xreq *xreq = xreq_from_request(closure);
648 afb_hook_xreq_context_set(xreq, value, free_value);
649 xreq_context_set_cb(closure, value, free_value);
652 static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
654 struct afb_xreq *xreq = xreq_from_request(closure);
655 afb_hook_xreq_addref(xreq);
656 return xreq_addref_cb(closure);
659 static void xreq_hooked_unref_cb(struct afb_request *closure)
661 struct afb_xreq *xreq = xreq_from_request(closure);
662 afb_hook_xreq_unref(xreq);
663 xreq_unref_cb(closure);
666 static void xreq_hooked_session_close_cb(struct afb_request *closure)
668 struct afb_xreq *xreq = xreq_from_request(closure);
669 afb_hook_xreq_session_close(xreq);
670 xreq_session_close_cb(closure);
673 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
675 int r = xreq_session_set_LOA_cb(closure, level);
676 struct afb_xreq *xreq = xreq_from_request(closure);
677 return afb_hook_xreq_session_set_LOA(xreq, level, r);
680 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
681 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
683 return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
686 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
688 int r = xreq_subscribe_eventid_cb(closure, eventid);
689 struct afb_xreq *xreq = xreq_from_request(closure);
690 return afb_hook_xreq_subscribe(xreq, eventid, r);
693 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
694 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
696 return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
699 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
701 int r = xreq_unsubscribe_eventid_cb(closure, eventid);
702 struct afb_xreq *xreq = xreq_from_request(closure);
703 return afb_hook_xreq_unsubscribe(xreq, eventid, r);
706 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)
708 struct afb_xreq *xreq = xreq_from_request(closure);
711 afb_hook_xreq_subcall(xreq, api, verb, args);
712 sc = subcall_alloc(xreq, api, verb, args);
715 callback(cb_closure, 1, afb_msg_json_internal_error());
716 json_object_put(args);
718 subcall_hooked(sc, callback, cb_closure);
722 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)
724 struct afb_xreq *xreq = xreq_from_request(closure);
727 afb_hook_xreq_subcall_req(xreq, api, verb, args);
728 sc = subcall_alloc(xreq, api, verb, args);
731 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
732 json_object_put(args);
734 subcall_req_hooked(sc, callback, cb_closure);
738 static void xreq_hooked_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
740 struct afb_xreq *xreq = xreq_from_request(closure);
743 afb_hook_xreq_subcall(xreq, api, verb, args);
744 sc = subcall_alloc(xreq, api, verb, args);
747 callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_request(xreq));
748 json_object_put(args);
750 subcall_request_hooked(sc, callback, cb_closure);
754 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
757 struct afb_xreq *xreq = xreq_from_request(closure);
758 afb_hook_xreq_subcallsync(xreq, api, verb, args);
759 r = xreq_subcallsync_cb(closure, api, verb, args, result);
760 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
763 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)
765 struct afb_xreq *xreq = xreq_from_request(closure);
768 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
769 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
773 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
775 struct afb_xreq *xreq = xreq_from_request(closure);
776 struct afb_stored_req *r = xreq_store_cb(closure);
777 afb_hook_xreq_store(xreq, r);
781 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
783 struct afb_xreq *xreq = xreq_from_request(closure);
784 int r = xreq_has_permission_cb(closure, permission);
785 return afb_hook_xreq_has_permission(xreq, permission, r);
788 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
790 struct afb_xreq *xreq = xreq_from_request(closure);
791 char *r = xreq_get_application_id_cb(closure);
792 return afb_hook_xreq_get_application_id(xreq, r);
795 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
797 struct afb_xreq *xreq = xreq_from_request(closure);
798 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
799 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
802 static int xreq_hooked_get_uid_cb(struct afb_request *closure)
804 struct afb_xreq *xreq = xreq_from_request(closure);
805 int r = xreq_get_uid_cb(closure);
806 return afb_hook_xreq_get_uid(xreq, r);
809 /******************************************************************************/
811 const struct afb_request_itf xreq_itf = {
812 .json = xreq_json_cb,
814 .success = xreq_success_cb,
815 .fail = xreq_fail_cb,
816 .vsuccess = xreq_vsuccess_cb,
817 .vfail = xreq_vfail_cb,
818 .context_get = xreq_context_get_cb,
819 .context_set = xreq_context_set_cb,
820 .addref = xreq_addref_cb,
821 .unref = xreq_unref_cb,
822 .session_close = xreq_session_close_cb,
823 .session_set_LOA = xreq_session_set_LOA_cb,
824 .subscribe = xreq_subscribe_cb,
825 .unsubscribe = xreq_unsubscribe_cb,
826 .subcall = xreq_subcall_cb,
827 .subcallsync = xreq_subcallsync_cb,
828 .vverbose = xreq_vverbose_cb,
829 .store = xreq_store_cb,
830 .subcall_req = xreq_subcall_req_cb,
831 .has_permission = xreq_has_permission_cb,
832 .get_application_id = xreq_get_application_id_cb,
833 .context_make = xreq_context_make_cb,
834 .subscribe_eventid = xreq_subscribe_eventid_cb,
835 .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
836 .subcall_request = xreq_subcall_request_cb,
837 .get_uid = xreq_get_uid_cb,
840 const struct afb_request_itf xreq_hooked_itf = {
841 .json = xreq_hooked_json_cb,
842 .get = xreq_hooked_get_cb,
843 .success = xreq_hooked_success_cb,
844 .fail = xreq_hooked_fail_cb,
845 .vsuccess = xreq_hooked_vsuccess_cb,
846 .vfail = xreq_hooked_vfail_cb,
847 .context_get = xreq_hooked_context_get_cb,
848 .context_set = xreq_hooked_context_set_cb,
849 .addref = xreq_hooked_addref_cb,
850 .unref = xreq_hooked_unref_cb,
851 .session_close = xreq_hooked_session_close_cb,
852 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
853 .subscribe = xreq_hooked_subscribe_cb,
854 .unsubscribe = xreq_hooked_unsubscribe_cb,
855 .subcall = xreq_hooked_subcall_cb,
856 .subcallsync = xreq_hooked_subcallsync_cb,
857 .vverbose = xreq_hooked_vverbose_cb,
858 .store = xreq_hooked_store_cb,
859 .subcall_req = xreq_hooked_subcall_req_cb,
860 .has_permission = xreq_hooked_has_permission_cb,
861 .get_application_id = xreq_hooked_get_application_id_cb,
862 .context_make = xreq_hooked_context_make_cb,
863 .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
864 .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
865 .subcall_request = xreq_hooked_subcall_request_cb,
866 .get_uid = xreq_hooked_get_uid_cb,
869 /******************************************************************************/
871 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
873 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
875 afb_hook_xreq_unstore(xreq);
879 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
881 return afb_request_json(xreq_to_request(xreq));
884 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
886 afb_request_success(xreq_to_request(xreq), obj, info);
889 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
893 va_start(args, info);
894 afb_request_success_v(xreq_to_request(xreq), obj, info, args);
898 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
900 afb_request_fail(xreq_to_request(xreq), status, info);
903 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
907 va_start(args, info);
908 afb_request_fail_v(xreq_to_request(xreq), status, info, args);
913 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
915 struct json_object *obj = xreq_json_cb(xreq_to_request(xreq));
916 const char *result = json_object_to_json_string(obj);
918 *size = strlen(result);
922 void afb_xreq_addref(struct afb_xreq *xreq)
924 afb_request_addref(xreq_to_request(xreq));
927 void afb_xreq_unref(struct afb_xreq *xreq)
929 afb_request_unref(xreq_to_request(xreq));
932 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*, struct afb_request *), void *cb_closure)
934 xreq_subcall_request_cb(xreq_to_request(xreq), api, verb, args, callback, cb_closure);
937 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*, struct afb_request *), void *cb_closure)
939 afb_request_subcall(xreq_to_request(xreq), api, verb, args, callback, cb_closure);
942 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
944 return xreq_subcallsync_cb(xreq_to_request(xreq), api, verb, args, result);
947 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
949 return afb_request_subcall_sync(xreq_to_request(xreq), api, verb, args, result);
952 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
956 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
957 if (!afb_context_check(&xreq->context)) {
958 afb_context_close(&xreq->context);
959 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
965 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
966 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
967 if (!afb_context_check_loa(&xreq->context, loa)) {
968 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
974 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
975 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
976 if (afb_context_check_loa(&xreq->context, loa + 1)) {
977 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
983 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
984 afb_context_refresh(&xreq->context);
986 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
987 afb_context_change_loa(&xreq->context, 0);
988 afb_context_close(&xreq->context);
994 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
998 if (sessionflags != 0) {
999 if (!afb_context_check(&xreq->context)) {
1000 afb_context_close(&xreq->context);
1001 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
1007 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
1008 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
1009 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
1014 if (auth && !afb_auth_check(xreq, auth)) {
1015 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
1020 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
1021 afb_context_refresh(&xreq->context);
1023 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
1024 afb_context_close(&xreq->context);
1030 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
1033 afb_xreq_fail_unknown_verb(xreq);
1035 if (!xreq_session_check_apply_v1(xreq, verb->session))
1036 verb->callback(to_req(xreq));
1039 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
1042 afb_xreq_fail_unknown_verb(xreq);
1044 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
1045 verb->callback(to_req(xreq));
1048 void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb)
1051 afb_xreq_fail_unknown_verb(xreq);
1053 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
1054 verb->callback(xreq_to_request(xreq));
1057 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
1059 memset(xreq, 0, sizeof *xreq);
1060 xreq->request.itf = &xreq_hooked_itf; /* hook by default */
1062 xreq->queryitf = queryitf;
1065 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
1067 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1070 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
1072 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
1075 static void init_hooking(struct afb_xreq *xreq)
1077 afb_hook_init_xreq(xreq);
1078 if (xreq->hookflags)
1079 afb_hook_xreq_begin(xreq);
1081 xreq->request.itf = &xreq_itf; /* unhook the interface */
1085 * job callback for asynchronous and secured processing of the request.
1087 static void process_async(int signum, void *arg)
1089 struct afb_xreq *xreq = arg;
1090 const struct afb_api *api;
1093 /* emit the error (assumes that hooking is initialised) */
1094 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
1098 /* invoke api call method to process the reqiest */
1099 api = (const struct afb_api*)xreq->context.api_key;
1100 api->itf->call(api->closure, xreq);
1102 /* release the request */
1103 afb_xreq_unhooked_unref(xreq);
1107 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1108 * The early failure occurs only in function 'afb_xreq_process' where normally,
1109 * the hooking is not initialised. So this "early" failure takes care to initialise
1110 * the hooking in first.
1112 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1120 va_start(args, info);
1121 afb_request_fail_v(xreq_to_request(xreq), status, info, args);
1126 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1127 * Errors are reported as request failures.
1129 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1131 const struct afb_api *api;
1132 struct afb_xreq *caller;
1134 /* lookup at the api */
1135 xreq->apiset = apiset;
1136 api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
1138 if (errno == ENOENT)
1139 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1141 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
1144 xreq->context.api_key = api;
1146 /* check self locking */
1148 caller = xreq->caller;
1150 if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1151 /* noconcurrency lock detected */
1152 ERROR("self-lock detected in call stack for API %s", xreq->request.api);
1153 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
1156 caller = caller->caller;
1160 /* queue the request job */
1161 afb_xreq_unhooked_addref(xreq);
1162 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1163 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1164 ERROR("can't process job with threads: %m");
1165 early_failure(xreq, "cancelled", "not able to create a job for the task");
1166 afb_xreq_unhooked_unref(xreq);
1169 afb_xreq_unhooked_unref(xreq);