refactoring req interface
[src/app-framework-binder.git] / src / session.c
index e9cf298..936bb4d 100644 (file)
@@ -63,13 +63,13 @@ static void ctxUuidFreeCB (struct AFB_clientCtx *client)
 }
 
 // Create a new store in RAM, not that is too small it will be automatically extended
-void ctxStoreInit (int nbSession, int timeout, int apicount, const char *initok)
+void ctxStoreInit (int nbSession, int timeout, const char *initok)
 {
        // 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.timeout = timeout;
-       sessions.apicount = apicount;
+       sessions.apicount = afb_apis_count();
        if (strlen(initok) >= 37) {
                fprintf(stderr, "Error: initial token '%s' too long (max length 36)", initok);
                exit(1);
@@ -168,7 +168,7 @@ static void ctxStoreCleanUp (time_t now)
 }
 
 // This function will return exiting client context or newly created client context
-struct AFB_clientCtx *ctxClientGet (const char *uuid)
+struct AFB_clientCtx *ctxClientGetForUuid (const char *uuid)
 {
        uuid_t newuuid;
        struct AFB_clientCtx *clientCtx;
@@ -215,6 +215,13 @@ struct AFB_clientCtx *ctxClientGet (const char *uuid)
        return NULL;
 }
 
+struct AFB_clientCtx *ctxClientGet(struct AFB_clientCtx *clientCtx)
+{
+       if (clientCtx != NULL)
+               clientCtx->refcount++;
+       return clientCtx;
+}
+
 void ctxClientPut(struct AFB_clientCtx *clientCtx)
 {
        if (clientCtx != NULL) {
@@ -236,7 +243,7 @@ void ctxClientClose (struct AFB_clientCtx *clientCtx)
 }
 
 // Sample Generic Ping Debug API
-int ctxTokenCheck (struct AFB_clientCtx *clientCtx, const char *token)
+int ctxTokenCheckLen (struct AFB_clientCtx *clientCtx, const char *token, size_t length)
 {
        assert(clientCtx != NULL);
        assert(token != NULL);
@@ -245,13 +252,20 @@ int ctxTokenCheck (struct AFB_clientCtx *clientCtx, const char *token)
        if (ctxStoreTooOld (clientCtx, NOW))
                return 0;
 
-       if (!clientCtx->token[0] || 0 == strcmp (token, clientCtx->token)) {
-               clientCtx->created = 1; /* creates by default */
-               return 1;
-       }
+       if (clientCtx->token[0] && (length >= sizeof(clientCtx->token) || strncmp (token, clientCtx->token, length) || clientCtx->token[length]))
+               return 0;
+
+       clientCtx->created = 1; /* creates by default */
+       return 1;
+}
+
+// Sample Generic Ping Debug API
+int ctxTokenCheck (struct AFB_clientCtx *clientCtx, const char *token)
+{
+       assert(clientCtx != NULL);
+       assert(token != NULL);
 
-       // Token is not valid let move level of assurance to zero and free attached client handle
-       return 0;
+       return ctxTokenCheckLen(clientCtx, token, strlen(token));
 }
 
 // generate a new token and update client context