rest-api: fix verbosity of upload message
[src/app-framework-binder.git] / src / helper-api.c
index 77b013e..54093e4 100644 (file)
@@ -34,9 +34,7 @@ PUBLIC json_object* getPingTest(AFB_request *request) {
     json_object *response;
     char query  [256];
     char session[256];
-
     int len;
-    AFB_clientCtx *client=request->client; // get client context from request
     
     // request all query key/value
     len = getQueryAll (request, query, sizeof(query));
@@ -44,20 +42,16 @@ PUBLIC json_object* getPingTest(AFB_request *request) {
     
     // check if we have some post data
     if (request->post == NULL)  request->post->data="NoData"; 
-    
-    // check is we have a session and a plugin handle
-    if (client == NULL) strncpy (session,"NoSession", sizeof(session));       
-    else snprintf(session, sizeof(session),"uuid=%s token=%s ctx=0x%x handle=0x%x", client->uuid, client->token, client->ctx, client->ctx); 
-        
+          
     // return response to caller
-    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d CtxtId=%d query={%s} session={%s} PostData: [%s] "
-               , pingcount++, request->client->cid, query, session, request->post->data);
+    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d uuid=%s query={%s} session={0x%x} PostData: [%s] "
+               , pingcount++, request->uuid, query, session, request->post->data);
     return (response);
 }
 
 
 // Helper to retrieve argument from  connection
-PUBLIC const char* getQueryValue(AFB_request * request, char *name) {
+PUBLIC const char* getQueryValue(const AFB_request * request, const char *name) {
     const char *value;
 
     value = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, name);
@@ -99,6 +93,18 @@ PUBLIC AFB_PostCtx* getPostContext (AFB_request *request) {
     return ((AFB_PostCtx*) postHandle->ctx);
 }
 
+PUBLIC char* getPostPath (AFB_request *request) {
+    AFB_PostHandle *postHandle = getPostHandle(request);
+    AFB_PostCtx *postFileCtx;
+    
+    if (postHandle == NULL) return NULL;
+    
+    postFileCtx = (AFB_PostCtx*) postHandle->ctx;
+    if (postFileCtx == NULL) return NULL;
+  
+    return (postFileCtx->path);
+}
+
 PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
 
     AFB_PostHandle *postHandle = getPostHandle(request);
@@ -118,11 +124,9 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char*
         }
         
         // We have a context but last Xform iteration fail or application set a message
-        if (postFileCtx->jresp != NULL) {
-            jresp = postFileCtx->jresp;  // retrieve previous error from postCtx
-            if (postFileCtx->errcode != 0) request->errcode=postFileCtx->errcode;
-        }
-        else jresp = jsonNewMessage(AFB_FAIL,"getPostFile Post Request done");
+        if (request->jresp != NULL) {
+            jresp = request->jresp;  // retrieve previous error from postCtx
+        } else jresp = jsonNewMessage(AFB_SUCCESS,"getPostFile Post Request done");
         
         // Error or not let's free all resources
         close(postFileCtx->fd);
@@ -158,7 +162,6 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char*
         
         // Create an application specific context
         postFileCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
-        postFileCtx->path = strdup (filepath);
         
         // attach application to postHandle
         postHandle->ctx = (void*) postFileCtx;   // May place anything here until post->completeCB handle resources liberation  
@@ -169,12 +172,12 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char*
            strncat (filepath, "/", sizeof(filepath));
            strncat (filepath, destination, sizeof(filepath)); 
         } else strncpy (filepath, destination, sizeof(filepath));
-        
 
+        
         // make sure destination directory exist
         destDir = opendir (filepath);
         if (destDir == NULL) {
-          if ( 0 <= mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP)) {
+          if (mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP) < 0) {
             postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
             goto ExitOnError;
           }
@@ -183,6 +186,9 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char*
         strncat (filepath, "/", sizeof(filepath));
         strncat (filepath, item->filename, sizeof(filepath));  
 
+        postFileCtx->path = strdup (filepath);       
+        if (verbose) fprintf(stderr, "getPostFile path=%s\n", filepath);
+       
         if((postFileCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) <= 0) {
             postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination File=[%s] error=%s\n", filepath, strerror(errno));
             goto ExitOnError;