splitting rest-api in two parts
[src/app-framework-binder.git] / src / afb-api.c
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  * 
19  * Contain all generic part to handle REST/API
20  * 
21  *  https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c']
22  */
23
24 #define _GNU_SOURCE
25
26 #include "../include/local-def.h"
27
28 #include <dirent.h>
29 #include <dlfcn.h>
30 #include <setjmp.h>
31 #include <signal.h>
32
33 #define AFB_MSG_JTYPE "AJB_reply"
34
35 #define JSON_CONTENT  "application/json"
36 #define FORM_CONTENT  "multipart/form-data"     /* TODO: replace with MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA */
37
38 static json_object *afbJsonType;
39
40 // Because of POST call multiple time requestApi we need to free POST handle here
41 // Note this method is called from http-svc just before closing session
42 PUBLIC void endPostRequest(AFB_PostHandle * postHandle)
43 {
44
45         if (postHandle->type == AFB_POST_JSON) {
46                 // if (verbose) fprintf(stderr, "End PostJson Request UID=%d\n", postHandle->uid);
47         }
48
49         if (postHandle->type == AFB_POST_FORM) {
50                 if (verbose)
51                         fprintf(stderr, "End PostForm Request UID=%d\n", postHandle->uid);
52         }
53         if (postHandle->privatebuf)
54                 free(postHandle->privatebuf);
55         free(postHandle);
56 }
57
58 // Check of apiurl is declare in this plugin and call it
59 STATIC AFB_error callPluginApi(AFB_request * request, int plugidx, void *context)
60 {
61         json_object *jresp, *jcall, *jreqt;
62         int idx, status, sig;
63         AFB_clientCtx *clientCtx = NULL;
64         AFB_plugin *plugin = request->plugins[plugidx];
65         int signals[] = { SIGALRM, SIGSEGV, SIGFPE, 0 };
66
67     /*---------------------------------------------------------------
68     | Signal handler defined inside CallPluginApi to access Request
69     +---------------------------------------------------------------- */
70         void pluginError(int signum) {
71                 sigset_t sigset;
72
73                 // unlock signal to allow a new signal to come
74                 sigemptyset(&sigset);
75                 sigaddset(&sigset, signum);
76                 sigprocmask(SIG_UNBLOCK, &sigset, 0);
77
78                 fprintf(stderr, "Oops: Plugin Api Timeout timeout\n");
79                 longjmp(request->checkPluginCall, signum);
80         }
81
82         // If a plugin hold this urlpath call its callback
83         for (idx = 0; plugin->apis[idx].callback != NULL; idx++) {
84                 if (!strcmp(plugin->apis[idx].name, request->api)) {
85
86                         // Request was found and at least partially executed
87                         jreqt = json_object_new_object();
88                         json_object_get(afbJsonType);   // increate jsontype reference count
89                         json_object_object_add(jreqt, "jtype", afbJsonType);
90
91                         // prepare an object to store calling values
92                         jcall = json_object_new_object();
93                         json_object_object_add(jcall, "prefix", json_object_new_string(plugin->prefix));
94                         json_object_object_add(jcall, "api", json_object_new_string(plugin->apis[idx].name));
95
96                         // save context before calling the API
97                         status = setjmp(request->checkPluginCall);
98                         if (status != 0) {
99
100                                 // Plugin aborted somewhere during its execution
101                                 json_object_object_add(jcall, "status", json_object_new_string("abort"));
102                                 json_object_object_add(jcall, "info", json_object_new_string("Plugin broke during execution"));
103                                 json_object_object_add(jreqt, "request", jcall);
104
105                         } else {
106
107                                 // If timeout protection==0 we are in debug and we do not apply signal protection
108                                 if (request->config->apiTimeout > 0) {
109                                         for (sig = 0; signals[sig] != 0; sig++) {
110                                                 if (signal(signals[sig], pluginError) == SIG_ERR) {
111                                                         request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY;
112                                                         json_object_object_add(jcall, "status", json_object_new_string("fail"));
113                                                         json_object_object_add(jcall, "info", json_object_new_string("Setting Timeout Handler Failed"));
114                                                         json_object_object_add(jreqt, "request", jcall);
115                                                         goto ExitOnDone;
116                                                 }
117                                         }
118                                         // Trigger a timer to protect from unacceptable long time execution
119                                         alarm((unsigned)request->config->apiTimeout);
120                                 }
121                                 // Out of SessionNone every call get a client context session
122                                 if (AFB_SESSION_NONE != plugin->apis[idx].session) {
123
124                                         // add client context to request
125                                         clientCtx = ctxClientGet(request, plugidx);
126                                         if (clientCtx == NULL) {
127                                                 request->errcode = MHD_HTTP_INSUFFICIENT_STORAGE;
128                                                 json_object_object_add(jcall, "status", json_object_new_string("fail"));
129                                                 json_object_object_add(jcall, "info", json_object_new_string("Client Session Context Full !!!"));
130                                                 json_object_object_add(jreqt, "request", jcall);
131                                                 goto ExitOnDone;
132                                         };
133
134                                         if (verbose)
135                                                 fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[%p] Uuid=[%s] Token=[%s]\n", request->prefix, request->api, plugin->apis[idx].session, clientCtx, clientCtx->uuid, clientCtx->token);
136
137                                         switch (plugin->apis[idx].session) {
138
139                                         case AFB_SESSION_CREATE:
140                                                 if (clientCtx->token[0] != '\0' && request->config->token[0] != '\0') {
141                                                         request->errcode = MHD_HTTP_UNAUTHORIZED;
142                                                         json_object_object_add(jcall, "status", json_object_new_string("exist"));
143                                                         json_object_object_add(jcall, "info", json_object_new_string("AFB_SESSION_CREATE Session already exist"));
144                                                         json_object_object_add(jreqt, "request", jcall);
145                                                         goto ExitOnDone;
146                                                 }
147
148                                                 if (AFB_SUCCESS != ctxTokenCreate(clientCtx, request)) {
149                                                         request->errcode = MHD_HTTP_UNAUTHORIZED;
150                                                         json_object_object_add(jcall, "status", json_object_new_string("fail"));
151                                                         json_object_object_add(jcall, "info", json_object_new_string("AFB_SESSION_CREATE Invalid Initial Token"));
152                                                         json_object_object_add(jreqt, "request", jcall);
153                                                         goto ExitOnDone;
154                                                 } else {
155                                                         json_object_object_add(jcall, "uuid", json_object_new_string(clientCtx->uuid));
156                                                         json_object_object_add(jcall, "token", json_object_new_string(clientCtx->token));
157                                                         json_object_object_add(jcall, "timeout", json_object_new_int(request->config->cntxTimeout));
158                                                 }
159                                                 break;
160
161                                         case AFB_SESSION_RENEW:
162                                                 if (AFB_SUCCESS != ctxTokenRefresh(clientCtx, request)) {
163                                                         request->errcode = MHD_HTTP_UNAUTHORIZED;
164                                                         json_object_object_add(jcall, "status", json_object_new_string("fail"));
165                                                         json_object_object_add(jcall, "info", json_object_new_string("AFB_SESSION_REFRESH Broken Exchange Token Chain"));
166                                                         json_object_object_add(jreqt, "request", jcall);
167                                                         goto ExitOnDone;
168                                                 } else {
169                                                         json_object_object_add(jcall, "uuid", json_object_new_string(clientCtx->uuid));
170                                                         json_object_object_add(jcall, "token", json_object_new_string(clientCtx->token));
171                                                         json_object_object_add(jcall, "timeout", json_object_new_int(request->config->cntxTimeout));
172                                                 }
173                                                 break;
174
175                                         case AFB_SESSION_CLOSE:
176                                                 if (AFB_SUCCESS != ctxTokenCheck(clientCtx, request)) {
177                                                         request->errcode = MHD_HTTP_UNAUTHORIZED;
178                                                         json_object_object_add(jcall, "status", json_object_new_string("empty"));
179                                                         json_object_object_add(jcall, "info", json_object_new_string("AFB_SESSION_CLOSE Not a Valid Access Token"));
180                                                         json_object_object_add(jreqt, "request", jcall);
181                                                         goto ExitOnDone;
182                                                 } else {
183                                                         json_object_object_add(jcall, "uuid", json_object_new_string(clientCtx->uuid));
184                                                 }
185                                                 break;
186
187                                         case AFB_SESSION_CHECK:
188                                         default:
189                                                 // default action is check
190                                                 if (AFB_SUCCESS != ctxTokenCheck(clientCtx, request)) {
191                                                         request->errcode = MHD_HTTP_UNAUTHORIZED;
192                                                         json_object_object_add(jcall, "status", json_object_new_string("fail"));
193                                                         json_object_object_add(jcall, "info", json_object_new_string("AFB_SESSION_CHECK Invalid Active Token"));
194                                                         json_object_object_add(jreqt, "request", jcall);
195                                                         goto ExitOnDone;
196                                                 }
197                                                 break;
198                                         }
199                                 }
200                                 // Effectively CALL PLUGIN API with a subset of the context
201                                 jresp = plugin->apis[idx].callback(request, context);
202
203                                 // Store context in case it was updated by plugins
204                                 if (request->context != NULL)
205                                         clientCtx->contexts[plugidx] = request->context;
206
207                                 // handle intermediary Post Iterates out of band
208                                 if ((jresp == NULL)
209                                     && (request->errcode == MHD_HTTP_OK))
210                                         return (AFB_SUCCESS);
211
212                                 // Session close is done after the API call so API can still use session in closing API
213                                 if (AFB_SESSION_CLOSE == plugin->apis[idx].session)
214                                         ctxTokenReset(clientCtx, request);
215
216                                 // API should return NULL of a valid Json Object
217                                 if (jresp == NULL) {
218                                         json_object_object_add(jcall, "status", json_object_new_string("null"));
219                                         json_object_object_add(jreqt, "request", jcall);
220                                         request->errcode = MHD_HTTP_NO_RESPONSE;
221
222                                 } else {
223                                         json_object_object_add(jcall, "status", json_object_new_string("processed"));
224                                         json_object_object_add(jreqt, "request", jcall);
225                                         json_object_object_add(jreqt, "response", jresp);
226                                 }
227                                 // cancel timeout and plugin signal handle before next call
228                                 if (request->config->apiTimeout > 0) {
229                                         alarm(0);
230                                         for (sig = 0; signals[sig] != 0; sig++) {
231                                                 signal(signals[sig], SIG_DFL);
232                                         }
233                                 }
234                         }
235                         goto ExitOnDone;
236                 }
237         }
238         return (AFB_FAIL);
239
240  ExitOnDone:
241         request->jresp = jreqt;
242         return (AFB_DONE);
243 }
244
245 STATIC AFB_error findAndCallApi(AFB_request * request, void *context)
246 {
247         int idx;
248         AFB_error status;
249
250         if (!request->api || !request->prefix)
251                 return (AFB_FAIL);
252
253         // Search for a plugin with this urlpath
254         for (idx = 0; request->plugins[idx] != NULL; idx++) {
255                 if (!strcmp(request->plugins[idx]->prefix, request->prefix)) {
256                         status = callPluginApi(request, idx, context);
257                         break;
258                 }
259         }
260         // No plugin was found
261         if (request->plugins[idx] == NULL) {
262                 request->jresp = jsonNewMessage(AFB_FATAL, "No Plugin=[%s] Url=%s", request->prefix, request->url);
263                 goto ExitOnError;
264         }
265         // plugin callback did not return a valid Json Object
266         if (status == AFB_FAIL) {
267                 request->jresp = jsonNewMessage(AFB_FATAL, "No API=[%s] for Plugin=[%s] url=[%s]", request->api, request->prefix, request->url);
268                 goto ExitOnError;
269         }
270         // Everything look OK
271         return (status);
272
273  ExitOnError:
274         request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY;
275         return (AFB_FAIL);
276 }
277
278 // This CB is call for every item with a form post it reformat iterator values
279 // and callback Plugin API for each Item within PostForm.
280 STATIC int doPostIterate(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *mimetype, const char *encoding, const char *data, uint64_t offset, size_t size)
281 {
282
283         AFB_error status;
284         AFB_PostItem item;
285
286         // retrieve API request from Post iterator handle  
287         AFB_PostHandle *postHandle = (AFB_PostHandle *) cls;
288         AFB_request *request = (AFB_request *) postHandle->privatebuf;
289         AFB_PostRequest postRequest;
290
291         if (verbose)
292                 fprintf(stderr, "postHandle key=%s filename=%s len=%zu mime=%s\n", key, filename, size, mimetype);
293
294         // Create and Item value for Plugin API
295         item.kind = kind;
296         item.key = key;
297         item.filename = filename;
298         item.mimetype = mimetype;
299         item.encoding = encoding;
300         item.len = size;
301         item.data = data;
302         item.offset = offset;
303
304         // Reformat Request to make it somehow similar to GET/PostJson case
305         postRequest.data = (char *)postHandle;
306         postRequest.len = size;
307         postRequest.type = AFB_POST_FORM;;
308         request->post = &postRequest;
309
310         // effectively call plugin API                 
311         status = findAndCallApi(request, &item);
312         // when returning no processing of postform stop
313         if (status != AFB_SUCCESS)
314                 return MHD_NO;
315
316         // let's allow iterator to move to next item
317         return MHD_YES;
318 }
319
320 STATIC void freeRequest(AFB_request * request)
321 {
322
323         free(request->prefix);
324         free(request->api);
325         free(request);
326 }
327
328 STATIC AFB_request *createRequest(struct MHD_Connection *connection, AFB_session * session, const char *url)
329 {
330
331         AFB_request *request;
332
333         // Start with a clean request
334         request = calloc(1, sizeof(AFB_request));
335         char *urlcpy1, *urlcpy2;
336         char *baseapi, *baseurl;
337
338         // Extract plugin urlpath from request and make two copy because strsep overload copy
339         urlcpy1 = urlcpy2 = strdup(url);
340         baseurl = strsep(&urlcpy2, "/");
341         if (baseurl == NULL) {
342                 request->jresp = jsonNewMessage(AFB_FATAL, "Invalid API call url=[%s]", url);
343                 request->errcode = MHD_HTTP_BAD_REQUEST;
344                 goto Done;
345         }
346         // let's compute URL and call API
347         baseapi = strsep(&urlcpy2, "/");
348         if (baseapi == NULL) {
349                 request->jresp = jsonNewMessage(AFB_FATAL, "Invalid API call plugin=[%s] url=[%s]", baseurl, url);
350                 request->errcode = MHD_HTTP_BAD_REQUEST;
351                 goto Done;
352         }
353         // build request structure
354         request->connection = connection;
355         request->config = session->config;
356         request->url = url;
357         request->prefix = strdup(baseurl);
358         request->api = strdup(baseapi);
359         request->plugins = session->plugins;
360         // note request->handle is fed with request->context in ctxClientGet
361
362  Done:
363         free(urlcpy1);
364         return (request);
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381 static int doRestApiPost(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)
382 {
383
384         static int postcount = 0;       // static counter to debug POST protocol
385         json_object *errMessage;
386         AFB_error status;
387         struct MHD_Response *webResponse;
388         const char *serialized;
389         AFB_request *request = NULL;
390         AFB_PostHandle *postHandle;
391         AFB_PostRequest postRequest;
392         int ret;
393
394         // fprintf (stderr, "doRestAPI method=%s posthandle=%p\n", method, con_cls);
395
396         // if post data may come in multiple calls
397                 const char *encoding, *param;
398                 int contentlen = -1;
399                 postHandle = *con_cls;
400
401                 // This is the initial post event let's create form post structure POST data come in multiple events
402                 if (postHandle == NULL) {
403
404                         // allocate application POST processor handle to zero
405                         postHandle = calloc(1, sizeof(AFB_PostHandle));
406                         postHandle->uid = postcount++;  // build a UID for DEBUG
407                         *con_cls = postHandle;  // update context with posthandle
408
409                         // Let make sure we have the right encoding and a valid length
410                         encoding = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE);
411
412                         // We are facing an empty post let's process it as a get
413                         if (encoding == NULL) {
414                                 postHandle->type = AFB_POST_EMPTY;
415                                 return MHD_YES;
416                         }
417                         // Form post is handle through a PostProcessor and call API once per form key
418                         if (strcasestr(encoding, FORM_CONTENT) != NULL) {
419                                 if (verbose)
420                                         fprintf(stderr, "Create doPostIterate[uid=%d posthandle=%p]\n", postHandle->uid, postHandle);
421
422                                 request = createRequest(connection, session, url);
423                                 if (request->jresp != NULL)
424                                         goto ProcessApiCall;
425                                 postHandle->type = AFB_POST_FORM;
426                                 postHandle->privatebuf = (void *)request;
427                                 postHandle->pp = MHD_create_post_processor(connection, MAX_POST_SIZE, &doPostIterate, postHandle);
428
429                                 if (NULL == postHandle->pp) {
430                                         fprintf(stderr, "OOPS: Internal error fail to allocate MHD_create_post_processor\n");
431                                         free(postHandle);
432                                         return MHD_NO;
433                                 }
434                                 return MHD_YES;
435                         }
436                         // POST json is store into a buffer and present in one piece to API
437                         if (strcasestr(encoding, JSON_CONTENT) != NULL) {
438
439                                 param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
440                                 if (param)
441                                         sscanf(param, "%i", &contentlen);
442
443                                 // Because PostJson are build in RAM size is constrained
444                                 if (contentlen > MAX_POST_SIZE) {
445                                         errMessage = jsonNewMessage(AFB_FATAL, "Post Date to big %d > %d", contentlen, MAX_POST_SIZE);
446                                         goto ExitOnError;
447                                 }
448                                 // Size is OK, let's allocate a buffer to hold post data
449                                 postHandle->type = AFB_POST_JSON;
450                                 postHandle->privatebuf = malloc((unsigned)contentlen + 1);      // allocate memory for full POST data + 1 for '\0' enf of string
451
452                                 // if (verbose) fprintf(stderr, "Create PostJson[uid=%d] Size=%d\n", postHandle->uid, contentlen);
453                                 return MHD_YES;
454
455                         } else {
456                                 // We only support Json and Form Post format
457                                 errMessage = jsonNewMessage(AFB_FATAL, "Post Date wrong type encoding=%s != %s", encoding, JSON_CONTENT);
458                                 goto ExitOnError;
459                         }
460                 }
461                 // This time we receive partial/all Post data. Note that even if we get all POST data. We should nevertheless
462                 // return MHD_YES and not process the request directly. Otherwise Libmicrohttpd is unhappy and fails with
463                 // 'Internal application error, closing connection'.            
464                 if (*upload_data_size) {
465
466                         if (postHandle->type == AFB_POST_FORM) {
467                                 // if (verbose) fprintf(stderr, "Processing PostForm[uid=%d]\n", postHandle->uid);
468                                 MHD_post_process(postHandle->pp, upload_data, *upload_data_size);
469                         }
470                         // Process JsonPost request when buffer is completed let's call API    
471                         if (postHandle->type == AFB_POST_JSON) {
472                                 // if (verbose) fprintf(stderr, "Updating PostJson[uid=%d]\n", postHandle->uid);
473                                 memcpy(&postHandle->privatebuf[postHandle->len], upload_data, *upload_data_size);
474                                 postHandle->len = postHandle->len + *upload_data_size;
475                         }
476
477                         *upload_data_size = 0;
478                         return MHD_YES;
479
480                 } else {        // we have finish with Post reception let's finish the work
481
482                         // Create a request structure to finalise the request
483                         request = createRequest(connection, session, url);
484                         if (request->jresp != NULL) {
485                                 errMessage = request->jresp;
486                                 goto ExitOnError;
487                         }
488                         postRequest.type = postHandle->type;
489
490                         // Postform add application context handle to request
491                         if (postHandle->type == AFB_POST_FORM) {
492                                 postRequest.data = (char *)postHandle;
493                                 request->post = &postRequest;
494                         }
495
496                         if (postHandle->type == AFB_POST_JSON) {
497                                 // if (verbose) fprintf(stderr, "Processing PostJson[uid=%d]\n", postHandle->uid);
498
499                                 param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
500                                 if (param)
501                                         sscanf(param, "%i", &contentlen);
502
503                                 // At this level we're may verify that we got everything and process DATA
504                                 if (postHandle->len != contentlen) {
505                                         errMessage = jsonNewMessage(AFB_FATAL, "Post Data Incomplete UID=%d Len %d != %d", postHandle->uid, contentlen, postHandle->len);
506                                         goto ExitOnError;
507                                 }
508                                 // Before processing data, make sure buffer string is properly ended
509                                 postHandle->privatebuf[postHandle->len] = '\0';
510                                 postRequest.data = postHandle->privatebuf;
511                                 request->post = &postRequest;
512
513                                 // if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", postHandle->uid, request->post->data);
514                         }
515                 }
516
517  ProcessApiCall:
518         // Request is ready let's call API without any extra handle
519         status = findAndCallApi(request, NULL);
520
521         serialized = json_object_to_json_string(request->jresp);
522         webResponse = MHD_create_response_from_buffer(strlen(serialized), (void *)serialized, MHD_RESPMEM_MUST_COPY);
523
524         // client did not pass token on URI let's use cookies 
525         if ((!request->restfull) && (request->context != NULL)) {
526                 char cookie[256];
527                 snprintf(cookie, sizeof(cookie), "%s-%d=%s; Path=%s; Max-Age=%d; HttpOnly", COOKIE_NAME, request->config->httpdPort, request->uuid, request->config->rootapi, request->config->cntxTimeout);
528                 MHD_add_response_header(webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie);
529         }
530         // if requested add an error status
531         if (request->errcode != 0)
532                 ret = MHD_queue_response(connection, request->errcode, webResponse);
533         else
534                 MHD_queue_response(connection, MHD_HTTP_OK, webResponse);
535
536         MHD_destroy_response(webResponse);
537         json_object_put(request->jresp);        // decrease reference rqtcount to free the json object
538         freeRequest(request);
539         return MHD_YES;
540
541  ExitOnError:
542         freeRequest(request);
543         serialized = json_object_to_json_string(errMessage);
544         webResponse = MHD_create_response_from_buffer(strlen(serialized), (void *)serialized, MHD_RESPMEM_MUST_COPY);
545         MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, webResponse);
546         MHD_destroy_response(webResponse);
547         json_object_put(errMessage);    // decrease reference rqtcount to free the json object
548         return MHD_YES;
549 }
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570 static int doRestApiGet(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)
571 {
572
573         static int postcount = 0;       // static counter to debug POST protocol
574         json_object *errMessage;
575         AFB_error status;
576         struct MHD_Response *webResponse;
577         const char *serialized;
578         AFB_request *request = NULL;
579         AFB_PostHandle *postHandle;
580         AFB_PostRequest postRequest;
581         int ret;
582
583         // fprintf (stderr, "doRestAPI method=%s posthandle=%p\n", method, con_cls);
584
585         // if post data may come in multiple calls
586         // this is a get we only need a request
587         request = createRequest(connection, session, url);
588
589  ProcessApiCall:
590         // Request is ready let's call API without any extra handle
591         status = findAndCallApi(request, NULL);
592
593         serialized = json_object_to_json_string(request->jresp);
594         webResponse = MHD_create_response_from_buffer(strlen(serialized), (void *)serialized, MHD_RESPMEM_MUST_COPY);
595
596         // client did not pass token on URI let's use cookies 
597         if ((!request->restfull) && (request->context != NULL)) {
598                 char cookie[256];
599                 snprintf(cookie, sizeof(cookie), "%s-%d=%s; Path=%s; Max-Age=%d; HttpOnly", COOKIE_NAME, request->config->httpdPort, request->uuid, request->config->rootapi, request->config->cntxTimeout);
600                 MHD_add_response_header(webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie);
601         }
602         // if requested add an error status
603         if (request->errcode != 0)
604                 ret = MHD_queue_response(connection, request->errcode, webResponse);
605         else
606                 MHD_queue_response(connection, MHD_HTTP_OK, webResponse);
607
608         MHD_destroy_response(webResponse);
609         json_object_put(request->jresp);        // decrease reference rqtcount to free the json object
610         freeRequest(request);
611         return MHD_YES;
612
613  ExitOnError:
614         freeRequest(request);
615         serialized = json_object_to_json_string(errMessage);
616         webResponse = MHD_create_response_from_buffer(strlen(serialized), (void *)serialized, MHD_RESPMEM_MUST_COPY);
617         MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, webResponse);
618         MHD_destroy_response(webResponse);
619         json_object_put(errMessage);    // decrease reference rqtcount to free the json object
620         return MHD_YES;
621 }
622
623 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)
624 {
625         int rc;
626
627         if (0 == strcmp(method, MHD_HTTP_METHOD_POST)) {
628                 rc = doRestApiPost(connection, session, url, method, upload_data, upload_data_size, con_cls);
629         } else {
630                 rc = doRestApiGet(connection, session, url, method, upload_data, upload_data_size, con_cls);
631         }
632         return rc;
633 }
634