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/>.
29 #include <sys/types.h>
32 #include "local-def.h"
33 #include "afb-req-itf.h"
35 // handle to hold queryAll values
42 // Error code are requested through function to manage json usage count
49 static AFB_errorT AFBerr [AFB_UNAUTH+1];
50 static json_object *jTypeStatic;
54 static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success", "done", "unauth"};
58 // Helper to retrieve argument from connection
59 const char* getQueryValue(const AFB_request * request, const char *name) {
60 return afb_req_argument(*request->areq, name);
63 static int getQueryCB (queryHandleT *query, struct afb_arg arg) {
64 if (query->idx >= query->len)
66 query->idx += (unsigned)snprintf (&query->msg[query->idx], query->len-query->idx, " %s: %s\'%s\',", arg.name, arg.is_file?"FILE=":"", arg.value);
67 return 1; /* continue to iterate */
70 // Helper to retrieve argument from connection
71 size_t getQueryAll(AFB_request * request, char *buffer, size_t len) {
73 buffer[0] = '\0'; // start with an empty string
78 afb_req_iterate(*request->areq, (void*)getQueryCB, &query);
80 return query.idx >= len ? len - 1 : query.idx;
84 char* getPostPath (AFB_request *request) {
85 AFB_PostHandle *postHandle = getPostHandle(request);
86 AFB_PostCtx *postFileCtx;
88 if (postHandle == NULL) return NULL;
90 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
91 if (postFileCtx == NULL) return NULL;
93 return (postFileCtx->path);
96 json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
98 AFB_PostHandle *postHandle = getPostHandle(request);
99 AFB_PostCtx *postFileCtx;
103 // This is called after PostForm and then after DonePostForm
106 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
108 // No Post Application Context [something really bad happen]
109 if (postFileCtx == NULL) {
110 request->errcode = MHD_HTTP_EXPECTATION_FAILED;
111 return(jsonNewMessage(AFB_FAIL,"Error: PostForm no PostContext to free\n"));
114 // We have a context but last Xform iteration fail or application set a message
115 if (request->jresp != NULL) {
116 jresp = request->jresp; // retrieve previous error from postCtx
117 } else jresp = jsonNewMessage(AFB_SUCCESS,"getPostFile Post Request done");
119 // Error or not let's free all resources
120 close(postFileCtx->fd);
121 free (postFileCtx->path);
125 #if defined(PLEASE_FIX_ME_THE_ERROR_IS_postFileCtx_NOT_INITIALIZED)
126 // Make sure it's a valid PostForm request
127 if (!request->post && request->post->type != AFB_POST_FORM) {
128 postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n");
132 // Check this is a file element
133 if (item->filename == NULL) {
134 postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"No Filename attached to key=%s\n", item->key);
138 // Check we got something in buffer
139 if (item->len <= 0) {
140 postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key);
144 // Extract Application Context from posthandle [NULL == 1st iteration]
145 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
147 // This is the 1st Item iteration let's open output file and allocate necessary resources
148 if (postFileCtx == NULL) {
151 // Create an application specific context
152 postFileCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
154 // attach application to postHandle
155 postHandle->ctx = (void*) postFileCtx; // May place anything here until post->completeCB handle resources liberation
157 // Build destination directory full path
158 if (destination[0] != '/') {
159 strncpy (filepath, request->config->sessiondir, sizeof(filepath));
160 strncat (filepath, "/", sizeof(filepath));
161 strncat (filepath, destination, sizeof(filepath));
162 } else strncpy (filepath, destination, sizeof(filepath));
165 // make sure destination directory exist
166 destDir = opendir (filepath);
167 if (destDir == NULL) {
168 if (mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP) < 0) {
169 postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
172 } else closedir (destDir);
174 strncat (filepath, "/", sizeof(filepath));
175 strncat (filepath, item->filename, sizeof(filepath));
177 postFileCtx->path = strdup (filepath);
178 if (verbose) fprintf(stderr, "getPostFile path=%s\n", filepath);
180 if((postFileCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) <= 0) {
181 postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination File=[%s] error=%s\n", filepath, strerror(errno));
185 // reuse existing application context
186 postFileCtx = (AFB_PostCtx*) postHandle->ctx;
189 // Check we successfully wrote full buffer
190 len = write (postFileCtx->fd, item->data, item->len);
191 if ((ssize_t)item->len != len) {
192 postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno));
196 // every intermediary iteration should return Success & NULL
197 request->errcode = MHD_HTTP_OK;
201 request->errcode = MHD_HTTP_EXPECTATION_FAILED;
207 static void jsoninit()
214 // initialise JSON constant messages and increase reference count to make them permanent
215 verbosesav = verbose;
216 verbose = 0; // run initialisation in silent mode
217 jTypeStatic = json_object_new_string ("AFB_message");
218 for (idx = 0; idx <= AFB_UNAUTH; idx++) {
219 AFBerr[idx].level = idx;
220 AFBerr[idx].label = ERROR_LABEL [idx];
221 AFBerr[idx].json = jsonNewMessage (idx, NULL);
223 verbose = verbosesav;
227 // build an ERROR message and return it as a valid json object
228 json_object *json_add_status (json_object *obj, const char *status, const char *info)
231 obj = json_object_new_object();
232 json_object_object_add(obj, "status", json_object_new_string(status));
234 json_object_object_add(obj, "info", json_object_new_string(info));
238 // build an ERROR message and return it as a valid json object
239 json_object *json_add_status_v (json_object *obj, const char *status, const char *info, va_list args)
242 if (info == NULL || vasprintf(&message, info, args) < 0)
244 obj = json_add_status(obj, status, message);
250 // build an ERROR message and return it as a valid json object
251 json_object *json_add_status_f (json_object *obj, const char *status, const char *info, ...)
254 va_start(args, info);
255 obj = json_add_status_v(obj, status, info, args);
262 // build an ERROR message and return it as a valid json object
263 struct json_object *jsonNewMessage (AFB_error level, char* format, ...) {
264 static int count = 0;
265 json_object * AFBResponse;
272 if (format != NULL) {
273 va_start(args, format);
274 vsnprintf (message, sizeof (message), format, args);
278 AFBResponse = json_object_new_object();
279 json_object_object_add (AFBResponse, "jtype", json_object_get (jTypeStatic));
280 json_object_object_add (AFBResponse, "status" , json_object_new_string (ERROR_LABEL[level]));
281 if (format != NULL) {
282 json_object_object_add (AFBResponse, "info" , json_object_new_string (message));
285 fprintf (stderr, "AFB:%-6s [%3d]: ", AFBerr [level].label, count++);
286 if (format != NULL) {
287 fprintf (stderr, "%s", message);
289 fprintf (stderr, "No Message");
291 fprintf (stderr, "\n");
294 return (AFBResponse);
304 status: "", /* exist, fail, empty, null, processed */