2 * Copyright (C) 2016, 2017 "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-json1.h"
30 #include "afb-common.h"
31 #include "afb-msg-json.h"
32 #include "afb-session.h"
34 #include "afb-apiset.h"
36 #include "afb-context.h"
40 /* predeclaration of structures */
44 /* predeclaration of websocket callbacks */
45 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1);
46 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg);
47 static void aws_on_event(struct afb_ws_json1 *ws, const char *event, int eventid, struct json_object *object);
49 /* predeclaration of wsreq callbacks */
50 static void wsreq_destroy(struct afb_xreq *xreq);
51 static void wsreq_reply(struct afb_xreq *xreq, int status, json_object *obj);
53 /* declaration of websocket structure */
57 void (*cleanup)(void*);
58 void *cleanup_closure;
59 struct afb_session *session;
60 struct afb_evt_listener *listener;
61 struct afb_wsj1 *wsj1;
62 struct afb_cred *cred;
63 struct afb_apiset *apiset;
67 /* declaration of wsreq structure */
71 struct afb_ws_json1 *aws;
72 struct afb_wsreq *next;
73 struct afb_wsj1_msg *msgj1;
76 /* interface for afb_ws_json1 / afb_wsj1 */
77 static struct afb_wsj1_itf wsj1_itf = {
78 .on_hangup = (void*)aws_on_hangup,
79 .on_call = (void*)aws_on_call
82 /* interface for xreq */
83 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
85 .unref = wsreq_destroy
88 /* the interface for events */
89 static const struct afb_evt_itf evt_itf = {
90 .broadcast = (void*)aws_on_event,
91 .push = (void*)aws_on_event
94 /***************************************************************
95 ****************************************************************
97 ** functions of afb_ws_json1 / afb_wsj1
99 ****************************************************************
100 ***************************************************************/
102 struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
104 struct afb_ws_json1 *result;
107 assert(context != NULL);
109 result = malloc(sizeof * result);
113 result->refcount = 1;
114 result->cleanup = cleanup;
115 result->cleanup_closure = cleanup_closure;
116 result->session = afb_session_addref(context->session);
117 result->new_session = context->created != 0;
118 if (result->session == NULL)
121 result->wsj1 = afb_wsj1_create(afb_common_get_event_loop(), fd, &wsj1_itf, result);
122 if (result->wsj1 == NULL)
125 result->listener = afb_evt_listener_create(&evt_itf, result);
126 if (result->listener == NULL)
129 result->cred = afb_cred_create_for_socket(fd);
130 result->apiset = afb_apiset_addref(apiset);
134 afb_wsj1_unref(result->wsj1);
136 afb_session_unref(result->session);
144 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
146 __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
150 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
152 if (!__atomic_sub_fetch(&ws->refcount, 1, __ATOMIC_RELAXED)) {
153 afb_evt_listener_unref(ws->listener);
154 afb_wsj1_unref(ws->wsj1);
155 if (ws->cleanup != NULL)
156 ws->cleanup(ws->cleanup_closure);
157 afb_session_unref(ws->session);
158 afb_cred_unref(ws->cred);
159 afb_apiset_unref(ws->apiset);
164 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1)
166 afb_ws_json1_unref(ws);
169 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg)
171 struct afb_wsreq *wsreq;
173 DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
176 wsreq = calloc(1, sizeof *wsreq);
178 afb_wsj1_close(ws->wsj1, 1008, NULL);
182 /* init the context */
183 afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
184 afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
185 if (!wsreq->xreq.context.invalidated)
186 wsreq->xreq.context.validated = 1;
187 if (ws->new_session != 0) {
188 wsreq->xreq.context.created = 1;
192 /* fill and record the request */
193 afb_wsj1_msg_addref(msg);
195 wsreq->xreq.cred = afb_cred_addref(ws->cred);
196 wsreq->xreq.request.api = api;
197 wsreq->xreq.request.verb = verb;
198 wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
199 wsreq->aws = afb_ws_json1_addref(ws);
200 wsreq->xreq.listener = wsreq->aws->listener;
203 afb_xreq_process(&wsreq->xreq, ws->apiset);
206 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, int eventid, struct json_object *object)
208 afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
211 /***************************************************************
212 ****************************************************************
214 ** functions of wsreq / afb_req
216 ****************************************************************
217 ***************************************************************/
219 static void wsreq_destroy(struct afb_xreq *xreq)
221 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
223 afb_context_disconnect(&wsreq->xreq.context);
224 afb_wsj1_msg_unref(wsreq->msgj1);
225 afb_cred_unref(wsreq->xreq.cred);
226 afb_ws_json1_unref(wsreq->aws);
230 static void wsreq_reply(struct afb_xreq *xreq, int status, json_object *obj)
232 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
235 rc = (status < 0 ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
236 wsreq->msgj1, obj, afb_context_sent_token(&wsreq->xreq.context));
238 ERROR("Can't send reply: %m");