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,
145 .context_get = (void*)afb_context_get,
146 .context_set = (void*)afb_context_set
150 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
160 while(pos < end && *pos == ' ') pos++;
161 if (pos == end) goto bad_header;
162 if (*pos++ != '[') goto bad_header;
164 /* scans code: 2|3|4 */
165 while(pos < end && *pos == ' ') pos++;
166 if (pos == end) goto bad_header;
168 case '2': r->code = CALL; break;
169 case '3': r->code = RETOK; break;
170 case '4': r->code = RETERR; break;
171 default: goto bad_header;
175 while(pos < end && *pos == ' ') pos++;
176 if (pos == end) goto bad_header;
177 if (*pos++ != ',') goto bad_header;
180 while(pos < end && *pos == ' ') pos++;
181 if (pos == end) goto bad_header;
182 if (*pos++ != '"') goto bad_header;
184 while(pos < end && *pos != '"') pos++;
185 if (pos == end) goto bad_header;
186 r->idlen = (size_t)(pos++ - r->id);
189 while(pos < end && *pos == ' ') pos++;
190 if (pos == end) goto bad_header;
191 if (*pos++ != ',') goto bad_header;
193 /* scans the method if needed */
194 if (r->code == CALL) {
196 while(pos < end && *pos == ' ') pos++;
197 if (pos == end) goto bad_header;
198 if (*pos++ != '"') goto bad_header;
202 while(pos < end && *pos != '"' && *pos != '/') pos++;
203 if (pos == end) goto bad_header;
204 if (*pos != '/') goto bad_header;
205 r->apilen = (size_t)(pos++ - r->api);
206 if (r->apilen && r->api[r->apilen - 1] == '\\')
211 while(pos < end && *pos != '"') pos++;
212 if (pos == end) goto bad_header;
213 r->verblen = (size_t)(pos++ - r->verb);
216 while(pos < end && *pos == ' ') pos++;
217 if (pos == end) goto bad_header;
218 if (*pos++ != ',') goto bad_header;
222 while(pos < end && *pos == ' ') pos++;
223 if (pos == end) goto bad_header;
226 while (pos < end && (aux != 0 || (*pos != ',' && *pos != ']'))) {
227 if (pos == end) goto bad_header;
229 case '{': case '[': aux++; break;
230 case '}': case ']': if (!aux--) goto bad_header; break;
233 pos += 1 + (*pos == '\\');
234 } while(pos < end && *pos != '"');
240 if (pos > end) goto bad_header;
241 if (pos == end && aux != 0) goto bad_header;
243 r->objlen = (size_t)(pos++ - r->obj);
244 while (r->objlen && r->obj[r->objlen - 1] == ' ')
247 /* scan the token (if any) */
249 /* scans token: "token" */
250 while(pos < end && *pos == ' ') pos++;
251 if (pos == end) goto bad_header;
252 if (*pos++ != '"') goto bad_header;
254 while(pos < end && *pos != '"') pos++;
255 if (pos == end) goto bad_header;
256 r->toklen = (size_t)(pos++ - r->tok);
257 while(pos < end && *pos == ' ') pos++;
258 if (pos == end) goto bad_header;
263 if (c != ']') goto bad_header;
264 while(pos < end && *pos == ' ') pos++;
265 if (pos != end) goto bad_header;
270 fprintf(stderr, "\n\nONTEXT([%d, %.*s, %.*s/%.*s, %.*s, %.*s])\n\n",
272 (int)r->idlen, r->id,
273 (int)r->apilen, r->api,
274 (int)r->verblen, r->verb,
275 (int)r->objlen, r->obj,
276 (int)r->toklen, r->tok
284 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
287 struct afb_wsreq *wsreq;
290 wsreq = calloc(1, sizeof *wsreq);
295 if (!aws_wsreq_parse(wsreq, text, size))
298 /* fill and record the request */
300 wsreq->next = ws->requests;
301 ws->requests = wsreq;
303 r.req_closure = wsreq;
305 afb_apis_call(r, ws->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
312 afb_ws_close(ws->ws, 1008);
316 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
318 struct json_object *root = wsreq->root;
320 json_tokener_reset(wsreq->aws->tokener);
321 root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
323 /* lazy discovering !!!! not good TODO improve*/
324 root = json_object_new_object();
331 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
334 struct json_object *value, *root;
336 root = wsreq_json(wsreq);
337 if (json_object_object_get_ex(root, name, &value)) {
339 arg.value = json_object_get_string(value);
348 static int wsreq_session_create(struct afb_wsreq *wsreq)
350 struct AFB_clientCtx *context = wsreq->aws->context;
351 if (context->created)
353 return wsreq_session_check(wsreq, 1);
356 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
358 struct AFB_clientCtx *context = wsreq->aws->context;
360 if (wsreq->tok == NULL)
363 if (!ctxTokenCheckLen (context, wsreq->tok, wsreq->toklen))
367 ctxTokenNew (context);
373 static void wsreq_session_close(struct afb_wsreq *wsreq)
375 struct AFB_clientCtx *context = wsreq->aws->context;
376 ctxClientClose(context);
380 static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
382 json_object *root, *request, *reply;
385 /* builds the answering structure */
386 root = json_object_new_object();
387 json_object_object_add(root, "jtype", json_object_new_string("afb-reply"));
388 request = json_object_new_object();
389 json_object_object_add(root, "request", request);
390 json_object_object_add(request, "status", json_object_new_string(status));
392 json_object_object_add(request, "info", json_object_new_string(info));
394 json_object_object_add(root, "response", resp);
397 reply = json_object_new_array();
398 json_object_array_add(reply, json_object_new_int(retcode));
399 json_object_array_add(reply, json_object_new_string_len(wsreq->id, (int)wsreq->idlen));
400 json_object_array_add(reply, root);
401 json_object_array_add(reply, json_object_new_string(wsreq->aws->context->token));
403 /* emits the reply */
404 message = json_object_to_json_string(reply);
405 afb_ws_text(wsreq->aws->ws, message, strlen(message));
406 json_object_put(reply);
408 /* TODO eliminates the wsreq */
411 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
413 wsreq_reply(wsreq, RETERR, status, info, NULL);
416 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
418 wsreq_reply(wsreq, RETOK, "success", info, obj);
421 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
423 *size = wsreq->objlen;
427 static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size)
429 afb_ws_text(wsreq->aws->ws, buffer, size);