From 520bbafb30ce49b94b7825ff553d8cb5bda6d612 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Wed, 30 Mar 2016 14:35:34 +0200 Subject: refactor context usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I5ba57724eac605ef6e5a134ab7d9db56d2df4a07 Signed-off-by: José Bollo --- include/proto-def.h | 8 -------- src/afb-rest-api.c | 6 ++++-- src/main.c | 1 + src/session.c | 9 ++------- src/session.h | 24 ++++++++++++++++++++++++ 5 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 src/session.h diff --git a/include/proto-def.h b/include/proto-def.h index 87bc63b0..21e03035 100644 --- a/include/proto-def.h +++ b/include/proto-def.h @@ -38,14 +38,6 @@ extern void endPostRequest(AFB_PostHandle *posthandle); extern int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method , const char *upload_data, size_t *upload_data_size, void **con_cls); -// Session handling -extern AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request); -extern AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request); -extern AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request); -extern AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request); -extern AFB_clientCtx *ctxClientGet (AFB_request *request, int idx); -extern void ctxStoreInit (int); - // Httpd server diff --git a/src/afb-rest-api.c b/src/afb-rest-api.c index febe19f8..0de762b1 100644 --- a/src/afb-rest-api.c +++ b/src/afb-rest-api.c @@ -79,14 +79,16 @@ static AFB_error doCallPluginApi(AFB_request * request, int apiidx, int verbidx, if (AFB_SESSION_NONE != session) { // add client context to request - clientCtx = ctxClientGet(request, apiidx); + clientCtx = ctxClientGet(request); 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 !!!")); json_object_object_add(jreqt, "request", jcall); goto ExitOnDone; - }; + } + request->context = clientCtx->contexts[apiidx]; + request->uuid = clientCtx->uuid; if (verbose) fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[%p] Uuid=[%s] Token=[%s]\n", request->prefix, request->api, session, clientCtx, clientCtx->uuid, clientCtx->token); diff --git a/src/main.c b/src/main.c index b42f2ab6..400a55c0 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,7 @@ #include "local-def.h" #include "afb-apis.h" +#include "session.h" #if !defined(PLUGIN_INSTALL_DIR) #error "you should define PLUGIN_INSTALL_DIR" diff --git a/src/session.c b/src/session.c index 9569b46c..9504fdbe 100644 --- a/src/session.c +++ b/src/session.c @@ -31,6 +31,7 @@ #include #include "afb-apis.h" +#include "session.h" // Session UUID are store in a simple array [for 10 sessions this should be enough] static struct { @@ -169,7 +170,7 @@ void ctxStoreGarbage (const int timeout) } // This function will return exiting client context or newly created client context -AFB_clientCtx *ctxClientGet (AFB_request *request, int apiidx) +AFB_clientCtx *ctxClientGet (AFB_request *request) { AFB_clientCtx *clientCtx=NULL; const char *uuid; @@ -201,8 +202,6 @@ AFB_clientCtx *ctxClientGet (AFB_request *request, int apiidx) ctxStoreDel (clientCtx); clientCtx = NULL; } else { - request->context = clientCtx->contexts[apiidx]; - request->uuid = uuid; return clientCtx; } } @@ -225,10 +224,6 @@ AFB_clientCtx *ctxClientGet (AFB_request *request, int apiidx) free (clientCtx); return NULL; } - - // if (verbose) fprintf (stderr, "ctxClientGet New uuid=[%s] token=[%s] timestamp=%d\n", clientCtx->uuid, clientCtx->token, clientCtx->timeStamp); - request->context = clientCtx->contexts[apiidx]; - request->uuid = clientCtx->uuid; return clientCtx; } diff --git a/src/session.h b/src/session.h new file mode 100644 index 00000000..e3b9b811 --- /dev/null +++ b/src/session.h @@ -0,0 +1,24 @@ +/* + * Copyright 2016 IoT.bzh + * Author: José Bollo + * + * 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. + */ + +extern AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request); +extern AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request); +extern AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request); +extern AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request); +extern AFB_clientCtx *ctxClientGet (AFB_request *request); +extern void ctxStoreInit (int); + -- cgit 1.2.3-korg