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"
31 #include "afb-req-itf.h"
34 static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size);
35 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size);
37 static struct afb_ws_itf aws_itf = {
38 .on_close = (void*)aws_on_close,
39 .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;
56 static void aws_send_event(struct afb_ws_json *ws, const char *event, struct json_object *object);
58 static const struct afb_event_sender_itf event_sender_itf = {
59 .send = (void*)aws_send_event
62 struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *cleanup_closure)
64 struct afb_ws_json *result;
67 assert(context != NULL);
69 result = malloc(sizeof * result);
73 result->cleanup = cleanup;
74 result->cleanup_closure = cleanup_closure;
75 result->requests = NULL;
76 result->context = ctxClientGet(context);
77 if (result->context == 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 > ctxClientEventSenderAdd(result->context, (struct afb_event_sender){ .itf = &event_sender_itf, .closure = result }))
96 json_tokener_free(result->tokener);
98 ctxClientPut(result->context);
106 static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size)
108 /* do nothing but free the text */
119 struct afb_ws_json *aws;
120 struct afb_wsreq *next;
134 struct json_object *root;
137 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
138 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
139 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
140 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
141 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
142 static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size);
143 static int wsreq_session_create(struct afb_wsreq *wsreq);
144 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh);
145 static void wsreq_session_close(struct afb_wsreq *wsreq);
148 static const struct afb_req_itf wsreq_itf = {
149 .json = (void*)wsreq_json,
150 .get = (void*)wsreq_get,
151 .success = (void*)wsreq_success,
152 .fail = (void*)wsreq_fail,
153 .raw = (void*)wsreq_raw,
154 .send = (void*)wsreq_send,
155 .session_create = (void*)wsreq_session_create,
156 .session_check = (void*)wsreq_session_check,
157 .session_close = (void*)wsreq_session_close,
158 .context_get = (void*)afb_context_get,
159 .context_set = (void*)afb_context_set
163 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
173 while(pos < end && *pos == ' ') pos++;
174 if (pos == end) goto bad_header;
175 if (*pos++ != '[') goto bad_header;
177 /* scans code: 2|3|4 */
178 while(pos < end && *pos == ' ') pos++;
179 if (pos == end) goto bad_header;
181 case '2': r->code = CALL; break;
182 case '3': r->code = RETOK; break;
183 case '4': r->code = RETERR; break;
184 default: goto bad_header;
188 while(pos < end && *pos == ' ') pos++;
189 if (pos == end) goto bad_header;
190 if (*pos++ != ',') goto bad_header;
193 while(pos < end && *pos == ' ') pos++;
194 if (pos == end) goto bad_header;
195 if (*pos++ != '"') goto bad_header;
197 while(pos < end && *pos != '"') pos++;
198 if (pos == end) goto bad_header;
199 r->idlen = (size_t)(pos++ - r->id);
202 while(pos < end && *pos == ' ') pos++;
203 if (pos == end) goto bad_header;
204 if (*pos++ != ',') goto bad_header;
206 /* scans the method if needed */
207 if (r->code == CALL) {
209 while(pos < end && *pos == ' ') pos++;
210 if (pos == end) goto bad_header;
211 if (*pos++ != '"') goto bad_header;
215 while(pos < end && *pos != '"' && *pos != '/') pos++;
216 if (pos == end) goto bad_header;
217 if (*pos != '/') goto bad_header;
218 r->apilen = (size_t)(pos++ - r->api);
219 if (r->apilen && r->api[r->apilen - 1] == '\\')
224 while(pos < end && *pos != '"') pos++;
225 if (pos == end) goto bad_header;
226 r->verblen = (size_t)(pos++ - r->verb);
229 while(pos < end && *pos == ' ') pos++;
230 if (pos == end) goto bad_header;
231 if (*pos++ != ',') goto bad_header;
235 while(pos < end && *pos == ' ') pos++;
236 if (pos == end) goto bad_header;
239 while (pos < end && (aux != 0 || (*pos != ',' && *pos != ']'))) {
240 if (pos == end) goto bad_header;
242 case '{': case '[': aux++; break;
243 case '}': case ']': if (!aux--) goto bad_header; break;
246 pos += 1 + (*pos == '\\');
247 } while(pos < end && *pos != '"');
253 if (pos > end) goto bad_header;
254 if (pos == end && aux != 0) goto bad_header;
256 r->objlen = (size_t)(pos++ - r->obj);
257 while (r->objlen && r->obj[r->objlen - 1] == ' ')
260 /* scan the token (if any) */
262 /* scans token: "token" */
263 while(pos < end && *pos == ' ') pos++;
264 if (pos == end) goto bad_header;
265 if (*pos++ != '"') goto bad_header;
267 while(pos < end && *pos != '"') pos++;
268 if (pos == end) goto bad_header;
269 r->toklen = (size_t)(pos++ - r->tok);
270 while(pos < end && *pos == ' ') pos++;
271 if (pos == end) goto bad_header;
276 if (c != ']') goto bad_header;
277 while(pos < end && *pos == ' ') pos++;
278 if (pos != end) goto bad_header;
283 fprintf(stderr, "\n\nONTEXT([%d, %.*s, %.*s/%.*s, %.*s, %.*s])\n\n",
285 (int)r->idlen, r->id,
286 (int)r->apilen, r->api,
287 (int)r->verblen, r->verb,
288 (int)r->objlen, r->obj,
289 (int)r->toklen, r->tok
297 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
300 struct afb_wsreq *wsreq;
303 wsreq = calloc(1, sizeof *wsreq);
308 if (!aws_wsreq_parse(wsreq, text, size))
311 /* fill and record the request */
313 wsreq->next = ws->requests;
314 ws->requests = wsreq;
316 r.req_closure = wsreq;
318 afb_apis_call(r, ws->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
325 afb_ws_close(ws->ws, 1008);
329 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
331 struct json_object *root = wsreq->root;
333 json_tokener_reset(wsreq->aws->tokener);
334 root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
336 /* lazy error detection of json request. Is it to improve? */
337 root = json_object_new_string_len(wsreq->obj, (int)wsreq->objlen);
344 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
347 struct json_object *value, *root;
349 root = wsreq_json(wsreq);
350 if (json_object_object_get_ex(root, name, &value)) {
352 arg.value = json_object_get_string(value);
361 static int wsreq_session_create(struct afb_wsreq *wsreq)
363 struct AFB_clientCtx *context = wsreq->aws->context;
364 if (context->created)
366 return wsreq_session_check(wsreq, 1);
369 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
371 struct AFB_clientCtx *context = wsreq->aws->context;
373 if (wsreq->tok == NULL)
376 if (!ctxTokenCheckLen (context, wsreq->tok, wsreq->toklen))
380 ctxTokenNew (context);
386 static void wsreq_session_close(struct afb_wsreq *wsreq)
388 struct AFB_clientCtx *context = wsreq->aws->context;
389 ctxClientClose(context);
393 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
395 json_object *root, *request, *reply;
398 /* builds the answering structure */
399 root = json_object_new_object();
400 json_object_object_add(root, "jtype", json_object_new_string("afb-reply"));
401 request = json_object_new_object();
402 json_object_object_add(root, "request", request);
403 json_object_object_add(request, "status", json_object_new_string(status));
405 json_object_object_add(request, "info", json_object_new_string(info));
407 json_object_object_add(root, "response", resp);
410 reply = json_object_new_array();
411 json_object_array_add(reply, json_object_new_int(retcode));
412 json_object_array_add(reply, json_object_new_string_len(wsreq->id, (int)wsreq->idlen));
413 json_object_array_add(reply, root);
414 json_object_array_add(reply, json_object_new_string(wsreq->aws->context->token));
416 /* emits the reply */
417 message = json_object_to_json_string(reply);
418 afb_ws_text(wsreq->aws->ws, message, strlen(message));
419 json_object_put(reply);
421 /* TODO eliminates the wsreq */
424 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
426 wsreq_reply(wsreq, RETERR, status, info, NULL);
429 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
431 wsreq_reply(wsreq, RETOK, "success", info, obj);
434 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
436 *size = wsreq->objlen;
440 static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size)
442 afb_ws_text(wsreq->aws->ws, buffer, size);
445 static void aws_send_event(struct afb_ws_json *aws, const char *event, struct json_object *object)
447 json_object *root, *reply;
450 /* builds the answering structure */
451 root = json_object_new_object();
452 json_object_object_add(root, "jtype", json_object_new_string("afb-event"));
453 json_object_object_add(root, "event", json_object_new_string(event));
455 json_object_object_add(root, "data", object);
458 reply = json_object_new_array();
459 json_object_array_add(reply, json_object_new_int(EVENT));
460 json_object_array_add(reply, json_object_new_string(event));
461 json_object_array_add(reply, root);
462 json_object_array_add(reply, json_object_new_string(aws->context->token));
464 /* emits the reply */
465 message = json_object_to_json_string(reply);
466 afb_ws_text(aws->ws, message, strlen(message));
467 json_object_put(reply);