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