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 // handle to hold queryAll values
30 // Sample Generic Ping Debug API
31 PUBLIC json_object* getPingTest(AFB_request *request) {
33 json_object *response;
38 AFB_clientCtx *client=request->client; // get client context from request
40 // request all query key/value
41 len = getQueryAll (request, query, sizeof(query));
42 if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
44 // check if we have some post data
45 if (request->post == NULL) request->post->data="NoData";
47 // check is we have a session and a plugin handle
48 if (client == NULL) strncpy (session,"NoSession", sizeof(session));
49 else snprintf(session, sizeof(session),"uuid=%s token=%s ctx=0x%x handle=0x%x", client->uuid, client->token, client->ctx, client->ctx);
51 // return response to caller
52 response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d CtxtId=%d query={%s} session={%s} PostData: [%s] "
53 , pingcount++, request->client->cid, query, session, request->post->data);
58 // Helper to retrieve argument from connection
59 PUBLIC const char* getQueryValue(AFB_request * request, char *name) {
62 value = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, name);
66 STATIC int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) {
67 queryHandleT *query = (queryHandleT*)handle;
69 query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value);
72 // Helper to retrieve argument from connection
73 PUBLIC int getQueryAll(AFB_request * request, char *buffer, size_t len) {
75 buffer[0] = '\0'; // start with an empty string
80 MHD_get_connection_values (request->connection, MHD_GET_ARGUMENT_KIND, getQueryCB, &query);
84 // Helper to retrieve POST handle
85 PUBLIC AFB_PostHandle* getPostHandle (AFB_request *request) {
86 if (request->post == NULL) return (NULL);
87 return ((AFB_PostHandle*) request->post->data);
90 // Helper to retrieve POST file context
91 PUBLIC AFB_PostCtx* getPostContext (AFB_request *request) {
92 AFB_PostHandle* postHandle;
93 if (request->post == NULL) return (NULL);
95 postHandle = (AFB_PostHandle*) request->post->data;
96 if (postHandle == NULL) return NULL;
98 return ((AFB_PostCtx*) postHandle->ctx);
101 PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
103 AFB_PostHandle *postHandle = getPostHandle(request);
108 // This is called after PostForm and then after DonePostForm
111 appCtx = (AFB_PostCtx*) postHandle->ctx;
113 // No Post Application Context [something really bad happen]
114 if (appCtx == NULL) {
115 request->errcode = MHD_HTTP_EXPECTATION_FAILED;
116 return(jsonNewMessage(AFB_FAIL,"Error: PostForm no PostContext to free\n"));
119 // We have a context but last Xform iteration fail.
120 if (appCtx->jerror != NULL) {
121 // request->errcode = appCtx->errcode;
122 jresp = appCtx->jerror; // retrieve previous error from postCtx
123 } else jresp = jsonNewMessage(AFB_FAIL,"UploadFile Post Request file=[%s] done", appCtx->path);
125 // Error or not let's free all resources
132 // Make sure it's a valid PostForm request
133 if (!request->post && request->post->type != AFB_POST_FORM) {
134 appCtx->jerror= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n");
138 // Check this is a file element
139 if (item->filename == NULL) {
140 appCtx->jerror= jsonNewMessage(AFB_FAIL,"No Filename attached to key=%s\n", item->key);
144 // Check we got something in buffer
145 if (item->len <= 0) {
146 appCtx->jerror= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key);
150 // Extract Application Context from posthandle [NULL == 1st iteration]
151 appCtx = (AFB_PostCtx*) postHandle->ctx;
153 // This is the 1st Item iteration let's open output file and allocate necessary resources
154 if (appCtx == NULL) {
157 // Create an application specific context
158 appCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
159 appCtx->path = strdup (filepath);
161 // attach application to postHandle
162 postHandle->ctx = (void*) appCtx; // May place anything here until post->completeCB handle resources liberation
164 // Build destination directory full path
165 if (destination[0] != '/') {
166 strncpy (filepath, request->config->sessiondir, sizeof(filepath));
167 strncat (filepath, destination, sizeof(filepath));
168 strncat (filepath, "/", sizeof(filepath));
169 strncat (filepath, destination, sizeof(filepath));
170 } else strncpy (filepath, destination, sizeof(filepath));
173 // make sure destination directory exist
174 destDir = openat (filepath, request->plugin, O_DIRECTORY);
176 destDir = mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP);
178 appCtx->jerror= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
181 } else close (destDir);
183 strncat (filepath, "/", sizeof(filepath));
184 strncat (filepath, item->filename, sizeof(filepath));
186 if((appCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) < 0) {
187 appCtx->jerror= jsonNewMessage(AFB_FAIL,"Fail to Create destination=[%s] error=%s\n", filepath, strerror(errno));
191 // reuse existing application context
192 appCtx = (AFB_PostCtx*) postHandle->ctx;
195 // Check we successfully wrote full buffer
196 len = write (appCtx->fd, item->data, item->len);
197 if (item->len != len) {
198 appCtx->jerror= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno));
202 // every intermediary iteration should return Success & NULL
203 request->errcode = MHD_HTTP_OK;
207 request->errcode = MHD_HTTP_EXPECTATION_FAILED;