refactor context usage
authorJosé Bollo <jose.bollo@iot.bzh>
Wed, 30 Mar 2016 12:35:34 +0000 (14:35 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Wed, 30 Mar 2016 12:35:34 +0000 (14:35 +0200)
Change-Id: I5ba57724eac605ef6e5a134ab7d9db56d2df4a07
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/proto-def.h
src/afb-rest-api.c
src/main.c
src/session.c
src/session.h [new file with mode: 0644]

index 87bc63b..21e0303 100644 (file)
@@ -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
index febe19f..0de762b 100644 (file)
@@ -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);
index b42f2ab..400a55c 100644 (file)
@@ -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"
index 9569b46..9504fdb 100644 (file)
@@ -31,6 +31,7 @@
 #include <search.h>
 
 #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 (file)
index 0000000..e3b9b81
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2016 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.
+ */
+
+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);
+