X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fhelper-api.c;h=b3763505e761d27d9b4d68b1663ab141b797ad8d;hb=da5a2bfe0571fbee3378f730633c17efcac94c30;hp=a166027a4aa6d638a0d451585e5f6450ea6ba627;hpb=00857f9da5ae802794a4cb94aca576b11bfe6628;p=src%2Fapp-framework-binder.git diff --git a/src/helper-api.c b/src/helper-api.c index a166027a..b3763505 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -19,6 +19,8 @@ #include "../include/local-def.h" #include +#include +#include // handle to hold queryAll values @@ -30,7 +32,7 @@ typedef struct { // Sample Generic Ping Debug API PUBLIC json_object* getPingTest(AFB_request *request) { - static pingcount = 0; + static int pingcount = 0; json_object *response; char query [256]; char session[256]; @@ -62,6 +64,7 @@ STATIC int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, co queryHandleT *query = (queryHandleT*)handle; query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value); + return MHD_YES; /* continue to iterate */ } // Helper to retrieve argument from connection @@ -93,12 +96,24 @@ 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); AFB_PostCtx *postFileCtx; char filepath[512]; - int len; + ssize_t len; // This is called after PostForm and then after DonePostForm if (item == NULL) { @@ -112,11 +127,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); @@ -124,7 +137,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* free (postFileCtx); return (jresp); } - +#if defined(PLEASE_FIX_ME_THE_ERROR_IS_postFileCtx_NOT_INITIALIZED) // Make sure it's a valid PostForm request if (!request->post && request->post->type != AFB_POST_FORM) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n"); @@ -142,7 +155,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key); goto ExitOnError; } - +#endif // Extract Application Context from posthandle [NULL == 1st iteration] postFileCtx = (AFB_PostCtx*) postHandle->ctx; @@ -152,7 +165,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 @@ -163,12 +175,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; } @@ -177,6 +189,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; @@ -188,7 +203,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* // Check we successfully wrote full buffer len = write (postFileCtx->fd, item->data, item->len); - if (item->len != len) { + if ((ssize_t)item->len != len) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno)); goto ExitOnError; }