2 * Copyright (C) 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.
26 #include <json-c/json.h>
29 #include "afb-ws-json1.h"
30 #include "afb-msg-json.h"
32 #include <afb/afb-req-itf.h>
34 #include "afb-context.h"
36 static void aws_on_hangup(struct afb_ws_json1 *ws);
37 static void aws_on_text(struct afb_ws_json1 *ws, char *text, size_t size);
39 static struct afb_ws_itf aws_itf = {
40 .on_hangup = (void*)aws_on_hangup,
41 .on_text = (void*)aws_on_text
48 void (*cleanup)(void*);
49 void *cleanup_closure;
50 struct afb_wsreq *requests;
51 struct AFB_clientCtx *session;
52 struct json_tokener *tokener;
57 static void aws_send_event(struct afb_ws_json1 *ws, const char *event, struct json_object *object);
59 static const struct afb_event_listener_itf event_listener_itf = {
60 .send = (void*)aws_send_event,
64 static inline struct afb_event_listener listener_for(struct afb_ws_json1 *aws)
66 return (struct afb_event_listener){ .itf = &event_listener_itf, .closure = aws };
69 struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
71 struct afb_ws_json1 *result;
74 assert(context != NULL);
76 result = malloc(sizeof * result);
80 result->cleanup = cleanup;
81 result->cleanup_closure = cleanup_closure;
82 result->requests = NULL;
83 result->session = ctxClientAddRef(context->session);
84 result->new_session = context->created != 0;
85 if (result->session == NULL)
88 result->tokener = json_tokener_new();
89 if (result->tokener == NULL)
92 result->ws = afb_ws_create(fd, &aws_itf, result);
93 if (result->ws == NULL)
96 if (0 > ctxClientEventListenerAdd(result->session, listener_for(result)))
102 afb_ws_destroy(result->ws);
104 json_tokener_free(result->tokener);
106 ctxClientUnref(result->session);
114 static void aws_on_hangup(struct afb_ws_json1 *ws)
116 ctxClientEventListenerRemove(ws->session, listener_for(ws));
117 afb_ws_destroy(ws->ws);
118 json_tokener_free(ws->tokener);
119 if (ws->cleanup != NULL)
120 ws->cleanup(ws->cleanup_closure);
121 ctxClientUnref(ws->session);
133 * CAUTION: 'context' field should be the first because there
134 * is an implicit convertion to struct afb_context
136 struct afb_context context;
138 struct afb_ws_json1 *aws;
139 struct afb_wsreq *next;
153 struct json_object *root;
156 static void wsreq_addref(struct afb_wsreq *wsreq);
157 static void wsreq_unref(struct afb_wsreq *wsreq);
158 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
159 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
160 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
161 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
162 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
163 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size);
166 static const struct afb_req_itf wsreq_itf = {
167 .json = (void*)wsreq_json,
168 .get = (void*)wsreq_get,
169 .success = (void*)wsreq_success,
170 .fail = (void*)wsreq_fail,
171 .raw = (void*)wsreq_raw,
172 .send = (void*)wsreq_send,
173 .context_get = (void*)afb_context_get,
174 .context_set = (void*)afb_context_set,
175 .addref = (void*)wsreq_addref,
176 .unref = (void*)wsreq_unref,
177 .session_close = (void*)afb_context_close,
178 .session_set_LOA = (void*)afb_context_change_loa
181 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
191 while(pos < end && *pos == ' ') pos++;
192 if (pos == end) goto bad_header;
193 if (*pos++ != '[') goto bad_header;
195 /* scans code: 2|3|4 */
196 while(pos < end && *pos == ' ') pos++;
197 if (pos == end) goto bad_header;
199 case '2': r->code = CALL; break;
200 case '3': r->code = RETOK; break;
201 case '4': r->code = RETERR; break;
202 default: goto bad_header;
206 while(pos < end && *pos == ' ') pos++;
207 if (pos == end) goto bad_header;
208 if (*pos++ != ',') goto bad_header;
211 while(pos < end && *pos == ' ') pos++;
212 if (pos == end) goto bad_header;
213 if (*pos++ != '"') goto bad_header;
215 while(pos < end && *pos != '"') pos++;
216 if (pos == end) goto bad_header;
217 r->idlen = (size_t)(pos++ - r->id);
220 while(pos < end && *pos == ' ') pos++;
221 if (pos == end) goto bad_header;
222 if (*pos++ != ',') goto bad_header;
224 /* scans the method if needed */
225 if (r->code == CALL) {
227 while(pos < end && *pos == ' ') pos++;
228 if (pos == end) goto bad_header;
229 if (*pos++ != '"') goto bad_header;
233 while(pos < end && *pos != '"' && *pos != '/') pos++;
234 if (pos == end) goto bad_header;
235 if (*pos != '/') goto bad_header;
236 r->apilen = (size_t)(pos++ - r->api);
237 if (r->apilen && r->api[r->apilen - 1] == '\\')
242 while(pos < end && *pos != '"') pos++;
243 if (pos == end) goto bad_header;
244 r->verblen = (size_t)(pos++ - r->verb);
247 while(pos < end && *pos == ' ') pos++;
248 if (pos == end) goto bad_header;
249 if (*pos++ != ',') goto bad_header;
253 while(pos < end && *pos == ' ') pos++;
254 if (pos == end) goto bad_header;
257 while (pos < end && (aux != 0 || (*pos != ',' && *pos != ']'))) {
258 if (pos == end) goto bad_header;
260 case '{': case '[': aux++; break;
261 case '}': case ']': if (!aux--) goto bad_header; break;
264 pos += 1 + (*pos == '\\');
265 } while(pos < end && *pos != '"');
271 if (pos > end) goto bad_header;
272 if (pos == end && aux != 0) goto bad_header;
274 r->objlen = (size_t)(pos++ - r->obj);
275 while (r->objlen && r->obj[r->objlen - 1] == ' ')
278 /* scan the token (if any) */
280 /* scans token: "token" */
281 while(pos < end && *pos == ' ') pos++;
282 if (pos == end) goto bad_header;
283 if (*pos++ != '"') goto bad_header;
285 while(pos < end && *pos != '"') pos++;
286 if (pos == end) goto bad_header;
287 r->toklen = (size_t)(pos++ - r->tok);
288 while(pos < end && *pos == ' ') pos++;
289 if (pos == end) goto bad_header;
294 if (c != ']') goto bad_header;
295 while(pos < end && *pos == ' ') pos++;
296 if (pos != end) goto bad_header;
307 static void aws_on_text(struct afb_ws_json1 *ws, char *text, size_t size)
310 struct afb_wsreq *wsreq;
313 wsreq = calloc(1, sizeof *wsreq);
318 if (!aws_wsreq_parse(wsreq, text, size))
321 /* fill and record the request */
322 if (wsreq->tok != NULL)
323 wsreq->tok[wsreq->toklen] = 0;
324 afb_context_init(&wsreq->context, ws->session, wsreq->tok);
325 if (!wsreq->context.invalidated)
326 wsreq->context.validated = 1;
327 if (ws->new_session != 0) {
328 wsreq->context.created = 1;
333 wsreq->next = ws->requests;
334 ws->requests = wsreq;
338 afb_apis_call(r, &wsreq->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
346 afb_ws_close(ws->ws, 1008, NULL);
350 static void wsreq_addref(struct afb_wsreq *wsreq)
355 static void wsreq_unref(struct afb_wsreq *wsreq)
357 if (--wsreq->refcount == 0) {
358 struct afb_wsreq **prv = &wsreq->aws->requests;
359 while(*prv != NULL) {
366 afb_context_disconnect(&wsreq->context);
367 json_object_put(wsreq->root);
373 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
375 struct json_object *root = wsreq->root;
377 json_tokener_reset(wsreq->aws->tokener);
378 root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
380 /* lazy error detection of json request. Is it to improve? */
381 root = json_object_new_string_len(wsreq->obj, (int)wsreq->objlen);
388 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
391 struct json_object *value, *root;
393 root = wsreq_json(wsreq);
394 if (json_object_object_get_ex(root, name, &value)) {
396 arg.value = json_object_get_string(value);
405 static void aws_emit(struct afb_ws_json1 *aws, int code, const char *id, size_t idlen, struct json_object *data, const char *token)
410 /* pack the message */
411 msg = json_object_new_array();
412 json_object_array_add(msg, json_object_new_int(code));
413 json_object_array_add(msg, json_object_new_string_len(id, (int)idlen));
414 json_object_array_add(msg, data);
416 json_object_array_add(msg, json_object_new_string(token));
418 /* emits the reply */
419 txt = json_object_to_json_string_ext(msg, JSON_C_TO_STRING_PLAIN);
420 afb_ws_text(aws->ws, txt, strlen(txt));
421 json_object_put(msg);
424 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
426 struct json_object *reply = afb_msg_json_reply(status, info, resp, &wsreq->context, NULL);
427 aws_emit(wsreq->aws, retcode, wsreq->id, wsreq->idlen, reply, afb_context_sent_token(&wsreq->context));
430 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
432 wsreq_reply(wsreq, RETERR, status, info, NULL);
435 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
437 wsreq_reply(wsreq, RETOK, "success", info, obj);
440 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
442 *size = wsreq->objlen;
446 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size)
448 afb_ws_text(wsreq->aws->ws, buffer, size);
451 static void aws_send_event(struct afb_ws_json1 *aws, const char *event, struct json_object *object)
453 aws_emit(aws, EVENT, event, strlen(event), afb_msg_json_event(event, object), NULL);