diff options
-rw-r--r-- | include/local-def.h | 2 | ||||
-rw-r--r-- | nbproject/configurations.xml | 121 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/rest-api.c | 91 |
4 files changed, 165 insertions, 51 deletions
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 "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>" #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 @@ </toolsSet> <flagsDictionary> <element flagsID="0" commonFlags="-fPIE"/> + <element flagsID="1" commonFlags="-mtune=generic -march=x86-64 -fPIE"/> </flagsDictionary> <codeAssistance> </codeAssistance> @@ -57,12 +58,6 @@ <buildCommand>${MAKE} -f Makefile</buildCommand> <cleanCommand>${MAKE} -f Makefile clean</cleanCommand> <executablePath>build/src/afb-daemon</executablePath> - <cTool> - <incDir> - <pElem>include</pElem> - <pElem>/usr/include/json-c</pElem> - </incDir> - </cTool> </makeTool> <preBuild> <preBuildCommandWorkingDir>build</preBuildCommandWorkingDir> @@ -71,50 +66,124 @@ </preBuild> </makefileType> <item path="src/afbs-api.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/alsa-api.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/config.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/dbus-api.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/http-svc.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/main.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/radio-api.c" ex="false" tool="0" flavor2="2"> <cTool flags="0"> <incDir> + <pElem>include</pElem> + <pElem>/usr/include/json-c</pElem> <pElem>/usr/include/libusb-1.0</pElem> <pElem>build/src</pElem> </incDir> @@ -124,17 +193,41 @@ </cTool> </item> <item path="src/rest-api.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/session.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> + <pElem>src</pElem> + <pElem>/usr/include/json-c</pElem> + <pElem>/usr/include/uuid</pElem> + <pElem>include</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> </conf> @@ -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); |