Hack --plugins=path
[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
22
23 // handle to hold queryAll values
24 typedef struct {
25      char    *msg;
26      int     idx;
27      size_t  len;
28 } queryHandleT;
29
30 // Sample Generic Ping Debug API
31 PUBLIC json_object* getPingTest(AFB_request *request) {
32     static pingcount = 0;
33     json_object *response;
34     char query  [256];
35     char session[256];
36
37     int len;
38     AFB_clientCtx *client=request->client; // get client context from request
39     
40     // request all query key/value
41     len = getQueryAll (request, query, sizeof(query));
42     if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
43     
44     // check if we have some post data
45     if (request->post == NULL)  request->post->data="NoData"; 
46     
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); 
50         
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);
54     return (response);
55 }
56
57
58 // Helper to retrieve argument from  connection
59 PUBLIC const char* getQueryValue(AFB_request * request, char *name) {
60     const char *value;
61
62     value = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, name);
63     return (value);
64 }
65
66 STATIC int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) {
67     queryHandleT *query = (queryHandleT*)handle;
68         
69     query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value);
70 }
71
72 // Helper to retrieve argument from  connection
73 PUBLIC int getQueryAll(AFB_request * request, char *buffer, size_t len) {
74     queryHandleT query;
75     buffer[0] = '\0'; // start with an empty string
76     query.msg= buffer;
77     query.len= len;
78     query.idx= 0;
79
80     MHD_get_connection_values (request->connection, MHD_GET_ARGUMENT_KIND, getQueryCB, &query);
81     return (len);
82 }
83
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);
88 }
89
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);
94     
95     postHandle = (AFB_PostHandle*) request->post->data;
96     if (postHandle == NULL) return NULL;
97        
98     return ((AFB_PostCtx*) postHandle->ctx);
99 }
100
101 PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
102
103     AFB_PostHandle *postHandle = getPostHandle(request);
104     AFB_PostCtx *appCtx;
105     char filepath[512];
106     int len;
107             
108     // This is called after PostForm and then after DonePostForm
109     if (item == NULL) {
110         json_object* jresp;
111         appCtx = (AFB_PostCtx*) postHandle->ctx;
112         
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"));          
117         }
118         
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);
124         
125         // Error or not let's free all resources
126         close(appCtx->fd);
127         free (appCtx->path);
128         free (appCtx);
129         return (jresp);  
130     }
131     
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");
135         goto ExitOnError;
136     } 
137     
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);
141         goto ExitOnError;
142     }
143     
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);
147         goto ExitOnError;
148     }
149
150     // Extract Application Context from posthandle [NULL == 1st iteration]    
151     appCtx = (AFB_PostCtx*) postHandle->ctx;
152
153     // This is the 1st Item iteration let's open output file and allocate necessary resources
154     if (appCtx == NULL)  {
155         int destDir;
156         
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);
160         
161         // attach application to postHandle
162         postHandle->ctx = (void*) appCtx;   // May place anything here until post->completeCB handle resources liberation  
163         
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));
171         
172
173         // make sure destination directory exist
174         destDir = openat (filepath, request->plugin,  O_DIRECTORY);
175         if (destDir < 0) {
176           destDir = mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP); 
177           if (destDir < 0) {
178             appCtx->jerror= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
179             goto ExitOnError;
180           }
181         } else close (destDir);
182         
183         strncat (filepath, "/", sizeof(filepath));
184         strncat (filepath, item->filename, sizeof(filepath));  
185
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));
188             goto ExitOnError;
189         } 
190     } else {     
191         // reuse existing application context
192         appCtx = (AFB_PostCtx*) postHandle->ctx;  
193     } 
194
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));
199         goto ExitOnError;
200     }
201   
202     // every intermediary iteration should return Success & NULL
203     request->errcode = MHD_HTTP_OK;
204     return NULL;
205     
206 ExitOnError:    
207     request->errcode = MHD_HTTP_EXPECTATION_FAILED;
208     return NULL;
209 }