Media Plugin list API now returns a JSON object
[src/app-framework-binder.git] / src / rest-api.c
index e19e1f2..dd5c59b 100644 (file)
@@ -45,7 +45,7 @@ PUBLIC void endPostRequest(AFB_PostHandle *postHandle) {
     if (postHandle->type == AFB_POST_FORM) {
          if (verbose) fprintf(stderr, "End PostForm Request UID=%d\n", postHandle->uid);
     }
-    free(postHandle->private);
+    if (postHandle->private) free(postHandle->private);
     free(postHandle);
 }
 
@@ -388,8 +388,8 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co
             
             // We are facing an empty post let's process it as a get
             if (encoding == NULL) {
-                request= createRequest (connection, session, url);
-                goto ProcessApiCall;
+                postHandle->type   = AFB_POST_EMPTY;
+                return MHD_YES;
             }
         
             // Form post is handle through a PostProcessor and call API once per form key
@@ -466,11 +466,11 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co
                 errMessage = request->jresp;
                 goto ExitOnError;
             }
+            postRequest.type = postHandle->type;
             
             // Postform add application context handle to request
             if (postHandle->type == AFB_POST_FORM) {
                postRequest.data = (char*) postHandle;
-               postRequest.type = postHandle->type;
                request->post = &postRequest;
             }
             
@@ -489,7 +489,6 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co
                 // Before processing data, make sure buffer string is properly ended
                 postHandle->private[postHandle->len] = '\0';
                 postRequest.data = postHandle->private;
-                postRequest.type = postHandle->type;
                 request->post = &postRequest;
 
                 // if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", postHandle->uid, request->post->data);
@@ -509,8 +508,8 @@ ProcessApiCall:
     
     // client did not pass token on URI let's use cookies 
     if ((!request->restfull) && (request->context != NULL)) {
-       char cookie[64]; 
-       snprintf (cookie, sizeof (cookie), "%s=%s", COOKIE_NAME,  request->uuid); 
+       char cookie[256]; 
+       snprintf (cookie, sizeof (cookie), "%s=%s;path=%s;max-age=%d", COOKIE_NAME, request->uuid, request->config->rootapi,request->config->cntxTimeout); 
        MHD_add_response_header (webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie);
     }
     
@@ -578,10 +577,10 @@ STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) {
 STATIC void scanDirectory(char *dirpath, int dirfd, AFB_plugin **plugins, int *count) {
     DIR *dir;
     void *libso;
-    struct dirent *pluginDir;  
+    struct dirent pluginDir, *result;
     AFB_plugin* (*pluginRegisterFct)(void);
     char pluginPath[255];   
-    
+
     // Open Directory to scan over it
     dir = fdopendir (dirfd);
     if (dir == NULL) {
@@ -590,39 +589,40 @@ STATIC void scanDirectory(char *dirpath, int dirfd, AFB_plugin **plugins, int *c
     }
     if (verbose) fprintf (stderr, "Scanning dir=[%s] for plugins\n", dirpath);
 
-    while ((pluginDir = readdir(dir)) != NULL) {
-        
+    for (;;) {
+         readdir_r(dir, &pluginDir, &result);
+         if (result == NULL) break;
+
         // Loop on any contained directory
-        if ((pluginDir->d_type == DT_DIR) && (pluginDir->d_name[0] != '.')) {
-           int fd = openat (dirfd, pluginDir->d_name, O_DIRECTORY);
+        if ((pluginDir.d_type == DT_DIR) && (pluginDir.d_name[0] != '.')) {
+           int fd = openat (dirfd, pluginDir.d_name, O_DIRECTORY);
            char newpath[255];
            strncpy (newpath, dirpath, sizeof(newpath));
            strncat (newpath, "/", sizeof(newpath));
-           strncat (newpath, pluginDir->d_name, sizeof(newpath));
+           strncat (newpath, pluginDir.d_name, sizeof(newpath));
            
            scanDirectory (newpath, fd, plugins, count);
            close (fd);
-           
+
         } else {
-            
+
             // This is a file but not a plugin let's move to next directory element
-            if (!strstr (pluginDir->d_name, ".so")) continue;
+            if (!strstr (pluginDir.d_name, ".so")) continue;
 
             // This is a loadable library let's check if it's a plugin
-            snprintf (pluginPath, sizeof(pluginPath), "%s/%s", dirpath, pluginDir->d_name);
+            snprintf (pluginPath, sizeof(pluginPath), "%s/%s", dirpath, pluginDir.d_name);
             libso = dlopen (pluginPath, RTLD_NOW | RTLD_LOCAL);
 
             // Load fail we ignore this .so file            
             if (!libso) {
-                fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir->d_name);
+                fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir.d_name);
                 continue;
             }
-            
+
             pluginRegisterFct = dlsym (libso, "pluginRegister");
-            free (libso);
-            
+
             if (!pluginRegisterFct) {
-                fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir->d_name);
+                fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir.d_name);
                 continue;
             }
 
@@ -632,11 +632,11 @@ STATIC void scanDirectory(char *dirpath, int dirfd, AFB_plugin **plugins, int *c
                 continue;
             }
 
-            if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading pos[%d]\n", pluginDir->d_name, *count);
+            if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading pos[%d]\n", pluginDir.d_name, *count);
             plugins[*count] = (AFB_plugin *) malloc (sizeof(AFB_plugin));
             plugins[*count] = (**pluginRegisterFct)();
             *count = *count +1;
-            
+
         }
     }
     closedir (dir);