58a2a8b81243108bc7308e3fd1fae84124983493
[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 #define _GNU_SOURCE
20 #include <stdio.h>
21 #include <string.h>
22 #include <json.h>
23
24 #include "afb-plugin.h"
25 #include "afb-req-itf.h"
26
27
28 static int fillargs(json_object *args, struct afb_arg arg)
29 {
30     json_object *obj;
31
32     obj = json_object_new_object();
33     json_object_object_add (obj, "value", json_object_new_string(arg.value));
34     json_object_object_add (obj, "path", json_object_new_string(arg.path));
35     json_object_object_add (obj, "size", json_object_new_int64((int64_t)arg.size));
36     json_object_object_add (args, arg.name && *arg.name ? arg.name : "<empty-string>", obj);
37     return 1; /* continue to iterate */
38 }
39
40 // Sample Generic Ping Debug API
41 static void getPingTest(struct afb_req request)
42 {
43     static int pingcount = 0;
44     json_object *query;
45
46     query = json_object_new_object();
47     afb_req_iterate(request, (void*)fillargs, query);
48
49     afb_req_success_f(request, query, "Ping Binder Daemon count=%d", ++pingcount);
50 }
51
52 // With content-type=json data are directly avaliable in request->post->data
53 static void GetJsonByPost (struct afb_req request)
54 {
55     json_object* jresp;
56     json_object *query;
57     struct afb_arg arg;
58
59     query = json_object_new_object();
60     afb_req_iterate(request, (void*)fillargs, query);
61
62     arg = afb_req_get(request, "");
63     jresp = arg.value ? json_tokener_parse(arg.value) : NULL;
64     afb_req_success_f(request, jresp, "GetJsonByPost query={%s}", json_object_to_json_string(query));
65 }
66
67 // Upload a file and execute a function when upload is done
68 static void Uploads (struct afb_req request, const char *destination)
69 {
70    afb_req_fail_f(request, "unimplemented", "destination: %s", destination);
71 }
72
73 // Upload a file and execute a function when upload is done
74 static void UploadAppli (struct afb_req request)
75 {
76     Uploads(request, "applications");
77 }
78
79 // Simples Upload case just upload a file
80 static void UploadMusic (struct afb_req request)
81 {
82     Uploads(request, "musics");
83 }
84
85 // PostForm callback is called multiple times (one or each key within form, or once per file buffer)
86 // When file has been fully uploaded call is call with item==NULL 
87 static void UploadImage (struct afb_req request)
88 {
89     Uploads(request, "images");
90 }
91
92
93 // NOTE: this sample does not use session to keep test a basic as possible
94 //       in real application upload-xxx should be protected with AFB_SESSION_CHECK
95 static const struct AFB_restapi pluginApis[]= {
96   {"ping"         , AFB_SESSION_NONE  , getPingTest    ,"Ping Rest Test Service"},
97   {"upload-json"  , AFB_SESSION_NONE  , GetJsonByPost  ,"Demo for Json Buffer on Post"},
98   {"upload-image" , AFB_SESSION_NONE  , UploadImage    ,"Demo for file upload"},
99   {"upload-music" , AFB_SESSION_NONE  , UploadMusic    ,"Demo for file upload"},
100   {"upload-appli" , AFB_SESSION_NONE  , UploadAppli    ,"Demo for file upload"},
101   {NULL}
102 };
103
104 static const struct AFB_plugin plugin_desc = {
105         .type = AFB_PLUGIN_JSON,
106         .info = "Sample with Post Upload Files",
107         .prefix = "post",
108         .apis = pluginApis
109 };
110
111 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
112 {
113     return &plugin_desc;
114 };