typo
[src/app-framework-binder.git] / src / afb-ws.c
1 /*
2  * Copyright 2016 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 #include <assert.h>
20 #include <errno.h>
21 #include <sys/uio.h>
22 #include <string.h>
23
24 #include <json.h>
25
26 #include "websock.h"
27
28 #include "utils-upoll.h"
29
30 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
31 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
32 static void aws_disconnect(struct afb_ws *ws);
33 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size);
34 static void aws_on_content(struct afb_ws *ws, int last, size_t size);
35 static void aws_on_readable(struct afb_ws *ws);
36
37 static struct websock_itf aws_itf = {
38         .writev = (void*)aws_writev,
39         .readv = (void*)aws_readv,
40         .disconnect = (void*)aws_disconnect,
41
42         .on_ping = NULL,
43         .on_pong = NULL,
44         .on_close = (void*)aws_on_close,
45         .on_text = (void*)aws_on_content,
46         .on_binary = (void*)aws_on_content,
47         .on_continue = (void*)aws_on_content,
48         .on_extension = NULL,
49 };
50
51 struct afb_wsreq
52 {
53         struct afb_ws *aws;
54         struct afb_wsreq *next;
55         struct json_object *id;
56         struct json_object *name;
57         struct json_object *token;
58         struct json_object *request;
59 };
60
61 struct afb_ws
62 {
63         int fd;
64         struct upoll *up;
65         struct websock *ws;
66         void (*cleanup)(void*);
67         void *cleanup_closure;
68         struct AFB_clientCtx *context;
69         struct afb_wsreq *requests;
70 };
71
72 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
73 static void wsreq_iterate(struct afb_wsreq *wsreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure);
74 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
75 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
76 static int wsreq_session_create(struct afb_wsreq *wsreq);
77 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh);
78 static void wsreq_session_close(struct afb_wsreq *wsreq);
79
80 static const struct afb_req_itf wsreq_itf = {
81         .get = (void*)wsreq_get,
82         .iterate = (void*)wsreq_iterate,
83         .fail = (void*)wsreq_fail,
84         .success = (void*)wsreq_success,
85         .session_create = (void*)wsreq_session_create,
86         .session_check = (void*)wsreq_session_check,
87         .session_close = (void*)wsreq_session_close
88 };
89
90 struct afb_ws *afb_ws_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure)
91 {
92         struct afb_ws *result;
93
94         assert(fd >= 0);
95         assert(context != NULL);
96
97         result = malloc(sizeof * result);
98         if (result == NULL)
99                 goto error;
100
101         result->fd = fd;
102         result->cleanup = cleanup;
103         result->cleanup_closure = closure;
104         result->context = ctxClientGet(context);
105         if (result->context == NULL)
106                 goto error2;
107
108         result->ws = websock_create_v13(&aws_itf, result);
109         if (result->ws == NULL)
110                 goto error3;
111
112         result->up = upoll_open(result->fd, result);
113         if (result->up == NULL)
114                 goto error4;
115
116         upoll_on_readable(result->up, (void*)aws_on_readable);
117         upoll_on_hangup(result->up, (void*)aws_disconnect);
118         return result;
119 error4:
120         websock_destroy(result->ws);
121 error3:
122         ctxClientPut(result->context);
123 error2:
124         free(result);
125 error:
126         close(fd);
127         return NULL;
128 }
129
130 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
131 {
132         ssize_t rc;
133         do {
134                 rc = writev(ws->fd, iov, iovcnt);
135         } while(rc == -1 && errno == EINTR);
136         return rc;
137 }
138
139 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
140 {
141         ssize_t rc;
142         do {
143                 rc = readv(ws->fd, iov, iovcnt);
144         } while(rc == -1 && errno == EINTR);
145         return rc;
146 }
147
148 static void aws_disconnect(struct afb_ws *ws)
149 {
150         upoll_close(ws->up);
151         websock_destroy(ws->ws);
152         close(ws->fd);
153         MHD_resume_connection (ws->connection);
154         ctxClientPut(ws->context);
155         json_tokener_free(ws->tokener);
156         free(ws);
157 }
158
159 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
160 {
161         /* do nothing */
162 }
163
164 static void aws_on_readable(struct afb_ws *ws)
165 {
166         websock_dispatch(ws->ws);
167 }
168
169 static int aws_handle_json(struct afb_ws *aws, struct json_object *obj)
170 {
171         struct afb_req r;
172         int count, num;
173         struct json_object *type, *id, *name, *req, *token;
174         struct afb_wsreq *wsreq;
175         const char *api, *verb;
176         size_t lenapi, lenverb;
177
178         /* protocol inspired by http://www.gir.fr/ocppjs/ocpp_srpc_spec.shtml */
179
180         /* the object must be an array of 4 or 5 elements */
181         if (!json_object_is_type(obj, json_type_array))
182                 goto error;
183         count = json_object_array_length(obj);
184         if (count < 4 || count > 5)
185                 goto error;
186
187         /* get the 5 elements: type id name request token */
188         type = json_object_array_get_idx(obj, 0);
189         id = json_object_array_get_idx(obj, 1);
190         name = json_object_array_get_idx(obj, 2);
191         req = json_object_array_get_idx(obj, 3);
192         token = json_object_array_get_idx(obj, 4);
193
194         /* check the types: int string string object string */
195         if (!json_object_is_type(type, json_type_int))
196                 goto error;
197         if (!json_object_is_type(id, json_type_string))
198                 goto error;
199         if (!json_object_is_type(name, json_type_string))
200                 goto error;
201         if (!json_object_is_type(req, json_type_object))
202                 goto error;
203         if (token != NULL && !json_object_is_type(token, json_type_string))
204                 goto error;
205
206         /* the type is only 2 */
207         num = json_object_get_int(type);
208         if (num != 2)
209                 goto error;
210
211         /* checks the api/verb structure of name */
212         api = json_object_get_string(name);
213         for (lenapi = 0 ; api[lenapi] && api[lenapi] != '/' ; lenapi++);
214         if (!lenapi || !api[lenapi])
215                 goto error;
216         verb = &api[lenapi+1];
217         for (lenverb = 0 ; verb[lenverb] && verb[lenverb] != '/' ; lenverb++);
218         if (!lenverb || verb[lenverb])
219                 goto error;
220
221         /* allocates the request data */
222         wsreq = malloc(sizeof *wsreq);
223         if (wsreq == NULL)
224                 goto error;
225
226         /* fill and record the request */
227         wsreq->aws = aws;
228         wsreq->id = json_object_get(id);
229         wsreq->name = json_object_get(name);
230         wsreq->token = json_object_get(token);
231         wsreq->request = json_object_get(req);
232         wsreq->next = aws->requests;
233         aws->requests = wsreq;
234         json_object_put(obj);
235
236         r.data = wsreq;
237         r.itf = &wsreq_itf;
238         afb_apis_call(r, aws->context, api, lenapi, verb, lenverb);
239         return 1;
240
241 error:
242         json_object_put(obj);
243         return 0;
244 }
245
246 static void aws_on_content(struct afb_ws *ws, int last, size_t size)
247 {
248         ssize_t rrc;
249         char buffer[8000];
250         struct json_object *obj;
251
252         json_tokener_reset(ws->tokener);
253         while(size) {
254                 rrc = websock_read(ws->ws, buffer,
255                                 size > sizeof buffer ? sizeof buffer : size);
256                 if (rrc < 0) {
257                         websock_close(ws->ws);
258                         return;
259                 }
260                 size -= (size_t)rrc;
261                 obj = json_tokener_parse_ex(ws->tokener, buffer, (int)rrc);
262                 if (obj != NULL) {
263                         if (!aws_handle_json(ws, obj)) {
264                                 websock_close(ws->ws);
265                                 return;
266                         }
267                 } else if (json_tokener_get_error(ws->tokener) != json_tokener_continue) {
268                         websock_close(ws->ws);
269                         return;
270                 }
271         }
272 }
273
274 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
275 {
276         struct afb_arg arg;
277         struct json_object *value;
278
279         if (json_object_object_get_ex(wsreq->request, name, &value)) {
280                 arg.name = name;
281                 arg.value = json_object_get_string(value);
282                 arg.size = strlen(arg.value);
283         } else {
284                 arg.name = NULL;
285                 arg.value = NULL;
286                 arg.size = 0;
287         }
288         arg.path = NULL;
289         return arg;
290 }
291
292 static void wsreq_iterate(struct afb_wsreq *wsreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure)
293 {
294         struct afb_arg arg;
295         struct json_object_iterator it = json_object_iter_begin(wsreq->request);
296         struct json_object_iterator end = json_object_iter_end(wsreq->request);
297
298         arg.size = 0;
299         arg.path = NULL;
300         while(!json_object_iter_equal(&it, &end)) {
301                 arg.name = json_object_iter_peek_name(&it);
302                 arg.value = json_object_get_string(json_object_iter_peek_value(&it));
303                 if (!iterator(closure, arg))
304                         break;
305                 json_object_iter_next(&it);
306         }
307 }
308
309 static int wsreq_session_create(struct afb_wsreq *wsreq)
310 {
311         struct AFB_clientCtx *context = wsreq->aws->context;
312         if (context->created)
313                 return 0;
314         return wsreq_session_check(wsreq, 1);
315 }
316
317 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
318 {
319         const char *token;
320         struct AFB_clientCtx *context = wsreq->aws->context;
321
322         if (wsreq->token == NULL)
323                 return 0;
324
325         token = json_object_get_string(wsreq->token);
326         if (token == NULL)
327                 return 0;
328
329         if (!ctxTokenCheck (context, token))
330                 return 0;
331
332         if (refresh) {
333                 ctxTokenNew (context);
334         }
335
336         return 1;
337 }
338
339 static void wsreq_session_close(struct afb_wsreq *wsreq)
340 {
341         struct AFB_clientCtx *context = wsreq->aws->context;
342         ctxClientClose(context);
343 }
344
345
346 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
347 {
348         json_object *root, *request, *reply;
349         const char *message;
350
351         /* builds the answering structure */
352         root = json_object_new_object();
353         json_object_object_add(root, "jtype", json_object_new_string("afb-reply"));
354         request = json_object_new_object();
355         json_object_object_add(root, "request", request);
356         json_object_object_add(request, "status", json_object_new_string(status));
357         if (info)
358                 json_object_object_add(request, "info", json_object_new_string(info));
359         if (resp)
360                 json_object_object_add(root, "response", resp);
361
362         /* make the reply */
363         reply = json_object_new_array();
364         json_object_array_add(reply, json_object_new_int(retcode));
365         json_object_array_add(reply, wsreq->id);
366         json_object_array_add(reply, root);
367         json_object_array_add(reply, json_object_new_string(wsreq->aws->context->token));
368
369         /* emits the reply */
370         message = json_object_to_json_string(reply);
371         websock_text(wsreq->aws->ws, message, strlen(message));
372         json_object_put(reply);
373
374         /* TODO eliminates the wsreq */
375 }
376
377 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
378 {
379         wsreq_reply(wsreq, 4, status, info, NULL);
380 }
381
382 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
383 {
384         wsreq_reply(wsreq, 3, "success", info, obj);
385 }
386