aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-hook.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-03-27 11:23:51 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-03-27 11:23:51 +0200
commit44f21bd2a3b50f92669223cdafe79993654c1e19 (patch)
treeb8656cf9c11f25183bd95822c085ce35a459a9e5 /src/afb-hook.c
parentfeccdb76f572a5fad947475c21b5b9aff696b04b (diff)
Simplify functions for calls
For historical reasons, the call to apis was passing the length of the api and the length of the verb. The reason was to avoid a copy of strings. But the copy occured only for HTTP requests. Having this implementation is of small interest and compromise future changes. This patch simplify things. Change-Id: I8157724c6c721b6797cd0eab52b07e1b8d6eb5f8 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/afb-hook.c')
-rw-r--r--src/afb-hook.c46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/afb-hook.c b/src/afb-hook.c
index d5c534d0..f59192a7 100644
--- a/src/afb-hook.c
+++ b/src/afb-hook.c
@@ -272,25 +272,33 @@ static void hook_req_unref(struct afb_hook_req *tr)
}
}
-static struct afb_hook_req *hook_req_create(struct afb_req req, struct afb_context *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
+static struct afb_hook_req *hook_req_create(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
{
+ int len;
+ char name[257];
unsigned id;
struct afb_hook_req *tr;
- tr = malloc(sizeof *tr + 8 + lenapi + lenverb);
- if (tr != NULL) {
- /* get the call id */
- id = ++hook_count;
- if (id == 1000000)
- id = hook_count = 1;
-
- /* init hook */
- tr->observers = NULL;
- tr->refcount = 1;
- tr->context = context;
- tr->req = req;
- afb_req_addref(req);
- snprintf(tr->name, 9 + lenapi + lenverb, "%06d:%.*s/%.*s", id, (int)lenapi, api, (int)lenverb, verb);
+ /* get the call id */
+ id = ++hook_count;
+ if (id == 1000000)
+ id = hook_count = 1;
+
+ /* creates the name */
+ len = snprintf(name, sizeof name, "%06d:%s/%s", id, api, verb);
+ if (len < 0 || (size_t)len >= sizeof name) {
+ tr = NULL;
+ } else {
+ tr = malloc(sizeof *tr + (size_t)len);
+ if (tr != NULL) {
+ /* init hook */
+ tr->observers = NULL;
+ tr->refcount = 1;
+ tr->context = context;
+ tr->req = req;
+ afb_req_addref(req);
+ memcpy(tr->name, name, (size_t)(len + 1));
+ }
}
return tr;
}
@@ -494,7 +502,7 @@ static struct afb_req_itf req_hook_itf = {
* section:
*****************************************************************************/
-struct afb_req afb_hook_req_call(struct afb_req req, struct afb_context *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
+struct afb_req afb_hook_req_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
{
int add;
struct afb_hook_req *tr;
@@ -506,11 +514,11 @@ struct afb_req afb_hook_req_call(struct afb_req req, struct afb_context *context
do {
add = (hook->flags & afb_hook_flags_req_all) != 0
&& (!hook->session || hook->session == context->session)
- && (!hook->api || !(memcmp(hook->api, api, lenapi) || hook->api[lenapi]))
- && (!hook->verb || !(memcmp(hook->verb, verb, lenverb) || hook->verb[lenverb]));
+ && (!hook->api || !strcasecmp(hook->api, api))
+ && (!hook->verb || !strcasecmp(hook->verb, verb));
if (add) {
if (!tr)
- tr = hook_req_create(req, context, api, lenapi, verb, lenverb);
+ tr = hook_req_create(req, context, api, verb);
if (tr)
hook_req_add_observer(tr, hook);
}