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_request *to_request(struct afb_xreq *xreq)
71 return &xreq->request;
74 static inline struct afb_req to_req(struct afb_xreq *xreq)
76 return (struct afb_req){ .itf = xreq->request.itf, .closure = &xreq->request };
79 static inline struct afb_xreq *from_request(struct afb_request *request)
81 return CONTAINER_OF(struct afb_xreq, request, request);
84 /******************************************************************************/
90 void (*completion)(struct subcall*, int, struct json_object*);
94 struct jobloop *jobloop;
95 struct json_object *result;
100 void (*callback)(void*, int, struct json_object*);
101 void (*callback_req)(void*, int, struct json_object*, struct afb_req);
102 void (*callback_request)(void*, int, struct json_object*, struct afb_request*);
109 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
111 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
113 return afb_xreq_subscribe(subcall->xreq.caller, eventid);
116 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
118 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
120 return afb_xreq_unsubscribe(subcall->xreq.caller, eventid);
123 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
125 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
127 subcall->completion(subcall, status, result);
128 json_object_put(result);
129 afb_xreq_unhooked_unref(&subcall->xreq);
132 static void subcall_destroy_cb(struct afb_xreq *xreq)
134 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
136 json_object_put(subcall->xreq.json);
137 afb_cred_unref(subcall->xreq.cred);
141 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
142 .reply = subcall_reply_cb,
143 .unref = subcall_destroy_cb,
144 .subscribe = subcall_subscribe_cb,
145 .unsubscribe = subcall_unsubscribe_cb
148 static struct subcall *subcall_alloc(
149 struct afb_xreq *caller,
152 struct json_object *args
155 struct subcall *subcall;
156 size_t lenapi, lenverb;
159 lenapi = 1 + strlen(api);
160 lenverb = 1 + strlen(verb);
161 subcall = malloc(lenapi + lenverb + sizeof *subcall);
163 ERROR("out of memory");
165 copy = (char*)&subcall[1];
166 memcpy(copy, api, lenapi);
168 copy = ©[lenapi];
169 memcpy(copy, verb, lenverb);
172 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
173 afb_context_subinit(&subcall->xreq.context, &caller->context);
174 subcall->xreq.cred = afb_cred_addref(caller->cred);
175 subcall->xreq.json = args;
176 subcall->xreq.request.api = api;
177 subcall->xreq.request.verb = verb;
178 subcall->xreq.caller = caller;
179 afb_xreq_unhooked_addref(caller);
185 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
187 subcall->callback(subcall->closure, status, result);
190 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
192 subcall->callback_req(subcall->closure, status, result, to_req(subcall->xreq.caller));
195 static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result)
197 subcall->callback_request(subcall->closure, status, result, to_request(subcall->xreq.caller));
200 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
202 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
203 subcall_on_reply(subcall, status, result);
206 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
208 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
209 subcall_req_on_reply(subcall, status, result);
212 static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
214 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
215 subcall_request_on_reply(subcall, status, result);
218 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
220 struct afb_xreq *xreq = closure;
223 ERROR("subcall replied more than one time!!");
224 json_object_put(result);
227 subcall_reply_cb(xreq, status, result);
231 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
233 subcall->completion = completion;
234 if (subcall->xreq.caller->queryitf->subcall) {
235 subcall->xreq.caller->queryitf->subcall(
236 subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb,
237 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
239 afb_xreq_unhooked_addref(&subcall->xreq);
240 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
244 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
246 subcall->callback = callback;
247 subcall->closure = cb_closure;
248 subcall_process(subcall, subcall_on_reply);
251 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
253 subcall->callback_req = callback;
254 subcall->closure = cb_closure;
255 subcall_process(subcall, subcall_req_on_reply);
258 static void subcall_request(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
260 subcall->callback_request = callback;
261 subcall->closure = cb_closure;
262 subcall_process(subcall, subcall_request_on_reply);
265 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
267 subcall->callback = callback;
268 subcall->closure = cb_closure;
269 subcall_process(subcall, subcall_hooked_on_reply);
272 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
274 subcall->callback_req = callback;
275 subcall->closure = cb_closure;
276 subcall_process(subcall, subcall_req_hooked_on_reply);
279 static void subcall_request_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
281 subcall->callback_request = callback;
282 subcall->closure = cb_closure;
283 subcall_process(subcall, subcall_request_hooked_on_reply);
286 static void subcall_sync_leave(struct subcall *subcall)
288 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
293 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
295 subcall->status = status;
296 subcall->result = json_object_get(result);
297 subcall_sync_leave(subcall);
300 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
302 struct subcall *subcall = closure;
305 subcall->jobloop = jobloop;
306 subcall->result = NULL;
308 subcall_process(subcall, subcall_sync_reply);
310 subcall->status = -1;
311 subcall_sync_leave(subcall);
315 static int subcallsync(struct subcall *subcall, struct json_object **result)
319 afb_xreq_unhooked_addref(&subcall->xreq);
320 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
321 *result = subcall->result;
322 if (rc < 0 || subcall->status < 0) {
323 *result = *result ?: afb_msg_json_internal_error();
326 afb_xreq_unhooked_unref(&subcall->xreq);
330 /******************************************************************************/
332 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
335 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
337 fun(first, second, info);
341 /******************************************************************************/
343 static struct json_object *xreq_json_cb(struct afb_request *closure)
345 struct afb_xreq *xreq = from_request(closure);
346 if (!xreq->json && xreq->queryitf->json)
347 xreq->json = xreq->queryitf->json(xreq);
351 static struct afb_arg xreq_get_cb(struct afb_request *closure, const char *name)
353 struct afb_xreq *xreq = from_request(closure);
355 struct json_object *object, *value;
357 if (xreq->queryitf->get)
358 arg = xreq->queryitf->get(xreq, name);
360 object = xreq_json_cb(closure);
361 if (json_object_object_get_ex(object, name, &value)) {
363 arg.value = json_object_get_string(value);
373 static void xreq_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
375 struct afb_xreq *xreq = from_request(closure);
378 ERROR("reply called more than one time!!");
379 json_object_put(obj);
382 if (xreq->queryitf->success)
383 xreq->queryitf->success(xreq, obj, info);
385 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
389 static void xreq_fail_cb(struct afb_request *closure, const char *status, const char *info)
391 struct afb_xreq *xreq = from_request(closure);
394 ERROR("reply called more than one time!!");
397 if (xreq->queryitf->fail)
398 xreq->queryitf->fail(xreq, status, info);
400 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
404 static void xreq_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
406 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
409 static void xreq_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
411 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
414 static void *xreq_context_get_cb(struct afb_request *closure)
416 struct afb_xreq *xreq = from_request(closure);
417 return afb_context_get(&xreq->context);
420 static void xreq_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
422 struct afb_xreq *xreq = from_request(closure);
423 afb_context_set(&xreq->context, value, free_value);
426 static struct afb_request *xreq_addref_cb(struct afb_request *closure)
428 struct afb_xreq *xreq = from_request(closure);
429 afb_xreq_unhooked_addref(xreq);
433 static void xreq_unref_cb(struct afb_request *closure)
435 struct afb_xreq *xreq = from_request(closure);
436 afb_xreq_unhooked_unref(xreq);
439 static void xreq_session_close_cb(struct afb_request *closure)
441 struct afb_xreq *xreq = from_request(closure);
442 afb_context_close(&xreq->context);
445 static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
447 struct afb_xreq *xreq = from_request(closure);
448 return afb_context_change_loa(&xreq->context, level);
451 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
452 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
454 return xreq_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
457 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
459 struct afb_xreq *xreq = from_request(closure);
460 return afb_xreq_subscribe(xreq, eventid);
463 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
466 return afb_evt_eventid_add_watch(xreq->listener, eventid);
467 if (xreq->queryitf->subscribe)
468 return xreq->queryitf->subscribe(xreq, eventid);
469 ERROR("no event listener, subscription impossible");
474 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
475 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
477 return xreq_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
480 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
482 struct afb_xreq *xreq = from_request(closure);
483 return afb_xreq_unsubscribe(xreq, eventid);
486 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
489 return afb_evt_eventid_remove_watch(xreq->listener, eventid);
490 if (xreq->queryitf->unsubscribe)
491 return xreq->queryitf->unsubscribe(xreq, eventid);
492 ERROR("no event listener, unsubscription impossible");
497 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)
499 struct afb_xreq *xreq = from_request(closure);
502 sc = subcall_alloc(xreq, api, verb, args);
505 callback(cb_closure, 1, afb_msg_json_internal_error());
506 json_object_put(args);
508 subcall(sc, callback, cb_closure);
512 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)
514 struct afb_xreq *xreq = from_request(closure);
517 sc = subcall_alloc(xreq, api, verb, args);
520 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
521 json_object_put(args);
523 subcall_req(sc, callback, cb_closure);
527 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)
529 struct afb_xreq *xreq = from_request(closure);
532 sc = subcall_alloc(xreq, api, verb, args);
535 callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq));
536 json_object_put(args);
538 subcall_request(sc, callback, cb_closure);
543 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
547 struct afb_xreq *xreq = from_request(closure);
548 struct json_object *resu;
550 sc = subcall_alloc(xreq, api, verb, args);
553 resu = afb_msg_json_internal_error();
554 json_object_put(args);
556 rc = subcallsync(sc, &resu);
561 json_object_put(resu);
565 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)
568 struct afb_xreq *xreq = from_request(closure);
570 if (!fmt || vasprintf(&p, fmt, args) < 0)
571 vverbose(level, file, line, func, fmt, args);
573 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p);
578 static struct afb_stored_req *xreq_store_cb(struct afb_request *closure)
580 xreq_addref_cb(closure);
581 return (struct afb_stored_req*)closure;
584 static int xreq_has_permission_cb(struct afb_request *closure, const char *permission)
586 struct afb_xreq *xreq = from_request(closure);
587 return afb_auth_has_permission(xreq, permission);
590 static char *xreq_get_application_id_cb(struct afb_request *closure)
592 struct afb_xreq *xreq = from_request(closure);
593 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
596 static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
598 struct afb_xreq *xreq = from_request(closure);
599 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
602 /******************************************************************************/
604 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
606 struct json_object *r = xreq_json_cb(closure);
607 struct afb_xreq *xreq = from_request(closure);
608 return afb_hook_xreq_json(xreq, r);
611 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
613 struct afb_arg r = xreq_get_cb(closure, name);
614 struct afb_xreq *xreq = from_request(closure);
615 return afb_hook_xreq_get(xreq, name, r);
618 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
620 struct afb_xreq *xreq = from_request(closure);
621 afb_hook_xreq_success(xreq, obj, info);
622 xreq_success_cb(closure, obj, info);
625 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
627 struct afb_xreq *xreq = from_request(closure);
628 afb_hook_xreq_fail(xreq, status, info);
629 xreq_fail_cb(closure, status, info);
632 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
634 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
637 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
639 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
642 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
644 void *r = xreq_context_get_cb(closure);
645 struct afb_xreq *xreq = from_request(closure);
646 return afb_hook_xreq_context_get(xreq, r);
649 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
651 struct afb_xreq *xreq = from_request(closure);
652 afb_hook_xreq_context_set(xreq, value, free_value);
653 xreq_context_set_cb(closure, value, free_value);
656 static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
658 struct afb_xreq *xreq = from_request(closure);
659 afb_hook_xreq_addref(xreq);
660 return xreq_addref_cb(closure);
663 static void xreq_hooked_unref_cb(struct afb_request *closure)
665 struct afb_xreq *xreq = from_request(closure);
666 afb_hook_xreq_unref(xreq);
667 xreq_unref_cb(closure);
670 static void xreq_hooked_session_close_cb(struct afb_request *closure)
672 struct afb_xreq *xreq = from_request(closure);
673 afb_hook_xreq_session_close(xreq);
674 xreq_session_close_cb(closure);
677 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
679 int r = xreq_session_set_LOA_cb(closure, level);
680 struct afb_xreq *xreq = from_request(closure);
681 return afb_hook_xreq_session_set_LOA(xreq, level, r);
684 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
685 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
687 return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
690 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
692 int r = xreq_subscribe_eventid_cb(closure, eventid);
693 struct afb_xreq *xreq = from_request(closure);
694 return afb_hook_xreq_subscribe(xreq, eventid, r);
697 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
698 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
700 return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
703 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
705 int r = xreq_unsubscribe_eventid_cb(closure, eventid);
706 struct afb_xreq *xreq = from_request(closure);
707 return afb_hook_xreq_unsubscribe(xreq, eventid, r);
710 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)
712 struct afb_xreq *xreq = from_request(closure);
715 afb_hook_xreq_subcall(xreq, api, verb, args);
716 sc = subcall_alloc(xreq, api, verb, args);
719 callback(cb_closure, 1, afb_msg_json_internal_error());
720 json_object_put(args);
722 subcall_hooked(sc, callback, cb_closure);
726 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)
728 struct afb_xreq *xreq = from_request(closure);
731 afb_hook_xreq_subcall_req(xreq, api, verb, args);
732 sc = subcall_alloc(xreq, api, verb, args);
735 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
736 json_object_put(args);
738 subcall_req_hooked(sc, callback, cb_closure);
742 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)
744 struct afb_xreq *xreq = from_request(closure);
747 afb_hook_xreq_subcall(xreq, api, verb, args);
748 sc = subcall_alloc(xreq, api, verb, args);
751 callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq));
752 json_object_put(args);
754 subcall_request_hooked(sc, callback, cb_closure);
758 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
761 struct afb_xreq *xreq = from_request(closure);
762 afb_hook_xreq_subcallsync(xreq, api, verb, args);
763 r = xreq_subcallsync_cb(closure, api, verb, args, result);
764 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
767 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)
769 struct afb_xreq *xreq = from_request(closure);
772 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
773 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
777 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
779 struct afb_xreq *xreq = from_request(closure);
780 struct afb_stored_req *r = xreq_store_cb(closure);
781 afb_hook_xreq_store(xreq, r);
785 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
787 struct afb_xreq *xreq = from_request(closure);
788 int r = xreq_has_permission_cb(closure, permission);
789 return afb_hook_xreq_has_permission(xreq, permission, r);
792 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
794 struct afb_xreq *xreq = from_request(closure);
795 char *r = xreq_get_application_id_cb(closure);
796 return afb_hook_xreq_get_application_id(xreq, r);
799 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
801 struct afb_xreq *xreq = from_request(closure);
802 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
803 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
806 /******************************************************************************/
808 const struct afb_request_itf xreq_itf = {
809 .json = xreq_json_cb,
811 .success = xreq_success_cb,
812 .fail = xreq_fail_cb,
813 .vsuccess = xreq_vsuccess_cb,
814 .vfail = xreq_vfail_cb,
815 .context_get = xreq_context_get_cb,
816 .context_set = xreq_context_set_cb,
817 .addref = xreq_addref_cb,
818 .unref = xreq_unref_cb,
819 .session_close = xreq_session_close_cb,
820 .session_set_LOA = xreq_session_set_LOA_cb,
821 .subscribe = xreq_subscribe_cb,
822 .unsubscribe = xreq_unsubscribe_cb,
823 .subcall = xreq_subcall_cb,
824 .subcallsync = xreq_subcallsync_cb,
825 .vverbose = xreq_vverbose_cb,
826 .store = xreq_store_cb,
827 .subcall_req = xreq_subcall_req_cb,
828 .has_permission = xreq_has_permission_cb,
829 .get_application_id = xreq_get_application_id_cb,
830 .context_make = xreq_context_make_cb,
831 .subscribe_eventid = xreq_subscribe_eventid_cb,
832 .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
833 .subcall_request = xreq_subcall_request_cb,
836 const struct afb_request_itf xreq_hooked_itf = {
837 .json = xreq_hooked_json_cb,
838 .get = xreq_hooked_get_cb,
839 .success = xreq_hooked_success_cb,
840 .fail = xreq_hooked_fail_cb,
841 .vsuccess = xreq_hooked_vsuccess_cb,
842 .vfail = xreq_hooked_vfail_cb,
843 .context_get = xreq_hooked_context_get_cb,
844 .context_set = xreq_hooked_context_set_cb,
845 .addref = xreq_hooked_addref_cb,
846 .unref = xreq_hooked_unref_cb,
847 .session_close = xreq_hooked_session_close_cb,
848 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
849 .subscribe = xreq_hooked_subscribe_cb,
850 .unsubscribe = xreq_hooked_unsubscribe_cb,
851 .subcall = xreq_hooked_subcall_cb,
852 .subcallsync = xreq_hooked_subcallsync_cb,
853 .vverbose = xreq_hooked_vverbose_cb,
854 .store = xreq_hooked_store_cb,
855 .subcall_req = xreq_hooked_subcall_req_cb,
856 .has_permission = xreq_hooked_has_permission_cb,
857 .get_application_id = xreq_hooked_get_application_id_cb,
858 .context_make = xreq_hooked_context_make_cb,
859 .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
860 .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
861 .subcall_request = xreq_hooked_subcall_request_cb,
864 /******************************************************************************/
866 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
868 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
870 afb_hook_xreq_unstore(xreq);
874 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
876 return afb_request_json(to_request(xreq));
879 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
881 afb_request_success(to_request(xreq), obj, info);
884 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
888 va_start(args, info);
889 afb_request_success_v(to_request(xreq), obj, info, args);
893 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
895 afb_request_fail(to_request(xreq), status, info);
898 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
902 va_start(args, info);
903 afb_request_fail_v(to_request(xreq), status, info, args);
908 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
910 struct json_object *obj = xreq_json_cb(to_request(xreq));
911 const char *result = json_object_to_json_string(obj);
913 *size = strlen(result);
917 void afb_xreq_addref(struct afb_xreq *xreq)
919 afb_request_addref(to_request(xreq));
922 void afb_xreq_unref(struct afb_xreq *xreq)
924 afb_request_unref(to_request(xreq));
927 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)
929 xreq_subcall_request_cb(to_request(xreq), api, verb, args, callback, cb_closure);
932 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)
934 afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure);
937 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
939 return xreq_subcallsync_cb(to_request(xreq), api, verb, args, result);
942 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
944 return afb_request_subcall_sync(to_request(xreq), api, verb, args, result);
947 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
951 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
952 if (!afb_context_check(&xreq->context)) {
953 afb_context_close(&xreq->context);
954 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
960 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
961 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
962 if (!afb_context_check_loa(&xreq->context, loa)) {
963 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
969 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
970 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
971 if (afb_context_check_loa(&xreq->context, loa + 1)) {
972 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
978 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
979 afb_context_refresh(&xreq->context);
981 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
982 afb_context_change_loa(&xreq->context, 0);
983 afb_context_close(&xreq->context);
989 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
993 if (sessionflags != 0) {
994 if (!afb_context_check(&xreq->context)) {
995 afb_context_close(&xreq->context);
996 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
1002 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
1003 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
1004 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
1009 if (auth && !afb_auth_check(xreq, auth)) {
1010 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
1015 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
1016 afb_context_refresh(&xreq->context);
1018 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
1019 afb_context_close(&xreq->context);
1025 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
1028 afb_xreq_fail_unknown_verb(xreq);
1030 if (!xreq_session_check_apply_v1(xreq, verb->session))
1031 verb->callback(to_req(xreq));
1034 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
1037 afb_xreq_fail_unknown_verb(xreq);
1039 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
1040 verb->callback(to_req(xreq));
1043 void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb)
1046 afb_xreq_fail_unknown_verb(xreq);
1048 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
1049 verb->callback(to_request(xreq));
1052 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
1054 memset(xreq, 0, sizeof *xreq);
1055 xreq->request.itf = &xreq_hooked_itf; /* hook by default */
1057 xreq->queryitf = queryitf;
1060 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
1062 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1065 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
1067 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
1070 static void init_hooking(struct afb_xreq *xreq)
1072 afb_hook_init_xreq(xreq);
1073 if (xreq->hookflags)
1074 afb_hook_xreq_begin(xreq);
1076 xreq->request.itf = &xreq_itf; /* unhook the interface */
1080 * job callback for asynchronous and secured processing of the request.
1082 static void process_async(int signum, void *arg)
1084 struct afb_xreq *xreq = arg;
1085 const struct afb_api *api;
1088 /* emit the error (assumes that hooking is initialised) */
1089 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
1093 /* invoke api call method to process the reqiest */
1094 api = (const struct afb_api*)xreq->context.api_key;
1095 api->itf->call(api->closure, xreq);
1097 /* release the request */
1098 afb_xreq_unhooked_unref(xreq);
1102 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1103 * The early failure occurs only in function 'afb_xreq_process' where normally,
1104 * the hooking is not initialised. So this "early" failure takes care to initialise
1105 * the hooking in first.
1107 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1115 va_start(args, info);
1116 afb_request_fail_v(to_request(xreq), status, info, args);
1121 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1122 * Errors are reported as request failures.
1124 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1126 const struct afb_api *api;
1127 struct afb_xreq *caller;
1129 /* lookup at the api */
1130 xreq->apiset = apiset;
1131 api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
1133 if (errno == ENOENT)
1134 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1136 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
1139 xreq->context.api_key = api;
1141 /* check self locking */
1143 caller = xreq->caller;
1145 if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1146 /* noconcurrency lock detected */
1147 ERROR("self-lock detected in call stack for API %s", xreq->request.api);
1148 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
1151 caller = caller->caller;
1155 /* queue the request job */
1156 afb_xreq_unhooked_addref(xreq);
1157 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1158 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1159 ERROR("can't process job with threads: %m");
1160 early_failure(xreq, "cancelled", "not able to create a job for the task");
1161 afb_xreq_unhooked_unref(xreq);
1164 afb_xreq_unhooked_unref(xreq);