2 * Copyright 2016 IoT.bzh
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include "afb-ws-json.h"
30 #include "afb-msg-json.h"
32 #include "afb-req-itf.h"
35 static void aws_on_hangup(struct afb_ws_json *ws);
36 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size);
38 static struct afb_ws_itf aws_itf = {
39 .on_hangup = (void*)aws_on_hangup,
40 .on_text = (void*)aws_on_text
47 void (*cleanup)(void*);
48 void *cleanup_closure;
49 struct afb_wsreq *requests;
50 struct AFB_clientCtx *context;
51 struct json_tokener *tokener;
55 static void aws_send_event(struct afb_ws_json *ws, const char *event, struct json_object *object);
57 static const struct afb_event_listener_itf event_listener_itf = {
58 .send = (void*)aws_send_event
61 struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *cleanup_closure)
63 struct afb_ws_json *result;
66 assert(context != NULL);
68 result = malloc(sizeof * result);
72 result->cleanup = cleanup;
73 result->cleanup_closure = cleanup_closure;
74 result->requests = NULL;
75 result->context = ctxClientGet(context);
76 if (result->context == NULL)
79 result->tokener = json_tokener_new();
80 if (result->tokener == NULL)
83 result->ws = afb_ws_create(fd, &aws_itf, result);
84 if (result->ws == NULL)
87 if (0 > ctxClientEventListenerAdd(result->context, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = result }))
95 json_tokener_free(result->tokener);
97 ctxClientPut(result->context);
105 static void aws_on_hangup(struct afb_ws_json *ws)
107 ctxClientEventListenerRemove(ws->context, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = ws });
108 afb_ws_destroy(ws->ws);
109 json_tokener_free(ws->tokener);
110 if (ws->cleanup != NULL)
111 ws->cleanup(ws->cleanup_closure);
122 struct afb_ws_json *aws;
123 struct afb_wsreq *next;
137 struct json_object *root;
140 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
141 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
142 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
143 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
144 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
145 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size);
146 static int wsreq_session_create(struct afb_wsreq *wsreq);
147 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh);
148 static void wsreq_session_close(struct afb_wsreq *wsreq);
151 static const struct afb_req_itf wsreq_itf = {
152 .json = (void*)wsreq_json,
153 .get = (void*)wsreq_get,
154 .success = (void*)wsreq_success,
155 .fail = (void*)wsreq_fail,
156 .raw = (void*)wsreq_raw,
157 .send = (void*)wsreq_send,
158 .session_create = (void*)wsreq_session_create,
159 .session_check = (void*)wsreq_session_check,
160 .session_close = (void*)wsreq_session_close,
161 .context_get = (void*)afb_context_get,
162 .context_set = (void*)afb_context_set
165 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
175 while(pos < end && *pos == ' ') pos++;
176 if (pos == end) goto bad_header;
177 if (*pos++ != '[') goto bad_header;
179 /* scans code: 2|3|4 */
180 while(pos < end && *pos == ' ') pos++;
181 if (pos == end) goto bad_header;
183 case '2': r->code = CALL; break;
184 case '3': r->code = RETOK; break;
185 case '4': r->code = RETERR; break;
186 default: goto bad_header;
190 while(pos < end && *pos == ' ') pos++;
191 if (pos == end) goto bad_header;
192 if (*pos++ != ',') goto bad_header;
195 while(pos < end && *pos == ' ') pos++;
196 if (pos == end) goto bad_header;
197 if (*pos++ != '"') goto bad_header;
199 while(pos < end && *pos != '"') pos++;
200 if (pos == end) goto bad_header;
201 r->idlen = (size_t)(pos++ - r->id);
204 while(pos < end && *pos == ' ') pos++;
205 if (pos == end) goto bad_header;
206 if (*pos++ != ',') goto bad_header;
208 /* scans the method if needed */
209 if (r->code == CALL) {
211 while(pos < end && *pos == ' ') pos++;
212 if (pos == end) goto bad_header;
213 if (*pos++ != '"') goto bad_header;
217 while(pos < end && *pos != '"' && *pos != '/') pos++;
218 if (pos == end) goto bad_header;
219 if (*pos != '/') goto bad_header;
220 r->apilen = (size_t)(pos++ - r->api);
221 if (r->apilen && r->api[r->apilen - 1] == '\\')
226 while(pos < end && *pos != '"') pos++;
227 if (pos == end) goto bad_header;
228 r->verblen = (size_t)(pos++ - r->verb);
231 while(pos < end && *pos == ' ') pos++;
232 if (pos == end) goto bad_header;
233 if (*pos++ != ',') goto bad_header;
237 while(pos < end && *pos == ' ') pos++;
238 if (pos == end) goto bad_header;
241 while (pos < end && (aux != 0 || (*pos != ',' && *pos != ']'))) {
242 if (pos == end) goto bad_header;
244 case '{': case '[': aux++; break;
245 case '}': case ']': if (!aux--) goto bad_header; break;
248 pos += 1 + (*pos == '\\');
249 } while(pos < end && *pos != '"');
255 if (pos > end) goto bad_header;
256 if (pos == end && aux != 0) goto bad_header;
258 r->objlen = (size_t)(pos++ - r->obj);
259 while (r->objlen && r->obj[r->objlen - 1] == ' ')
262 /* scan the token (if any) */
264 /* scans token: "token" */
265 while(pos < end && *pos == ' ') pos++;
266 if (pos == end) goto bad_header;
267 if (*pos++ != '"') goto bad_header;
269 while(pos < end && *pos != '"') pos++;
270 if (pos == end) goto bad_header;
271 r->toklen = (size_t)(pos++ - r->tok);
272 while(pos < end && *pos == ' ') pos++;
273 if (pos == end) goto bad_header;
278 if (c != ']') goto bad_header;
279 while(pos < end && *pos == ' ') pos++;
280 if (pos != end) goto bad_header;
291 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
294 struct afb_wsreq *wsreq;
297 wsreq = calloc(1, sizeof *wsreq);
302 if (!aws_wsreq_parse(wsreq, text, size))
305 /* fill and record the request */
307 wsreq->next = ws->requests;
308 ws->requests = wsreq;
310 r.req_closure = wsreq;
312 afb_apis_call(r, ws->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
319 afb_ws_close(ws->ws, 1008, NULL);
323 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
325 struct json_object *root = wsreq->root;
327 json_tokener_reset(wsreq->aws->tokener);
328 root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
330 /* lazy error detection of json request. Is it to improve? */
331 root = json_object_new_string_len(wsreq->obj, (int)wsreq->objlen);
338 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
341 struct json_object *value, *root;
343 root = wsreq_json(wsreq);
344 if (json_object_object_get_ex(root, name, &value)) {
346 arg.value = json_object_get_string(value);
355 static int wsreq_session_create(struct afb_wsreq *wsreq)
357 struct AFB_clientCtx *context = wsreq->aws->context;
358 if (context->created)
360 return wsreq_session_check(wsreq, 1);
363 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
365 struct AFB_clientCtx *context = wsreq->aws->context;
367 if (wsreq->tok == NULL)
370 if (!ctxTokenCheckLen (context, wsreq->tok, wsreq->toklen))
374 ctxTokenNew (context);
380 static void wsreq_session_close(struct afb_wsreq *wsreq)
382 struct AFB_clientCtx *context = wsreq->aws->context;
383 ctxClientClose(context);
386 static void aws_emit(struct afb_ws_json *aws, int code, const char *id, size_t idlen, struct json_object *data)
392 /* pack the message */
393 msg = json_object_new_array();
394 json_object_array_add(msg, json_object_new_int(code));
395 json_object_array_add(msg, json_object_new_string_len(id, (int)idlen));
396 json_object_array_add(msg, data);
397 token = aws->context->token;
399 json_object_array_add(msg, json_object_new_string(token));
401 /* emits the reply */
402 txt = json_object_to_json_string(msg);
403 afb_ws_text(aws->ws, txt, strlen(txt));
404 json_object_put(msg);
407 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
409 aws_emit(wsreq->aws, retcode, wsreq->id, wsreq->idlen, afb_msg_json_reply(status, info, resp, NULL, NULL));
410 /* TODO eliminates the wsreq */
413 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
415 wsreq_reply(wsreq, RETERR, status, info, NULL);
418 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
420 wsreq_reply(wsreq, RETOK, "success", info, obj);
423 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
425 *size = wsreq->objlen;
429 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size)
431 afb_ws_text(wsreq->aws->ws, buffer, size);
434 static void aws_send_event(struct afb_ws_json *aws, const char *event, struct json_object *object)
436 aws_emit(aws, EVENT, event, strlen(event), afb_msg_json_event(event, object));