rest-api: fixup AFB_plugins allocated references
[src/app-framework-binder.git] / src / http-svc.c
index 034753f..839fc2f 100644 (file)
@@ -70,14 +70,10 @@ static AFB_error initLibMagic (AFB_session *session) {
 
 // Because of POST call multiple time requestApi we need to free POST handle here
 static void endRequest (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe) {
-  AFB_HttpPost *posthandle = *con_cls;
+  AFB_PostHandle *posthandle = *con_cls;
 
   // if post handle was used let's free everything
-  if (posthandle) {
-     if (verbose) fprintf (stderr, "End Post Request UID=%d\n", posthandle->uid);
-     free (posthandle->data);
-     free (posthandle);
-  }
+  if (posthandle != NULL) endPostRequest (posthandle);
 }
 
 
@@ -99,11 +95,7 @@ STATIC int servFile (struct MHD_Connection *connection, AFB_session *session, co
         fprintf(stderr, "Fail to stat file: [%s] error:%s\n", staticfile->path, strerror(errno));
         goto abortRequest;
     }
-    
-    
-    
-
-    
+       
     // if url is a directory let's add index.html and redirect client
     if (S_ISDIR (sbuf.st_mode)) {
         close (staticfile->fd); // close directory check for Index
@@ -111,6 +103,7 @@ STATIC int servFile (struct MHD_Connection *connection, AFB_session *session, co
         // No trailing '/'. Let's add one and redirect for relative paths to work
         if (url [strlen (url) -1] != '/') {
             response = MHD_create_response_from_buffer(0,"", MHD_RESPMEM_PERSISTENT);
+            strncpy(staticfile->path, url, sizeof (staticfile->path));
             strncat(staticfile->path, "/", sizeof (staticfile->path));
             MHD_add_response_header (response, "Location", staticfile->path);
             MHD_queue_response (connection, MHD_HTTP_MOVED_PERMANENTLY, response);
@@ -122,7 +115,7 @@ STATIC int servFile (struct MHD_Connection *connection, AFB_session *session, co
         if (-1 == (staticfile->fd = open(staticfile->path, O_RDONLY)) || (fstat (staticfile->fd, &sbuf) != 0)) {
            fprintf(stderr, "No Index.html in direcory [%s]\n", staticfile->path);
            goto abortRequest;  
-        }      
+        } 
     } else if (! S_ISREG (sbuf.st_mode)) { // only standard file any other one including symbolic links are refused.
         close (staticfile->fd); // nothing useful to do with this file
         fprintf (stderr, "Fail file: [%s] is not a regular file\n", staticfile->path);
@@ -153,7 +146,7 @@ STATIC int servFile (struct MHD_Connection *connection, AFB_session *session, co
         if (session->magic) {          
            mimetype= magic_descriptor(session->magic, staticfile->fd);
            if (mimetype != NULL)  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
-        } else mimetype="Unknown";
+        } else mimetype="application/unknown";
         
         if (verbose) fprintf(stderr, "Serving: [%s] mime=%s\n", staticfile->path, mimetype);
         response = MHD_create_response_from_fd(sbuf.st_size, staticfile->fd);
@@ -256,7 +249,7 @@ STATIC int newRequest(void *cls,
     }
 
      // Nothing respond to this request Files, API, Angular Base
-    const char *errorstr = "<html><body>Alsa-Json-Gateway Unknown or Not readable file</body></html>";
+    const char *errorstr = "<html><body>AFB-Daemon File Not Find file</body></html>";
     response = MHD_create_response_from_buffer(strlen(errorstr), (void*)errorstr, MHD_RESPMEM_PERSISTENT);
     ret = MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
     return (MHD_YES);
@@ -274,9 +267,12 @@ PUBLIC AFB_error httpdStart(AFB_session *session) {
     apiUrlLen = strlen (session->config->rootapi);
     baseUrlLen= strlen (session->config->rootbase);
     rootUrlLen= strlen (session->config->rootdir);
+    
+    // Initialise Client Session Hash Table
+    ctxStoreInit (CTX_NBCLIENTS);
      
-    // TBD open libmagic cache [fail to pass EFENCE check]
-    // initLibMagic (session);
+    //TBD open libmagic cache [fail to pass EFENCE check (allocating 0 bytes)]
+    //initLibMagic (session);
     
     
     if (verbose) {
@@ -302,20 +298,11 @@ PUBLIC AFB_error httpdStart(AFB_session *session) {
 
 // infinite loop
 PUBLIC AFB_error httpdLoop(AFB_session *session) {
-    static int  count = 0;
-
+    int count = 0;
     if (verbose) fprintf(stderr, "AFB:notice entering httpd waiting loop\n");
-    if (session->foreground) {
-
-        while (TRUE) {
-            fprintf(stderr, "AFB:notice Use Ctrl-C to quit\n");
-            (void) getc(stdin);
-        }
-    } else {
-        while (TRUE) {
-            sleep(3600);
-            if (verbose) fprintf(stderr, "AFB:notice httpd alive [%d]\n", count++);
-        }
+    while (TRUE) {
+        sleep(3600);
+        if (verbose) fprintf(stderr, "AFB:notice httpd alive [%d]\n", count++);
     }
 
     // should never return from here