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;
55 struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure)
57 struct afb_ws_json *result;
60 assert(context != NULL);
62 result = malloc(sizeof * result);
66 result->cleanup = cleanup;
67 result->cleanup_closure = closure;
68 result->requests = NULL;
69 result->context = ctxClientGet(context);
70 if (result->context == NULL)
73 result->tokener = json_tokener_new();
74 if (result->tokener == NULL)
77 result->ws = afb_ws_create(fd, &aws_itf, result);
78 if (result->ws == NULL)
84 json_tokener_free(result->tokener);
86 ctxClientPut(result->context);
94 static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size)
96 /* do nothing but free the text */
106 struct afb_ws_json *aws;
107 struct afb_wsreq *next;
121 struct json_object *root;
124 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
125 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
126 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
127 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
128 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
129 static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size);
130 static int wsreq_session_create(struct afb_wsreq *wsreq);
131 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh);
132 static void wsreq_session_close(struct afb_wsreq *wsreq);
135 static const struct afb_req_itf wsreq_itf = {
136 .json = (void*)wsreq_json,
137 .get = (void*)wsreq_get,
138 .success = (void*)wsreq_success,
139 .fail = (void*)wsreq_fail,
140 .raw = (void*)wsreq_raw,
141 .send = (void*)wsreq_send,
142 .session_create = (void*)wsreq_session_create,
143 .session_check = (void*)wsreq_session_check,
144 .session_close = (void*)wsreq_session_close
147 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
157 while(pos < end && *pos == ' ') pos++;
158 if (pos == end) goto bad_header;
159 if (*pos++ != '[') goto bad_header;
161 /* scans code: 2|3|4 */
162 while(pos < end && *pos == ' ') pos++;
163 if (pos == end) goto bad_header;
165 case '2': r->code = CALL; break;
166 case '3': r->code = RETOK; break;
167 case '4': r->code = RETERR; break;
168 default: goto bad_header;
172 while(pos < end && *pos == ' ') pos++;
173 if (pos == end) goto bad_header;
174 if (*pos++ != ',') goto bad_header;
177 while(pos < end && *pos == ' ') pos++;
178 if (pos == end) goto bad_header;
179 if (*pos++ != '"') goto bad_header;
181 while(pos < end && *pos != '"') pos++;
182 if (pos == end) goto bad_header;
183 r->idlen = (size_t)(pos++ - r->id);
186 while(pos < end && *pos == ' ') pos++;
187 if (pos == end) goto bad_header;
188 if (*pos++ != ',') goto bad_header;
190 /* scans the method if needed */
191 if (r->code == CALL) {
193 while(pos < end && *pos == ' ') pos++;
194 if (pos == end) goto bad_header;
195 if (*pos++ != '"') goto bad_header;
199 while(pos < end && *pos != '"' && *pos != '/') pos++;
200 if (pos == end) goto bad_header;
201 if (*pos != '/') goto bad_header;
202 r->apilen = (size_t)(pos++ - r->api);
203 if (r->apilen && r->api[r->apilen - 1] == '\\')
208 while(pos < end && *pos != '"') pos++;
209 if (pos == end) goto bad_header;
210 r->verblen = (size_t)(pos++ - r->verb);
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++;
220 if (pos == end) goto bad_header;
223 while (pos < end && (aux != 0 || (*pos != ',' && *pos != ']'))) {
224 if (pos == end) goto bad_header;
226 case '{': case '[': aux++; break;
227 case '}': case ']': if (!aux--) goto bad_header; break;
230 pos += 1 + (*pos == '\\');
231 } while(pos < end && *pos != '"');
237 if (pos > end) goto bad_header;
238 if (pos == end && aux != 0) goto bad_header;
240 r->objlen = (size_t)(pos++ - r->obj);
241 while (r->objlen && r->obj[r->objlen - 1] == ' ')
244 /* scan the token (if any) */
246 /* scans token: "token" */
247 while(pos < end && *pos == ' ') pos++;
248 if (pos == end) goto bad_header;
249 if (*pos++ != '"') goto bad_header;
251 while(pos < end && *pos != '"') pos++;
252 if (pos == end) goto bad_header;
253 r->toklen = (size_t)(pos++ - r->tok);
254 while(pos < end && *pos == ' ') pos++;
255 if (pos == end) goto bad_header;
260 if (c != ']') goto bad_header;
261 while(pos < end && *pos == ' ') pos++;
262 if (pos != end) goto bad_header;
267 fprintf(stderr, "\n\nONTEXT([%d, %.*s, %.*s/%.*s, %.*s, %.*s])\n\n",
269 (int)r->idlen, r->id,
270 (int)r->apilen, r->api,
271 (int)r->verblen, r->verb,
272 (int)r->objlen, r->obj,
273 (int)r->toklen, r->tok
281 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
284 struct afb_wsreq *wsreq;
287 wsreq = calloc(1, sizeof *wsreq);
292 if (!aws_wsreq_parse(wsreq, text, size))
295 /* fill and record the request */
297 wsreq->next = ws->requests;
298 ws->requests = wsreq;
302 afb_apis_call(r, ws->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
309 afb_ws_close(ws->ws, 1008);
313 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
315 struct json_object *root = wsreq->root;
317 json_tokener_reset(wsreq->aws->tokener);
318 root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
320 /* lazy discovering !!!! not good TODO improve*/
321 root = json_object_new_object();
328 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
331 struct json_object *value, *root;
333 root = wsreq_json(wsreq);
334 if (json_object_object_get_ex(root, name, &value)) {
336 arg.value = json_object_get_string(value);
345 static int wsreq_session_create(struct afb_wsreq *wsreq)
347 struct AFB_clientCtx *context = wsreq->aws->context;
348 if (context->created)
350 return wsreq_session_check(wsreq, 1);
353 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
355 struct AFB_clientCtx *context = wsreq->aws->context;
357 if (wsreq->tok == NULL)
360 if (!ctxTokenCheckLen (context, wsreq->tok, wsreq->toklen))
364 ctxTokenNew (context);
370 static void wsreq_session_close(struct afb_wsreq *wsreq)
372 struct AFB_clientCtx *context = wsreq->aws->context;
373 ctxClientClose(context);
377 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
379 json_object *root, *request, *reply;
382 /* builds the answering structure */
383 root = json_object_new_object();
384 json_object_object_add(root, "jtype", json_object_new_string("afb-reply"));
385 request = json_object_new_object();
386 json_object_object_add(root, "request", request);
387 json_object_object_add(request, "status", json_object_new_string(status));
389 json_object_object_add(request, "info", json_object_new_string(info));
391 json_object_object_add(root, "response", resp);
394 reply = json_object_new_array();
395 json_object_array_add(reply, json_object_new_int(retcode));
396 json_object_array_add(reply, json_object_new_string_len(wsreq->id, (int)wsreq->idlen));
397 json_object_array_add(reply, root);
398 json_object_array_add(reply, json_object_new_string(wsreq->aws->context->token));
400 /* emits the reply */
401 message = json_object_to_json_string(reply);
402 afb_ws_text(wsreq->aws->ws, message, strlen(message));
403 json_object_put(reply);
405 /* TODO eliminates the wsreq */
408 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
410 wsreq_reply(wsreq, RETERR, status, info, NULL);
413 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
415 wsreq_reply(wsreq, RETOK, "success", info, obj);
418 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
420 *size = wsreq->objlen;
424 static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size)
426 afb_ws_text(wsreq->aws->ws, buffer, size);