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_sender_itf event_sender_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 > ctxClientEventSenderAdd(result->context, (struct afb_event_sender){ .itf = &event_sender_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 ctxClientEventSenderRemove(ws->context, (struct afb_event_sender){ .itf = &event_sender_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
166 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
176 while(pos < end && *pos == ' ') pos++;
177 if (pos == end) goto bad_header;
178 if (*pos++ != '[') goto bad_header;
180 /* scans code: 2|3|4 */
181 while(pos < end && *pos == ' ') pos++;
182 if (pos == end) goto bad_header;
184 case '2': r->code = CALL; break;
185 case '3': r->code = RETOK; break;
186 case '4': r->code = RETERR; break;
187 default: goto bad_header;
191 while(pos < end && *pos == ' ') pos++;
192 if (pos == end) goto bad_header;
193 if (*pos++ != ',') goto bad_header;
196 while(pos < end && *pos == ' ') pos++;
197 if (pos == end) goto bad_header;
198 if (*pos++ != '"') goto bad_header;
200 while(pos < end && *pos != '"') pos++;
201 if (pos == end) goto bad_header;
202 r->idlen = (size_t)(pos++ - r->id);
205 while(pos < end && *pos == ' ') pos++;
206 if (pos == end) goto bad_header;
207 if (*pos++ != ',') goto bad_header;
209 /* scans the method if needed */
210 if (r->code == CALL) {
212 while(pos < end && *pos == ' ') pos++;
213 if (pos == end) goto bad_header;
214 if (*pos++ != '"') goto bad_header;
218 while(pos < end && *pos != '"' && *pos != '/') pos++;
219 if (pos == end) goto bad_header;
220 if (*pos != '/') goto bad_header;
221 r->apilen = (size_t)(pos++ - r->api);
222 if (r->apilen && r->api[r->apilen - 1] == '\\')
227 while(pos < end && *pos != '"') pos++;
228 if (pos == end) goto bad_header;
229 r->verblen = (size_t)(pos++ - r->verb);
232 while(pos < end && *pos == ' ') pos++;
233 if (pos == end) goto bad_header;
234 if (*pos++ != ',') goto bad_header;
238 while(pos < end && *pos == ' ') pos++;
239 if (pos == end) goto bad_header;
242 while (pos < end && (aux != 0 || (*pos != ',' && *pos != ']'))) {
243 if (pos == end) goto bad_header;
245 case '{': case '[': aux++; break;
246 case '}': case ']': if (!aux--) goto bad_header; break;
249 pos += 1 + (*pos == '\\');
250 } while(pos < end && *pos != '"');
256 if (pos > end) goto bad_header;
257 if (pos == end && aux != 0) goto bad_header;
259 r->objlen = (size_t)(pos++ - r->obj);
260 while (r->objlen && r->obj[r->objlen - 1] == ' ')
263 /* scan the token (if any) */
265 /* scans token: "token" */
266 while(pos < end && *pos == ' ') pos++;
267 if (pos == end) goto bad_header;
268 if (*pos++ != '"') goto bad_header;
270 while(pos < end && *pos != '"') pos++;
271 if (pos == end) goto bad_header;
272 r->toklen = (size_t)(pos++ - r->tok);
273 while(pos < end && *pos == ' ') pos++;
274 if (pos == end) goto bad_header;
279 if (c != ']') goto bad_header;
280 while(pos < end && *pos == ' ') pos++;
281 if (pos != end) goto bad_header;
292 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
295 struct afb_wsreq *wsreq;
298 wsreq = calloc(1, sizeof *wsreq);
303 if (!aws_wsreq_parse(wsreq, text, size))
306 /* fill and record the request */
308 wsreq->next = ws->requests;
309 ws->requests = wsreq;
311 r.req_closure = wsreq;
313 afb_apis_call(r, ws->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
320 afb_ws_close(ws->ws, 1008, NULL);
324 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
326 struct json_object *root = wsreq->root;
328 json_tokener_reset(wsreq->aws->tokener);
329 root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
331 /* lazy error detection of json request. Is it to improve? */
332 root = json_object_new_string_len(wsreq->obj, (int)wsreq->objlen);
339 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
342 struct json_object *value, *root;
344 root = wsreq_json(wsreq);
345 if (json_object_object_get_ex(root, name, &value)) {
347 arg.value = json_object_get_string(value);
356 static int wsreq_session_create(struct afb_wsreq *wsreq)
358 struct AFB_clientCtx *context = wsreq->aws->context;
359 if (context->created)
361 return wsreq_session_check(wsreq, 1);
364 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
366 struct AFB_clientCtx *context = wsreq->aws->context;
368 if (wsreq->tok == NULL)
371 if (!ctxTokenCheckLen (context, wsreq->tok, wsreq->toklen))
375 ctxTokenNew (context);
381 static void wsreq_session_close(struct afb_wsreq *wsreq)
383 struct AFB_clientCtx *context = wsreq->aws->context;
384 ctxClientClose(context);
387 static void aws_emit(struct afb_ws_json *aws, int code, const char *id, size_t idlen, struct json_object *data)
393 /* pack the message */
394 msg = json_object_new_array();
395 json_object_array_add(msg, json_object_new_int(code));
396 json_object_array_add(msg, json_object_new_string_len(id, (int)idlen));
397 json_object_array_add(msg, data);
398 token = aws->context->token;
400 json_object_array_add(msg, json_object_new_string(token));
402 /* emits the reply */
403 txt = json_object_to_json_string(msg);
404 afb_ws_text(aws->ws, txt, strlen(txt));
405 json_object_put(msg);
408 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
410 aws_emit(wsreq->aws, retcode, wsreq->id, wsreq->idlen, afb_msg_json_reply(status, info, resp, NULL, NULL));
411 /* TODO eliminates the wsreq */
414 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
416 wsreq_reply(wsreq, RETERR, status, info, NULL);
419 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
421 wsreq_reply(wsreq, RETOK, "success", info, obj);
424 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
426 *size = wsreq->objlen;
430 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size)
432 afb_ws_text(wsreq->aws->ws, buffer, size);
435 static void aws_send_event(struct afb_ws_json *aws, const char *event, struct json_object *object)
437 aws_emit(aws, EVENT, event, strlen(event), afb_msg_json_event(event, object));