summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/afb-api-so.c15
-rw-r--r--src/afb-apis.c9
-rw-r--r--src/afb-apis.h2
-rw-r--r--src/afb-hreq.c6
-rw-r--r--src/afb-ws-json.c7
-rw-r--r--src/main.c3
-rw-r--r--src/session.c33
-rw-r--r--src/session.h13
8 files changed, 45 insertions, 43 deletions
diff --git a/src/afb-api-so.c b/src/afb-api-so.c
index 60af72ef..c3cb08d7 100644
--- a/src/afb-api-so.c
+++ b/src/afb-api-so.c
@@ -86,17 +86,6 @@ static const struct afb_daemon_itf daemon_itf = {
.get_pollmgr = (void*)afb_api_so_get_pollmgr
};
-static void free_context(struct api_so_desc *desc, void *context)
-{
- void (*cb)(void*);
-
- cb = desc->plugin->freeCtxCB;
- if (cb)
- cb(context);
- else
- free(context);
-}
-
static void trapping_call(struct afb_req req, void(*cb)(struct afb_req))
{
volatile int signum, timerset;
@@ -159,7 +148,6 @@ static void call_check(struct afb_req req, const struct AFB_restapi *verb)
break;
}
trapping_call(req, verb->callback);
-
if (verb->session == AFB_SESSION_CLOSE)
afb_req_session_close(req);
}
@@ -238,8 +226,7 @@ int afb_api_so_add_plugin(const char *path)
/* records the plugin */
if (afb_apis_add(desc->plugin->prefix, (struct afb_api){
.closure = desc,
- .call = (void*)call,
- .free_context = (void*)free_context}) < 0) {
+ .call = (void*)call}) < 0) {
fprintf(stderr, "ERROR: plugin [%s] can't be registered...\n", path);
goto error3;
}
diff --git a/src/afb-apis.c b/src/afb-apis.c
index 0af3b42c..2822575b 100644
--- a/src/afb-apis.c
+++ b/src/afb-apis.c
@@ -41,13 +41,6 @@ int afb_apis_count()
return apis_count;
}
-void afb_apis_free_context(int apiidx, void *context)
-{
- const struct afb_api *api;
- api = &apis_array[apiidx].api;
- api->free_context(api->closure, context);
-}
-
int afb_apis_add(const char *name, struct afb_api api)
{
struct api_desc *apis;
@@ -98,7 +91,7 @@ void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char
a = apis_array;
for (i = 0 ; i < apis_count ; i++, a++) {
if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
- req.context = &context->contexts[i];
+ req.ctx_closure = &context->contexts[i];
a->api.call(a->api.closure, req, verb, lenverb);
return;
}
diff --git a/src/afb-apis.h b/src/afb-apis.h
index 59c7008f..985923f5 100644
--- a/src/afb-apis.h
+++ b/src/afb-apis.h
@@ -24,7 +24,6 @@ struct afb_api
{
void *closure;
void (*call)(void *closure, struct afb_req req, const char *verb, size_t lenverb);
- void (*free_context)(void *closure, void *context);
};
@@ -32,5 +31,4 @@ extern int afb_apis_count();
extern int afb_apis_add(const char *name, struct afb_api api);
extern void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb);
-extern void afb_apis_free_context(int apiidx, void *context);
diff --git a/src/afb-hreq.c b/src/afb-hreq.c
index 93cce62b..72b90504 100644
--- a/src/afb-hreq.c
+++ b/src/afb-hreq.c
@@ -81,7 +81,9 @@ static const struct afb_req_itf afb_hreq_itf = {
.send = (void*)req_send,
.session_create = (void*)req_session_create,
.session_check = (void*)req_session_check,
- .session_close = (void*)req_session_close
+ .session_close = (void*)req_session_close,
+ .context_get = (void*)afb_context_get,
+ .context_set = (void*)afb_context_set
};
static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
@@ -584,7 +586,7 @@ int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *f
struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
{
- return (struct afb_req){ .itf = &afb_hreq_itf, .data = hreq };
+ return (struct afb_req){ .itf = &afb_hreq_itf, .req_closure = hreq };
}
static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
diff --git a/src/afb-ws-json.c b/src/afb-ws-json.c
index a1766888..a34142c9 100644
--- a/src/afb-ws-json.c
+++ b/src/afb-ws-json.c
@@ -141,7 +141,10 @@ static const struct afb_req_itf wsreq_itf = {
.send = (void*)wsreq_send,
.session_create = (void*)wsreq_session_create,
.session_check = (void*)wsreq_session_check,
- .session_close = (void*)wsreq_session_close
+ .session_close = (void*)wsreq_session_close,
+ .context_get = (void*)afb_context_get,
+ .context_set = (void*)afb_context_set
+
};
static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
@@ -297,7 +300,7 @@ static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
wsreq->next = ws->requests;
ws->requests = wsreq;
- r.data = wsreq;
+ r.req_closure = wsreq;
r.itf = &wsreq_itf;
afb_apis_call(r, ws->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
return;
diff --git a/src/main.c b/src/main.c
index e899739c..8ec684fe 100644
--- a/src/main.c
+++ b/src/main.c
@@ -34,6 +34,7 @@
#include "afb-config.h"
#include "afb-hswitch.h"
+#include "afb-apis.h"
#include "afb-api-so.h"
#include "afb-hsrv.h"
#include "afb-hreq.h"
@@ -577,7 +578,7 @@ int main(int argc, char *argv[]) {
if (config->ldpaths)
afb_api_so_add_pathset(config->ldpaths);
- ctxStoreInit(CTX_NBCLIENTS, config->cntxTimeout, config->token);
+ ctxStoreInit(CTX_NBCLIENTS, config->cntxTimeout, config->token, afb_apis_count());
if (!afb_hreq_init_cookie(config->httpdPort, config->rootapi, DEFLT_CNTX_TIMEOUT)) {
fprintf (stderr, "ERR: initialisation of cookies failed\n");
exit (1);
diff --git a/src/session.c b/src/session.c
index 329e17ab..fbc7f2fb 100644
--- a/src/session.c
+++ b/src/session.c
@@ -24,7 +24,6 @@
#include <uuid/uuid.h>
#include <assert.h>
-#include "afb-apis.h"
#include "session.h"
#define NOW (time(NULL))
@@ -40,6 +39,20 @@ static struct {
const char *initok;
} sessions;
+void *afb_context_get(struct afb_context *actx)
+{
+ return actx->context;
+}
+
+void afb_context_set(struct afb_context *actx, void *context, void (*free_context)(void*))
+{
+fprintf(stderr, "afb_context_set(%p,%p) was (%p,%p)\n",context, free_context, actx->context, actx->free_context);
+ if (actx->context != NULL && actx->free_context != NULL)
+ actx->free_context(actx->context);
+ actx->context = context;
+ actx->free_context = free_context;
+}
+
// Free context [XXXX Should be protected again memory abort XXXX]
static void ctxUuidFreeCB (struct AFB_clientCtx *client)
{
@@ -49,22 +62,18 @@ static void ctxUuidFreeCB (struct AFB_clientCtx *client)
assert (client->contexts != NULL);
// Free client handle with a standard Free function, with app callback or ignore it
- for (idx=0; idx < sessions.apicount; idx ++) {
- if (client->contexts[idx] != NULL) {
- afb_apis_free_context(idx, client->contexts[idx]);
- client->contexts[idx] = NULL;
- }
- }
+ for (idx=0; idx < sessions.apicount; idx ++)
+ afb_context_set(&client->contexts[idx], NULL, NULL);
}
// Create a new store in RAM, not that is too small it will be automatically extended
-void ctxStoreInit (int nbSession, int timeout, const char *initok)
+void ctxStoreInit (int max_session_count, int timeout, const char *initok, int context_count)
{
// let's create as store as hashtable does not have any
- sessions.store = calloc (1 + (unsigned)nbSession, sizeof(struct AFB_clientCtx));
- sessions.max = nbSession;
+ sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct AFB_clientCtx));
+ sessions.max = max_session_count;
sessions.timeout = timeout;
- sessions.apicount = afb_apis_count();
+ sessions.apicount = context_count;
if (strlen(initok) >= 37) {
fprintf(stderr, "Error: initial token '%s' too long (max length 36)", initok);
exit(1);
@@ -191,7 +200,7 @@ TODO remove? not remove?
/* returns a new one */
clientCtx = calloc(1, sizeof(struct AFB_clientCtx)); // init NULL clientContext
if (clientCtx != NULL) {
- clientCtx->contexts = calloc ((unsigned)sessions.apicount, sizeof (void*));
+ clientCtx->contexts = calloc ((unsigned)sessions.apicount, sizeof(*clientCtx->contexts));
if (clientCtx->contexts != NULL) {
/* generate the uuid */
if (uuid == NULL) {
diff --git a/src/session.h b/src/session.h
index 0a61612d..569af654 100644
--- a/src/session.h
+++ b/src/session.h
@@ -18,17 +18,26 @@
#pragma once
+struct afb_context
+{
+ void *context;
+ void (*free_context)(void*);
+};
+
+extern void *afb_context_get(struct afb_context *actx);
+extern void afb_context_set(struct afb_context *actx, void *context, void (*free_context)(void*));
+
struct AFB_clientCtx
{
time_t expiration; // expiration time of the token
int created;
unsigned refcount;
- void **contexts; // application specific context [one per plugin]
+ struct afb_context *contexts;
char uuid[37]; // long term authentication of remote client
char token[37]; // short term authentication of remote client
};
-extern void ctxStoreInit (int nbSession, int timeout, const char *initok);
+extern void ctxStoreInit (int max_session_count, int timeout, const char *initok, int context_count);
extern struct AFB_clientCtx *ctxClientGetForUuid (const char *uuid);
extern struct AFB_clientCtx *ctxClientGet(struct AFB_clientCtx *clientCtx);