Add warning detection and improve
[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 // Sample Generic Ping Debug API
34 PUBLIC json_object* getPingTest(AFB_request *request) {
35     static int pingcount = 0;
36     json_object *response;
37     char query  [256];
38     char session[256];
39     int len;
40     
41     // request all query key/value
42     len = getQueryAll (request, query, sizeof(query));
43     if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
44     
45     // check if we have some post data
46     if (request->post == NULL)  request->post->data="NoData"; 
47           
48     // return response to caller
49     response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d uuid=%s query={%s} session={0x%x} PostData: [%s] "
50                , pingcount++, request->uuid, query, session, request->post->data);
51     return (response);
52 }
53
54
55 // Helper to retrieve argument from  connection
56 PUBLIC const char* getQueryValue(const AFB_request * request, const char *name) {
57     const char *value;
58
59     value = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, name);
60     return (value);
61 }
62
63 STATIC int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) {
64     queryHandleT *query = (queryHandleT*)handle;
65         
66     query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value);
67     return MHD_YES; /* continue to iterate */
68 }
69
70 // Helper to retrieve argument from  connection
71 PUBLIC int getQueryAll(AFB_request * request, char *buffer, size_t len) {
72     queryHandleT query;
73     buffer[0] = '\0'; // start with an empty string
74     query.msg= buffer;
75     query.len= len;
76     query.idx= 0;
77
78     MHD_get_connection_values (request->connection, MHD_GET_ARGUMENT_KIND, getQueryCB, &query);
79     return (len);
80 }
81
82 // Helper to retrieve POST handle
83 PUBLIC AFB_PostHandle* getPostHandle (AFB_request *request) {
84     if (request->post == NULL) return (NULL);
85     return ((AFB_PostHandle*) request->post->data);
86 }
87
88 // Helper to retrieve POST file context
89 PUBLIC AFB_PostCtx* getPostContext (AFB_request *request) {
90     AFB_PostHandle* postHandle;
91     if (request->post == NULL) return (NULL);
92     
93     postHandle = (AFB_PostHandle*) request->post->data;
94     if (postHandle == NULL) return NULL;
95        
96     return ((AFB_PostCtx*) postHandle->ctx);
97 }
98
99 PUBLIC char* getPostPath (AFB_request *request) {
100     AFB_PostHandle *postHandle = getPostHandle(request);
101     AFB_PostCtx *postFileCtx;
102     
103     if (postHandle == NULL) return NULL;
104     
105     postFileCtx = (AFB_PostCtx*) postHandle->ctx;
106     if (postFileCtx == NULL) return NULL;
107   
108     return (postFileCtx->path);
109 }
110
111 PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
112
113     AFB_PostHandle *postHandle = getPostHandle(request);
114     AFB_PostCtx *postFileCtx;
115     char filepath[512];
116     ssize_t len;
117             
118     // This is called after PostForm and then after DonePostForm
119     if (item == NULL) {
120         json_object* jresp;
121         postFileCtx = (AFB_PostCtx*) postHandle->ctx;
122         
123         // No Post Application Context [something really bad happen]
124         if (postFileCtx == NULL) {
125             request->errcode = MHD_HTTP_EXPECTATION_FAILED;
126             return(jsonNewMessage(AFB_FAIL,"Error: PostForm no PostContext to free\n"));          
127         }
128         
129         // We have a context but last Xform iteration fail or application set a message
130         if (request->jresp != NULL) {
131             jresp = request->jresp;  // retrieve previous error from postCtx
132         } else jresp = jsonNewMessage(AFB_SUCCESS,"getPostFile Post Request done");
133         
134         // Error or not let's free all resources
135         close(postFileCtx->fd);
136         free (postFileCtx->path);
137         free (postFileCtx);
138         return (jresp);  
139     }
140 #if defined(PLEASE_FIX_ME_THE_ERROR_IS_postFileCtx_NOT_INITIALIZED)
141     // Make sure it's a valid PostForm request
142     if (!request->post && request->post->type != AFB_POST_FORM) {
143         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n");
144         goto ExitOnError;
145     } 
146     
147     // Check this is a file element
148     if (item->filename == NULL) {
149         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"No Filename attached to key=%s\n", item->key);
150         goto ExitOnError;
151     }
152     
153     // Check we got something in buffer
154     if (item->len <= 0) {       
155         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key);
156         goto ExitOnError;
157     }
158 #endif
159     // Extract Application Context from posthandle [NULL == 1st iteration]    
160     postFileCtx = (AFB_PostCtx*) postHandle->ctx;
161
162     // This is the 1st Item iteration let's open output file and allocate necessary resources
163     if (postFileCtx == NULL)  {
164         DIR* destDir;
165         
166         // Create an application specific context
167         postFileCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
168         
169         // attach application to postHandle
170         postHandle->ctx = (void*) postFileCtx;   // May place anything here until post->completeCB handle resources liberation  
171         
172         // Build destination directory full path
173         if (destination[0] != '/') {
174            strncpy (filepath, request->config->sessiondir, sizeof(filepath)); 
175            strncat (filepath, "/", sizeof(filepath));
176            strncat (filepath, destination, sizeof(filepath)); 
177         } else strncpy (filepath, destination, sizeof(filepath));
178
179         
180         // make sure destination directory exist
181         destDir = opendir (filepath);
182         if (destDir == NULL) {
183           if (mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP) < 0) {
184             postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
185             goto ExitOnError;
186           }
187         } else closedir (destDir);
188         
189         strncat (filepath, "/", sizeof(filepath));
190         strncat (filepath, item->filename, sizeof(filepath));  
191
192         postFileCtx->path = strdup (filepath);       
193         if (verbose) fprintf(stderr, "getPostFile path=%s\n", filepath);
194        
195         if((postFileCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) <= 0) {
196             postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination File=[%s] error=%s\n", filepath, strerror(errno));
197             goto ExitOnError;
198         } 
199     } else {     
200         // reuse existing application context
201         postFileCtx = (AFB_PostCtx*) postHandle->ctx;  
202     } 
203
204     // Check we successfully wrote full buffer
205     len = write (postFileCtx->fd, item->data, item->len);
206     if ((ssize_t)item->len != len) {
207         postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno));
208         goto ExitOnError;
209     }
210   
211     // every intermediary iteration should return Success & NULL
212     request->errcode = MHD_HTTP_OK;
213     return NULL;
214     
215 ExitOnError:    
216     request->errcode = MHD_HTTP_EXPECTATION_FAILED;
217     return NULL;
218 }