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 "local-def.h"
22 // Sample Generic Ping Debug API
23 static json_object* getPingTest(AFB_request *request) {
24 static int pingcount = 0;
25 json_object *response;
30 // request all query key/value
31 len = getQueryAll (request, query, sizeof(query));
32 if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
34 // check if we have some post data
35 if (request->post == NULL) request->post->data="NoData";
37 // return response to caller
38 response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d uuid=%s query={%s} session={0x%x} PostData: [%s] "
39 , pingcount++, request->uuid, query, session, request->post->data);
43 // With content-type=json data are directly avaliable in request->post->data
44 STATIC json_object* GetJsonByPost (AFB_request *request) {
49 // check if we have some post data
50 if (request->post == NULL) request->post->data="NoData";
52 // Get all query string [Note real app should probably use value=getQueryValue(request,"key")]
53 len = getQueryAll (request, query, sizeof(query));
54 if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
56 // for debug/test return response to caller
57 jresp = jsonNewMessage(AFB_SUCCESS, "GetJsonByPost query={%s} PostData: [%s]", query, request->post->data);
64 // Upload a file and execute a function when upload is done
65 STATIC json_object* UploadAppli (AFB_request *request, AFB_PostItem *item) {
67 char *destination = "applications";
69 // This is called after PostForm and then after DonePostForm
71 // Do something intelligent here to install application
72 request->errcode = MHD_HTTP_OK; // or error is something went wrong;
73 request->jresp = jsonNewMessage(AFB_SUCCESS,"UploadFile Post Appli=%s done", getPostPath (request));
74 // Note: should not return here in order getPostedFile to clear Post resources.
77 // upload multi iteration logic is handle by getPostedFile
78 return (getPostFile (request, item, destination));
81 // Simples Upload case just upload a file
82 STATIC json_object* UploadMusic (AFB_request *request, AFB_PostItem *item) {
84 // upload multi iteration logic is handle by getPostedFile
85 return (getPostFile (request, item, "musics"));
88 // PostForm callback is called multiple times (one or each key within form, or once per file buffer)
89 // When file has been fully uploaded call is call with item==NULL
90 STATIC json_object* UploadImage (AFB_request *request, AFB_PostItem *item) {
92 // note if directory is relative it will be prefixed by request->config->sessiondir
93 char *destination = "images";
95 // This is called after PostForm and then after DonePostForm
96 if (item == NULL && getPostPath (request) != NULL) {
97 // Do something with your newly upload filepath=postFileCtx->path
98 request->errcode = MHD_HTTP_OK;
99 request->jresp = jsonNewMessage(AFB_SUCCESS,"UploadFile Post Image done");
101 // Note: should not return here in order getPostedFile to clear Post resources.
104 // upload multi iteration logic is handle by getPostedFile
105 return (getPostFile (request, item, destination));
109 // NOTE: this sample does not use session to keep test a basic as possible
110 // in real application upload-xxx should be protected with AFB_SESSION_CHECK
111 STATIC AFB_restapi pluginApis[]= {
112 {"ping" , AFB_SESSION_NONE , (AFB_apiCB)getPingTest ,"Ping Rest Test Service"},
113 {"upload-json" , AFB_SESSION_NONE , (AFB_apiCB)GetJsonByPost ,"Demo for Json Buffer on Post"},
114 {"upload-image" , AFB_SESSION_NONE , (AFB_apiCB)UploadImage ,"Demo for file upload"},
115 {"upload-music" , AFB_SESSION_NONE , (AFB_apiCB)UploadMusic ,"Demo for file upload"},
116 {"upload-appli" , AFB_SESSION_NONE , (AFB_apiCB)UploadAppli ,"Demo for file upload"},
120 PUBLIC AFB_plugin *pluginRegister () {
121 AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
122 plugin->type = AFB_PLUGIN_JSON;
123 plugin->info = "Sample with Post Upload Files";
124 plugin->prefix= "post"; // url base
125 plugin->apis = pluginApis;
126 plugin->handle= (void*) "What ever you want";