diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-09-21 15:17:11 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-10-09 14:08:32 +0200 |
commit | e0fa1cdd56ffb97fedc3ba8df482a14323d876c7 (patch) | |
tree | b46aa71354c964373ca5fc6f575a4eaca6f2363f | |
parent | c12c74c208a30c5b18716b8faa464f0b4f643255 (diff) |
Introduce afb_request
This prepares the new api with pointers.
Change-Id: I4eb299551145226e428934b6835a1a7810ae6439
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | include/afb/afb-binding-v1.h | 2 | ||||
-rw-r--r-- | include/afb/afb-binding-v2.h | 2 | ||||
-rw-r--r-- | include/afb/afb-req-v1.h | 2 | ||||
-rw-r--r-- | include/afb/afb-req-v2.h | 2 | ||||
-rw-r--r-- | include/afb/afb-req.h (renamed from include/afb/afb-req-common.h) | 66 | ||||
-rw-r--r-- | include/afb/afb-request-itf.h | 167 | ||||
-rw-r--r-- | include/afb/afb-request.h | 362 | ||||
-rw-r--r-- | src/afb-hook.c | 2 | ||||
-rw-r--r-- | src/afb-hreq.c | 1 | ||||
-rw-r--r-- | src/afb-xreq.c | 215 | ||||
-rw-r--r-- | src/afb-xreq.h | 3 |
11 files changed, 652 insertions, 172 deletions
diff --git a/include/afb/afb-binding-v1.h b/include/afb/afb-binding-v1.h index 4f881cd9..bafb8761 100644 --- a/include/afb/afb-binding-v1.h +++ b/include/afb/afb-binding-v1.h @@ -19,7 +19,7 @@ struct json_object; -#include "afb-req-common.h" +#include "afb-req.h" #include "afb-event.h" #include "afb-service-common.h" #include "afb-daemon-common.h" diff --git a/include/afb/afb-binding-v2.h b/include/afb/afb-binding-v2.h index 8e88b4fd..621ddce6 100644 --- a/include/afb/afb-binding-v2.h +++ b/include/afb/afb-binding-v2.h @@ -21,7 +21,7 @@ #include "afb-auth.h" #include "afb-event.h" -#include "afb-req-common.h" +#include "afb-req.h" #include "afb-service-common.h" #include "afb-daemon-common.h" diff --git a/include/afb/afb-req-v1.h b/include/afb/afb-req-v1.h index db231352..3a737699 100644 --- a/include/afb/afb-req-v1.h +++ b/include/afb/afb-req-v1.h @@ -18,7 +18,7 @@ #pragma once #include <stdlib.h> -#include "afb-req-common.h" +#include "afb-req.h" /* * Stores 'req' on heap for asynchrnous use. diff --git a/include/afb/afb-req-v2.h b/include/afb/afb-req-v2.h index fe94e898..9ed203dc 100644 --- a/include/afb/afb-req-v2.h +++ b/include/afb/afb-req-v2.h @@ -17,7 +17,7 @@ #pragma once -#include "afb-req-common.h" +#include "afb-req.h" /* * Stores 'req' on heap for asynchrnous use. diff --git a/include/afb/afb-req-common.h b/include/afb/afb-req.h index 6784a3b1..71dd31d1 100644 --- a/include/afb/afb-req-common.h +++ b/include/afb/afb-req.h @@ -17,77 +17,17 @@ #pragma once -#include <stdarg.h> +#include "afb-request-itf.h" #include "afb-event.h" -struct json_object; -struct afb_stored_req; -struct afb_req; - -/* - * Describes an argument (or parameter) of a request - */ -struct afb_arg -{ - const char *name; /* name of the argument or NULL if invalid */ - const char *value; /* string representation of the value of the argument */ - /* original filename of the argument if path != NULL */ - const char *path; /* if not NULL, path of the received file for the argument */ - /* when the request is finalized this file is removed */ -}; - -/* - * Interface for handling requests. - * It records the functions to be called for the request. - * Don't use this structure directly. - * Use the helper functions documented below. - */ -struct afb_req_itf -{ - /* CAUTION: respect the order, add at the end */ - - struct json_object *(*json)(void *closure); - struct afb_arg (*get)(void *closure, const char *name); - - void (*success)(void *closure, struct json_object *obj, const char *info); - void (*fail)(void *closure, const char *status, const char *info); - - void (*vsuccess)(void *closure, struct json_object *obj, const char *fmt, va_list args); - void (*vfail)(void *closure, const char *status, const char *fmt, va_list args); - - void *(*context_get)(void *closure); - void (*context_set)(void *closure, void *value, void (*free_value)(void*)); - - void (*addref)(void *closure); - void (*unref)(void *closure); - - void (*session_close)(void *closure); - int (*session_set_LOA)(void *closure, unsigned level); - - int (*subscribe)(void *closure, struct afb_event event); - int (*unsubscribe)(void *closure, struct afb_event event); - - void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure); - int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result); - - void (*vverbose)(void *closure, int level, const char *file, int line, const char * func, const char *fmt, va_list args); - struct afb_stored_req *(*store)(void *closure); - void (*subcall_req)(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); - - int (*has_permission)(void *closure, const char *permission); - char *(*get_application_id)(void *closure); - - void *(*context_make)(void *closure, int replace, void *(*create_value)(void *creation_closure), void (*free_value)(void*), void *creation_closure); -}; - /* * Describes the request by bindings from afb-daemon */ struct afb_req { - const struct afb_req_itf *itf; /* the interface to use */ - void *closure; /* the closure argument for functions of 'itf' */ + const struct afb_request_itf *itf; /* the interface to use */ + struct afb_request *closure; /* the closure argument for functions of 'itf' */ }; /* diff --git a/include/afb/afb-request-itf.h b/include/afb/afb-request-itf.h new file mode 100644 index 00000000..f28788ee --- /dev/null +++ b/include/afb/afb-request-itf.h @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2016, 2017 "IoT.bzh" + * Author: José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +/* defined here */ +struct afb_arg; +struct afb_request; +struct afb_request_itf; + +/* referenced here */ +#include <stdarg.h> +struct json_object; +struct afb_req; +struct afb_event; +struct afb_eventid; +struct afb_stored_req; + +/* + * Describes an argument (or parameter) of a request + */ +struct afb_arg +{ + const char *name; /* name of the argument or NULL if invalid */ + const char *value; /* string representation of the value of the argument */ + /* original filename of the argument if path != NULL */ + const char *path; /* if not NULL, path of the received file for the argument */ + /* when the request is finalized this file is removed */ +}; + +struct afb_request +{ + const struct afb_request_itf *itf; +}; + +/* + * Interface for handling requests. + * It records the functions to be called for the request. + * Don't use this structure directly. + * Use the helper functions + */ +struct afb_request_itf +{ + /* CAUTION: respect the order, add at the end */ + + struct json_object *(*json)( + struct afb_request *request); + + struct afb_arg (*get)( + struct afb_request *request, + const char *name); + + void (*success)( + struct afb_request *request, + struct json_object *obj, + const char *info); + + void (*fail)( + struct afb_request *request, + const char *status, + const char *info); + + void (*vsuccess)( + struct afb_request *request, + struct json_object *obj, + const char *fmt, + va_list args); + + void (*vfail)( + struct afb_request *request, + const char *status, + const char *fmt, + va_list args); + + void *(*context_get)( + struct afb_request *request); + + void (*context_set)( + struct afb_request *request, + void *value, + void (*free_value)(void*)); + + void (*addref)( + struct afb_request *request); + + void (*unref)( + struct afb_request *request); + + void (*session_close)( + struct afb_request *request); + + int (*session_set_LOA)( + struct afb_request *request, + unsigned level); + + int (*subscribe)( + struct afb_request *request, + struct afb_event event); + + int (*unsubscribe)( + struct afb_request *request, + struct afb_event event); + + void (*subcall)( + struct afb_request *request, + const char *api, + const char *verb, + struct json_object *args, + void (*callback)(void*, int, struct json_object*), + void *cb_closure); + + int (*subcallsync)( + struct afb_request *request, + const char *api, + const char *verb, + struct json_object *args, + struct json_object **result); + + void (*vverbose)( + struct afb_request *request, + int level, + const char *file, + int line, + const char * func, + const char *fmt, + va_list args); + + struct afb_stored_req *(*store)( + struct afb_request *request); + + void (*subcall_req)( + struct afb_request *request, + const char *api, + const char *verb, + struct json_object *args, + void (*callback)(void*, int, struct json_object*, struct afb_req), + void *cb_closure); + + int (*has_permission)( + struct afb_request *request, + const char *permission); + + char *(*get_application_id)( + struct afb_request *request); + + void *(*context_make)( + struct afb_request *request, + int replace, + void *(*create_value)(void *creation_closure), + void (*free_value)(void*), + void *creation_closure); +}; + diff --git a/include/afb/afb-request.h b/include/afb/afb-request.h new file mode 100644 index 00000000..052cbab9 --- /dev/null +++ b/include/afb/afb-request.h @@ -0,0 +1,362 @@ +/* + * Copyright (C) 2016, 2017 "IoT.bzh" + * Author: José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "afb-request-itf.h" + +typedef struct afb_request afb_request; + +#include "afb-event.h" + +/* + * Gets from the request 'request' the argument of 'name'. + * Returns a PLAIN structure of type 'struct afb_arg'. + * When the argument of 'name' is not found, all fields of result are set to NULL. + * When the argument of 'name' is found, the fields are filled, + * in particular, the field 'result.name' is set to 'name'. + * + * There is a special name value: the empty string. + * The argument of name "" is defined only if the request was made using + * an HTTP POST of Content-Type "application/json". In that case, the + * argument of name "" receives the value of the body of the HTTP request. + */ +static inline struct afb_arg afb_request_get(struct afb_request *request, const char *name) +{ + return request->itf->get(request, name); +} + +/* + * Gets from the request 'request' the string value of the argument of 'name'. + * Returns NULL if when there is no argument of 'name'. + * Returns the value of the argument of 'name' otherwise. + * + * Shortcut for: afb_request_get(request, name).value + */ +static inline const char *afb_request_value(struct afb_request *request, const char *name) +{ + return afb_request_get(request, name).value; +} + +/* + * Gets from the request 'request' the path for file attached to the argument of 'name'. + * Returns NULL if when there is no argument of 'name' or when there is no file. + * Returns the path of the argument of 'name' otherwise. + * + * Shortcut for: afb_request_get(request, name).path + */ +static inline const char *afb_request_path(struct afb_request *request, const char *name) +{ + return afb_request_get(request, name).path; +} + +/* + * Gets from the request 'request' the json object hashing the arguments. + * The returned object must not be released using 'json_object_put'. + */ +static inline struct json_object *afb_request_json(struct afb_request *request) +{ + return request->itf->json(request); +} + +/* + * Sends a reply of kind success to the request 'request'. + * The status of the reply is automatically set to "success". + * Its send the object 'obj' (can be NULL) with an + * informationnal comment 'info (can also be NULL). + * + * For convenience, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +static inline void afb_request_success(struct afb_request *request, struct json_object *obj, const char *info) +{ + request->itf->success(request, obj, info); +} + +/* + * Same as 'afb_request_success' but the 'info' is a formatting + * string followed by arguments. + * + * For convenience, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +static inline void afb_request_success_f(struct afb_request *request, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4))); +static inline void afb_request_success_f(struct afb_request *request, struct json_object *obj, const char *info, ...) +{ + va_list args; + va_start(args, info); + request->itf->vsuccess(request, obj, info, args); + va_end(args); +} + +/* + * Same as 'afb_request_success_f' but the arguments to the format 'info' + * are given as a variable argument list instance. + * + * For convenience, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +static inline void afb_request_success_v(struct afb_request *request, struct json_object *obj, const char *info, va_list args) +{ + request->itf->vsuccess(request, obj, info, args); +} + +/* + * Sends a reply of kind failure to the request 'request'. + * The status of the reply is set to 'status' and an + * informationnal comment 'info' (can also be NULL) can be added. + * + * Note that calling afb_request_fail("success", info) is equivalent + * to call afb_request_success(NULL, info). Thus even if possible it + * is strongly recommanded to NEVER use "success" for status. + */ +static inline void afb_request_fail(struct afb_request *request, const char *status, const char *info) +{ + request->itf->fail(request, status, info); +} + +/* + * Same as 'afb_request_fail' but the 'info' is a formatting + * string followed by arguments. + */ +static inline void afb_request_fail_f(struct afb_request *request, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4))); +static inline void afb_request_fail_f(struct afb_request *request, const char *status, const char *info, ...) +{ + va_list args; + va_start(args, info); + request->itf->vfail(request, status, info, args); + va_end(args); +} + +/* + * Same as 'afb_request_fail_f' but the arguments to the format 'info' + * are given as a variable argument list instance. + */ +static inline void afb_request_fail_v(struct afb_request *request, const char *status, const char *info, va_list args) +{ + request->itf->vfail(request, status, info, args); +} + +/* + * Gets the pointer stored by the binding for the session of 'request'. + * When the binding has not yet recorded a pointer, NULL is returned. + */ +static inline void *afb_request_context_get(struct afb_request *request) +{ + return request->itf->context_make(request, 0, 0, 0, 0); +} + +/* + * Stores for the binding the pointer 'context' to the session of 'request'. + * The function 'free_context' will be called when the session is closed + * or if binding stores an other pointer. + */ +static inline void afb_request_context_set(struct afb_request *request, void *context, void (*free_context)(void*)) +{ + request->itf->context_make(request, 1, 0, free_context, context); +} + +/* + * Gets the pointer stored by the binding for the session of 'request'. + * If no previous pointer is stored or if 'replace' is not zero, a new value + * is generated using the function 'create_context' called with the 'closure'. + * If 'create_context' is NULL the generated value is 'closure'. + * When a value is created, the function 'free_context' is recorded and will + * be called (with the created value as argument) to free the created value when + * it is not more used. + * This function is atomic: it ensures that 2 threads will not race together. + */ +static inline void *afb_request_context(struct afb_request *request, int replace, void *(*create_context)(void *closure), void (*free_context)(void*), void *closure) +{ + return request->itf->context_make(request, replace, create_context, free_context, closure); +} + +/* + * Frees the pointer stored by the binding for the session of 'request' + * and sets it to NULL. + * + * Shortcut for: afb_request_context_set(request, NULL, NULL) + */ +static inline void afb_request_context_clear(struct afb_request *request) +{ + request->itf->context_make(request, 1, 0, 0, 0); +} + +/* + * Adds one to the count of references of 'request'. + * This function MUST be called by asynchronous implementations + * of verbs if no reply was sent before returning. + */ +static inline void afb_request_addref(struct afb_request *request) +{ + request->itf->addref(request); +} + +/* + * Substracts one to the count of references of 'request'. + * This function MUST be called by asynchronous implementations + * of verbs after sending the asynchronous reply. + */ +static inline void afb_request_unref(struct afb_request *request) +{ + request->itf->unref(request); +} + +/* + * Closes the session associated with 'request' + * and delete all associated contexts. + */ +static inline void afb_request_session_close(struct afb_request *request) +{ + request->itf->session_close(request); +} + +/* + * Sets the level of assurance of the session of 'request' + * to 'level'. The effect of this function is subject of + * security policies. + * Returns 1 on success or 0 if failed. + */ +static inline int afb_request_session_set_LOA(struct afb_request *request, unsigned level) +{ + return request->itf->session_set_LOA(request, level); +} + +/* + * Establishes for the client link identified by 'request' a subscription + * to the 'event'. + * Returns 0 in case of successful subscription or -1 in case of error. + */ +static inline int afb_request_subscribe(struct afb_request *request, struct afb_event event) +{ + return request->itf->subscribe(request, event); +} + +/* + * Revokes the subscription established to the 'event' for the client + * link identified by 'request'. + * Returns 0 in case of successful subscription or -1 in case of error. + */ +static inline int afb_request_unsubscribe(struct afb_request *request, struct afb_event event) +{ + return request->itf->unsubscribe(request, event); +} + +/* + * Makes a call to the method of name 'api' / 'verb' with the object 'args'. + * This call is made in the context of the request 'request'. + * On completion, the function 'callback' is invoked with the + * 'closure' given at call and two other parameters: 'iserror' and 'result'. + * 'status' is 0 on success or negative when on an error reply. + * 'result' is the json object of the reply, you must not call json_object_put + * on the result. + * + * For convenience, the function calls 'json_object_put' for 'args'. + * Thus, in the case where 'args' should remain available after + * the function returns, the function 'json_object_get' shall be used. + * + * See also: + * - 'afb_request_subcall_req' that is convenient to keep request alive automatically. + * - 'afb_request_subcall_sync' the synchronous version + */ +static inline void afb_request_subcall(struct afb_request *request, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result), void *closure) +{ + request->itf->subcall(request, api, verb, args, callback, closure); +} + +/* + * Makes a call to the method of name 'api' / 'verb' with the object 'args'. + * This call is made in the context of the request 'request'. + * This call is synchronous, it waits untill completion of the request. + * It returns 0 on success or a negative value on error answer. + * The object pointed by 'result' is filled and must be released by the caller + * after its use by calling 'json_object_put'. + * + * For convenience, the function calls 'json_object_put' for 'args'. + * Thus, in the case where 'args' should remain available after + * the function returns, the function 'json_object_get' shall be used. + * + * See also: + * - 'afb_request_subcall_req' that is convenient to keep request alive automatically. + * - 'afb_request_subcall' that doesn't keep request alive automatically. + */ +static inline int afb_request_subcall_sync(struct afb_request *request, const char *api, const char *verb, struct json_object *args, struct json_object **result) +{ + return request->itf->subcallsync(request, api, verb, args, result); +} + +/* + * Send associated to 'request' a message described by 'fmt' and following parameters + * to the journal for the verbosity 'level'. + * + * 'file', 'line' and 'func' are indicators of position of the code in source files + * (see macros __FILE__, __LINE__ and __func__). + * + * 'level' is defined by syslog standard: + * EMERGENCY 0 System is unusable + * ALERT 1 Action must be taken immediately + * CRITICAL 2 Critical conditions + * ERROR 3 Error conditions + * WARNING 4 Warning conditions + * NOTICE 5 Normal but significant condition + * INFO 6 Informational + * DEBUG 7 Debug-level messages + */ +static inline void afb_request_verbose(struct afb_request *request, int level, const char *file, int line, const char * func, const char *fmt, ...) __attribute__((format(printf, 6, 7))); +static inline void afb_request_verbose(struct afb_request *request, int level, const char *file, int line, const char * func, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + request->itf->vverbose(request, level, file, line, func, fmt, args); + va_end(args); +} + +/* macro for setting file, line and function automatically */ +# if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS) +#define AFB_REQUEST_VERBOSE(request,level,...) afb_request_verbose(request,level,__FILE__,__LINE__,__func__,__VA_ARGS__) +#else +#define AFB_REQUEST_VERBOSE(request,level,...) afb_request_verbose(request,level,NULL,0,NULL,__VA_ARGS__) +#endif + +/* + * Check whether the 'permission' is granted or not to the client + * identified by 'request'. + * + * Returns 1 if the permission is granted or 0 otherwise. + */ +static inline int afb_request_has_permission(struct afb_request *request, const char *permission) +{ + return request->itf->has_permission(request, permission); +} + +/* + * Get the application identifier of the client application for the + * request 'request'. + * + * Returns the application identifier or NULL when the application + * can not be identified. + * + * The returned value if not NULL must be freed by the caller + */ +static inline char *afb_request_get_application_id(struct afb_request *request) +{ + return request->itf->get_application_id(request); +} + diff --git a/src/afb-hook.c b/src/afb-hook.c index 68e524fd..12cf7a12 100644 --- a/src/afb-hook.c +++ b/src/afb-hook.c @@ -28,7 +28,7 @@ #include <json-c/json.h> -#include <afb/afb-req-common.h> +#include <afb/afb-req.h> #include <afb/afb-event.h> #include "afb-context.h" diff --git a/src/afb-hreq.c b/src/afb-hreq.c index ba11e741..db804eb2 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -33,7 +33,6 @@ #endif #include "afb-method.h" -#include <afb/afb-req-common.h> #include "afb-msg-json.h" #include "afb-context.h" #include "afb-hreq.h" diff --git a/src/afb-xreq.c b/src/afb-xreq.c index 9c0ba16e..8a050705 100644 --- a/src/afb-xreq.c +++ b/src/afb-xreq.c @@ -25,6 +25,7 @@ #include <json-c/json.h> #include <afb/afb-binding-v1.h> #include <afb/afb-binding-v2.h> +#include <afb/afb-request.h> #include "afb-context.h" #include "afb-xreq.h" @@ -64,9 +65,19 @@ inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq) /******************************************************************************/ +static inline struct afb_request *to_request(struct afb_xreq *xreq) +{ + return &xreq->request; +} + static inline struct afb_req to_req(struct afb_xreq *xreq) { - return (struct afb_req){ .itf = xreq->itf, .closure = xreq }; + return (struct afb_req){ .itf = xreq->request.itf, .closure = &xreq->request }; +} + +static inline struct afb_xreq *from_request(struct afb_request *request) +{ + return CONTAINER_OF(struct afb_xreq, request, request); } /******************************************************************************/ @@ -302,17 +313,17 @@ static void vinfo(void *first, void *second, const char *fmt, va_list args, void /******************************************************************************/ -static struct json_object *xreq_json_cb(void *closure) +static struct json_object *xreq_json_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); if (!xreq->json && xreq->queryitf->json) xreq->json = xreq->queryitf->json(xreq); return xreq->json; } -static struct afb_arg xreq_get_cb(void *closure, const char *name) +static struct afb_arg xreq_get_cb(struct afb_request *closure, const char *name) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); struct afb_arg arg; struct json_object *object, *value; @@ -332,9 +343,9 @@ static struct afb_arg xreq_get_cb(void *closure, const char *name) return arg; } -static void xreq_success_cb(void *closure, struct json_object *obj, const char *info) +static void xreq_success_cb(struct afb_request *closure, struct json_object *obj, const char *info) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); if (xreq->replied) { ERROR("reply called more than one time!!"); @@ -348,9 +359,9 @@ static void xreq_success_cb(void *closure, struct json_object *obj, const char * } } -static void xreq_fail_cb(void *closure, const char *status, const char *info) +static void xreq_fail_cb(struct afb_request *closure, const char *status, const char *info) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); if (xreq->replied) { ERROR("reply called more than one time!!"); @@ -363,55 +374,55 @@ static void xreq_fail_cb(void *closure, const char *status, const char *info) } } -static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args) +static void xreq_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args) { vinfo(closure, obj, fmt, args, (void*)xreq_success_cb); } -static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args) +static void xreq_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args) { vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb); } -static void *xreq_context_get_cb(void *closure) +static void *xreq_context_get_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_context_get(&xreq->context); } -static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*)) +static void xreq_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*)) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_context_set(&xreq->context, value, free_value); } -static void xreq_addref_cb(void *closure) +static void xreq_addref_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_xreq_unhooked_addref(xreq); } -static void xreq_unref_cb(void *closure) +static void xreq_unref_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_xreq_unhooked_unref(xreq); } -static void xreq_session_close_cb(void *closure) +static void xreq_session_close_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_context_close(&xreq->context); } -static int xreq_session_set_LOA_cb(void *closure, unsigned level) +static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_context_change_loa(&xreq->context, level); } -static int xreq_subscribe_cb(void *closure, struct afb_event event) +static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_xreq_subscribe(xreq, event); } @@ -426,9 +437,9 @@ int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event) return -1; } -static int xreq_unsubscribe_cb(void *closure, struct afb_event event) +static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_xreq_unsubscribe(xreq, event); } @@ -443,9 +454,9 @@ int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event) return -1; } -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) +static void xreq_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); struct subcall *sc; sc = subcall_alloc(xreq, api, verb, args); @@ -458,9 +469,9 @@ static void xreq_subcall_cb(void *closure, const char *api, const char *verb, st } } -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) +static void xreq_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); struct subcall *sc; sc = subcall_alloc(xreq, api, verb, args); @@ -474,11 +485,11 @@ static void xreq_subcall_req_cb(void *closure, const char *api, const char *verb } -static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result) +static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result) { int rc; struct subcall *sc; - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); struct json_object *resu; sc = subcall_alloc(xreq, api, verb, args); @@ -496,10 +507,10 @@ static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, return rc; } -static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args) +static void xreq_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args) { char *p; - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); if (!fmt || vasprintf(&p, fmt, args) < 0) vverbose(level, file, line, func, fmt, args); @@ -509,129 +520,129 @@ static void xreq_vverbose_cb(void*closure, int level, const char *file, int line } } -static struct afb_stored_req *xreq_store_cb(void *closure) +static struct afb_stored_req *xreq_store_cb(struct afb_request *closure) { xreq_addref_cb(closure); - return closure; + return (struct afb_stored_req*)closure; } -static int xreq_has_permission_cb(void*closure, const char *permission) +static int xreq_has_permission_cb(struct afb_request *closure, const char *permission) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_auth_has_permission(xreq, permission); } -static char *xreq_get_application_id_cb(void*closure) +static char *xreq_get_application_id_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL; } -static void *xreq_context_make_cb(void *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure) +static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure); } /******************************************************************************/ -static struct json_object *xreq_hooked_json_cb(void *closure) +static struct json_object *xreq_hooked_json_cb(struct afb_request *closure) { struct json_object *r = xreq_json_cb(closure); - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_hook_xreq_json(xreq, r); } -static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name) +static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name) { struct afb_arg r = xreq_get_cb(closure, name); - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_hook_xreq_get(xreq, name, r); } -static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info) +static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_hook_xreq_success(xreq, obj, info); xreq_success_cb(closure, obj, info); } -static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info) +static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_hook_xreq_fail(xreq, status, info); xreq_fail_cb(closure, status, info); } -static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args) +static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args) { vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb); } -static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args) +static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args) { vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb); } -static void *xreq_hooked_context_get_cb(void *closure) +static void *xreq_hooked_context_get_cb(struct afb_request *closure) { void *r = xreq_context_get_cb(closure); - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_hook_xreq_context_get(xreq, r); } -static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*)) +static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*)) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_hook_xreq_context_set(xreq, value, free_value); xreq_context_set_cb(closure, value, free_value); } -static void xreq_hooked_addref_cb(void *closure) +static void xreq_hooked_addref_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_hook_xreq_addref(xreq); xreq_addref_cb(closure); } -static void xreq_hooked_unref_cb(void *closure) +static void xreq_hooked_unref_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_hook_xreq_unref(xreq); xreq_unref_cb(closure); } -static void xreq_hooked_session_close_cb(void *closure) +static void xreq_hooked_session_close_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_hook_xreq_session_close(xreq); xreq_session_close_cb(closure); } -static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level) +static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level) { int r = xreq_session_set_LOA_cb(closure, level); - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_hook_xreq_session_set_LOA(xreq, level, r); } -static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event) +static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event) { int r = xreq_subscribe_cb(closure, event); - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_hook_xreq_subscribe(xreq, event, r); } -static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event) +static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event) { int r = xreq_unsubscribe_cb(closure, event); - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); return afb_hook_xreq_unsubscribe(xreq, event, r); } -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) +static void xreq_hooked_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); struct subcall *sc; afb_hook_xreq_subcall(xreq, api, verb, args); @@ -645,9 +656,9 @@ static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *v } } -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) +static void xreq_hooked_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); struct subcall *sc; afb_hook_xreq_subcall_req(xreq, api, verb, args); @@ -661,18 +672,18 @@ static void xreq_hooked_subcall_req_cb(void *closure, const char *api, const cha } } -static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result) +static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result) { int r; - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); afb_hook_xreq_subcallsync(xreq, api, verb, args); r = xreq_subcallsync_cb(closure, api, verb, args, result); return afb_hook_xreq_subcallsync_result(xreq, r, *result); } -static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args) +static void xreq_hooked_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); va_list ap; va_copy(ap, args); xreq_vverbose_cb(closure, level, file, line, func, fmt, args); @@ -680,38 +691,38 @@ static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, i va_end(ap); } -static struct afb_stored_req *xreq_hooked_store_cb(void *closure) +static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); struct afb_stored_req *r = xreq_store_cb(closure); afb_hook_xreq_store(xreq, r); return r; } -static int xreq_hooked_has_permission_cb(void*closure, const char *permission) +static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); int r = xreq_has_permission_cb(closure, permission); return afb_hook_xreq_has_permission(xreq, permission, r); } -static char *xreq_hooked_get_application_id_cb(void*closure) +static char *xreq_hooked_get_application_id_cb(struct afb_request *closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); char *r = xreq_get_application_id_cb(closure); return afb_hook_xreq_get_application_id(xreq, r); } -static void *xreq_hooked_context_make_cb(void *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure) +static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure) { - struct afb_xreq *xreq = closure; + struct afb_xreq *xreq = from_request(closure); void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure); return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result); } /******************************************************************************/ -const struct afb_req_itf xreq_itf = { +const struct afb_request_itf xreq_itf = { .json = xreq_json_cb, .get = xreq_get_cb, .success = xreq_success_cb, @@ -736,7 +747,7 @@ const struct afb_req_itf xreq_itf = { .context_make = xreq_context_make_cb }; -const struct afb_req_itf xreq_hooked_itf = { +const struct afb_request_itf xreq_hooked_itf = { .json = xreq_hooked_json_cb, .get = xreq_hooked_get_cb, .success = xreq_hooked_success_cb, @@ -773,12 +784,12 @@ struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq) struct json_object *afb_xreq_json(struct afb_xreq *xreq) { - return afb_req_json(to_req(xreq)); + return afb_request_json(to_request(xreq)); } void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info) { - afb_req_success(to_req(xreq), obj, info); + afb_request_success(to_request(xreq), obj, info); } void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...) @@ -786,13 +797,13 @@ void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const ch va_list args; va_start(args, info); - afb_req_success_v(to_req(xreq), obj, info, args); + afb_request_success_v(to_request(xreq), obj, info, args); va_end(args); } void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info) { - afb_req_fail(to_req(xreq), status, info); + afb_request_fail(to_request(xreq), status, info); } void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...) @@ -800,14 +811,14 @@ void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info va_list args; va_start(args, info); - afb_req_fail_v(to_req(xreq), status, info, args); + afb_request_fail_v(to_request(xreq), status, info, args); va_end(args); } const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size) { - struct json_object *obj = xreq_json_cb(xreq); + struct json_object *obj = xreq_json_cb(to_request(xreq)); const char *result = json_object_to_json_string(obj); if (size != NULL) *size = strlen(result); @@ -816,32 +827,32 @@ const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size) void afb_xreq_addref(struct afb_xreq *xreq) { - afb_req_addref(to_req(xreq)); + afb_request_addref(to_request(xreq)); } void afb_xreq_unref(struct afb_xreq *xreq) { - afb_req_unref(to_req(xreq)); + afb_request_unref(to_request(xreq)); } 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) { - xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure); + xreq_subcall_cb(to_request(xreq), api, verb, args, callback, cb_closure); } 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) { - afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure); + afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure); } int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result) { - return xreq_subcallsync_cb(xreq, api, verb, args, result); + return xreq_subcallsync_cb(to_request(xreq), api, verb, args, result); } int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result) { - return afb_req_subcall_sync(to_req(xreq), api, verb, args, result); + return afb_request_subcall_sync(to_request(xreq), api, verb, args, result); } static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags) @@ -943,7 +954,7 @@ void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf) { memset(xreq, 0, sizeof *xreq); - xreq->itf = &xreq_hooked_itf; /* hook by default */ + xreq->request.itf = &xreq_hooked_itf; /* hook by default */ xreq->refcount = 1; xreq->queryitf = queryitf; } @@ -964,7 +975,7 @@ static void init_hooking(struct afb_xreq *xreq) if (xreq->hookflags) afb_hook_xreq_begin(xreq); else - xreq->itf = &xreq_itf; /* unhook the interface */ + xreq->request.itf = &xreq_itf; /* unhook the interface */ } /** @@ -1004,7 +1015,7 @@ static void early_failure(struct afb_xreq *xreq, const char *status, const char /* send error */ va_start(args, info); - afb_req_fail_v(to_req(xreq), status, info, args); + afb_request_fail_v(to_request(xreq), status, info, args); va_end(args); } diff --git a/src/afb-xreq.h b/src/afb-xreq.h index 4978f0a5..df868bf6 100644 --- a/src/afb-xreq.h +++ b/src/afb-xreq.h @@ -17,6 +17,7 @@ #pragma once +#include <afb/afb-request-itf.h> #include "afb-context.h" struct json_object; @@ -55,7 +56,7 @@ struct afb_xreq_query_itf { */ struct afb_xreq { - const struct afb_req_itf *itf; /**< interface functions */ + struct afb_request request; /**< exported request */ struct afb_context context; /**< context of the request */ struct afb_apiset *apiset; /**< apiset of the xreq */ const char *api; /**< the requested API */ |