2 * Copyright (C) 2015-2020 "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.
26 #include <json-c/json.h>
27 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
28 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
31 #include <afb/afb-binding-v1.h>
32 #include <afb/afb-binding-v2.h>
33 #include <afb/afb-binding-v3.h>
34 #include <afb/afb-req-x2.h>
37 #include "afb-apiset.h"
39 #include "afb-calls.h"
40 #include "afb-context.h"
44 #include "afb-msg-json.h"
46 #include "afb-error-text.h"
51 /******************************************************************************/
53 static void xreq_finalize(struct afb_xreq *xreq)
56 afb_xreq_reply(xreq, NULL, afb_error_text_not_replied, NULL);
59 afb_hook_xreq_end(xreq);
62 afb_xreq_unhooked_unref(xreq->caller);
63 xreq->queryitf->unref(xreq);
66 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
68 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
71 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
73 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
77 /******************************************************************************/
79 struct json_object *afb_xreq_unhooked_json(struct afb_xreq *xreq)
81 if (!xreq->json && xreq->queryitf->json)
82 xreq->json = xreq->queryitf->json(xreq);
86 static struct json_object *xreq_json_cb(struct afb_req_x2 *closure)
88 struct afb_xreq *xreq = xreq_from_req_x2(closure);
89 return afb_xreq_unhooked_json(xreq);
92 static struct afb_arg xreq_get_cb(struct afb_req_x2 *closure, const char *name)
94 struct afb_xreq *xreq = xreq_from_req_x2(closure);
96 struct json_object *object, *value;
98 if (xreq->queryitf->get)
99 arg = xreq->queryitf->get(xreq, name);
101 object = xreq_json_cb(closure);
102 if (json_object_object_get_ex(object, name, &value)) {
104 arg.value = json_object_get_string(value);
114 static void xreq_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
116 struct afb_xreq *xreq = xreq_from_req_x2(closure);
119 ERROR("reply called more than one time!!");
120 json_object_put(obj);
123 xreq->queryitf->reply(xreq, obj, error, info);
127 static void xreq_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
130 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
132 xreq_reply_cb(closure, obj, error, info);
136 static void xreq_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
138 xreq_reply_cb(closure, obj, NULL, info);
141 static void xreq_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
143 xreq_reply_cb(closure, NULL, status, info);
146 static void xreq_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
148 xreq_vreply_cb(closure, obj, NULL, fmt, args);
151 static void xreq_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
153 xreq_vreply_cb(closure, NULL, status, fmt, args);
156 static void *xreq_legacy_context_get_cb(struct afb_req_x2 *closure)
158 struct afb_xreq *xreq = xreq_from_req_x2(closure);
159 return afb_context_get(&xreq->context);
162 static void xreq_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
164 struct afb_xreq *xreq = xreq_from_req_x2(closure);
165 afb_context_set(&xreq->context, value, free_value);
168 static struct afb_req_x2 *xreq_addref_cb(struct afb_req_x2 *closure)
170 struct afb_xreq *xreq = xreq_from_req_x2(closure);
171 afb_xreq_unhooked_addref(xreq);
175 static void xreq_unref_cb(struct afb_req_x2 *closure)
177 struct afb_xreq *xreq = xreq_from_req_x2(closure);
178 afb_xreq_unhooked_unref(xreq);
181 static void xreq_session_close_cb(struct afb_req_x2 *closure)
183 struct afb_xreq *xreq = xreq_from_req_x2(closure);
184 afb_context_close(&xreq->context);
187 static int xreq_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
189 struct afb_xreq *xreq = xreq_from_req_x2(closure);
190 return afb_context_change_loa(&xreq->context, level);
193 static int xreq_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
195 struct afb_xreq *xreq = xreq_from_req_x2(closure);
196 return afb_xreq_subscribe(xreq, event);
199 static int xreq_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
201 return xreq_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
204 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
207 ERROR("request replied, subscription impossible");
210 if (xreq->queryitf->subscribe)
211 return xreq->queryitf->subscribe(xreq, event);
212 ERROR("no event listener, subscription impossible");
218 static int xreq_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
220 struct afb_xreq *xreq = xreq_from_req_x2(closure);
221 return afb_xreq_unsubscribe(xreq, event);
224 static int xreq_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
226 return xreq_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
229 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
232 ERROR("request replied, unsubscription impossible");
235 if (xreq->queryitf->unsubscribe)
236 return xreq->queryitf->unsubscribe(xreq, event);
237 ERROR("no event listener, unsubscription impossible");
243 static void xreq_legacy_subcall_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
245 struct afb_xreq *xreq = xreq_from_req_x2(req);
246 return afb_calls_legacy_subcall_v1(xreq, api, verb, args, callback, closure);
249 static void xreq_legacy_subcall_req_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x1), void *closure)
251 struct afb_xreq *xreq = xreq_from_req_x2(req);
252 return afb_calls_legacy_subcall_v2(xreq, api, verb, args, callback, closure);
255 static void xreq_legacy_subcall_request_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2*), void *closure)
257 struct afb_xreq *xreq = xreq_from_req_x2(req);
258 return afb_calls_legacy_subcall_v3(xreq, api, verb, args, callback, closure);
262 static int xreq_legacy_subcallsync_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
264 struct afb_xreq *xreq = xreq_from_req_x2(req);
265 return afb_calls_legacy_subcall_sync(xreq, api, verb, args, result);
268 static void xreq_vverbose_cb(struct afb_req_x2 *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
271 struct afb_xreq *xreq = xreq_from_req_x2(closure);
273 if (!fmt || vasprintf(&p, fmt, args) < 0)
274 vverbose(level, file, line, func, fmt, args);
276 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.called_api, p);
281 static struct afb_stored_req *xreq_legacy_store_cb(struct afb_req_x2 *closure)
283 xreq_addref_cb(closure);
284 return (struct afb_stored_req*)closure;
287 static int xreq_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
289 struct afb_xreq *xreq = xreq_from_req_x2(closure);
290 return afb_context_has_permission(&xreq->context, permission);
293 static char *xreq_get_application_id_cb(struct afb_req_x2 *closure)
295 struct afb_xreq *xreq = xreq_from_req_x2(closure);
296 struct afb_cred *cred = xreq->context.credentials;
297 return cred && cred->id ? strdup(cred->id) : NULL;
300 static void *xreq_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
302 struct afb_xreq *xreq = xreq_from_req_x2(closure);
303 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
306 static int xreq_get_uid_cb(struct afb_req_x2 *closure)
308 struct afb_xreq *xreq = xreq_from_req_x2(closure);
309 struct afb_cred *cred = xreq->context.credentials;
310 return cred && cred->id ? (int)cred->uid : -1;
313 static struct json_object *xreq_get_client_info_cb(struct afb_req_x2 *closure)
315 struct afb_xreq *xreq = xreq_from_req_x2(closure);
316 struct afb_cred *cred = xreq->context.credentials;
317 struct json_object *r = json_object_new_object();
318 if (cred && cred->id) {
319 json_object_object_add(r, "uid", json_object_new_int(cred->uid));
320 json_object_object_add(r, "gid", json_object_new_int(cred->gid));
321 json_object_object_add(r, "pid", json_object_new_int(cred->pid));
322 json_object_object_add(r, "user", json_object_new_string(cred->user));
323 json_object_object_add(r, "label", json_object_new_string(cred->label));
324 json_object_object_add(r, "id", json_object_new_string(cred->id));
326 if (xreq->context.session) {
327 json_object_object_add(r, "uuid", json_object_new_string(afb_context_uuid(&xreq->context)?:""));
328 json_object_object_add(r, "LOA", json_object_new_int(afb_context_get_loa(&xreq->context)));
333 static void xreq_subcall_cb(
334 struct afb_req_x2 *req,
337 struct json_object *args,
339 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
342 struct afb_xreq *xreq = xreq_from_req_x2(req);
343 afb_calls_subcall(xreq, api, verb, args, flags, callback, closure);
346 static int xreq_subcallsync_cb(
347 struct afb_req_x2 *req,
350 struct json_object *args,
352 struct json_object **object,
356 struct afb_xreq *xreq = xreq_from_req_x2(req);
357 return afb_calls_subcall_sync(xreq, api, verb, args, flags, object, error, info);
360 /******************************************************************************/
362 const struct afb_req_x2_itf xreq_itf = {
363 .json = xreq_json_cb,
365 .legacy_success = xreq_legacy_success_cb,
366 .legacy_fail = xreq_legacy_fail_cb,
367 .legacy_vsuccess = xreq_legacy_vsuccess_cb,
368 .legacy_vfail = xreq_legacy_vfail_cb,
369 .legacy_context_get = xreq_legacy_context_get_cb,
370 .legacy_context_set = xreq_legacy_context_set_cb,
371 .addref = xreq_addref_cb,
372 .unref = xreq_unref_cb,
373 .session_close = xreq_session_close_cb,
374 .session_set_LOA = xreq_session_set_LOA_cb,
375 .legacy_subscribe_event_x1 = xreq_legacy_subscribe_event_x1_cb,
376 .legacy_unsubscribe_event_x1 = xreq_legacy_unsubscribe_event_x1_cb,
377 .legacy_subcall = xreq_legacy_subcall_cb,
378 .legacy_subcallsync = xreq_legacy_subcallsync_cb,
379 .vverbose = xreq_vverbose_cb,
380 .legacy_store_req = xreq_legacy_store_cb,
381 .legacy_subcall_req = xreq_legacy_subcall_req_cb,
382 .has_permission = xreq_has_permission_cb,
383 .get_application_id = xreq_get_application_id_cb,
384 .context_make = xreq_context_make_cb,
385 .subscribe_event_x2 = xreq_subscribe_event_x2_cb,
386 .unsubscribe_event_x2 = xreq_unsubscribe_event_x2_cb,
387 .legacy_subcall_request = xreq_legacy_subcall_request_cb,
388 .get_uid = xreq_get_uid_cb,
389 .reply = xreq_reply_cb,
390 .vreply = xreq_vreply_cb,
391 .get_client_info = xreq_get_client_info_cb,
392 .subcall = xreq_subcall_cb,
393 .subcallsync = xreq_subcallsync_cb,
395 /******************************************************************************/
398 static struct json_object *xreq_hooked_json_cb(struct afb_req_x2 *closure)
400 struct json_object *r = xreq_json_cb(closure);
401 struct afb_xreq *xreq = xreq_from_req_x2(closure);
402 return afb_hook_xreq_json(xreq, r);
405 static struct afb_arg xreq_hooked_get_cb(struct afb_req_x2 *closure, const char *name)
407 struct afb_arg r = xreq_get_cb(closure, name);
408 struct afb_xreq *xreq = xreq_from_req_x2(closure);
409 return afb_hook_xreq_get(xreq, name, r);
412 static void xreq_hooked_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
414 struct afb_xreq *xreq = xreq_from_req_x2(closure);
415 afb_hook_xreq_reply(xreq, obj, error, info);
416 xreq_reply_cb(closure, obj, error, info);
419 static void xreq_hooked_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
422 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
424 xreq_hooked_reply_cb(closure, obj, error, info);
428 static void xreq_hooked_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
430 xreq_hooked_reply_cb(closure, obj, NULL, info);
433 static void xreq_hooked_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
435 xreq_hooked_reply_cb(closure, NULL, status, info);
438 static void xreq_hooked_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
440 xreq_hooked_vreply_cb(closure, obj, NULL, fmt, args);
443 static void xreq_hooked_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
445 xreq_hooked_vreply_cb(closure, NULL, status, fmt, args);
448 static void *xreq_hooked_legacy_context_get_cb(struct afb_req_x2 *closure)
450 void *r = xreq_legacy_context_get_cb(closure);
451 struct afb_xreq *xreq = xreq_from_req_x2(closure);
452 return afb_hook_xreq_legacy_context_get(xreq, r);
455 static void xreq_hooked_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
457 struct afb_xreq *xreq = xreq_from_req_x2(closure);
458 afb_hook_xreq_legacy_context_set(xreq, value, free_value);
459 xreq_legacy_context_set_cb(closure, value, free_value);
462 static struct afb_req_x2 *xreq_hooked_addref_cb(struct afb_req_x2 *closure)
464 struct afb_xreq *xreq = xreq_from_req_x2(closure);
465 afb_hook_xreq_addref(xreq);
466 return xreq_addref_cb(closure);
469 static void xreq_hooked_unref_cb(struct afb_req_x2 *closure)
471 struct afb_xreq *xreq = xreq_from_req_x2(closure);
472 afb_hook_xreq_unref(xreq);
473 xreq_unref_cb(closure);
476 static void xreq_hooked_session_close_cb(struct afb_req_x2 *closure)
478 struct afb_xreq *xreq = xreq_from_req_x2(closure);
479 afb_hook_xreq_session_close(xreq);
480 xreq_session_close_cb(closure);
483 static int xreq_hooked_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
485 int r = xreq_session_set_LOA_cb(closure, level);
486 struct afb_xreq *xreq = xreq_from_req_x2(closure);
487 return afb_hook_xreq_session_set_LOA(xreq, level, r);
490 static int xreq_hooked_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
492 int r = xreq_subscribe_event_x2_cb(closure, event);
493 struct afb_xreq *xreq = xreq_from_req_x2(closure);
494 return afb_hook_xreq_subscribe(xreq, event, r);
497 static int xreq_hooked_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
499 return xreq_hooked_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
502 static int xreq_hooked_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
504 int r = xreq_unsubscribe_event_x2_cb(closure, event);
505 struct afb_xreq *xreq = xreq_from_req_x2(closure);
506 return afb_hook_xreq_unsubscribe(xreq, event, r);
509 static int xreq_hooked_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
511 return xreq_hooked_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
514 static void xreq_hooked_legacy_subcall_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
516 struct afb_xreq *xreq = xreq_from_req_x2(req);
517 afb_calls_legacy_hooked_subcall_v1(xreq, api, verb, args, callback, closure);
520 static void xreq_hooked_legacy_subcall_req_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x1), void *closure)
522 struct afb_xreq *xreq = xreq_from_req_x2(req);
523 afb_calls_legacy_hooked_subcall_v2(xreq, api, verb, args, callback, closure);
526 static void xreq_hooked_legacy_subcall_request_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *closure)
528 struct afb_xreq *xreq = xreq_from_req_x2(req);
529 afb_calls_legacy_hooked_subcall_v3(xreq, api, verb, args, callback, closure);
532 static int xreq_hooked_legacy_subcallsync_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
534 struct afb_xreq *xreq = xreq_from_req_x2(req);
535 return afb_calls_legacy_hooked_subcall_sync(xreq, api, verb, args, result);
538 static void xreq_hooked_vverbose_cb(struct afb_req_x2 *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
540 struct afb_xreq *xreq = xreq_from_req_x2(closure);
543 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
544 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
548 static struct afb_stored_req *xreq_hooked_legacy_store_cb(struct afb_req_x2 *closure)
550 struct afb_xreq *xreq = xreq_from_req_x2(closure);
551 struct afb_stored_req *r = xreq_legacy_store_cb(closure);
552 afb_hook_xreq_legacy_store(xreq, r);
556 static int xreq_hooked_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
558 struct afb_xreq *xreq = xreq_from_req_x2(closure);
559 int r = xreq_has_permission_cb(closure, permission);
560 return afb_hook_xreq_has_permission(xreq, permission, r);
563 static char *xreq_hooked_get_application_id_cb(struct afb_req_x2 *closure)
565 struct afb_xreq *xreq = xreq_from_req_x2(closure);
566 char *r = xreq_get_application_id_cb(closure);
567 return afb_hook_xreq_get_application_id(xreq, r);
570 static void *xreq_hooked_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
572 struct afb_xreq *xreq = xreq_from_req_x2(closure);
573 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
574 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
577 static int xreq_hooked_get_uid_cb(struct afb_req_x2 *closure)
579 struct afb_xreq *xreq = xreq_from_req_x2(closure);
580 int r = xreq_get_uid_cb(closure);
581 return afb_hook_xreq_get_uid(xreq, r);
584 static struct json_object *xreq_hooked_get_client_info_cb(struct afb_req_x2 *closure)
586 struct afb_xreq *xreq = xreq_from_req_x2(closure);
587 struct json_object *r = xreq_get_client_info_cb(closure);
588 return afb_hook_xreq_get_client_info(xreq, r);
591 static void xreq_hooked_subcall_cb(
592 struct afb_req_x2 *req,
595 struct json_object *args,
597 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
600 struct afb_xreq *xreq = xreq_from_req_x2(req);
601 afb_calls_hooked_subcall(xreq, api, verb, args, flags, callback, closure);
604 static int xreq_hooked_subcallsync_cb(
605 struct afb_req_x2 *req,
608 struct json_object *args,
610 struct json_object **object,
614 struct afb_xreq *xreq = xreq_from_req_x2(req);
615 return afb_calls_hooked_subcall_sync(xreq, api, verb, args, flags, object, error, info);
618 /******************************************************************************/
620 const struct afb_req_x2_itf xreq_hooked_itf = {
621 .json = xreq_hooked_json_cb,
622 .get = xreq_hooked_get_cb,
623 .legacy_success = xreq_hooked_legacy_success_cb,
624 .legacy_fail = xreq_hooked_legacy_fail_cb,
625 .legacy_vsuccess = xreq_hooked_legacy_vsuccess_cb,
626 .legacy_vfail = xreq_hooked_legacy_vfail_cb,
627 .legacy_context_get = xreq_hooked_legacy_context_get_cb,
628 .legacy_context_set = xreq_hooked_legacy_context_set_cb,
629 .addref = xreq_hooked_addref_cb,
630 .unref = xreq_hooked_unref_cb,
631 .session_close = xreq_hooked_session_close_cb,
632 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
633 .legacy_subscribe_event_x1 = xreq_hooked_legacy_subscribe_event_x1_cb,
634 .legacy_unsubscribe_event_x1 = xreq_hooked_legacy_unsubscribe_event_x1_cb,
635 .legacy_subcall = xreq_hooked_legacy_subcall_cb,
636 .legacy_subcallsync = xreq_hooked_legacy_subcallsync_cb,
637 .vverbose = xreq_hooked_vverbose_cb,
638 .legacy_store_req = xreq_hooked_legacy_store_cb,
639 .legacy_subcall_req = xreq_hooked_legacy_subcall_req_cb,
640 .has_permission = xreq_hooked_has_permission_cb,
641 .get_application_id = xreq_hooked_get_application_id_cb,
642 .context_make = xreq_hooked_context_make_cb,
643 .subscribe_event_x2 = xreq_hooked_subscribe_event_x2_cb,
644 .unsubscribe_event_x2 = xreq_hooked_unsubscribe_event_x2_cb,
645 .legacy_subcall_request = xreq_hooked_legacy_subcall_request_cb,
646 .get_uid = xreq_hooked_get_uid_cb,
647 .reply = xreq_hooked_reply_cb,
648 .vreply = xreq_hooked_vreply_cb,
649 .get_client_info = xreq_hooked_get_client_info_cb,
650 .subcall = xreq_hooked_subcall_cb,
651 .subcallsync = xreq_hooked_subcallsync_cb,
655 /******************************************************************************/
657 struct afb_req_x1 afb_xreq_unstore(struct afb_stored_req *sreq)
659 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
662 afb_hook_xreq_legacy_unstore(xreq);
664 return xreq_to_req_x1(xreq);
667 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
669 return afb_req_x2_json(xreq_to_req_x2(xreq));
672 void afb_xreq_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
674 afb_req_x2_reply(xreq_to_req_x2(xreq), obj, error, info);
677 void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info, ...)
681 va_start(args, info);
682 afb_req_x2_reply_v(xreq_to_req_x2(xreq), obj, error, info, args);
686 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
688 struct json_object *obj = xreq_json_cb(xreq_to_req_x2(xreq));
689 const char *result = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE);
691 *size = strlen(result);
695 void afb_xreq_unhooked_legacy_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *cb_closure)
697 xreq_legacy_subcall_request_cb(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
700 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags, void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *), void *closure)
702 xreq_subcall_cb(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
705 int afb_xreq_unhooked_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
707 return xreq_legacy_subcallsync_cb(xreq_to_req_x2(xreq), api, verb, args, result);
710 void afb_xreq_addref(struct afb_xreq *xreq)
712 afb_req_x2_addref(xreq_to_req_x2(xreq));
715 void afb_xreq_unref(struct afb_xreq *xreq)
717 afb_req_x2_unref(xreq_to_req_x2(xreq));
720 void afb_xreq_legacy_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *cb_closure)
722 afb_req_x2_subcall_legacy(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
725 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags, void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *), void *closure)
727 afb_req_x2_subcall(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
730 int afb_xreq_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
732 return afb_req_x2_subcall_sync_legacy(xreq_to_req_x2(xreq), api, verb, args, result);
735 int afb_xreq_reply_unknown_api(struct afb_xreq *xreq)
737 afb_xreq_reply_f(xreq, NULL, afb_error_text_unknown_api, "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
742 int afb_xreq_reply_unknown_verb(struct afb_xreq *xreq)
744 afb_xreq_reply_f(xreq, NULL, afb_error_text_unknown_verb, "verb %s unknown within api %s", xreq->request.called_verb, xreq->request.called_api);
749 int afb_xreq_reply_invalid_token(struct afb_xreq *xreq)
751 afb_xreq_reply(xreq, NULL, afb_error_text_invalid_token, "invalid token"); /* TODO: or "no token" */
756 int afb_xreq_reply_insufficient_scope(struct afb_xreq *xreq, const char *scope)
758 afb_xreq_reply(xreq, NULL, afb_error_text_insufficient_scope, scope ?: "insufficient scope");
763 #if WITH_LEGACY_BINDING_V1
764 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
767 afb_xreq_reply_unknown_verb(xreq);
769 if (afb_auth_check_and_set_session_x1(xreq, verb->session) >= 0)
770 verb->callback(xreq_to_req_x1(xreq));
774 #if WITH_LEGACY_BINDING_V2
775 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
778 afb_xreq_reply_unknown_verb(xreq);
780 if (afb_auth_check_and_set_session_x2(xreq, verb->auth, verb->session) > 0)
781 verb->callback(xreq_to_req_x1(xreq));
785 void afb_xreq_call_verb_v3(struct afb_xreq *xreq, const struct afb_verb_v3 *verb)
788 afb_xreq_reply_unknown_verb(xreq);
790 if (afb_auth_check_and_set_session_x2(xreq, verb->auth, verb->session) > 0)
791 verb->callback(xreq_to_req_x2(xreq));
794 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
796 memset(xreq, 0, sizeof *xreq);
797 xreq->request.itf = &xreq_itf; /* no hook by default */
799 xreq->queryitf = queryitf;
803 static void init_hooking(struct afb_xreq *xreq)
805 afb_hook_init_xreq(xreq);
806 if (xreq->hookflags) {
807 xreq->request.itf = &xreq_hooked_itf; /* unhook the interface */
808 afb_hook_xreq_begin(xreq);
814 * job callback for asynchronous and secured processing of the request.
816 static void process_async(int signum, void *arg)
818 struct afb_xreq *xreq = arg;
819 const struct afb_api_item *api;
822 /* emit the error (assumes that hooking is initialised) */
823 afb_xreq_reply_f(xreq, NULL, afb_error_text_aborted, "signal %s(%d) caught", strsignal(signum), signum);
829 /* invoke api call method to process the request */
830 api = (const struct afb_api_item*)xreq->context.api_key;
831 api->itf->call(api->closure, xreq);
833 /* release the request */
834 afb_xreq_unhooked_unref(xreq);
838 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
839 * The early failure occurs only in function 'afb_xreq_process' where normally,
840 * the hooking is not initialised. So this "early" failure takes care to initialise
841 * the hooking in first.
843 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
853 va_start(args, info);
854 afb_req_x2_reply_v(xreq_to_req_x2(xreq), NULL, status, info, args);
859 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
860 * Errors are reported as request failures.
862 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
864 const struct afb_api_item *api;
865 struct afb_xreq *caller;
867 /* lookup at the api */
868 xreq->apiset = apiset;
869 api = afb_apiset_lookup_started(apiset, xreq->request.called_api, 1);
872 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
874 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.called_api);
877 xreq->context.api_key = api;
879 /* check self locking */
881 caller = xreq->caller;
883 if (((const struct afb_api_item*)caller->context.api_key)->group == api->group) {
884 /* noconcurrency lock detected */
885 ERROR("self-lock detected in call stack for API %s", xreq->request.called_api);
886 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.called_api);
889 caller = caller->caller;
893 /* queue the request job */
894 afb_xreq_unhooked_addref(xreq);
895 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
896 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
897 ERROR("can't process job with threads: %m");
898 early_failure(xreq, "cancelled", "not able to create a job for the task");
899 afb_xreq_unhooked_unref(xreq);
902 afb_xreq_unhooked_unref(xreq);
905 const char *xreq_on_behalf_cred_export(struct afb_xreq *xreq)
907 return afb_context_on_behalf_export(&xreq->context);