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.
26 #include <json-c/json.h>
29 #include "afb-ws-json.h"
30 #include "afb-msg-json.h"
32 #include "afb-req-itf.h"
34 #include "afb-context.h"
36 static void aws_on_hangup(struct afb_ws_json *ws);
37 static void aws_on_text(struct afb_ws_json *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;
56 static void aws_send_event(struct afb_ws_json *ws, const char *event, struct json_object *object);
58 static const struct afb_event_listener_itf event_listener_itf = {
59 .send = (void*)aws_send_event
62 struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *session, void (*cleanup)(void*), void *cleanup_closure)
64 struct afb_ws_json *result;
67 assert(session != NULL);
69 result = malloc(sizeof * result);
73 result->cleanup = cleanup;
74 result->cleanup_closure = cleanup_closure;
75 result->requests = NULL;
76 result->session = ctxClientAddRef(session);
77 if (result->session == NULL)
80 result->tokener = json_tokener_new();
81 if (result->tokener == NULL)
84 result->ws = afb_ws_create(fd, &aws_itf, result);
85 if (result->ws == NULL)
88 if (0 > ctxClientEventListenerAdd(result->session, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = result }))
94 afb_ws_destroy(result->ws);
96 json_tokener_free(result->tokener);
98 ctxClientUnref(result->session);
106 static void aws_on_hangup(struct afb_ws_json *ws)
108 ctxClientEventListenerRemove(ws->session, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = ws });
109 afb_ws_destroy(ws->ws);
110 json_tokener_free(ws->tokener);
111 if (ws->cleanup != NULL)
112 ws->cleanup(ws->cleanup_closure);
113 ctxClientUnref(ws->session);
124 struct afb_context context;
126 struct afb_ws_json *aws;
127 struct afb_wsreq *next;
141 struct json_object *root;
144 static void wsreq_addref(struct afb_wsreq *wsreq);
145 static void wsreq_unref(struct afb_wsreq *wsreq);
146 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
147 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
148 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
149 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
150 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
151 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size);
154 static const struct afb_req_itf wsreq_itf = {
155 .json = (void*)wsreq_json,
156 .get = (void*)wsreq_get,
157 .success = (void*)wsreq_success,
158 .fail = (void*)wsreq_fail,
159 .raw = (void*)wsreq_raw,
160 .send = (void*)wsreq_send,
161 .context_get = (void*)afb_context_get,
162 .context_set = (void*)afb_context_set,
163 .addref = (void*)wsreq_addref,
164 .unref = (void*)wsreq_unref
167 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
177 while(pos < end && *pos == ' ') pos++;
178 if (pos == end) goto bad_header;
179 if (*pos++ != '[') goto bad_header;
181 /* scans code: 2|3|4 */
182 while(pos < end && *pos == ' ') pos++;
183 if (pos == end) goto bad_header;
185 case '2': r->code = CALL; break;
186 case '3': r->code = RETOK; break;
187 case '4': r->code = RETERR; break;
188 default: goto bad_header;
192 while(pos < end && *pos == ' ') pos++;
193 if (pos == end) goto bad_header;
194 if (*pos++ != ',') goto bad_header;
197 while(pos < end && *pos == ' ') pos++;
198 if (pos == end) goto bad_header;
199 if (*pos++ != '"') goto bad_header;
201 while(pos < end && *pos != '"') pos++;
202 if (pos == end) goto bad_header;
203 r->idlen = (size_t)(pos++ - r->id);
206 while(pos < end && *pos == ' ') pos++;
207 if (pos == end) goto bad_header;
208 if (*pos++ != ',') goto bad_header;
210 /* scans the method if needed */
211 if (r->code == CALL) {
213 while(pos < end && *pos == ' ') pos++;
214 if (pos == end) goto bad_header;
215 if (*pos++ != '"') goto bad_header;
219 while(pos < end && *pos != '"' && *pos != '/') pos++;
220 if (pos == end) goto bad_header;
221 if (*pos != '/') goto bad_header;
222 r->apilen = (size_t)(pos++ - r->api);
223 if (r->apilen && r->api[r->apilen - 1] == '\\')
228 while(pos < end && *pos != '"') pos++;
229 if (pos == end) goto bad_header;
230 r->verblen = (size_t)(pos++ - r->verb);
233 while(pos < end && *pos == ' ') pos++;
234 if (pos == end) goto bad_header;
235 if (*pos++ != ',') goto bad_header;
239 while(pos < end && *pos == ' ') pos++;
240 if (pos == end) goto bad_header;
243 while (pos < end && (aux != 0 || (*pos != ',' && *pos != ']'))) {
244 if (pos == end) goto bad_header;
246 case '{': case '[': aux++; break;
247 case '}': case ']': if (!aux--) goto bad_header; break;
250 pos += 1 + (*pos == '\\');
251 } while(pos < end && *pos != '"');
257 if (pos > end) goto bad_header;
258 if (pos == end && aux != 0) goto bad_header;
260 r->objlen = (size_t)(pos++ - r->obj);
261 while (r->objlen && r->obj[r->objlen - 1] == ' ')
264 /* scan the token (if any) */
266 /* scans token: "token" */
267 while(pos < end && *pos == ' ') pos++;
268 if (pos == end) goto bad_header;
269 if (*pos++ != '"') goto bad_header;
271 while(pos < end && *pos != '"') pos++;
272 if (pos == end) goto bad_header;
273 r->toklen = (size_t)(pos++ - r->tok);
274 while(pos < end && *pos == ' ') pos++;
275 if (pos == end) goto bad_header;
280 if (c != ']') goto bad_header;
281 while(pos < end && *pos == ' ') pos++;
282 if (pos != end) goto bad_header;
293 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
296 struct afb_wsreq *wsreq;
299 wsreq = calloc(1, sizeof *wsreq);
304 if (!aws_wsreq_parse(wsreq, text, size))
307 /* fill and record the request */
308 if (wsreq->tok != NULL)
309 wsreq->tok[wsreq->toklen] = 0;
310 afb_context_init(&wsreq->context, ws->session, wsreq->tok);
311 if (!wsreq->context.invalidated)
312 wsreq->context.validated = 1;
315 wsreq->next = ws->requests;
316 ws->requests = wsreq;
320 afb_apis_call(r, &wsreq->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
328 afb_ws_close(ws->ws, 1008, NULL);
332 static void wsreq_addref(struct afb_wsreq *wsreq)
337 static void wsreq_unref(struct afb_wsreq *wsreq)
339 if (--wsreq->refcount == 0) {
340 afb_context_disconnect(&wsreq->context);
346 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
348 struct json_object *root = wsreq->root;
350 json_tokener_reset(wsreq->aws->tokener);
351 root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
353 /* lazy error detection of json request. Is it to improve? */
354 root = json_object_new_string_len(wsreq->obj, (int)wsreq->objlen);
361 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
364 struct json_object *value, *root;
366 root = wsreq_json(wsreq);
367 if (json_object_object_get_ex(root, name, &value)) {
369 arg.value = json_object_get_string(value);
378 static void aws_emit(struct afb_ws_json *aws, int code, const char *id, size_t idlen, struct json_object *data, const char *token)
383 /* pack the message */
384 msg = json_object_new_array();
385 json_object_array_add(msg, json_object_new_int(code));
386 json_object_array_add(msg, json_object_new_string_len(id, (int)idlen));
387 json_object_array_add(msg, data);
389 json_object_array_add(msg, json_object_new_string(token));
391 /* emits the reply */
392 txt = json_object_to_json_string(msg);
393 afb_ws_text(aws->ws, txt, strlen(txt));
394 json_object_put(msg);
397 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
399 const char *token = afb_context_sent_token(&wsreq->context);
400 aws_emit(wsreq->aws, retcode, wsreq->id, wsreq->idlen, afb_msg_json_reply(status, info, resp, token, NULL), token);
403 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
405 wsreq_reply(wsreq, RETERR, status, info, NULL);
408 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
410 wsreq_reply(wsreq, RETOK, "success", info, obj);
413 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
415 *size = wsreq->objlen;
419 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size)
421 afb_ws_text(wsreq->aws->ws, buffer, size);
424 static void aws_send_event(struct afb_ws_json *aws, const char *event, struct json_object *object)
426 aws_emit(aws, EVENT, event, strlen(event), afb_msg_json_event(event, object), NULL);