afb-xreq: handle when no reply was sent
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
28 #include <afb/afb-binding-v2.h>
29
30 #include "afb-context.h"
31 #include "afb-xreq.h"
32 #include "afb-evt.h"
33 #include "afb-msg-json.h"
34 #include "afb-cred.h"
35 #include "afb-hook.h"
36 #include "afb-api.h"
37 #include "afb-apiset.h"
38 #include "afb-auth.h"
39 #include "jobs.h"
40 #include "verbose.h"
41
42 /******************************************************************************/
43
44 static inline void xreq_addref(struct afb_xreq *xreq)
45 {
46         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
47 }
48
49 static inline void xreq_unref(struct afb_xreq *xreq)
50 {
51         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
52                 if (!xreq->replied)
53                         afb_xreq_fail(xreq, "error", "no reply");
54                 if (xreq->hookflags)
55                         afb_hook_xreq_end(xreq);
56                 xreq->queryitf->unref(xreq);
57         }
58 }
59
60 /******************************************************************************/
61
62 extern const struct afb_req_itf xreq_itf;
63 extern const struct afb_req_itf xreq_hooked_itf;
64
65 static inline struct afb_req to_req(struct afb_xreq *xreq)
66 {
67         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
68 }
69
70 /******************************************************************************/
71
72 struct subcall
73 {
74         struct afb_xreq xreq;
75         struct afb_xreq *caller;
76         void (*callback)(void*, int, struct json_object*);
77         void *closure;
78         union {
79                 struct {
80                         struct jobloop *jobloop;
81                         struct json_object *result;
82                         int status;
83                 };
84                 struct {
85                         union {
86                                 void (*callback)(void*, int, struct json_object*);
87                                 void (*callback2)(void*, int, struct json_object*, struct afb_req);
88                         };
89                         void *closure;
90                 } hooked;
91         };
92 };
93
94 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
95 {
96         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
97
98         return afb_xreq_subscribe(subcall->caller, event);
99 }
100
101 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
102 {
103         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
104
105         return afb_xreq_unsubscribe(subcall->caller, event);
106 }
107
108 static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
109 {
110         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
111
112         if (subcall->callback)
113                 subcall->callback(subcall->closure, status, obj);
114         json_object_put(obj);
115 }
116
117 static void subcall_destroy(struct afb_xreq *xreq)
118 {
119         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
120
121         json_object_put(subcall->xreq.json);
122         afb_cred_unref(subcall->xreq.cred);
123         xreq_unref(subcall->caller);
124         free(subcall);
125 }
126
127 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
128         .reply = subcall_reply,
129         .unref = subcall_destroy,
130         .subscribe = subcall_subscribe,
131         .unsubscribe = subcall_unsubscribe
132 };
133
134 static struct subcall *subcall_alloc(
135                 struct afb_xreq *caller,
136                 const char *api,
137                 const char *verb,
138                 struct json_object *args
139 )
140 {
141         struct subcall *subcall;
142         size_t lenapi, lenverb;
143         char *copy;
144
145         lenapi = 1 + strlen(api);
146         lenverb = 1 + strlen(verb);
147         subcall = malloc(lenapi + lenverb + sizeof *subcall);
148         if (!subcall)
149                 ERROR("out of memory");
150         else {
151                 copy = (char*)&subcall[1];
152                 memcpy(copy, api, lenapi);
153                 api = copy;
154                 copy = &copy[lenapi];
155                 memcpy(copy, verb, lenverb);
156                 verb = copy;
157
158                 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
159                 afb_context_subinit(&subcall->xreq.context, &caller->context);
160                 subcall->xreq.cred = afb_cred_addref(caller->cred);
161                 subcall->xreq.json = args;
162                 subcall->xreq.api = api;
163                 subcall->xreq.verb = verb;
164                 subcall->caller = caller;
165                 xreq_addref(caller);
166         }
167         return subcall;
168 }
169
170 static void subcall_process(struct subcall *subcall)
171 {
172         if (subcall->caller->queryitf->subcall) {
173                 subcall->caller->queryitf->subcall(
174                         subcall->caller, subcall->xreq.api, subcall->xreq.verb,
175                         subcall->xreq.json, subcall->callback, subcall->closure);
176                 xreq_unref(&subcall->xreq);
177         } else
178                 afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
179 }
180
181 static void subcall_sync_leave(struct subcall *subcall)
182 {
183         struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
184         if (jobloop)
185                 jobs_leave(jobloop);
186 }
187
188 static void subcall_sync_reply(void *closure, int status, struct json_object *obj)
189 {
190         struct subcall *subcall = closure;
191
192         subcall->status = status;
193         subcall->result = json_object_get(obj);
194         subcall_sync_leave(subcall);
195 }
196
197 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
198 {
199         struct subcall *subcall = closure;
200
201         if (!signum) {
202                 subcall->jobloop = jobloop;
203                 subcall_process(subcall);
204         } else {
205                 subcall->status = -1;
206                 subcall_sync_leave(subcall);
207         }
208 }
209
210 /******************************************************************************/
211
212 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
213 {
214         char *info;
215         if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
216                 info = NULL;
217         fun(first, second, info);
218         free(info);
219 }
220
221 /******************************************************************************/
222
223 static struct json_object *xreq_json_cb(void *closure)
224 {
225         struct afb_xreq *xreq = closure;
226         if (!xreq->json && xreq->queryitf->json)
227                 xreq->json = xreq->queryitf->json(xreq);
228         return xreq->json;
229 }
230
231 static struct afb_arg xreq_get_cb(void *closure, const char *name)
232 {
233         struct afb_xreq *xreq = closure;
234         struct afb_arg arg;
235         struct json_object *object, *value;
236
237         if (xreq->queryitf->get)
238                 arg = xreq->queryitf->get(xreq, name);
239         else {
240                 object = xreq_json_cb(closure);
241                 if (json_object_object_get_ex(object, name, &value)) {
242                         arg.name = name;
243                         arg.value = json_object_get_string(value);
244                 } else {
245                         arg.name = NULL;
246                         arg.value = NULL;
247                 }
248                 arg.path = NULL;
249         }
250         return arg;
251 }
252
253 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
254 {
255         struct afb_xreq *xreq = closure;
256
257         if (xreq->replied) {
258                 ERROR("reply called more than one time!!");
259                 json_object_put(obj);
260         } else {
261                 xreq->replied = 1;
262                 if (xreq->queryitf->success)
263                         xreq->queryitf->success(xreq, obj, info);
264                 else
265                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
266         }
267 }
268
269 static void xreq_fail_cb(void *closure, const char *status, const char *info)
270 {
271         struct afb_xreq *xreq = closure;
272
273         if (xreq->replied) {
274                 ERROR("reply called more than one time!!");
275         } else {
276                 xreq->replied = 1;
277                 if (xreq->queryitf->fail)
278                         xreq->queryitf->fail(xreq, status, info);
279                 else
280                         xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
281         }
282 }
283
284 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
285 {
286         vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
287 }
288
289 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
290 {
291         vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
292 }
293
294 static void *xreq_context_get_cb(void *closure)
295 {
296         struct afb_xreq *xreq = closure;
297         return afb_context_get(&xreq->context);
298 }
299
300 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
301 {
302         struct afb_xreq *xreq = closure;
303         afb_context_set(&xreq->context, value, free_value);
304 }
305
306 static void xreq_addref_cb(void *closure)
307 {
308         struct afb_xreq *xreq = closure;
309         xreq_addref(xreq);
310 }
311
312 static void xreq_unref_cb(void *closure)
313 {
314         struct afb_xreq *xreq = closure;
315         xreq_unref(xreq);
316 }
317
318 static void xreq_session_close_cb(void *closure)
319 {
320         struct afb_xreq *xreq = closure;
321         afb_context_close(&xreq->context);
322 }
323
324 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
325 {
326         struct afb_xreq *xreq = closure;
327         return afb_context_change_loa(&xreq->context, level);
328 }
329
330 static int xreq_subscribe_cb(void *closure, struct afb_event event)
331 {
332         struct afb_xreq *xreq = closure;
333         return afb_xreq_subscribe(xreq, event);
334 }
335
336 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
337 {
338         if (xreq->listener)
339                 return afb_evt_add_watch(xreq->listener, event);
340         if (xreq->queryitf->subscribe)
341                 return xreq->queryitf->subscribe(xreq, event);
342         ERROR("no event listener, subscription impossible");
343         errno = EINVAL;
344         return -1;
345 }
346
347 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
348 {
349         struct afb_xreq *xreq = closure;
350         return afb_xreq_unsubscribe(xreq, event);
351 }
352
353 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
354 {
355         if (xreq->listener)
356                 return afb_evt_remove_watch(xreq->listener, event);
357         if (xreq->queryitf->unsubscribe)
358                 return xreq->queryitf->unsubscribe(xreq, event);
359         ERROR("no event listener, unsubscription impossible");
360         errno = EINVAL;
361         return -1;
362 }
363
364 static void xreq_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
365 {
366         struct afb_xreq *xreq = closure;
367         struct subcall *subcall;
368
369         subcall = subcall_alloc(xreq, api, verb, args);
370         if (subcall == NULL) {
371                 if (callback)
372                         callback(cb_closure, 1, afb_msg_json_internal_error());
373                 json_object_put(args);
374         } else {
375                 subcall->callback = callback;
376                 subcall->closure = cb_closure;
377                 subcall_process(subcall);
378         }
379 }
380
381 static void xreq_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
382 {
383         struct subcall *subcall = closure;
384         subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
385 }
386
387 static void xreq_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
388 {
389         struct afb_xreq *xreq = closure;
390         struct subcall *subcall;
391
392         subcall = subcall_alloc(xreq, api, verb, args);
393         if (subcall == NULL) {
394                 if (callback)
395                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
396                 json_object_put(args);
397         } else {
398                 subcall->callback = xreq_subcall_req_reply_cb;
399                 subcall->closure = subcall;
400                 subcall->hooked.callback2 = callback;
401                 subcall->hooked.closure = cb_closure;
402                 subcall_process(subcall);
403         }
404 }
405
406
407 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
408 {
409         int rc;
410         struct subcall *subcall;
411         struct afb_xreq *xreq = closure;
412         struct json_object *resu;
413
414         subcall = subcall_alloc(xreq, api, verb, args);
415         if (!subcall) {
416                 rc = -1;
417                 resu = afb_msg_json_internal_error();
418                 json_object_put(args);
419         } else {
420                 subcall->callback = subcall_sync_reply;
421                 subcall->closure = subcall;
422                 subcall->jobloop = NULL;
423                 subcall->result = NULL;
424                 subcall->status = 0;
425                 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
426                 resu = subcall->result;
427                 if (rc < 0 || subcall->status < 0) {
428                         resu = resu ?: afb_msg_json_internal_error();
429                         rc = -1;
430                 }
431         }
432         if (result)
433                 *result = resu;
434         else
435                 json_object_put(resu);
436         return rc;
437 }
438
439 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
440 {
441         /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
442         vverbose(level, file, line, func, fmt, args);
443 }
444
445 static struct afb_stored_req *xreq_store_cb(void *closure)
446 {
447         xreq_addref_cb(closure);
448         return closure;
449 }
450
451 /******************************************************************************/
452
453 static struct json_object *xreq_hooked_json_cb(void *closure)
454 {
455         struct json_object *r = xreq_json_cb(closure);
456         struct afb_xreq *xreq = closure;
457         return afb_hook_xreq_json(xreq, r);
458 }
459
460 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
461 {
462         struct afb_arg r = xreq_get_cb(closure, name);
463         struct afb_xreq *xreq = closure;
464         return afb_hook_xreq_get(xreq, name, r);
465 }
466
467 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
468 {
469         struct afb_xreq *xreq = closure;
470         afb_hook_xreq_success(xreq, obj, info);
471         xreq_success_cb(closure, obj, info);
472 }
473
474 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
475 {
476         struct afb_xreq *xreq = closure;
477         afb_hook_xreq_fail(xreq, status, info);
478         xreq_fail_cb(closure, status, info);
479 }
480
481 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
482 {
483         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
484 }
485
486 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
487 {
488         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
489 }
490
491 static void *xreq_hooked_context_get_cb(void *closure)
492 {
493         void *r = xreq_context_get_cb(closure);
494         struct afb_xreq *xreq = closure;
495         return afb_hook_xreq_context_get(xreq, r);
496 }
497
498 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
499 {
500         struct afb_xreq *xreq = closure;
501         afb_hook_xreq_context_set(xreq, value, free_value);
502         xreq_context_set_cb(closure, value, free_value);
503 }
504
505 static void xreq_hooked_addref_cb(void *closure)
506 {
507         struct afb_xreq *xreq = closure;
508         afb_hook_xreq_addref(xreq);
509         xreq_addref_cb(closure);
510 }
511
512 static void xreq_hooked_unref_cb(void *closure)
513 {
514         struct afb_xreq *xreq = closure;
515         afb_hook_xreq_unref(xreq);
516         xreq_unref_cb(closure);
517 }
518
519 static void xreq_hooked_session_close_cb(void *closure)
520 {
521         struct afb_xreq *xreq = closure;
522         afb_hook_xreq_session_close(xreq);
523         xreq_session_close_cb(closure);
524 }
525
526 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
527 {
528         int r = xreq_session_set_LOA_cb(closure, level);
529         struct afb_xreq *xreq = closure;
530         return afb_hook_xreq_session_set_LOA(xreq, level, r);
531 }
532
533 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
534 {
535         int r = xreq_subscribe_cb(closure, event);
536         struct afb_xreq *xreq = closure;
537         return afb_hook_xreq_subscribe(xreq, event, r);
538 }
539
540 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
541 {
542         int r = xreq_unsubscribe_cb(closure, event);
543         struct afb_xreq *xreq = closure;
544         return afb_hook_xreq_unsubscribe(xreq, event, r);
545 }
546
547 static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
548 {
549         struct subcall *subcall = closure;
550
551         afb_hook_xreq_subcall_result(subcall->caller, status, result);
552         subcall->hooked.callback(subcall->hooked.closure, status, result);
553 }
554
555 static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
556 {
557         struct afb_xreq *xreq = closure;
558         struct subcall *subcall;
559
560         afb_hook_xreq_subcall(xreq, api, verb, args);
561         subcall = subcall_alloc(xreq, api, verb, args);
562         if (subcall == NULL) {
563                 if (callback)
564                         callback(cb_closure, 1, afb_msg_json_internal_error());
565                 json_object_put(args);
566         } else {
567                 subcall->callback = xreq_hooked_subcall_reply_cb;
568                 subcall->closure = subcall;
569                 subcall->hooked.callback = callback;
570                 subcall->hooked.closure = cb_closure;
571                 subcall_process(subcall);
572         }
573 }
574
575 static void xreq_hooked_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
576 {
577         struct subcall *subcall = closure;
578
579         afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
580         subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
581 }
582
583 static void xreq_hooked_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
584 {
585         struct afb_xreq *xreq = closure;
586         struct subcall *subcall;
587
588         afb_hook_xreq_subcall_req(xreq, api, verb, args);
589         subcall = subcall_alloc(xreq, api, verb, args);
590         if (subcall == NULL) {
591                 if (callback)
592                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
593                 json_object_put(args);
594         } else {
595                 subcall->callback = xreq_hooked_subcall_req_reply_cb;
596                 subcall->closure = subcall;
597                 subcall->hooked.callback2 = callback;
598                 subcall->hooked.closure = cb_closure;
599                 subcall_process(subcall);
600         }
601 }
602
603 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
604 {
605         int r;
606         struct afb_xreq *xreq = closure;
607         afb_hook_xreq_subcallsync(xreq, api, verb, args);
608         r = xreq_subcallsync_cb(closure, api, verb, args, result);
609         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
610 }
611
612 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
613 {
614         struct afb_xreq *xreq = closure;
615         va_list ap;
616         va_copy(ap, args);
617         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
618         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
619         va_end(ap);
620 }
621
622 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
623 {
624         struct afb_xreq *xreq = closure;
625         struct afb_stored_req *r = xreq_store_cb(closure);
626         afb_hook_xreq_store(xreq, r);
627         return r;
628 }
629
630 /******************************************************************************/
631
632 const struct afb_req_itf xreq_itf = {
633         .json = xreq_json_cb,
634         .get = xreq_get_cb,
635         .success = xreq_success_cb,
636         .fail = xreq_fail_cb,
637         .vsuccess = xreq_vsuccess_cb,
638         .vfail = xreq_vfail_cb,
639         .context_get = xreq_context_get_cb,
640         .context_set = xreq_context_set_cb,
641         .addref = xreq_addref_cb,
642         .unref = xreq_unref_cb,
643         .session_close = xreq_session_close_cb,
644         .session_set_LOA = xreq_session_set_LOA_cb,
645         .subscribe = xreq_subscribe_cb,
646         .unsubscribe = xreq_unsubscribe_cb,
647         .subcall = xreq_subcall_cb,
648         .subcallsync = xreq_subcallsync_cb,
649         .vverbose = xreq_vverbose_cb,
650         .store = xreq_store_cb,
651         .subcall_req = xreq_subcall_req_cb
652 };
653
654 const struct afb_req_itf xreq_hooked_itf = {
655         .json = xreq_hooked_json_cb,
656         .get = xreq_hooked_get_cb,
657         .success = xreq_hooked_success_cb,
658         .fail = xreq_hooked_fail_cb,
659         .vsuccess = xreq_hooked_vsuccess_cb,
660         .vfail = xreq_hooked_vfail_cb,
661         .context_get = xreq_hooked_context_get_cb,
662         .context_set = xreq_hooked_context_set_cb,
663         .addref = xreq_hooked_addref_cb,
664         .unref = xreq_hooked_unref_cb,
665         .session_close = xreq_hooked_session_close_cb,
666         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
667         .subscribe = xreq_hooked_subscribe_cb,
668         .unsubscribe = xreq_hooked_unsubscribe_cb,
669         .subcall = xreq_hooked_subcall_cb,
670         .subcallsync = xreq_hooked_subcallsync_cb,
671         .vverbose = xreq_hooked_vverbose_cb,
672         .store = xreq_hooked_store_cb,
673         .subcall_req = xreq_hooked_subcall_req_cb
674 };
675
676 /******************************************************************************/
677
678 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
679 {
680         struct afb_xreq *xreq = (struct afb_xreq *)sreq;
681         if (xreq->hookflags)
682                 afb_hook_xreq_unstore(xreq);
683         return to_req(xreq);
684 }
685
686 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
687 {
688         return afb_req_json(to_req(xreq));
689 }
690
691 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
692 {
693         afb_req_success(to_req(xreq), obj, info);
694 }
695
696 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
697 {
698         char *message;
699         va_list args;
700         va_start(args, info);
701         if (info == NULL || vasprintf(&message, info, args) < 0)
702                 message = NULL;
703         va_end(args);
704         afb_xreq_success(xreq, obj, message);
705         free(message);
706 }
707
708 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
709 {
710         afb_req_fail(to_req(xreq), status, info);
711 }
712
713 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
714 {
715         char *message;
716         va_list args;
717         va_start(args, info);
718         if (info == NULL || vasprintf(&message, info, args) < 0)
719                 message = NULL;
720         va_end(args);
721         afb_xreq_fail(xreq, status, message);
722         free(message);
723 }
724
725 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
726 {
727         struct json_object *obj = xreq_json_cb(xreq);
728         const char *result = json_object_to_json_string(obj);
729         if (size != NULL)
730                 *size = strlen(result);
731         return result;
732 }
733
734 void afb_xreq_addref(struct afb_xreq *xreq)
735 {
736         afb_req_addref(to_req(xreq));
737 }
738
739 void afb_xreq_unref(struct afb_xreq *xreq)
740 {
741         afb_req_unref(to_req(xreq));
742 }
743
744 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)
745 {
746         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
747 }
748
749 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)
750 {
751         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
752 }
753
754 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
755 {
756         return xreq_subcallsync_cb(xreq, api, verb, args, result);
757 }
758
759 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
760 {
761         return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
762 }
763
764 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
765 {
766         int loa;
767
768         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
769                 if (!afb_context_check(&xreq->context)) {
770                         afb_context_close(&xreq->context);
771                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
772                         errno = EINVAL;
773                         return -1;
774                 }
775         }
776
777         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
778                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
779                 if (!afb_context_check_loa(&xreq->context, loa)) {
780                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
781                         errno = EPERM;
782                         return -1;
783                 }
784         }
785
786         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
787                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
788                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
789                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
790                         errno = EPERM;
791                         return -1;
792                 }
793         }
794
795         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
796                 afb_context_refresh(&xreq->context);
797         }
798         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
799                 afb_context_change_loa(&xreq->context, 0);
800                 afb_context_close(&xreq->context);
801         }
802
803         return 0;
804 }
805
806 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
807 {
808         int loa;
809
810         if (sessionflags != 0) {
811                 if (!afb_context_check(&xreq->context)) {
812                         afb_context_close(&xreq->context);
813                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
814                         errno = EINVAL;
815                         return -1;
816                 }
817         }
818
819         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
820         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
821                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
822                 errno = EPERM;
823                 return -1;
824         }
825
826         if (auth && !afb_auth_check(auth, xreq)) {
827                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
828                 errno = EPERM;
829                 return -1;
830         }
831
832         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
833                 afb_context_refresh(&xreq->context);
834         }
835         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
836                 afb_context_close(&xreq->context);
837         }
838
839         return 0;
840 }
841
842 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
843 {
844         if (!verb)
845                 afb_xreq_fail_unknown_verb(xreq);
846         else
847                 if (!xreq_session_check_apply_v1(xreq, verb->session))
848                         verb->callback(to_req(xreq));
849 }
850
851 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
852 {
853         if (!verb)
854                 afb_xreq_fail_unknown_verb(xreq);
855         else
856                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
857                         verb->callback(to_req(xreq));
858 }
859
860 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
861 {
862         memset(xreq, 0, sizeof *xreq);
863         xreq->refcount = 1;
864         xreq->queryitf = queryitf;
865 }
866
867 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
868 {
869         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
870 }
871
872 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
873 {
874         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
875 }
876
877 static void process_sync(struct afb_xreq *xreq)
878 {
879         struct afb_api api;
880
881         /* init hooking */
882         afb_hook_init_xreq(xreq);
883         if (xreq->hookflags)
884                 afb_hook_xreq_begin(xreq);
885
886         /* search the api */
887         if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
888                 if (errno == ENOENT)
889                         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
890                 else
891                         afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
892         } else {
893                 xreq->context.api_key = api.closure;
894                 api.itf->call(api.closure, xreq);
895         }
896 }
897
898 static void process_async(int signum, void *arg)
899 {
900         struct afb_xreq *xreq = arg;
901
902         if (signum != 0) {
903                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
904         } else {
905                 process_sync(xreq);
906         }
907         xreq_unref(xreq);
908 }
909
910 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
911 {
912         xreq->apiset = apiset;
913
914         xreq_addref(xreq);
915         if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
916                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
917                 ERROR("can't process job with threads: %m");
918                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
919                 xreq_unref(xreq);
920         }
921         xreq_unref(xreq);
922 }
923