moves more helpers from config to helper-api
[src/app-framework-binder.git] / src / helper-api.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 #include "../include/local-def.h"
21 #include <dirent.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24
25
26 // handle to hold queryAll values
27 typedef struct {
28      char    *msg;
29      int     idx;
30      size_t  len;
31 } queryHandleT;
32
33 // Error code are requested through function to manage json usage count
34 typedef struct {
35   int   level;
36   const char* label;
37   json_object *json;
38 } AFB_errorT;
39
40 static AFB_errorT   AFBerr [AFB_SUCCESS+1];
41 static json_object *jTypeStatic;
42
43 PUBLIC int verbose;
44
45 static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success"};
46
47 /* ------------------------------------------------------------------------------
48  * Get localtime and return in a string
49  * ------------------------------------------------------------------------------ */
50
51 PUBLIC char * configTime (void) {
52   static char reqTime [26];
53   time_t tt;
54   struct tm *rt;
55
56   /* Get actual Date and Time */
57   time (&tt);
58   rt = localtime (&tt);
59
60   strftime (reqTime, sizeof (reqTime), "(%d-%b %H:%M)",rt);
61
62   // return pointer on static data
63   return (reqTime);
64 }
65
66
67 // Sample Generic Ping Debug API
68 json_object* getPingTest(AFB_request *request) {
69     static int pingcount = 0;
70     json_object *response;
71     char query  [256];
72     char session[256];
73     int len;
74     
75     // request all query key/value
76     len = getQueryAll (request, query, sizeof(query));
77     if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
78     
79     // check if we have some post data
80     if (request->post == NULL)  request->post->data="NoData"; 
81           
82     // return response to caller
83     response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d uuid=%s query={%s} session={0x%x} PostData: [%s] "
84                , pingcount++, request->uuid, query, session, request->post->data);
85     return (response);
86 }
87
88
89 // Helper to retrieve argument from  connection
90 const char* getQueryValue(const AFB_request * request, const char *name) {
91     const char *value;
92
93     value = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, name);
94     return (value);
95 }
96
97 static int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) {
98     queryHandleT *query = (queryHandleT*)handle;
99         
100     query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value);
101     return MHD_YES; /* continue to iterate */
102 }
103
104 // Helper to retrieve argument from  connection
105 int getQueryAll(AFB_request * request, char *buffer, size_t len) {
106     queryHandleT query;
107     buffer[0] = '\0'; // start with an empty string
108     query.msg = buffer;
109     query.len = len;
110     query.idx = 0;
111
112     MHD_get_connection_values (request->connection, MHD_GET_ARGUMENT_KIND, getQueryCB, &query);
113     return (len);
114 }
115
116 // Helper to retrieve POST handle
117 AFB_PostHandle* getPostHandle (AFB_request *request) {
118     if (request->post == NULL) return (NULL);
119     return ((AFB_PostHandle*) request->post->data);
120 }
121
122 // Helper to retrieve POST file context
123 AFB_PostCtx* getPostContext (AFB_request *request) {
124     AFB_PostHandle* postHandle;
125     if (request->post == NULL) return (NULL);
126     
127     postHandle = (AFB_PostHandle*) request->post->data;
128     if (postHandle == NULL) return NULL;
129        
130     return ((AFB_PostCtx*) postHandle->ctx);
131 }
132
133 char* getPostPath (AFB_request *request) {
134     AFB_PostHandle *postHandle = getPostHandle(request);
135     AFB_PostCtx *postFileCtx;
136     
137     if (postHandle == NULL) return NULL;
138     
139     postFileCtx = (AFB_PostCtx*) postHandle->ctx;
140     if (postFileCtx == NULL) return NULL;
141   
142     return (postFileCtx->path);
143 }
144
145 json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
146
147     AFB_PostHandle *postHandle = getPostHandle(request);
148     AFB_PostCtx *postFileCtx;
149     char filepath[512];
150     ssize_t len;
151             
152     // This is called after PostForm and then after DonePostForm
153     if (item == NULL) {
154         json_object* jresp;
155         postFileCtx = (AFB_PostCtx*) postHandle->ctx;
156         
157         // No Post Application Context [something really bad happen]
158         if (postFileCtx == NULL) {
159             request->errcode = MHD_HTTP_EXPECTATION_FAILED;
160             return(jsonNewMessage(AFB_FAIL,"Error: PostForm no PostContext to free\n"));          
161         }
162         
163         // We have a context but last Xform iteration fail or application set a message
164         if (request->jresp != NULL) {
165             jresp = request->jresp;  // retrieve previous error from postCtx
166         } else jresp = jsonNewMessage(AFB_SUCCESS,"getPostFile Post Request done");
167         
168         // Error or not let's free all resources
169         close(postFileCtx->fd);
170         free (postFileCtx->path);
171         free (postFileCtx);
172         return (jresp);  
173     }
174 #if defined(PLEASE_FIX_ME_THE_ERROR_IS_postFileCtx_NOT_INITIALIZED)
175     // Make sure it's a valid PostForm request
176     if (!request->post && request->post->type != AFB_POST_FORM) {
177         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n");
178         goto ExitOnError;
179     } 
180     
181     // Check this is a file element
182     if (item->filename == NULL) {
183         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"No Filename attached to key=%s\n", item->key);
184         goto ExitOnError;
185     }
186     
187     // Check we got something in buffer
188     if (item->len <= 0) {       
189         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key);
190         goto ExitOnError;
191     }
192 #endif
193     // Extract Application Context from posthandle [NULL == 1st iteration]    
194     postFileCtx = (AFB_PostCtx*) postHandle->ctx;
195
196     // This is the 1st Item iteration let's open output file and allocate necessary resources
197     if (postFileCtx == NULL)  {
198         DIR* destDir;
199         
200         // Create an application specific context
201         postFileCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
202         
203         // attach application to postHandle
204         postHandle->ctx = (void*) postFileCtx;   // May place anything here until post->completeCB handle resources liberation  
205         
206         // Build destination directory full path
207         if (destination[0] != '/') {
208            strncpy (filepath, request->config->sessiondir, sizeof(filepath)); 
209            strncat (filepath, "/", sizeof(filepath));
210            strncat (filepath, destination, sizeof(filepath)); 
211         } else strncpy (filepath, destination, sizeof(filepath));
212
213         
214         // make sure destination directory exist
215         destDir = opendir (filepath);
216         if (destDir == NULL) {
217           if (mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP) < 0) {
218             postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
219             goto ExitOnError;
220           }
221         } else closedir (destDir);
222         
223         strncat (filepath, "/", sizeof(filepath));
224         strncat (filepath, item->filename, sizeof(filepath));  
225
226         postFileCtx->path = strdup (filepath);       
227         if (verbose) fprintf(stderr, "getPostFile path=%s\n", filepath);
228        
229         if((postFileCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) <= 0) {
230             postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination File=[%s] error=%s\n", filepath, strerror(errno));
231             goto ExitOnError;
232         } 
233     } else {     
234         // reuse existing application context
235         postFileCtx = (AFB_PostCtx*) postHandle->ctx;  
236     } 
237
238     // Check we successfully wrote full buffer
239     len = write (postFileCtx->fd, item->data, item->len);
240     if ((ssize_t)item->len != len) {
241         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno));
242         goto ExitOnError;
243     }
244   
245     // every intermediary iteration should return Success & NULL
246     request->errcode = MHD_HTTP_OK;
247     return NULL;
248     
249 ExitOnError:    
250     request->errcode = MHD_HTTP_EXPECTATION_FAILED;
251     return NULL;
252 }
253
254
255
256 static void jsoninit()
257 {
258   int idx, verbosesav;
259
260   if (jTypeStatic)
261         return;
262
263   // initialise JSON constant messages and increase reference count to make them permanent
264   verbosesav = verbose;
265   verbose = 0;  // run initialisation in silent mode
266   jTypeStatic = json_object_new_string ("AFB_message");
267   for (idx = 0; idx <= AFB_SUCCESS; idx++) {
268      AFBerr[idx].level = idx;
269      AFBerr[idx].label = ERROR_LABEL [idx];
270      AFBerr[idx].json  = jsonNewMessage (idx, NULL);
271   }
272   verbose = verbosesav;
273 }
274
275
276
277 // get JSON object from error level and increase its reference count
278 struct json_object *jsonNewStatus (AFB_error level)
279 {
280   jsoninit();
281   json_object *target =  AFBerr[level].json;
282   json_object_get (target);
283
284   return (target);
285 }
286
287 // get AFB object type with adequate usage count
288 struct json_object *jsonNewjtype (void)
289 {
290   jsoninit();
291   json_object_get (jTypeStatic); // increase reference count
292   return (jTypeStatic);
293 }
294
295 // build an ERROR message and return it as a valid json object
296 struct json_object *jsonNewMessage (AFB_error level, char* format, ...) {
297    static int count = 0;
298    json_object * AFBResponse;
299    va_list args;
300    char message [512];
301
302   jsoninit();
303
304    // format message
305    if (format != NULL) {
306        va_start(args, format);
307        vsnprintf (message, sizeof (message), format, args);
308        va_end(args);
309    }
310
311    AFBResponse = json_object_new_object();
312    json_object_object_add (AFBResponse, "jtype", jsonNewjtype ());
313    json_object_object_add (AFBResponse, "status" , json_object_new_string (ERROR_LABEL[level]));
314    if (format != NULL) {
315         json_object_object_add (AFBResponse, "info"   , json_object_new_string (message));
316    }
317    if (verbose) {
318         fprintf (stderr, "AFB:%-6s [%3d]: ", AFBerr [level].label, count++);
319         if (format != NULL) {
320             fprintf (stderr, "%s", message);
321         } else {
322             fprintf (stderr, "No Message");
323         }
324         fprintf (stderr, "\n");
325    }
326
327    return (AFBResponse);
328 }
329
330 // Dump a message on stderr
331 void jsonDumpObject (struct json_object * jObject) {
332
333    if (verbose) {
334         fprintf (stderr, "AFB:dump [%s]\n", json_object_to_json_string(jObject));
335    }
336 }
337