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/>.
20 #include "../include/local-def.h"
23 #include <sys/types.h>
26 // handle to hold queryAll values
33 // Error code are requested through function to manage json usage count
40 static AFB_errorT AFBerr [AFB_SUCCESS+1];
41 static json_object *jTypeStatic;
45 static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success"};
47 /* ------------------------------------------------------------------------------
48 * Get localtime and return in a string
49 * ------------------------------------------------------------------------------ */
51 PUBLIC char * configTime (void) {
52 static char reqTime [26];
56 /* Get actual Date and Time */
60 strftime (reqTime, sizeof (reqTime), "(%d-%b %H:%M)",rt);
62 // return pointer on static data
67 // Sample Generic Ping Debug API
68 json_object* getPingTest(AFB_request *request) {
69 static int pingcount = 0;
70 json_object *response;
75 // request all query key/value
76 len = getQueryAll (request, query, sizeof(query));
77 if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
79 // check if we have some post data
80 if (request->post == NULL) request->post->data="NoData";
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);
89 // Helper to retrieve argument from connection
90 const char* getQueryValue(const AFB_request * request, const char *name) {
93 value = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, name);
97 static int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) {
98 queryHandleT *query = (queryHandleT*)handle;
100 query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value);
101 return MHD_YES; /* continue to iterate */
104 // Helper to retrieve argument from connection
105 int getQueryAll(AFB_request * request, char *buffer, size_t len) {
107 buffer[0] = '\0'; // start with an empty string
112 MHD_get_connection_values (request->connection, MHD_GET_ARGUMENT_KIND, getQueryCB, &query);
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);
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);
127 postHandle = (AFB_PostHandle*) request->post->data;
128 if (postHandle == NULL) return NULL;
130 return ((AFB_PostCtx*) postHandle->ctx);
133 char* getPostPath (AFB_request *request) {
134 AFB_PostHandle *postHandle = getPostHandle(request);
135 AFB_PostCtx *postFileCtx;
137 if (postHandle == NULL) return NULL;
139 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
140 if (postFileCtx == NULL) return NULL;
142 return (postFileCtx->path);
145 json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
147 AFB_PostHandle *postHandle = getPostHandle(request);
148 AFB_PostCtx *postFileCtx;
152 // This is called after PostForm and then after DonePostForm
155 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
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"));
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");
168 // Error or not let's free all resources
169 close(postFileCtx->fd);
170 free (postFileCtx->path);
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");
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);
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);
193 // Extract Application Context from posthandle [NULL == 1st iteration]
194 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
196 // This is the 1st Item iteration let's open output file and allocate necessary resources
197 if (postFileCtx == NULL) {
200 // Create an application specific context
201 postFileCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
203 // attach application to postHandle
204 postHandle->ctx = (void*) postFileCtx; // May place anything here until post->completeCB handle resources liberation
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));
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));
221 } else closedir (destDir);
223 strncat (filepath, "/", sizeof(filepath));
224 strncat (filepath, item->filename, sizeof(filepath));
226 postFileCtx->path = strdup (filepath);
227 if (verbose) fprintf(stderr, "getPostFile path=%s\n", filepath);
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));
234 // reuse existing application context
235 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
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));
245 // every intermediary iteration should return Success & NULL
246 request->errcode = MHD_HTTP_OK;
250 request->errcode = MHD_HTTP_EXPECTATION_FAILED;
256 static void jsoninit()
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);
272 verbose = verbosesav;
277 // get JSON object from error level and increase its reference count
278 struct json_object *jsonNewStatus (AFB_error level)
281 json_object *target = AFBerr[level].json;
282 json_object_get (target);
287 // get AFB object type with adequate usage count
288 struct json_object *jsonNewjtype (void)
291 json_object_get (jTypeStatic); // increase reference count
292 return (jTypeStatic);
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;
305 if (format != NULL) {
306 va_start(args, format);
307 vsnprintf (message, sizeof (message), format, args);
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));
318 fprintf (stderr, "AFB:%-6s [%3d]: ", AFBerr [level].label, count++);
319 if (format != NULL) {
320 fprintf (stderr, "%s", message);
322 fprintf (stderr, "No Message");
324 fprintf (stderr, "\n");
327 return (AFBResponse);
330 // Dump a message on stderr
331 void jsonDumpObject (struct json_object * jObject) {
334 fprintf (stderr, "AFB:dump [%s]\n", json_object_to_json_string(jObject));