From cf2b7c5be026071b696c62a8b0f260e624c9a357 Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Wed, 23 Dec 2015 00:55:32 +0100 Subject: Restore Previous functionalities with a unique Token --- plugins/session/token-api.c | 12 +++++++++++- src/rest-api.c | 9 +++++---- src/session.c | 5 +++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/plugins/session/token-api.c b/plugins/session/token-api.c index 0a5a2be4..25d0b993 100644 --- a/plugins/session/token-api.c +++ b/plugins/session/token-api.c @@ -77,6 +77,16 @@ STATIC json_object* clientContextReset (AFB_request *request) { // WARNING: if you free context resource manually here do not forget to set request->context=NULL; return (jresp); } +// Close and Free context +STATIC json_object* clientGetPing (AFB_request *request) { + static count=0; + json_object *jresp; + + jresp = json_object_new_object(); + json_object_object_add(jresp, "count", json_object_new_int (count ++)); + + return (jresp); +} // This function is call when Client Session Context is removed @@ -87,7 +97,7 @@ STATIC void clientContextFree(void *context, char* uuid) { } STATIC AFB_restapi pluginApis[]= { - {"ping" , AFB_SESSION_NONE , (AFB_apiCB)getPingTest ,"Ping Rest Test Service"}, + {"ping" , AFB_SESSION_NONE , (AFB_apiCB)clientGetPing ,"Ping Rest Test Service"}, {"create" , AFB_SESSION_CREATE, (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"}, {"refresh" , AFB_SESSION_RENEW , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"}, {"check" , AFB_SESSION_CHECK , (AFB_apiCB)clientContextCheck ,"Check Client Context Token"}, diff --git a/src/rest-api.c b/src/rest-api.c index 1a44b33f..3ae9cc0e 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -50,10 +50,11 @@ PUBLIC void endPostRequest(AFB_PostHandle *postHandle) { } // Check of apiurl is declare in this plugin and call it -STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *context) { +STATIC AFB_error callPluginApi(AFB_request *request, int plugidx, void *context) { json_object *jresp, *jcall; int idx, status, sig; AFB_clientCtx *clientCtx; + AFB_plugin *plugin = request->plugins[plugidx]; int signals[]= {SIGALRM, SIGSEGV, SIGFPE, 0}; /*--------------------------------------------------------------- @@ -117,8 +118,8 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *c if (AFB_SESSION_NONE != plugin->apis[idx].session) { // add client context to request - clientCtx = ctxClientGet(request, idx); - if (clientCtx != NULL) { + clientCtx = ctxClientGet(request, plugidx); + if (clientCtx == NULL) { request->errcode=MHD_HTTP_INSUFFICIENT_STORAGE; json_object_object_add(jcall, "status", json_object_new_string ("fail")); json_object_object_add(jcall, "info", json_object_new_string ("Client Session Context Full !!!")); @@ -237,7 +238,7 @@ STATIC AFB_error findAndCallApi (AFB_request *request, void *context) { // Search for a plugin with this urlpath for (idx = 0; request->plugins[idx] != NULL; idx++) { if (!strcmp(request->plugins[idx]->prefix, request->plugin)) { - status =callPluginApi(request->plugins[idx], request, context); + status =callPluginApi(request, idx, context); break; } } diff --git a/src/session.c b/src/session.c index 4769b10c..b44e37e8 100644 --- a/src/session.c +++ b/src/session.c @@ -476,6 +476,7 @@ PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx) { if (clientCtx == NULL) { clientCtx = calloc(1, sizeof(AFB_clientCtx)); // init NULL clientContext clientCtx->contexts = calloc (1, request->config->pluginCount * (sizeof (void*))); + clientCtx->plugins = request->plugins; } uuid_generate(newuuid); // create a new UUID @@ -500,7 +501,7 @@ PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx) { PUBLIC AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request) { const char *token; - if (request->context == NULL) return AFB_EMPTY; + if (clientCtx->contexts == NULL) return AFB_EMPTY; // this time have to extract token from query list token = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, "token"); @@ -558,7 +559,7 @@ PUBLIC AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request) uuid_unparse_lower(newuuid, clientCtx->token); // keep track of time for session timeout and further clean up - clientCtx->timeStamp=time(NULL); + clientCtx->timeStamp=time(NULL); // Token is also store in context but it might be convenient for plugin to access it directly return (AFB_SUCCESS); -- cgit 1.2.3-korg