Improve parsing of json string
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <errno.h>
26
27 #include <json-c/json.h>
28 #include <afb/afb-binding-v2.h>
29
30 #include "afb-api.h"
31 #include "afb-api-so-v2.h"
32 #include "afb-apiset.h"
33 #include "afb-auth.h"
34 #include "afb-export.h"
35 #include "afb-evt.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "sig-monitor.h"
40 #include "verbose.h"
41
42 /*
43  * names of symbols
44  */
45 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
46 static const char afb_api_so_v2_data[] = "afbBindingV2data";
47
48 struct preinit
49 {
50         int return_code;
51         const struct afb_binding_v2 *binding;
52 };
53
54 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
55 {
56         const struct afb_verb_v2 *verb;
57
58         verb = binding->verbs;
59         while (verb->verb && strcasecmp(verb->verb, name))
60                 verb++;
61         return verb->verb ? verb : NULL;
62         return NULL;
63 }
64
65 void afb_api_so_v2_process_call(const struct afb_binding_v2 *binding, struct afb_xreq *xreq)
66 {
67         const struct afb_verb_v2 *verb;
68
69         verb = search(binding, xreq->request.called_verb);
70         afb_xreq_call_verb_v2(xreq, verb);
71 }
72
73 struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_binding_v2 *binding, const char *apiname)
74 {
75         char buffer[256];
76         const struct afb_verb_v2 *verb;
77         struct json_object *r, *f, *a, *i, *p, *g;
78         enum json_tokener_error jerr;
79
80         if (binding->specification) {
81                 r = json_tokener_parse_verbose(binding->specification, &jerr);
82                 if (jerr == json_tokener_success)
83                         return r;
84         }
85
86         r = json_object_new_object();
87         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
88
89         i = json_object_new_object();
90         json_object_object_add(r, "info", i);
91         json_object_object_add(i, "title", json_object_new_string(apiname));
92         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
93         json_object_object_add(i, "description", json_object_new_string(binding->info ?: apiname));
94
95         p = json_object_new_object();
96         json_object_object_add(r, "paths", p);
97         verb = binding->verbs;
98         while (verb->verb) {
99                 buffer[0] = '/';
100                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
101                 buffer[sizeof buffer - 1] = 0;
102                 f = json_object_new_object();
103                 json_object_object_add(p, buffer, f);
104                 g = json_object_new_object();
105                 json_object_object_add(f, "get", g);
106
107                 a = afb_auth_json_v2(verb->auth, verb->session);
108                 if (a)
109                         json_object_object_add(g, "x-permissions", a);
110
111                 a = json_object_new_object();
112                 json_object_object_add(g, "responses", a);
113                 f = json_object_new_object();
114                 json_object_object_add(a, "200", f);
115                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
116                 verb++;
117         }
118         return r;
119 }
120
121 static void do_preinit(int sig, void *closure)
122 {
123         struct preinit *preinit = closure;
124
125         if (!sig)
126                 preinit->return_code = preinit->binding->preinit();
127         else {
128                 errno = EINTR;
129                 preinit->return_code = -1;
130         }
131 };
132
133 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set, struct afb_binding_data_v2 *data)
134 {
135         int rc;
136         struct afb_export *export;
137         struct preinit preinit;
138
139         /* basic checks */
140         assert(binding);
141         assert(binding->api);
142         assert(binding->verbs);
143         assert(data);
144
145         /* allocates the description */
146         export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent);
147         if (!export) {
148                 ERROR("out of memory");
149                 goto error;
150         }
151
152         /* records the binding */
153         if (afb_export_declare(export, binding->noconcurrency) < 0) {
154                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(export), afb_apiset_name(declare_set));
155                 goto error;
156         }
157         /* init the binding */
158         if (binding->preinit) {
159                 INFO("binding %s calling preinit function", binding->api);
160                 preinit.binding = binding;
161                 sig_monitor(0, do_preinit, &preinit);
162                 rc = preinit.return_code;
163                 if (rc < 0) {
164                         ERROR("binding %s preinit function failed...", afb_export_apiname(export));
165                         afb_export_undeclare(export);
166                         goto error;
167                 }
168         }
169
170         INFO("binding %s added to set %s", afb_export_apiname(export), afb_apiset_name(declare_set));
171         return 1;
172
173 error:
174         afb_export_unref(export);
175
176         return -1;
177 }
178
179 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
180 {
181         const struct afb_binding_v2 *binding;
182         struct afb_binding_data_v2 *data;
183
184         /* retrieves the register function */
185         binding = dlsym(handle, afb_api_so_v2_descriptor);
186         data = dlsym(handle, afb_api_so_v2_data);
187         if (!binding && !data)
188                 return 0;
189
190         INFO("binding [%s] looks like an AFB binding V2", path);
191
192         /* basic checks */
193         if (!binding || !data) {
194                 ERROR("binding [%s] incomplete symbol set: %s is missing",
195                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
196                 goto error;
197         }
198         if (binding->api == NULL || *binding->api == 0) {
199                 ERROR("binding [%s] bad api name...", path);
200                 goto error;
201         }
202         if (!afb_api_is_valid_name(binding->api)) {
203                 ERROR("binding [%s] invalid api name...", path);
204                 goto error;
205         }
206
207         if (binding->verbs == NULL) {
208                 ERROR("binding [%s] no verbs...", path);
209                 goto error;
210         }
211
212         return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data);
213
214  error:
215         return -1;
216 }
217