X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fhelper-api.c;h=9d1ec0b71d69b1cddfa545f24c7b20fda4e17751;hb=dde70b62b09f49ad672c104a3f81714bf11047be;hp=bf94c8989c76632426a9b5aa2bcc20ee806ff4f9;hpb=9c9253cd9106e656195aba6f0cadb29f6a940fe1;p=src%2Fapp-framework-binder.git diff --git a/src/helper-api.c b/src/helper-api.c index bf94c898..9d1ec0b7 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -17,17 +17,25 @@ * */ -#include "../include/local-def.h" +#define _GNU_SOURCE + +#include +#include +#include + +/* #include #include #include -#include +*/ +#include "local-def.h" +#include "afb-req-itf.h" // handle to hold queryAll values typedef struct { char *msg; - int idx; + size_t idx; size_t len; } queryHandleT; @@ -38,59 +46,40 @@ typedef struct { json_object *json; } AFB_errorT; -static AFB_errorT AFBerr [AFB_SUCCESS+1]; +static AFB_errorT AFBerr [AFB_UNAUTH+1]; static json_object *jTypeStatic; PUBLIC int verbose; -static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success"}; - +static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success", "done", "unauth"}; // Helper to retrieve argument from connection 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); - return (value); + return afb_req_argument(*request->areq, name); } -static int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) { - queryHandleT *query = (queryHandleT*)handle; - - query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value); - return MHD_YES; /* continue to iterate */ +static int getQueryCB (queryHandleT *query, struct afb_arg arg) { + if (query->idx >= query->len) + return 0; + query->idx += (unsigned)snprintf (&query->msg[query->idx], query->len-query->idx, " %s: %s\'%s\',", arg.name, arg.is_file?"FILE=":"", arg.value); + return 1; /* continue to iterate */ } // Helper to retrieve argument from connection -int getQueryAll(AFB_request * request, char *buffer, size_t len) { +size_t getQueryAll(AFB_request * request, char *buffer, size_t len) { queryHandleT query; buffer[0] = '\0'; // start with an empty string query.msg = buffer; query.len = len; query.idx = 0; - MHD_get_connection_values (request->connection, MHD_GET_ARGUMENT_KIND, getQueryCB, &query); - return (len); -} - -// Helper to retrieve POST handle -AFB_PostHandle* getPostHandle (AFB_request *request) { - if (request->post == NULL) return (NULL); - return ((AFB_PostHandle*) request->post->data); -} - -// Helper to retrieve POST file context -AFB_PostCtx* getPostContext (AFB_request *request) { - AFB_PostHandle* postHandle; - if (request->post == NULL) return (NULL); - - postHandle = (AFB_PostHandle*) request->post->data; - if (postHandle == NULL) return NULL; - - return ((AFB_PostCtx*) postHandle->ctx); + afb_req_iterate(*request->areq, (void*)getQueryCB, &query); + buffer[len-1] = 0; + return query.idx >= len ? len - 1 : query.idx; } +#if 0 char* getPostPath (AFB_request *request) { AFB_PostHandle *postHandle = getPostHandle(request); AFB_PostCtx *postFileCtx; @@ -212,7 +201,7 @@ ExitOnError: return NULL; } - +#endif static void jsoninit() { @@ -225,7 +214,7 @@ static void jsoninit() verbosesav = verbose; verbose = 0; // run initialisation in silent mode jTypeStatic = json_object_new_string ("AFB_message"); - for (idx = 0; idx <= AFB_SUCCESS; idx++) { + for (idx = 0; idx <= AFB_UNAUTH; idx++) { AFBerr[idx].level = idx; AFBerr[idx].label = ERROR_LABEL [idx]; AFBerr[idx].json = jsonNewMessage (idx, NULL); @@ -234,25 +223,41 @@ static void jsoninit() } - -// get JSON object from error level and increase its reference count -struct json_object *jsonNewStatus (AFB_error level) +// build an ERROR message and return it as a valid json object +json_object *json_add_status (json_object *obj, const char *status, const char *info) { - jsoninit(); - json_object *target = AFBerr[level].json; - json_object_get (target); + if (obj == NULL) + obj = json_object_new_object(); + json_object_object_add(obj, "status", json_object_new_string(status)); + if (info) + json_object_object_add(obj, "info", json_object_new_string(info)); + return obj; +} - return (target); +// build an ERROR message and return it as a valid json object +json_object *json_add_status_v (json_object *obj, const char *status, const char *info, va_list args) +{ + char *message; + if (info == NULL || vasprintf(&message, info, args) < 0) + message = NULL; + obj = json_add_status(obj, status, message); + free(message); + return obj; } -// get AFB object type with adequate usage count -struct json_object *jsonNewjtype (void) + +// build an ERROR message and return it as a valid json object +json_object *json_add_status_f (json_object *obj, const char *status, const char *info, ...) { - jsoninit(); - json_object_get (jTypeStatic); // increase reference count - return (jTypeStatic); + va_list args; + va_start(args, info); + obj = json_add_status_v(obj, status, info, args); + va_end(args); + return obj; } + + // build an ERROR message and return it as a valid json object struct json_object *jsonNewMessage (AFB_error level, char* format, ...) { static int count = 0; @@ -270,7 +275,7 @@ struct json_object *jsonNewMessage (AFB_error level, char* format, ...) { } AFBResponse = json_object_new_object(); - json_object_object_add (AFBResponse, "jtype", jsonNewjtype ()); + json_object_object_add (AFBResponse, "jtype", json_object_get (jTypeStatic)); json_object_object_add (AFBResponse, "status" , json_object_new_string (ERROR_LABEL[level])); if (format != NULL) { json_object_object_add (AFBResponse, "info" , json_object_new_string (message)); @@ -288,11 +293,20 @@ struct json_object *jsonNewMessage (AFB_error level, char* format, ...) { return (AFBResponse); } -// Dump a message on stderr -void jsonDumpObject (struct json_object * jObject) { - - if (verbose) { - fprintf (stderr, "AFB:dump [%s]\n", json_object_to_json_string(jObject)); - } +#if 0 +{ + jtype: "AFB_message" + request: + { + prefix: "", + api: "", + status: "", /* exist, fail, empty, null, processed */ + info: "", + uuid: "", + token: "", + timeout: "" + } + response: ... } +#endif