Remove Hashtable for session and cleanup
[src/app-framework-binder.git] / plugins / samples / SamplePost.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
23 // In this case or handle is quite basic
24 typedef struct {
25    int fd; 
26 } appPostCtx;
27
28 // This function is call when PostForm processing is completed
29 STATIC void DonePostForm (AFB_request *request) {
30     AFB_PostHandle  *postHandle = (AFB_PostHandle*)request->post->data;
31     appPostCtx *appCtx= postHandle->ctx;
32     
33     // Close upload file ID
34     close (appCtx->fd);
35
36     // Free application specific handle
37     free (postHandle->ctx);
38     
39     if (verbose) fprintf (stderr, "DonePostForm upload done\n");
40 }
41
42
43 // WARNING: PostForm callback are call multiple time (one or each key within form)
44 // When processing POST_JSON request->data hold a PostHandle and not data directly as for POST_JSON
45 STATIC json_object* ProcessPostForm (AFB_request *request, AFB_PostItem *item) {
46
47     AFB_PostHandle  *postHandle;
48     appPostCtx *appCtx;
49     char filepath[512];
50             
51     // When Post is fully processed the same callback is call with a item==NULL
52     if (item == NULL) {
53         // Close file, Free handle
54         
55         request->errcode = MHD_HTTP_OK;
56         return(jsonNewMessage(AFB_SUCCESS,"File [%s] uploaded at [%s] error=\n", item->filename, request->config->sessiondir));  
57     }
58     
59     // Let's make sure this is a valid PostForm request
60     if (!request->post && request->post->type != AFB_POST_FORM) {
61         request->errcode = MHD_HTTP_FORBIDDEN;
62         return(jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n"));          
63     } else {
64         // In AFB_POST_FORM case post->data is a PostForm handle
65         postHandle = (AFB_PostHandle*) request->post->data;
66         appCtx = (appPostCtx*) postHandle->ctx;
67     }
68
69     // Check this is a file element
70     if (0 != strcmp (item->key, "file")) {
71         request->errcode = MHD_HTTP_FORBIDDEN;
72         return (jsonNewMessage(AFB_FAIL,"No File within element key=%s\n", item->key));
73     }
74
75     // This is the 1st Item iteration let's open output file and allocate necessary resources
76     if (postHandle->ctx == NULL)  {
77         int fd;
78         
79         strncpy (filepath, request->config->sessiondir, sizeof(filepath));
80         strncat (filepath, "/", sizeof(filepath));
81         strncat (filepath, item->filename, sizeof(filepath));  
82
83         if((fd = open(request->config->sessiondir, O_RDONLY)) < 0) {
84             request->errcode = MHD_HTTP_FORBIDDEN;
85             return (jsonNewMessage(AFB_FAIL,"Fail to Upload file [%s] at [%s] error=\n", item->filename, request->config->sessiondir, strerror(errno)));
86         };            
87
88         // Create an application specific context
89         appCtx = malloc (sizeof(appPostCtx)); // May place anything here until post->completeCB handle resources liberation
90         appCtx->fd = fd;
91         
92         // attach application to postHandle
93         postHandle->ctx = (void*) appCtx;   // May place anything here until post->completeCB handle resources liberation        
94         postHandle->completeCB = (AFB_apiCB)DonePostForm; // CallBack when Form Processing is finished
95         
96     } else {
97         // this is not the call, FD is already open
98         appCtx = (appPostCtx*) postHandle->ctx;
99     }
100
101     // We have something to write
102     if (item->len > 0) {
103         
104         if (!write (appCtx->fd, item->data, item->len)) {
105             request->errcode = MHD_HTTP_FORBIDDEN;
106             return (jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno)));
107         }
108     }
109   
110     // every event should return Sucess or Form processing stop
111     request->errcode = MHD_HTTP_OK;
112     return NULL;
113 }
114
115 // This function is call when Client Session Context is removed
116 // Note: when freeCtxCB==NULL standard free/malloc is called
117 STATIC void clientContextFree(AFB_clientCtx *client) {
118     fprintf (stderr,"Plugin[%s] Closing Session uuid=[%s]\n", client->plugin->prefix, client->uuid);
119     free (client->ctx);
120 }
121
122 STATIC  AFB_restapi pluginApis[]= {
123   {"ping"   , AFB_SESSION_NONE  , (AFB_apiCB)apiPingTest         ,"Ping Rest Test Service"},
124   {"upload" , AFB_SESSION_NONE  , (AFB_apiCB)ProcessPostForm     ,"Demo for file upload"},
125   {NULL}
126 };
127
128 PUBLIC AFB_plugin *afsvRegister () {
129     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
130     plugin->type  = AFB_PLUGIN_JSON; 
131     plugin->info  = "Application Framework Binder Service";
132     plugin->prefix= "post";  // url base
133     plugin->apis  = pluginApis;
134     plugin->handle= (void*) "What ever you want";
135     plugin->freeCtxCB= (void*) clientContextFree;
136     
137     return (plugin);
138 };