From: Fulup Ar Foll Date: Mon, 14 Dec 2015 17:04:00 +0000 (+0100) Subject: Post En court Ne fonctionne pas X-Git-Tag: blowfish_2.0.1~356 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=170c94e97336aedf2d22235479bc346ad5c42046;p=src%2Fapp-framework-binder.git Post En court Ne fonctionne pas --- diff --git a/include/local-def.h b/include/local-def.h index a5d48bfd..805a434d 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -76,7 +76,6 @@ typedef int BOOL; extern int verbose; // this is the only global variable - // Plugin Type typedef enum {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321, AFB_PLUGIN_RAW=987123546} AFB_pluginT; @@ -88,6 +87,7 @@ extern char *ERROR_LABEL[]; #define BANNER "Application Framework BinderApplication Framework " #define JSON_CONTENT "application/json" +#define FORM_CONTENT "multipart/form-data" #define MAX_POST_SIZE 4096 // maximum size for POST data #define CTX_NBCLIENTS 10 // allow a default of 10 authenticated clients diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 7a2bc668..8a3562a2 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -48,6 +48,7 @@ + @@ -57,12 +58,6 @@ ${MAKE} -f Makefile ${MAKE} -f Makefile clean build/src/afb-daemon - - - include - /usr/include/json-c - - build @@ -71,50 +66,124 @@ - + + src + /usr/include/json-c + include + /usr/include/uuid build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + - + + src + /usr/include/json-c + include + /usr/include/uuid build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + - + + src + /usr/include/json-c + include + /usr/include/uuid build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + - + + src + /usr/include/json-c + include + /usr/include/uuid build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + - + + src + /usr/include/json-c + include + /usr/include/uuid build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + - + + src + /usr/include/json-c + include + /usr/include/uuid build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + + include + /usr/include/json-c /usr/include/libusb-1.0 build/src @@ -124,17 +193,41 @@ - + + src + /usr/include/json-c + include + /usr/include/uuid build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + - + + src + /usr/include/json-c + /usr/include/uuid + include build/src + + __PIC__=2 + __PIE__=2 + __REGISTER_PREFIX__= + __USER_LABEL_PREFIX__= + __pic__=2 + __pie__=2 + diff --git a/src/main.c b/src/main.c index 5d898ae6..d45267b7 100644 --- a/src/main.c +++ b/src/main.c @@ -246,7 +246,7 @@ static void listenLoop (AFB_session *session) { fprintf (stderr, "hoops returned from infinite loop [report bug]\n"); } } - + /*--------------------------------------------------------- | main | Parse option and launch action diff --git a/src/rest-api.c b/src/rest-api.c index 62b782d5..097f03d3 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -149,7 +149,7 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request) { for (sig=0; signals[sig] != 0; sig++) { if (signal (signals[sig], pluginError) == SIG_ERR) { request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY; - fprintf (stderr, "%s ERR: main no Signal/timeout handler installed.", configTime()); + request->jresp = jsonNewMessage(AFB_FATAL, "%s ERR: Signal/timeout handler activation fail.", configTime()); return AFB_FAIL; } } @@ -188,6 +188,47 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request) { return (AFB_FAIL); } +STATIC AFB_error findAndCallApi (struct MHD_Connection *connection, AFB_session *session, const char* url, AFB_request *request) { + int idx; + char *baseurl, *baseapi; + AFB_error status; + + // build request structure + memset(request, 0, sizeof (request)); + request->connection = connection; + request->config = session->config; + request->url = url; + request->plugin = baseurl; + request->api = baseapi; + request->jresp = json_object_new_object(); + + // increase reference count and add jtype to response + json_object_get (afbJsonType); + json_object_object_add (request->jresp, "jtype", afbJsonType); + + // Search for a plugin with this urlpath + for (idx = 0; session->plugins[idx] != NULL; idx++) { + if (!strcmp(session->plugins[idx]->prefix, baseurl)) { + status =callPluginApi(session->plugins[idx], request); + break; + } + } + // No plugin was found + if (session->plugins[idx] == NULL) { + request->jresp = jsonNewMessage(AFB_FATAL, "No Plugin=[%s]", request->plugin); + request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY; + return (AFB_FAIL); + } + + // plugin callback did not return a valid Json Object + if (status != AFB_DONE) { + request->jresp = jsonNewMessage(AFB_FATAL, "No API=[%s] for Plugin=[%s]", request->api, request->plugin); + request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY; + return (AFB_FAIL); + } + + return (status); +} // process rest API query PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method @@ -231,9 +272,18 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co if (param) sscanf(param, "%i", &contentlen); // POST datas may come in multiple chunk. Even when it never happen on AFB, we still have to handle the case - if (strcasestr(encoding, JSON_CONTENT) == 0) { + + // This is FORM post only file upload is supported + if (strcasestr(encoding, FORM_CONTENT) != NULL) { + request.post= (void*)upload_data; + request.len = *upload_data_size; + status = findAndCallApi (connection, session, url, &request); + return MHD_YES; + } + + // POST datas may come in multiple chunk. Even when it never happen on AFB, we still have to handle the case + if (strcasestr(encoding, JSON_CONTENT) == NULL) { errMessage = jsonNewMessage(AFB_FATAL, "Post Date wrong type encoding=%s != %s", encoding, JSON_CONTENT); - goto ExitOnError; } if (contentlen > MAX_POST_SIZE) { @@ -281,39 +331,10 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co } else { request.post = NULL; }; - - // build request structure - memset(&request, 0, sizeof (request)); - request.connection = connection; - request.config = session->config; - request.url = url; - request.plugin = baseurl; - request.api = baseapi; - request.jresp = json_object_new_object(); - - // increase reference count and add jtype to response - json_object_get (afbJsonType); - json_object_object_add (request.jresp, "jtype", afbJsonType); - - // Search for a plugin with this urlpath - for (idx = 0; session->plugins[idx] != NULL; idx++) { - if (!strcmp(session->plugins[idx]->prefix, baseurl)) { - status =callPluginApi(session->plugins[idx], &request); - break; - } - } - // No plugin was found - if (session->plugins[idx] == NULL) { - errMessage = jsonNewMessage(AFB_FATAL, "No Plugin=[%s]", request.plugin); - goto ExitOnError; - } - - // plugin callback did not return a valid Json Object - if (status != AFB_DONE) { - errMessage = jsonNewMessage(AFB_FATAL, "No API=[%s] for Plugin=[%s]", request.api, request.plugin); - goto ExitOnError; - } + // Now that we got request data let's call the API + status = findAndCallApi (connection, session, url, &request); + serialized = json_object_to_json_string(request.jresp); webResponse = MHD_create_response_from_buffer(strlen(serialized), (void*) serialized, MHD_RESPMEM_MUST_COPY); free(urlcpy1);