2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Contain all generic part to handle REST/API
20 * https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c']
23 #include "../include/local-def.h"
30 #define AFB_MSG_JTYPE "AJB_reply"
34 static json_object *afbJsonType;
37 // Because of POST call multiple time requestApi we need to free POST handle here
38 // Note this method is called from http-svc just before closing session
39 PUBLIC void endPostRequest(AFB_PostHandle *postHandle) {
41 if (postHandle->type == AFB_POST_JSON) {
42 // if (verbose) fprintf(stderr, "End PostJson Request UID=%d\n", postHandle->uid);
45 if (postHandle->type == AFB_POST_FORM) {
46 if (verbose) fprintf(stderr, "End PostForm Request UID=%d\n", postHandle->uid);
48 free(postHandle->private);
52 // Check of apiurl is declare in this plugin and call it
53 STATIC AFB_error callPluginApi(AFB_request *request, int plugidx, void *context) {
54 json_object *jresp, *jcall;
56 AFB_clientCtx *clientCtx;
57 AFB_plugin *plugin = request->plugins[plugidx];
58 int signals[]= {SIGALRM, SIGSEGV, SIGFPE, 0};
60 /*---------------------------------------------------------------
61 | Signal handler defined inside CallPluginApi to access Request
62 +---------------------------------------------------------------- */
63 void pluginError (int signum) {
67 // unlock signal to allow a new signal to come
68 sigemptyset (&sigset);
69 sigaddset (&sigset, signum);
70 sigprocmask (SIG_UNBLOCK, &sigset, 0);
72 fprintf (stderr, "Oops:%s Plugin Api Timeout timeout\n", configTime());
73 longjmp (request->checkPluginCall, signum);
77 // If a plugin hold this urlpath call its callback
78 for (idx = 0; plugin->apis[idx].callback != NULL; idx++) {
79 if (!strcmp(plugin->apis[idx].name, request->api)) {
81 // Request was found and at least partially executed
82 request->jresp = json_object_new_object();
83 json_object_get (afbJsonType); // increate jsontype reference count
84 json_object_object_add (request->jresp, "jtype", afbJsonType);
86 // prepare an object to store calling values
87 jcall=json_object_new_object();
88 json_object_object_add(jcall, "prefix", json_object_new_string (plugin->prefix));
89 json_object_object_add(jcall, "api" , json_object_new_string (plugin->apis[idx].name));
91 // save context before calling the API
92 status = setjmp (request->checkPluginCall);
95 // Plugin aborted somewhere during its execution
96 json_object_object_add(jcall, "status", json_object_new_string ("abort"));
97 json_object_object_add(jcall, "info" , json_object_new_string ("Plugin broke during execution"));
98 json_object_object_add(request->jresp, "request", jcall);
102 // If timeout protection==0 we are in debug and we do not apply signal protection
103 if (request->config->apiTimeout > 0) {
104 for (sig=0; signals[sig] != 0; sig++) {
105 if (signal (signals[sig], pluginError) == SIG_ERR) {
106 request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY;
107 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
108 json_object_object_add(jcall, "info", json_object_new_string ("Setting Timeout Handler Failed"));
109 json_object_object_add(request->jresp, "request", jcall);
113 // Trigger a timer to protect from unacceptable long time execution
114 alarm (request->config->apiTimeout);
117 // Out of SessionNone every call get a client context session
118 if (AFB_SESSION_NONE != plugin->apis[idx].session) {
120 // add client context to request
121 clientCtx = ctxClientGet(request, plugidx);
122 if (clientCtx == NULL) {
123 request->errcode=MHD_HTTP_INSUFFICIENT_STORAGE;
124 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
125 json_object_object_add(jcall, "info", json_object_new_string ("Client Session Context Full !!!"));
126 json_object_object_add(request->jresp, "request", jcall);
130 if (verbose) fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[0x%x] Uuid=[%s] Token=[%s]\n"
131 , request->prefix, request->api, plugin->apis[idx].session, clientCtx, clientCtx->uuid, clientCtx->token);
133 switch(plugin->apis[idx].session) {
135 case AFB_SESSION_CREATE:
136 if (clientCtx->token[0] != '\0') {
137 request->errcode=MHD_HTTP_UNAUTHORIZED;
138 json_object_object_add(jcall, "status", json_object_new_string ("exist"));
139 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CREATE Session already exist"));
140 json_object_object_add(request->jresp, "request", jcall);
144 if (AFB_SUCCESS != ctxTokenCreate (clientCtx, request)) {
145 request->errcode=MHD_HTTP_UNAUTHORIZED;
146 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
147 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CREATE Invalid Initial Token"));
148 json_object_object_add(request->jresp, "request", jcall);
151 json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));
152 json_object_object_add(jcall, "token", json_object_new_string (clientCtx->token));
153 json_object_object_add(jcall, "timeout", json_object_new_int (request->config->cntxTimeout));
158 case AFB_SESSION_RENEW:
159 if (AFB_SUCCESS != ctxTokenRefresh (clientCtx, request)) {
160 request->errcode=MHD_HTTP_UNAUTHORIZED;
161 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
162 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_REFRESH Broken Exchange Token Chain"));
163 json_object_object_add(request->jresp, "request", jcall);
166 json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));
167 json_object_object_add(jcall, "token", json_object_new_string (clientCtx->token));
168 json_object_object_add(jcall, "timeout", json_object_new_int (request->config->cntxTimeout));
172 case AFB_SESSION_CLOSE:
173 if (AFB_SUCCESS != ctxTokenCheck (clientCtx, request)) {
174 request->errcode=MHD_HTTP_UNAUTHORIZED;
175 json_object_object_add(jcall, "status", json_object_new_string ("empty"));
176 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CLOSE Not a Valid Access Token"));
177 json_object_object_add(request->jresp, "request", jcall);
180 json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));
184 case AFB_SESSION_CHECK:
186 // default action is check
187 if (AFB_SUCCESS != ctxTokenCheck (clientCtx, request)) {
188 request->errcode=MHD_HTTP_UNAUTHORIZED;
189 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
190 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CHECK Invalid Active Token"));
191 json_object_object_add(request->jresp, "request", jcall);
198 // Effectively call the API with a subset of the context
199 jresp = plugin->apis[idx].callback(request, context);
201 // handle intermediary Post Iterates out of band
202 if ((jresp == NULL) && (request->errcode == MHD_HTTP_OK)) return (AFB_SUCCESS);
204 // Session close is done after the API call so API can still use session in closing API
205 if (AFB_SESSION_CLOSE == plugin->apis[idx].session) ctxTokenReset (clientCtx, request);
207 // API should return NULL of a valid Json Object
209 json_object_object_add(jcall, "status", json_object_new_string ("null"));
210 json_object_object_add(request->jresp, "request", jcall);
211 request->errcode = MHD_HTTP_NO_RESPONSE;
214 json_object_object_add(jcall, "status", json_object_new_string ("processed"));
215 json_object_object_add(request->jresp, "request", jcall);
216 json_object_object_add(request->jresp, "response", jresp);
218 // cancel timeout and plugin signal handle before next call
219 if (request->config->apiTimeout > 0) {
221 for (sig=0; signals[sig] != 0; sig++) {
222 signal (signals[sig], SIG_DFL);
232 STATIC AFB_error findAndCallApi (AFB_request *request, void *context) {
236 if (!request->api || !request->prefix) return (AFB_FAIL);
238 // Search for a plugin with this urlpath
239 for (idx = 0; request->plugins[idx] != NULL; idx++) {
240 if (!strcmp(request->plugins[idx]->prefix, request->prefix)) {
241 status =callPluginApi(request, idx, context);
245 // No plugin was found
246 if (request->plugins[idx] == NULL) {
247 request->jresp = jsonNewMessage(AFB_FATAL, "No Plugin=[%s] Url=%s", request->prefix, request->url);
251 // plugin callback did not return a valid Json Object
252 if (status == AFB_FAIL) {
253 request->jresp = jsonNewMessage(AFB_FATAL, "No API=[%s] for Plugin=[%s] url=[%s]", request->api, request->prefix, request->url);
257 // Everything look OK
261 request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY;
265 // This CB is call for every item with a form post it reformat iterator values
266 // and callback Plugin API for each Item within PostForm.
267 doPostIterate (void *cls, enum MHD_ValueKind kind, const char *key,
268 const char *filename, const char *mimetype,
269 const char *encoding, const char *data, uint64_t offset,
275 // retrieve API request from Post iterator handle
276 AFB_PostHandle *postHandle = (AFB_PostHandle*)cls;
277 AFB_request *request = (AFB_request*)postHandle->private;
278 AFB_PostRequest postRequest;
280 fprintf (stderr, "postHandle key=%s filename=%s len=%d mime=%s\n", key, filename, size, mimetype);
282 // Create and Item value for Plugin API
285 item.filename = filename;
286 item.mimetype = mimetype;
287 item.encoding = encoding;
290 item.offset = offset;
292 // Reformat Request to make it somehow similar to GET/PostJson case
293 postRequest.data= (char*) postHandle;
294 postRequest.len = size;
295 postRequest.type= AFB_POST_FORM;;
296 request->post = &postRequest;
298 // effectively call plugin API
299 status = findAndCallApi (request, &item);
300 // when returning no processing of postform stop
301 if (status != AFB_SUCCESS) return MHD_NO;
303 // let's allow iterator to move to next item
307 STATIC void freeRequest (AFB_request *request) {
309 free (request->prefix);
314 STATIC AFB_request *createRequest (struct MHD_Connection *connection, AFB_session *session, const char* url) {
316 AFB_request *request;
319 // Start with a clean request
320 request = calloc (1, sizeof (AFB_request));
321 char *urlcpy1, *urlcpy2;
322 char *baseapi, *baseurl;
324 // Extract plugin urlpath from request and make two copy because strsep overload copy
325 urlcpy1 = urlcpy2 = strdup(url);
326 baseurl = strsep(&urlcpy2, "/");
327 if (baseurl == NULL) {
328 request->jresp = jsonNewMessage(AFB_FATAL, "Invalid API call url=[%s]", url);
329 request->errcode = MHD_HTTP_BAD_REQUEST;
333 // let's compute URL and call API
334 baseapi = strsep(&urlcpy2, "/");
335 if (baseapi == NULL) {
336 request->jresp = jsonNewMessage(AFB_FATAL, "Invalid API call plugin=[%s] url=[%s]", baseurl, url);
337 request->errcode = MHD_HTTP_BAD_REQUEST;
341 // build request structure
342 request->connection = connection;
343 request->config = session->config;
345 request->prefix = strdup (baseurl);
346 request->api = strdup (baseapi);
347 request->plugins= session->plugins;
348 for (idx = 0; idx < session->config->pluginCount; idx++) {
349 if (!strcmp(baseurl, session->plugins[idx]->prefix)) {
350 request->plugin = session->plugins[idx];
360 // process rest API query
361 PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method
362 , const char *upload_data, size_t *upload_data_size, void **con_cls) {
364 static int postcount = 0; // static counter to debug POST protocol
365 json_object *errMessage;
367 struct MHD_Response *webResponse;
368 const char *serialized;
369 AFB_request *request;
370 AFB_PostHandle *postHandle;
371 AFB_PostRequest postRequest;
374 // if post data may come in multiple calls
375 if (0 == strcmp(method, MHD_HTTP_METHOD_POST)) {
376 const char *encoding, *param;
378 postHandle = *con_cls;
380 // This is the initial post event let's create form post structure POST datas come in multiple events
381 if (postHandle == NULL) {
383 // allocate application POST processor handle to zero
384 postHandle = calloc(1, sizeof (AFB_PostHandle));
385 postHandle->uid = postcount++; // build a UID for DEBUG
386 *con_cls = postHandle; // update context with posthandle
388 // Let make sure we have the right encoding and a valid length
389 encoding = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE);
391 // We are facing an empty post let's process it as a get
392 if (encoding == NULL) {
393 request= createRequest (connection, session, url);
397 // Form post is handle through a PostProcessor and call API once per form key
398 if (strcasestr(encoding, FORM_CONTENT) != NULL) {
399 if (verbose) fprintf(stderr, "Create PostForm[uid=%d]\n", postHandle->uid);
401 request = createRequest (connection, session, url);
402 if (request->jresp != NULL) goto ProcessApiCall;
404 postHandle = malloc(sizeof (AFB_PostHandle)); // allocate application POST processor handle
405 postHandle->type = AFB_POST_FORM;
406 postHandle->pp = MHD_create_post_processor (connection, MAX_POST_SIZE, doPostIterate, postHandle);
407 postHandle->private= (void*)request;
409 if (NULL == postHandle->pp) {
410 fprintf(stderr,"OOPS: Internal error fail to allocate MHD_create_post_processor\n");
417 // POST json is store into a buffer and present in one piece to API
418 if (strcasestr(encoding, JSON_CONTENT) != NULL) {
420 param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
421 if (param) sscanf(param, "%i", &contentlen);
423 // Because PostJson are build in RAM size is constrained
424 if (contentlen > MAX_POST_SIZE) {
425 errMessage = jsonNewMessage(AFB_FATAL, "Post Date to big %d > %d", contentlen, MAX_POST_SIZE);
429 // Size is OK, let's allocate a buffer to hold post data
430 postHandle->type = AFB_POST_JSON;
431 postHandle->private = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string
433 // if (verbose) fprintf(stderr, "Create PostJson[uid=%d] Size=%d\n", postHandle->uid, contentlen);
437 // We only support Json and Form Post format
438 errMessage = jsonNewMessage(AFB_FATAL, "Post Date wrong type encoding=%s != %s", encoding, JSON_CONTENT);
443 // This time we receive partial/all Post data. Note that even if we get all POST data. We should nevertheless
444 // return MHD_YES and not process the request directly. Otherwise Libmicrohttpd is unhappy and fails with
445 // 'Internal application error, closing connection'.
446 if (*upload_data_size) {
448 if (postHandle->type == AFB_POST_FORM) {
449 // if (verbose) fprintf(stderr, "Processing PostForm[uid=%d]\n", postHandle->uid);
450 MHD_post_process (postHandle->pp, upload_data, *upload_data_size);
453 // Process JsonPost request when buffer is completed let's call API
454 if (postHandle->type == AFB_POST_JSON) {
455 // if (verbose) fprintf(stderr, "Updating PostJson[uid=%d]\n", postHandle->uid);
456 memcpy(&postHandle->private[postHandle->len], upload_data, *upload_data_size);
457 postHandle->len = postHandle->len + *upload_data_size;
460 *upload_data_size = 0;
463 } else { // we have finish with Post reception let's finish the work
465 // Create a request structure to finalise the request
466 request= createRequest (connection, session, url);
467 if (request->jresp != NULL) {
468 errMessage = request->jresp;
472 // Postform add application context handle to request
473 if (postHandle->type == AFB_POST_FORM) {
474 postRequest.data = (char*) postHandle;
475 postRequest.type = postHandle->type;
476 request->post = &postRequest;
479 if (postHandle->type == AFB_POST_JSON) {
480 // if (verbose) fprintf(stderr, "Processing PostJson[uid=%d]\n", postHandle->uid);
482 param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
483 if (param) sscanf(param, "%i", &contentlen);
485 // At this level we're may verify that we got everything and process DATA
486 if (postHandle->len != contentlen) {
487 errMessage = jsonNewMessage(AFB_FATAL, "Post Data Incomplete UID=%d Len %d != %d", postHandle->uid, contentlen, postHandle->len);
491 // Before processing data, make sure buffer string is properly ended
492 postHandle->private[postHandle->len] = '\0';
493 postRequest.data = postHandle->private;
494 postRequest.type = postHandle->type;
495 request->post = &postRequest;
497 // if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", postHandle->uid, request->post->data);
501 // this is a get we only need a request
502 request= createRequest (connection, session, url);
506 // Request is ready let's call API without any extra handle
507 status = findAndCallApi (request, NULL);
509 serialized = json_object_to_json_string(request->jresp);
510 webResponse = MHD_create_response_from_buffer(strlen(serialized), (void*) serialized, MHD_RESPMEM_MUST_COPY);
512 // client did not pass token on URI let's use cookies
513 if ((!request->restfull) && (request->context != NULL)) {
515 snprintf (cookie, sizeof (cookie), "%s=%s", COOKIE_NAME, request->uuid);
516 MHD_add_response_header (webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie);
519 // if requested add an error status
520 if (request->errcode != 0) ret=MHD_queue_response (connection, request->errcode, webResponse);
521 else MHD_queue_response(connection, MHD_HTTP_OK, webResponse);
523 MHD_destroy_response(webResponse);
524 json_object_put(request->jresp); // decrease reference rqtcount to free the json object
525 freeRequest (request);
529 freeRequest (request);
530 serialized = json_object_to_json_string(errMessage);
531 webResponse = MHD_create_response_from_buffer(strlen(serialized), (void*) serialized, MHD_RESPMEM_MUST_COPY);
532 MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, webResponse);
533 MHD_destroy_response(webResponse);
534 json_object_put(errMessage); // decrease reference rqtcount to free the json object
539 // Loop on plugins. Check that they have the right type, prepare a JSON object with prefix
540 STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) {
543 for (idx = 0; plugins[idx] != NULL; idx++) {
544 if (plugins[idx]->type != AFB_PLUGIN_JSON) {
545 fprintf(stderr, "ERROR: AFSV plugin[%d] invalid type=%d != %d\n", idx, AFB_PLUGIN_JSON, plugins[idx]->type);
547 // some sanity controls
548 if ((plugins[idx]->prefix == NULL) || (plugins[idx]->info == NULL) || (plugins[idx]->apis == NULL)) {
549 if (plugins[idx]->prefix == NULL) plugins[idx]->prefix = "No URL prefix for APIs";
550 if (plugins[idx]->info == NULL) plugins[idx]->info = "No Info describing plugin APIs";
551 fprintf(stderr, "ERROR: plugin[%d] invalid prefix=%s info=%s", idx, plugins[idx]->prefix, plugins[idx]->info);
555 if (verbose) fprintf(stderr, "Loading plugin[%d] prefix=[%s] info=%s\n", idx, plugins[idx]->prefix, plugins[idx]->info);
557 // Prebuild plugin jtype to boost API response
558 plugins[idx]->jtype = json_object_new_string(plugins[idx]->prefix);
559 json_object_get(plugins[idx]->jtype); // increase reference count to make it permanent
560 plugins[idx]->prefixlen = strlen(plugins[idx]->prefix);
563 // Prebuild each API jtype to boost API json response
564 for (jdx = 0; plugins[idx]->apis[jdx].name != NULL; jdx++) {
565 AFB_privateApi *private = malloc (sizeof (AFB_privateApi));
566 if (plugins[idx]->apis[jdx].private != NULL) {
567 fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL=0x%x\n"
568 ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].private);
570 private->len = strlen (plugins[idx]->apis[jdx].name);
571 private->jtype=json_object_new_string(plugins[idx]->apis[jdx].name);
572 json_object_get(private->jtype); // increase reference count to make it permanent
573 plugins[idx]->apis[jdx].private = private;
580 void initPlugins(AFB_session *session) {
581 static AFB_plugin **plugins;
582 AFB_plugin* (*pluginRegisterFct)(void);
585 struct dirent *pluginDir;
587 afbJsonType = json_object_new_string (AFB_MSG_JTYPE);
590 /* pre-allocate for 20 plugins, we will downsize if necessary */
591 plugins = (AFB_plugin **) malloc (20*sizeof(AFB_plugin));
593 if ((dir = opendir(session->config->plugins)) == NULL) {
594 fprintf(stderr, "Could not open plugin directory [%s], exiting...\n", session->config->plugins);
598 while ((pluginDir = readdir(dir)) != NULL) {
600 if (!strstr (pluginDir->d_name, ".so"))
603 asprintf (&pluginPath, "%s/%s", session->config->plugins, pluginDir->d_name);
604 plugin = dlopen (pluginPath, RTLD_NOW | RTLD_LOCAL);
605 pluginRegisterFct = dlsym (plugin, "pluginRegister");
608 if (verbose) fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir->d_name);
610 } else if (!pluginRegisterFct) {
611 if (verbose) fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir->d_name);
615 if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading it\n", pluginDir->d_name);
616 plugins[num] = (AFB_plugin *) malloc (sizeof(AFB_plugin));
617 plugins[num] = (**pluginRegisterFct)();
619 /* only 20 plugins are supported at that time */
620 if (num == 20) break;
622 plugins = (AFB_plugin **) realloc (plugins, (num+1)*sizeof(AFB_plugin));
627 if (plugins[0] == NULL) {
628 fprintf(stderr, "No plugins found, afb-daemon is unlikely to work in this configuration, exiting...\n");
632 // complete plugins and save them within current sessions
633 session->plugins = RegisterJsonPlugins(plugins);
634 session->config->pluginCount = num;