From: Fulup Ar Foll Date: Tue, 15 Dec 2015 11:08:12 +0000 (+0100) Subject: Intermediary Version Does not Compile X-Git-Tag: blowfish_2.0.1~355 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=a644a48c2dfdc4ba5129aa3b85a21aadc429f65d;p=src%2Fapp-framework-binder.git Intermediary Version Does not Compile --- diff --git a/include/local-def.h b/include/local-def.h index 805a434d..47513e68 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -101,12 +101,35 @@ typedef struct { json_object *json; } AFB_errorT; +typedef enum {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM} AFB_PostType; + +typedef struct { + int len; // post element size + char *data; // post data in raw format + AFB_PostType type; // Json type +} AFB_PostRequest; + // Post handler typedef struct { - char* data; - int len; - int uid; -} AFB_HttpPost; + void* handle; + int len; + int uid; + AFB_PostType type; + struct MHD_PostProcessor *pp; + AFB_apiCB completeCB; // callback when post is completed + void *private; +} AFB_PostHandle; + +typedef struct { + enum MHD_ValueKind kind; // kind type of the value + const char *key; // key 0-terminated key for the value + const char *filename; // filename of the uploaded file, NULL if not known + const char *mimetype; // content_type mime-type of the data, NULL if not known + const char *encoding; // transfer_encoding encoding of the data, NULL if not known + const char *data; // data pointer to size bytes of data at the specified offset + uint64_t offset; // offset of data in the overall value + size_t len; // number of bytes in data available +} AFB_PostItem; typedef struct { char path[512]; @@ -149,8 +172,6 @@ typedef struct { AFB_aliasdir *aliasdir; // alias mapping for icons,apps,... } AFB_config; - - typedef struct { int len; // command number within application json_object *jtype; @@ -200,8 +221,7 @@ typedef struct { const char *url; char *plugin; char *api; - char *post; // post data in raw format - int len; // post data len + AFB_PostRequest *post; json_object *jresp; AFB_clientCtx *client; // needed because libmicrohttp cannot create an empty response int restfull; // request is resfull [uuid token provided] @@ -209,6 +229,7 @@ typedef struct { sigjmp_buf checkPluginCall; // context save for timeout set/longjmp AFB_config *config; // plugin may need access to config struct MHD_Connection *connection; + AFB_plugin **plugins; } AFB_request; @@ -218,7 +239,6 @@ typedef struct { int killPrevious; int background; // run in backround mode int foreground; // run in forground mode - int checkAlsa; // Display active Alsa Board int configsave; // Save config on disk on start char *cacheTimeout; // http require timeout to be a string void *httpd; // anonymous structure for httpd handler diff --git a/include/proto-def.h b/include/proto-def.h index 55585e7b..1a3145f5 100644 --- a/include/proto-def.h +++ b/include/proto-def.h @@ -26,7 +26,7 @@ PUBLIC json_object* apiPingTest(AFB_request *request); PUBLIC const char* getQueryValue (AFB_request * request, char *name); PUBLIC int getQueryAll(AFB_request * request, char *query, size_t len); - +PUBLIC void endPostRequest(AFB_PostHandle *posthandle); PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method , const char *upload_data, size_t *upload_data_size, void **con_cls); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 8a3562a2..2435ae96 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -65,23 +65,15 @@ true + + - + - src - /usr/include/json-c include - /usr/include/uuid + /usr/include/json-c build/src - - __PIC__=2 - __PIE__=2 - __REGISTER_PREFIX__= - __USER_LABEL_PREFIX__= - __pic__=2 - __pie__=2 - @@ -104,22 +96,12 @@ - + - src - /usr/include/json-c include - /usr/include/uuid + /usr/include/json-c build/src - - __PIC__=2 - __PIE__=2 - __REGISTER_PREFIX__= - __USER_LABEL_PREFIX__= - __pic__=2 - __pie__=2 - @@ -142,41 +124,21 @@ - + - src - /usr/include/json-c include - /usr/include/uuid + /usr/include/json-c build/src - - __PIC__=2 - __PIE__=2 - __REGISTER_PREFIX__= - __USER_LABEL_PREFIX__= - __pic__=2 - __pie__=2 - - + - src - /usr/include/json-c include - /usr/include/uuid + /usr/include/json-c build/src - - __PIC__=2 - __PIE__=2 - __REGISTER_PREFIX__= - __USER_LABEL_PREFIX__= - __pic__=2 - __pie__=2 - @@ -193,41 +155,21 @@ - + - src - /usr/include/json-c include - /usr/include/uuid + /usr/include/json-c build/src - - __PIC__=2 - __PIE__=2 - __REGISTER_PREFIX__= - __USER_LABEL_PREFIX__= - __pic__=2 - __pie__=2 - - + - src - /usr/include/json-c - /usr/include/uuid include + /usr/include/json-c build/src - - __PIC__=2 - __PIE__=2 - __REGISTER_PREFIX__= - __USER_LABEL_PREFIX__= - __pic__=2 - __pie__=2 - diff --git a/nbproject/private/Default.properties b/nbproject/private/Default.properties index 372fed8c..a45c3d27 100644 --- a/nbproject/private/Default.properties +++ b/nbproject/private/Default.properties @@ -1,9 +1,2 @@ -/home/mbc/git/afb-daemon-new/src/session.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/session.c.o -c /home/mbc/git/afb-daemon-new/src/session.c -/home/mbc/git/afb-daemon-new/src/radio-api.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/radio-api.c.o -c /home/mbc/git/afb-daemon-new/src/radio-api.c -/home/mbc/git/afb-daemon-new/src/config.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/config.c.o -c /home/mbc/git/afb-daemon-new/src/config.c -/home/mbc/git/afb-daemon-new/src/main.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/main.c.o -c /home/mbc/git/afb-daemon-new/src/main.c -/home/mbc/git/afb-daemon-new/src/rest-api.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/rest-api.c.o -c /home/mbc/git/afb-daemon-new/src/rest-api.c -/home/mbc/git/afb-daemon-new/src/alsa-api.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/alsa-api.c.o -c /home/mbc/git/afb-daemon-new/src/alsa-api.c -/home/mbc/git/afb-daemon-new/src/dbus-api.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/dbus-api.c.o -c /home/mbc/git/afb-daemon-new/src/dbus-api.c -/home/mbc/git/afb-daemon-new/src/afbs-api.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/afbs-api.c.o -c /home/mbc/git/afb-daemon-new/src/afbs-api.c -/home/mbc/git/afb-daemon-new/src/http-svc.c=/home/mbc/git/afb-daemon-new/build/src#-DWITH_RADIO_PLUGIN=1 -g3 -gdwarf-2 -fPIE -I/home/mbc/git/afb-daemon-new/include -I/usr/include/json-c -I/usr/include/libusb-1.0 -o CMakeFiles/afb-daemon.dir/http-svc.c.o -c /home/mbc/git/afb-daemon-new/src/http-svc.c +/home/fulup/Workspace/afb-daemon/src/session.c=/home/fulup/Workspace/afb-daemon/build/src#-g3 -gdwarf-2 -fPIE -I/home/fulup/Workspace/afb-daemon/include -I/usr/include/json-c -o CMakeFiles/afb-daemon.dir/session.c.o -c /home/fulup/Workspace/afb-daemon/src/session.c +/home/fulup/Workspace/afb-daemon/src/http-svc.c=/home/fulup/Workspace/afb-daemon/build/src#-g3 -gdwarf-2 -fPIE -I/home/fulup/Workspace/afb-daemon/include -I/usr/include/json-c -o CMakeFiles/afb-daemon.dir/http-svc.c.o -c /home/fulup/Workspace/afb-daemon/src/http-svc.c diff --git a/nbproject/private/configurations.xml b/nbproject/private/configurations.xml index 71cd85e1..7306ab22 100644 --- a/nbproject/private/configurations.xml +++ b/nbproject/private/configurations.xml @@ -6,10 +6,14 @@ + CMakeCCompilerId.c + + + feature_tests.c @@ -18,6 +22,8 @@ + + local-def.h proto-def.h @@ -43,6 +49,8 @@ + + @@ -51,6 +59,8 @@ + + @@ -82,6 +92,8 @@ + + @@ -98,8 +110,9 @@ "${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons --token=123456789 "${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons --token=123456789 + "${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons --token=123456789 --rootdir=/home/fulup/Workspace/afb-client/dist.dev - "${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons --token=123456789 + "${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons --token=123456789 --rootdir=/home/fulup/Workspace/afb-client/dist.dev build true 0 diff --git a/src/SamplePost.c b/src/SamplePost.c new file mode 100644 index 00000000..6d5c44be --- /dev/null +++ b/src/SamplePost.c @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2015 "IoT.bzh" + * Author "Fulup Ar Foll" + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include + +#define PORT 8888 +#define POSTBUFFERSIZE 512 +#define MAXCLIENTS 2 + +#define GET 0 +#define POST 1 + +static unsigned int nr_of_uploading_clients = 0; + +struct connection_info_struct +{ + int connectiontype; + struct MHD_PostProcessor *postprocessor; + FILE *fp; + const char *answerstring; + int answercode; +}; + +const char *askpage = "\n\ + Upload a file, please!
\n\ + There are %u clients uploading at the moment.
\n\ +
\n\ + \n\ +
\n\ + "; + +const char *busypage = + "This server is busy, please try again later."; + +const char *completepage = + "The upload has been completed."; + +const char *errorpage = + "This doesn't seem to be right."; +const char *servererrorpage = + "An internal server error has occured."; +const char *fileexistspage = + "This file already exists."; + + +static int +send_page (struct MHD_Connection *connection, const char *page, + int status_code) +{ + int ret; + struct MHD_Response *response; + + response = + MHD_create_response_from_buffer (strlen (page), (void *) page, + MHD_RESPMEM_PERSISTENT); + if (!response) + return MHD_NO; + + ret = MHD_queue_response (connection, status_code, response); + MHD_destroy_response (response); + + return ret; +} + + +static int +iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, + const char *filename, const char *content_type, + const char *transfer_encoding, const char *data, uint64_t off, + size_t size) +{ + struct connection_info_struct *con_info = coninfo_cls; + FILE *fp; + + con_info->answerstring = servererrorpage; + con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR; + + if (0 != strcmp (key, "file")) + return MHD_NO; + + if (!con_info->fp) + { + if (NULL != (fp = fopen (filename, "rb"))) + { + fclose (fp); + con_info->answerstring = fileexistspage; + con_info->answercode = MHD_HTTP_FORBIDDEN; + return MHD_NO; + } + + con_info->fp = fopen (filename, "ab"); + if (!con_info->fp) + return MHD_NO; + } + + if (size > 0) + { + if (!fwrite (data, size, sizeof (char), con_info->fp)) + return MHD_NO; + } + + con_info->answerstring = completepage; + con_info->answercode = MHD_HTTP_OK; + + return MHD_YES; +} + +static void +request_completed (void *cls, struct MHD_Connection *connection, + void **con_cls, enum MHD_RequestTerminationCode toe) +{ + struct connection_info_struct *con_info = *con_cls; + + if (NULL == con_info) + return; + + if (con_info->connectiontype == POST) + { + if (NULL != con_info->postprocessor) + { + MHD_destroy_post_processor (con_info->postprocessor); + nr_of_uploading_clients--; + } + + if (con_info->fp) + fclose (con_info->fp); + } + + free (con_info); + *con_cls = NULL; +} + + +static int +answer_to_connection (void *cls, struct MHD_Connection *connection, + const char *url, const char *method, + const char *version, const char *upload_data, + size_t *upload_data_size, void **con_cls) +{ + if (NULL == *con_cls) + { + struct connection_info_struct *con_info; + + if (nr_of_uploading_clients >= MAXCLIENTS) + return send_page (connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE); + + con_info = malloc (sizeof (struct connection_info_struct)); + if (NULL == con_info) + return MHD_NO; + + con_info->fp = NULL; + + if (0 == strcmp (method, "POST")) + { + con_info->postprocessor = + MHD_create_post_processor (connection, POSTBUFFERSIZE, + iterate_post, (void *) con_info); + + if (NULL == con_info->postprocessor) + { + free (con_info); + return MHD_NO; + } + + nr_of_uploading_clients++; + + con_info->connectiontype = POST; + con_info->answercode = MHD_HTTP_OK; + con_info->answerstring = completepage; + } + else + con_info->connectiontype = GET; + + *con_cls = (void *) con_info; + + return MHD_YES; + } + + if (0 == strcmp (method, "GET")) + { + int ret; + char buffer[1024]; + + sprintf (buffer, askpage, nr_of_uploading_clients); + return send_page (connection, buffer, MHD_HTTP_OK); + } + + if (0 == strcmp (method, "POST")) + { + struct connection_info_struct *con_info = *con_cls; + + if (0 != *upload_data_size) + { + MHD_post_process (con_info->postprocessor, upload_data, + *upload_data_size); + *upload_data_size = 0; + + return MHD_YES; + } + else + return send_page (connection, con_info->answerstring, + con_info->answercode); + } + + return send_page (connection, errorpage, MHD_HTTP_BAD_REQUEST); +} + +int +main () +{ + struct MHD_Daemon *daemon; + + + daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, + &answer_to_connection, NULL, + MHD_OPTION_NOTIFY_COMPLETED, request_completed, + NULL, MHD_OPTION_END); + if (NULL == daemon) + return 1; + + getchar (); + + MHD_stop_daemon (daemon); + + return 0; +} \ No newline at end of file diff --git a/src/afbs-api.c b/src/afbs-api.c index c974efda..ae6ec639 100644 --- a/src/afbs-api.c +++ b/src/afbs-api.c @@ -114,37 +114,83 @@ STATIC json_object* clientContextReset (AFB_request *request) { return (jresp); } -// Some file upload sample -STATIC json_object* clientFileUpload (AFB_request *request) { - int fd; - json_object *jresp; - char filepath[512]; - char *filename; - - getQueryValue(request, "filename"); - if (filename == NULL) return (jsonNewMessage(AFB_FAIL, "No Filename provided")); - - // add an error code to respond - if (request->post == NULL) { - request->errcode=MHD_HTTP_UNAUTHORIZED; - return (jsonNewMessage(AFB_FAIL, "Post No Data")); +// In this case or handle is quite basic +typedef struct { + int fd; +} appPostCtx; + +// This function is call when PostForm processing is completed +STATIC void DonePostForm (AFB_request *request) { + AFB_PostHandle *postHandle = (AFB_PostHandle*)request->post->data;; + + int fd = (int)postHandle->handle; + close (fd); + + if (verbose) fprintf ("DonePostForm filename=%s upload done\n", form->filename); +} + + +// WARNING: PostForm callback are call one type for form value +STATIC AFB_error ProcessPostForm (AFB_request *request, AFB_PostItem *item) { + + AFB_PostHandle *postHandle; + appPostCtx *appCtx; + + // When Post is fully processed the same callback is call with a item==NULL + if (item == NULL) { + return(jsonNewMessage(AFB_SUCESS,"File [%s] uploaded at [%s] error=\n", item->filename, request->config->sessiondir)); } - // This is simple test let's write file in config->session->filename - strncpy (filepath, request->config->configfile, sizeof(filepath)); - strncat (filepath, "/", sizeof(filepath)); - strncat (filepath, "/", sizeof(filepath)); - + // Let's make sure this is a valid PostForm request + if (!request->post && request->post->type != AFB_POST_FORM) { + return(jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n")); + } else { + // In AFB_POST_FORM case post->data is a PostForm handle + postHandle = (AFB_PostHandle*) request->post->data; + } - if((fd = open(request->config->configfile, O_RDONLY)) < 0) { - return (jsonNewMessage(AFB_FAIL,"Fail to Upload file [%s] at [%s] error=\n", filename, filepath, strerror(errno))); - }; + // Check this is a file element + if (0 != strcmp (item->key, "file")) { + request->errcode = MHD_HTTP_FORBIDDEN; + request.jresp = jsonNewMessage(AFB_FAIL,"No File within element key=%s\n", item->key); + return AFB_FAIL; + } - // write file on disk and free fd - write (fd, request->post, request->len); - close(fd); + // This is the 1st Item iteration let's open output file and allocate necessary resources + if (postHandle->handle == NULL) { + strncpy (filepath, request->config->sessiondir, sizeof(filepath)); + strncat (filepath, "/", sizeof(filepath)); + strncat (filepath, item->filename, sizeof(filepath)); + + if((fd = open(request->config->sessiondir, O_RDONLY)) < 0) { + request->errcode = MHD_HTTP_FORBIDDEN; + request->jresp = jsonNewMessage(AFB_FAIL,"Fail to Upload file [%s] at [%s] error=\n", item->filename, request->config->sessiondir, strerror(errno)); + return AFB_FAIL; + }; + + // keep track of file handle with item + appCtx = malloc (size(appPostCtx)); // May place anything here until post->completeCB handle resources liberation + postHandle->handle = malloc (size(appPostCtx)); // May place anything here until post->completeCB handle resources liberation - return (jresp); + postHandle->completeCB = DonePostForm; // CallBack when Form Processing is finished + + } else { + // this is not the call, FD is already open + fd = (int)post->handle; + } + + // We have something to write + if (item.len > 0) { + + if (!write (fd, item->data, item->len)) { + request->errcode = MHD_HTTP_FORBIDDEN; + request->json = jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno)); + return AFB_FAIL; + } + } + + // every event should return Sucess or Form processing stop + return AFB_SUCCESS; } // This function is call when Client Session Context is removed @@ -160,7 +206,7 @@ STATIC AFB_restapi pluginApis[]= { {"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"}, {"token-check" , (AFB_apiCB)clientContextCheck ,"Check Client Context Token"}, {"token-reset" , (AFB_apiCB)clientContextReset ,"Close Client Context and Free resources"}, - {"file-upload" , (AFB_apiCB)clientFileUpload ,"Demo for file upload"}, + {"file-upload" , (AFB_apiCB)ProcessPostForm ,"Demo for file upload"}, {NULL} }; diff --git a/src/alsa-api.c b/src/alsa-api.c index 23964a68..46b971b3 100644 --- a/src/alsa-api.c +++ b/src/alsa-api.c @@ -42,7 +42,6 @@ STATIC json_object* pingSample (AFB_request *request) { response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon %d query={%s} handle=[%s] PostData: \'%s\' " , pingcount++, query, request->post); - if (verbose) fprintf(stderr, "%d: \n", pingcount); return (response); } diff --git a/src/http-svc.c b/src/http-svc.c index 0f7f80ca..ac0606eb 100644 --- a/src/http-svc.c +++ b/src/http-svc.c @@ -70,14 +70,10 @@ static AFB_error initLibMagic (AFB_session *session) { // Because of POST call multiple time requestApi we need to free POST handle here static void endRequest (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe) { - AFB_HttpPost *posthandle = *con_cls; + AFB_PostHandle *posthandle = *con_cls; // if post handle was used let's free everything - if (posthandle) { - if (verbose) fprintf (stderr, "End Post Request UID=%d\n", posthandle->uid); - free (posthandle->data); - free (posthandle); - } + if (posthandle != NULL) endPostRequest (posthandle); } diff --git a/src/rest-api.c b/src/rest-api.c index 097f03d3..cf13a7c9 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -16,6 +16,8 @@ * along with this program. If not, see . * * Contain all generic part to handle REST/API + * + * https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'FILE *fp'] */ #include "../include/local-def.h" @@ -90,15 +92,26 @@ PUBLIC int getQueryAll(AFB_request * request, char *buffer, size_t len) { } // Because of POST call multiple time requestApi we need to free POST handle here -STATIC void endRequest(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe) { - AFB_HttpPost *posthandle = *con_cls; - - // if post handle was used let's free everything - if (posthandle) { - if (verbose) fprintf(stderr, "End Post Request UID=%d\n", posthandle->uid); - free(posthandle->data); - free(posthandle); +PUBLIC void endPostRequest(AFB_PostHandle *posthandle) { + + if (posthandle->type == AFB_POST_JSON) { + if (verbose) fprintf(stderr, "End PostJson Request UID=%d\n", posthandle->uid); } + + if (posthandle->type == AFB_POST_FORM) { + AFB_PostHandle *postform = (AFB_PostHandle*) posthandle->private; + if (verbose) fprintf(stderr, "End PostForm Request UID=%d\n", posthandle->uid); + + // call API termination callback + if (!posthandle->private) { + + && !posthandle->private->completeCB) { + posthandle->private->completeCB (posthandle->private); + } + } + freeRequest (posthandle->private); + free(posthandle); + } // Check of apiurl is declare in this plugin and call it @@ -159,10 +172,15 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request) { // add client context to request ctxClientGet(request, plugin); - + // Effectively call the API with a subset of the context - jresp = plugin->apis[idx].callback(request, plugin->handle); + jresp = plugin->apis[idx].callback(request); + // Allocate Json object and build response + request->jresp = json_object_new_object(); + json_object_get (afbJsonType); // increate jsontype reference count + json_object_object_add (request->jresp, "jtype", afbJsonType); + // API should return NULL of a valid Json Object if (jresp == NULL) { json_object_object_add(jcall, "status", json_object_new_string ("null")); @@ -188,63 +206,94 @@ 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) { +STATIC AFB_error findAndCallApi (AFB_request *request, void *extractx) { 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); + for (idx = 0; request->plugins[idx] != NULL; idx++) { + if (!strcmp(request->plugins[idx]->prefix, baseurl)) { + status =callPluginApi(request->plugins[idx], request, extractx); break; } } // No plugin was found - if (session->plugins[idx] == NULL) { + if (request->plugins[idx] == NULL) { request->jresp = jsonNewMessage(AFB_FATAL, "No Plugin=[%s]", request->plugin); - request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY; - return (AFB_FAIL); + goto ExitOnError; } // 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); + goto ExitOnError; } - + + // Everything look OK return (status); + +ExitOnError: + request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY; + return (AFB_FAIL); } -// process rest API query -PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method - , const char *upload_data, size_t *upload_data_size, void **con_cls) { +// This CB is call for every item with a form post it reformat iterator values +// and callback Plugin API for each Item within PostForm. +doPostIterate (void *cls, enum MHD_ValueKind kind, const char *key, + const char *filename, const char *mimetype, + const char *encoding, const char *data, uint64_t off, + size_t size) { + + AFB_error status; + AFB_HttpItem item; - static int postcount = 0; // static counter to debug POST protocol - char *baseurl, *baseapi, *urlcpy1, *urlcpy2, *query, *token, *uuid; - json_object *errMessage; - AFB_error status; - struct MHD_Response *webResponse; - const char *serialized, parsedurl; - AFB_request request; - AFB_HttpPost *posthandle = *con_cls; - AFB_clientCtx clientCtx; - int idx, ret; + // retrieve API request from Post iterator handle + AFB_PostHandle *postctx = (AFB_PostHandle*)cls; + AFB_request *request = (AFB_request*)post->private; + AFB_PostRequest post; + + + // Create and Item value for Plugin API + item.kind = kind; + item.key = key; + item.filename = filename; + item.mimetype = mimetype; + item.encoding = encoding; + item.len = size; + item.data = data; + item.off = off; + + // Reformat Request to make it somehow similar to GET/PostJson case + post.data= (char*) postctx; + post.len = size; + post.type= AFB_POST_FORM;; + request->post = &post; + + // effectively call plugin API + status = findAndCallApi (request, &item); + + // when returning no processing of postform stop + if (status != AFB_SUCCESS) return MHD_NO; + + // let's allow iterator to move to next item + return (MHD_YES;); +} + +STATIC void freeRequest (AFB_request *request) { + free (request->plugin); + free (request->api); + free (request); +} +STATIC AFB_request *createRequest (struct MHD_Connection *connection, AFB_session *session, const char* url) { + + AFB_request *request; + + // Start with a clean request + request = calloc (1, sizeof (AFB_request)); + char *urlcpy1, urlcpy2; + // Extract plugin urlpath from request and make two copy because strsep overload copy urlcpy1 = urlcpy2 = strdup(url); baseurl = strsep(&urlcpy2, "/"); @@ -253,116 +302,175 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co goto ExitOnError; } + // let's compute URL and call API baseapi = strsep(&urlcpy2, "/"); if (baseapi == NULL) { errMessage = jsonNewMessage(AFB_FATAL, "Invalid API call url=[%s]", url); goto ExitOnError; } + // build request structure + request->connection = connection; + request->config = session.config; + request->url = url; + request->plugin = strdup (baseurl); + request->api = strdup (baseapi); + request->plugins= session->plugins; + + free(urlcpy1); +} +// process rest API query +PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method + , const char *upload_data, size_t *upload_data_size, void **con_cls) { + + static int postcount = 0; // static counter to debug POST protocol + json_object *errMessage; + AFB_error status; + struct MHD_Response *webResponse; + const char *serialized; + AFB_request request; + AFB_PostHandle *posthandle = *con_cls; + int ret; + // if post data may come in multiple calls if (0 == strcmp(method, MHD_HTTP_METHOD_POST)) { const char *encoding, *param; int contentlen = -1; - AFB_HttpPost *posthandle = *con_cls; + AFB_PostHandle *posthandle = *con_cls; - // Let make sure we have the right encoding and a valid length - encoding = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE); - param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH); - if (param) sscanf(param, "%i", &contentlen); + // This is the initial post event let's create form post structure POST datas come in multiple events + if (posthandle == NULL) { + fprintf(stderr, "This is the 1st Post Event postuid=%d\n", posthandle->uid); - // POST datas may come in multiple chunk. Even when it never happen on AFB, we still have to handle the case + // allocate application POST processor handle to zero + posthandle = cmalloc(1, sizeof (AFB_PostHandle)); + posthandle->uid = postcount++; // build a UID for DEBUG + *con_cls = posthandle; // attache POST handle to current HTTP request + + // Let make sure we have the right encoding and a valid length + encoding = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE); + param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH); + if (param) sscanf(param, "%i", &contentlen); - // 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; - } + // Form post is handle through a PostProcessor and call API once per form key + if (strcasestr(encoding, FORM_CONTENT) != NULL) { + + posthandle = malloc(sizeof (AFB_PostHandle)); // allocate application POST processor handle + posthandle->type = AFB_POST_FORM; + posthandle->private= (void*)createRequest (connection, session, url); + posthandle->pp = MHD_create_post_processor (connection, MAX_POST_SIZE, doPostIterate, posthandle); + + if (NULL == posthandle->pp) { + fprintf(stderr,"OOPS: Internal error fail to allocate MHD_create_post_processor\n"); + free (posthandle); + return MHD_NO; + } + 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); - } + // POST json is store into a buffer and present in one piece to API + if (strcasestr(encoding, JSON_CONTENT) != NULL) { - if (contentlen > MAX_POST_SIZE) { - errMessage = jsonNewMessage(AFB_FATAL, "Post Date to big %d > %d", contentlen, MAX_POST_SIZE); - goto ExitOnError; - } + if (contentlen > MAX_POST_SIZE) { + errMessage = jsonNewMessage(AFB_FATAL, "Post Date to big %d > %d", contentlen, MAX_POST_SIZE); + goto ExitOnError; + } - // In POST mode first libmicrohttp call only establishes POST handling. - if (posthandle == NULL) { - posthandle = malloc(sizeof (AFB_HttpPost)); // allocate application POST processor handle - posthandle->uid = postcount++; // build a UID for DEBUG - posthandle->len = 0; // effective length within POST handler - posthandle->data = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string - *con_cls = posthandle; // attache POST handle to current HTTP session + if (posthandle == NULL) { + posthandle->type = AFB_POST_JSON; + posthandle->private = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string - if (verbose) fprintf(stderr, "Create Post[%d] Size=%d\n", posthandle->uid, contentlen); - return MHD_YES; + if (verbose) fprintf(stderr, "Create PostJson[%d] Size=%d\n", posthandle->uid, contentlen); + return MHD_YES; + } + + // We only support Json and Form Post format + errMessage = jsonNewMessage(AFB_FATAL, "Post Date wrong type encoding=%s != %s", encoding, JSON_CONTENT); + goto ExitOnError; + } } // This time we receive partial/all Post data. Note that even if we get all POST data. We should nevertheless // return MHD_YES and not process the request directly. Otherwise Libmicrohttpd is unhappy and fails with - // 'Internal application error, closing connection'. + // 'Internal application error, closing connection'. if (*upload_data_size) { if (verbose) fprintf(stderr, "Update Post[%d]\n", posthandle->uid); + + if (posthandle->type == AFB_POST_FORM) { + MHD_post_process (con_info->postprocessor, upload_data, *upload_data_size); + } + + // Process JsonPost request when buffer is completed let's call API + if (posthandle->type == AFB_POST_JSON) { - memcpy(&posthandle->data[posthandle->len], upload_data, *upload_data_size); - posthandle->len = posthandle->len + *upload_data_size; - *upload_data_size = 0; + memcpy(&posthandle->private[posthandle->len], upload_data, *upload_data_size); + posthandle->len = posthandle->len + *upload_data_size; + *upload_data_size = 0; + } return MHD_YES; - } - - // We should only start to process DATA after Libmicrohttpd call or application handler with *upload_data_size==0 - // At this level we're may verify that we got everything and process DATA - if (posthandle->len != contentlen) { - errMessage = jsonNewMessage(AFB_FATAL, "Post Data Incomplete UID=%d Len %d != %s", posthandle->uid, contentlen, posthandle->len); - goto ExitOnError; - } + + } else { // we have finish with Post reception let's finish the work + + // Create a request structure to finalise the request + request= createRequest (connection, session, url); - // Before processing data, make sure buffer string is properly ended - posthandle->data[posthandle->len] = '\0'; - request.post = posthandle->data; + // We should only start to process DATA after Libmicrohttpd call or application handler with *upload_data_size==0 + if (posthandle->type == AFB_POST_FORM) { + MHD_post_process (posthandle->pp, upload_data, *upload_data_size); + } + + if (posthandle->type == AFB_POST_JSON) { + // At this level we're may verify that we got everything and process DATA + if (posthandle->len != contentlen) { + errMessage = jsonNewMessage(AFB_FATAL, "Post Data Incomplete UID=%d Len %d != %s", posthandle->uid, contentlen, posthandle->len); + goto ExitOnError; + } - if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", posthandle->uid, request.post); + // Before processing data, make sure buffer string is properly ended + posthandle->private[posthandle->len] = '\0'; + request->post.data = posthandle->private; + request->post.type = posthandle->type; + if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", posthandle->uid, request.post); + } + } } else { - request.post = NULL; + // this is a get we only need a request + request= createRequest (connection, session, url); }; - - // Now that we got request data let's call the API - status = findAndCallApi (connection, session, url, &request); - + + // Request is ready let's call API without any extra handle + status = findAndCallApi (request, NULL); + +ExitOnResponse: + freeRequest (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); // client did not pass token on URI let's use cookies if ((!request.restfull) && (request.client != NULL)) { char cookie[64]; snprintf (cookie, sizeof (cookie), "%s=%s", COOKIE_NAME, request.client->uuid); MHD_add_response_header (webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie); - // if(verbose) fprintf(stderr,"Cookie: [%s]\n", cookie); } // if requested add an error status if (request.errcode != 0) ret=MHD_queue_response (connection, request.errcode, webResponse); - else ret = MHD_queue_response(connection, MHD_HTTP_OK, webResponse); + else MHD_queue_response(connection, MHD_HTTP_OK, webResponse); MHD_destroy_response(webResponse); json_object_put(request.jresp); // decrease reference rqtcount to free the json object - return ret; + return MHD_YES; ExitOnError: - free(urlcpy1); + freeRequest (request); serialized = json_object_to_json_string(errMessage); webResponse = MHD_create_response_from_buffer(strlen(serialized), (void*) serialized, MHD_RESPMEM_MUST_COPY); - ret = MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, webResponse); + MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, webResponse); MHD_destroy_response(webResponse); json_object_put(errMessage); // decrease reference rqtcount to free the json object - return ret; + return MHD_YES; } diff --git a/src/session.c b/src/session.c index 45005ab4..3494dadb 100644 --- a/src/session.c +++ b/src/session.c @@ -257,12 +257,12 @@ PUBLIC json_object * sessionToDisk (AFB_session *session, AFB_request *request, // do we have extra session info ? - if (request->post) { + if (request->post->type == AFB_POST_JSON) { static json_object *info, *jtype; const char *ajglabel; // extract session info from args - info = json_tokener_parse (request->post); + info = json_tokener_parse (request->post->data); if (!info) { response = jsonNewMessage (AFB_FATAL,"sndcard=%s session=%s invalid json args=%s", request->plugin, name, request->post); goto OnErrorExit; @@ -489,8 +489,7 @@ PUBLIC AFB_error ctxTokenCreate (AFB_request *request) { if (token == NULL) return AFB_UNAUTH; // verify that presented initial tokens fit - if (strcmp(request->config->token, token)) return AFB_UNAUTH; - + if (strcmp(request->config->token, token)) return AFB_UNAUTH; }