1a3399f9b3999776002e8aa819bb87198f77cd51
[src/app-framework-binder.git] / src / afbs-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 "local-def.h"
21
22 // Dummy sample of Client Application Context
23 typedef struct {
24   int  something;       
25   void *whateveryouwant;
26 } MyClientApplicationHandle;
27
28
29 // Request Creation of new context if it does not exist
30 STATIC json_object* clientContextCreate (AFB_request *request) {
31     json_object *jresp;
32     int   res;
33     char *token;
34     AFB_clientCtx *client=request->client; // get client context from request
35    
36     // check we do not already have a session
37     if ((client != NULL) && (client->ctx != NULL)) {
38         request->errcode=MHD_HTTP_FORBIDDEN;
39         return (jsonNewMessage(AFB_FAIL, "Token exist use refresh"));
40     }
41         
42     // request a new client context token and check result 
43     if (AFB_UNAUTH == ctxTokenCreate (request)) {
44         request->errcode=MHD_HTTP_UNAUTHORIZED;
45         jresp= jsonNewMessage(AFB_FAIL, "No/Invalid initial token provided [should match --token=xxxx]");
46         return (jresp);
47     }
48     
49     // request a new client context token and check result 
50     if (AFB_SUCCESS != ctxTokenCreate (request)) {
51         request->errcode=MHD_HTTP_UNAUTHORIZED;
52         jresp= jsonNewMessage(AFB_FAIL, "Token Session Not Activated [restart with --token=xxxx]");
53         return (jresp);
54     }
55    
56     // add a client context to session
57     client->ctx = malloc (sizeof (MyClientApplicationHandle));
58     
59     // Send response to UI
60     jresp = json_object_new_object();               
61     json_object_object_add(jresp, "token", json_object_new_string (client->token));
62
63     return (jresp);
64 }
65
66 // Renew an existing context
67 STATIC json_object* clientContextRefresh (AFB_request *request) {
68     json_object *jresp;
69
70     // note: we do not need to parse the old token as clientContextRefresh doit for us
71     if (AFB_SUCCESS != ctxTokenRefresh (request)) {
72         request->errcode=MHD_HTTP_UNAUTHORIZED;
73         jresp= jsonNewMessage(AFB_FAIL, "Token Exchange Broken Refresh Refused");
74     } else {
75         jresp = json_object_new_object();
76         json_object_object_add(jresp, "token", json_object_new_string (request->client->token));              
77     }
78             
79     return (jresp);
80 }
81
82
83 // Verify a context is still valid 
84 STATIC json_object* clientContextCheck (AFB_request *request) {
85     
86     json_object *jresp = json_object_new_object();
87     
88     // add an error code to respond
89     if (AFB_SUCCESS != ctxTokenCheck (request)) {
90         request->errcode=MHD_HTTP_UNAUTHORIZED;
91         json_object_object_add(jresp, "isvalid", json_object_new_boolean (FALSE));
92     } else {
93         json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));       
94     }
95         
96     return (jresp); 
97 }
98
99
100
101 // Close and Free context
102 STATIC json_object* clientContextReset (AFB_request *request) {
103     json_object *jresp;
104    
105     // note: we do not need to parse the old token as clientContextRefresh doit for us
106     if (AFB_SUCCESS != ctxTokenReset (request)) {
107         request->errcode=MHD_HTTP_UNAUTHORIZED;
108         jresp= jsonNewMessage(AFB_FAIL, "No Token Client Context [use --token=xxx]");
109     } else {
110         jresp = json_object_new_object();
111         json_object_object_add(jresp, "uuid", json_object_new_string (request->client->uuid));              
112     }
113     
114     return (jresp); 
115 }
116
117 // In this case or handle is quite basic
118 typedef struct {
119    int fd; 
120 } appPostCtx;
121
122 // This function is call when PostForm processing is completed
123 STATIC void DonePostForm (AFB_request *request) {
124     AFB_PostHandle  *postHandle = (AFB_PostHandle*)request->post->data;
125     appPostCtx *appCtx= postHandle->ctx;
126     
127     // Close upload file ID
128     close (appCtx->fd);
129
130     // Free application specific handle
131     free (postHandle->ctx);
132     
133     if (verbose) fprintf (stderr, "DonePostForm upload done\n");
134 }
135
136
137 // WARNING: PostForm callback are call multiple time (one or each key within form)
138 // When processing POST_JSON request->data hold a PostHandle and not data directly as for POST_JSON
139 STATIC json_object* ProcessPostForm (AFB_request *request, AFB_PostItem *item) {
140
141     AFB_PostHandle  *postHandle;
142     appPostCtx *appCtx;
143     char filepath[512];
144             
145     // When Post is fully processed the same callback is call with a item==NULL
146     if (item == NULL) {
147         // Close file, Free handle
148         
149         request->errcode = MHD_HTTP_OK;
150         return(jsonNewMessage(AFB_SUCCESS,"File [%s] uploaded at [%s] error=\n", item->filename, request->config->sessiondir));  
151     }
152     
153     // Let's make sure this is a valid PostForm request
154     if (!request->post && request->post->type != AFB_POST_FORM) {
155         request->errcode = MHD_HTTP_FORBIDDEN;
156         return(jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n"));          
157     } else {
158         // In AFB_POST_FORM case post->data is a PostForm handle
159         postHandle = (AFB_PostHandle*) request->post->data;
160         appCtx = (appPostCtx*) postHandle->ctx;
161     }
162
163     // Check this is a file element
164     if (0 != strcmp (item->key, "file")) {
165         request->errcode = MHD_HTTP_FORBIDDEN;
166         return (jsonNewMessage(AFB_FAIL,"No File within element key=%s\n", item->key));
167     }
168
169     // This is the 1st Item iteration let's open output file and allocate necessary resources
170     if (postHandle->ctx == NULL)  {
171         int fd;
172         
173         strncpy (filepath, request->config->sessiondir, sizeof(filepath));
174         strncat (filepath, "/", sizeof(filepath));
175         strncat (filepath, item->filename, sizeof(filepath));  
176
177         if((fd = open(request->config->sessiondir, O_RDONLY)) < 0) {
178             request->errcode = MHD_HTTP_FORBIDDEN;
179             return (jsonNewMessage(AFB_FAIL,"Fail to Upload file [%s] at [%s] error=\n", item->filename, request->config->sessiondir, strerror(errno)));
180         };            
181
182         // Create an application specific context
183         appCtx = malloc (sizeof(appPostCtx)); // May place anything here until post->completeCB handle resources liberation
184         appCtx->fd = fd;
185         
186         // attach application to postHandle
187         postHandle->ctx = (void*) appCtx;   // May place anything here until post->completeCB handle resources liberation        
188         postHandle->completeCB = (AFB_apiCB)DonePostForm; // CallBack when Form Processing is finished
189         
190     } else {
191         // this is not the call, FD is already open
192         appCtx = (appPostCtx*) postHandle->ctx;
193     }
194
195     // We have something to write
196     if (item->len > 0) {
197         
198         if (!write (appCtx->fd, item->data, item->len)) {
199             request->errcode = MHD_HTTP_FORBIDDEN;
200             return (jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno)));
201         }
202     }
203   
204     // every event should return Sucess or Form processing stop
205     request->errcode = MHD_HTTP_OK;
206     return NULL;
207 }
208
209 // This function is call when Client Session Context is removed
210 // Note: when freeCtxCB==NULL standard free/malloc is called
211 STATIC void clientContextFree(AFB_clientCtx *client) {
212     fprintf (stderr,"Plugin[%s] Closing Session uuid=[%s]\n", client->plugin->prefix, client->uuid);
213     free (client->ctx);
214 }
215
216 STATIC  AFB_restapi pluginApis[]= {
217   {"ping"          , (AFB_apiCB)apiPingTest         ,"Ping Rest Test Service"},
218   {"token-create"  , (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"},
219   {"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"},
220   {"token-check"   , (AFB_apiCB)clientContextCheck  ,"Check Client Context Token"},
221   {"token-reset"   , (AFB_apiCB)clientContextReset  ,"Close Client Context and Free resources"},
222   {"file-upload"   , (AFB_apiCB)ProcessPostForm     ,"Demo for file upload"},
223   {NULL}
224 };
225
226 PUBLIC AFB_plugin *afsvRegister () {
227     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
228     plugin->type  = AFB_PLUGIN_JSON; 
229     plugin->info  = "Application Framework Binder Service";
230     plugin->prefix= "afbs";  // url base
231     plugin->apis  = pluginApis;
232     plugin->handle= (void*) "What ever you want";
233     plugin->freeCtxCB= (void*) clientContextFree;
234     
235     return (plugin);
236 };