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>
28 #include <afb/afb-req-itf.h>
31 #include "afb-ws-json1.h"
32 #include "afb-common.h"
33 #include "afb-msg-json.h"
34 #include "afb-session.h"
36 #include "afb-apiset.h"
38 #include "afb-context.h"
40 #include "afb-subcall.h"
43 /* predeclaration of structures */
47 /* predeclaration of websocket callbacks */
48 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1);
49 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg);
50 static void aws_on_event(struct afb_ws_json1 *ws, const char *event, int eventid, struct json_object *object);
52 /* predeclaration of wsreq callbacks */
53 static void wsreq_destroy(struct afb_xreq *xreq);
54 static void wsreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj);
56 /* declaration of websocket structure */
60 void (*cleanup)(void*);
61 void *cleanup_closure;
62 struct afb_session *session;
63 struct afb_evt_listener *listener;
64 struct afb_wsj1 *wsj1;
65 struct afb_cred *cred;
66 struct afb_apiset *apiset;
70 /* declaration of wsreq structure */
74 struct afb_ws_json1 *aws;
75 struct afb_wsreq *next;
76 struct afb_wsj1_msg *msgj1;
79 /* interface for afb_ws_json1 / afb_wsj1 */
80 static struct afb_wsj1_itf wsj1_itf = {
81 .on_hangup = (void*)aws_on_hangup,
82 .on_call = (void*)aws_on_call
85 /* interface for xreq */
86 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
88 .unref = wsreq_destroy
91 /* the interface for events */
92 static const struct afb_evt_itf evt_itf = {
93 .broadcast = (void*)aws_on_event,
94 .push = (void*)aws_on_event
97 /***************************************************************
98 ****************************************************************
100 ** functions of afb_ws_json1 / afb_wsj1
102 ****************************************************************
103 ***************************************************************/
105 struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
107 struct afb_ws_json1 *result;
110 assert(context != NULL);
112 result = malloc(sizeof * result);
116 result->refcount = 1;
117 result->cleanup = cleanup;
118 result->cleanup_closure = cleanup_closure;
119 result->session = afb_session_addref(context->session);
120 result->new_session = context->created != 0;
121 if (result->session == NULL)
124 result->wsj1 = afb_wsj1_create(afb_common_get_event_loop(), fd, &wsj1_itf, result);
125 if (result->wsj1 == NULL)
128 result->listener = afb_evt_listener_create(&evt_itf, result);
129 if (result->listener == NULL)
132 result->cred = afb_cred_create_for_socket(fd);
134 result->cred = afb_cred_current();
135 result->apiset = afb_apiset_addref(apiset);
139 afb_wsj1_unref(result->wsj1);
141 afb_session_unref(result->session);
149 static struct afb_ws_json1 *aws_addref(struct afb_ws_json1 *ws)
155 static void aws_unref(struct afb_ws_json1 *ws)
157 if (--ws->refcount == 0) {
158 afb_evt_listener_unref(ws->listener);
159 afb_wsj1_unref(ws->wsj1);
160 if (ws->cleanup != NULL)
161 ws->cleanup(ws->cleanup_closure);
162 afb_session_unref(ws->session);
163 afb_cred_unref(ws->cred);
164 afb_apiset_unref(ws->apiset);
169 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1)
174 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg)
176 struct afb_wsreq *wsreq;
178 DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
181 wsreq = calloc(1, sizeof *wsreq);
183 afb_wsj1_close(ws->wsj1, 1008, NULL);
187 /* init the context */
188 afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
189 afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
190 if (!wsreq->xreq.context.invalidated)
191 wsreq->xreq.context.validated = 1;
192 if (ws->new_session != 0) {
193 wsreq->xreq.context.created = 1;
197 /* fill and record the request */
198 afb_wsj1_msg_addref(msg);
200 wsreq->xreq.cred = afb_cred_addref(ws->cred);
201 wsreq->xreq.api = api;
202 wsreq->xreq.verb = verb;
203 wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
204 wsreq->aws = aws_addref(ws);
205 wsreq->xreq.listener = wsreq->aws->listener;
208 afb_xreq_process(&wsreq->xreq, ws->apiset);
211 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, int eventid, struct json_object *object)
213 afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
216 /***************************************************************
217 ****************************************************************
219 ** functions of wsreq / afb_req
221 ****************************************************************
222 ***************************************************************/
224 static void wsreq_destroy(struct afb_xreq *xreq)
226 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
228 afb_context_disconnect(&wsreq->xreq.context);
229 afb_wsj1_msg_unref(wsreq->msgj1);
230 afb_cred_unref(wsreq->xreq.cred);
231 aws_unref(wsreq->aws);
235 static void wsreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj)
237 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
240 rc = (iserror ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
241 wsreq->msgj1, obj, afb_context_sent_token(&wsreq->xreq.context));
243 ERROR("Can't send reply: %m");