Debian Codegraph @

res/res_ari_events.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 2013, Digium, Inc.
5 *
6 * David M. Lee, II <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*
20 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 * !!!!! DO NOT EDIT !!!!!
22 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 * This file is generated by a mustache template. Please see the original
24 * template in rest-api-templates/res_ari_resource.c.mustache
25 */
26
27 /*! \file
28 *
29 * \brief WebSocket resource
30 *
31 * \author David M. Lee, II <[email protected]>
32 */
33
34 /*** MODULEINFO
35 <depend type="module">res_ari</depend>
36 <depend type="module">res_ari_model</depend>
37 <depend type="module">res_stasis</depend>
38 <depend type="module">res_http_websocket</depend>
39 <support_level>core</support_level>
40 ***/
41
42 #include "asterisk.h"
43
44 #include "asterisk/app.h"
45 #include "asterisk/module.h"
46 #include "asterisk/stasis_app.h"
47 #include "ari/resource_events.h"
48 #if defined(AST_DEVMODE)
49 #include "ari/ari_model_validators.h"
50 #endif
51
52 #define MAX_VALS 128
53
54 int ast_ari_events_user_event_parse_body(
55 struct ast_json *body,
56 struct ast_ari_events_user_event_args *args)
57 {
58 struct ast_json *field;
59 /* Parse query parameters out of it */
60 field = ast_json_object_get(body, "application");
61 if (field) {
62 args->application = ast_json_string_get(field);
63 }
64 field = ast_json_object_get(body, "source");
65 if (field) {
66 /* If they were silly enough to both pass in a query param and a
67 * JSON body, free up the query value.
68 */
69 ast_free(args->source);
70 if (ast_json_typeof(field) == AST_JSON_ARRAY) {
71 /* Multiple param passed as array */
72 size_t i;
73 args->source_count = ast_json_array_size(field);
74 args->source = ast_malloc(sizeof(*args->source) * args->source_count);
75
76 if (!args->source) {
77 return -1;
78 }
79
80 for (i = 0; i < args->source_count; ++i) {
81 args->source[i] = ast_json_string_get(ast_json_array_get(field, i));
82 }
83 } else {
84 /* Multiple param passed as single value */
85 args->source_count = 1;
86 args->source = ast_malloc(sizeof(*args->source) * args->source_count);
87 if (!args->source) {
88 return -1;
89 }
90 args->source[0] = ast_json_string_get(field);
91 }
92 }
93 return 0;
94 }
95
96 /*!
97 * \brief Parameter parsing callback for /events/user/{eventName}.
98 * \param ser TCP/TLS session object
99 * \param get_params GET parameters in the HTTP request.
100 * \param path_vars Path variables extracted from the request.
101 * \param headers HTTP headers.
102 * \param body
103 * \param[out] response Response to the HTTP request.
104 */
105 static void ast_ari_events_user_event_cb(
106 struct ast_tcptls_session_instance *ser,
107 struct ast_variable *get_params, struct ast_variable *path_vars,
108 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
109 {
110 struct ast_ari_events_user_event_args args = {};
111 struct ast_variable *i;
112 #if defined(AST_DEVMODE)
113 int is_valid;
114 int code;
115 #endif /* AST_DEVMODE */
116
117 for (i = get_params; i; i = i->next) {
118 if (strcmp(i->name, "application") == 0) {
119 args.application = (i->value);
120 } else
121 if (strcmp(i->name, "source") == 0) {
122 /* Parse comma separated list */
123 char *vals[MAX_VALS];
124 size_t j;
125
126 args.source_parse = ast_strdup(i->value);
127 if (!args.source_parse) {
128 ast_ari_response_alloc_failed(response);
129 goto fin;
130 }
131
132 if (strlen(args.source_parse) == 0) {
133 /* ast_app_separate_args can't handle "" */
134 args.source_count = 1;
135 vals[0] = args.source_parse;
136 } else {
137 args.source_count = ast_app_separate_args(
138 args.source_parse, ',', vals,
139 ARRAY_LEN(vals));
140 }
141
142 if (args.source_count == 0) {
143 ast_ari_response_alloc_failed(response);
144 goto fin;
145 }
146
147 if (args.source_count >= MAX_VALS) {
148 ast_ari_response_error(response, 400,
149 "Bad Request",
150 "Too many values for source");
151 goto fin;
152 }
153
154 args.source = ast_malloc(sizeof(*args.source) * args.source_count);
155 if (!args.source) {
156 ast_ari_response_alloc_failed(response);
157 goto fin;
158 }
159
160 for (j = 0; j < args.source_count; ++j) {
161 args.source[j] = (vals[j]);
162 }
163 } else
164 {}
165 }
166 for (i = path_vars; i; i = i->next) {
167 if (strcmp(i->name, "eventName") == 0) {
168 args.event_name = (i->value);
169 } else
170 {}
171 }
172 args.variables = body;
173 ast_ari_events_user_event(headers, &args, response);
174 #if defined(AST_DEVMODE)
175 code = response->response_code;
176
177 switch (code) {
178 case 0: /* Implementation is still a stub, or the code wasn't set */
179 is_valid = response->message == NULL;
180 break;
181 case 500: /* Internal Server Error */
182 case 501: /* Not Implemented */
183 case 404: /* Application does not exist. */
184 case 422: /* Event source not found. */
185 case 400: /* Invalid even tsource URI or userevent data. */
186 is_valid = 1;
187 break;
188 default:
189 if (200 <= code && code <= 299) {
190 is_valid = ast_ari_validate_void(
191 response->message);
192 } else {
193 ast_log(LOG_ERROR, "Invalid error response %d for /events/user/{eventName}\n", code);
194 is_valid = 0;
195 }
196 }
197
198 if (!is_valid) {
199 ast_log(LOG_ERROR, "Response validation failed for /events/user/{eventName}\n");
200 ast_ari_response_error(response, 500,
201 "Internal Server Error", "Response validation failed");
202 }
203 #endif /* AST_DEVMODE */
204
205 fin: __attribute__((unused))
206 ast_free(args.source_parse);
207 ast_free(args.source);
208 return;
209 }
210 int ast_ari_events_claim_channel_parse_body(
211 struct ast_json *body,
212 struct ast_ari_events_claim_channel_args *args)
213 {
214 struct ast_json *field;
215 /* Parse query parameters out of it */
216 field = ast_json_object_get(body, "channelId");
217 if (field) {
218 args->channel_id = ast_json_string_get(field);
219 }
220 field = ast_json_object_get(body, "application");
221 if (field) {
222 args->application = ast_json_string_get(field);
223 }
224 return 0;
225 }
226
227 /*!
228 * \brief Parameter parsing callback for /events/claim.
229 * \param ser TCP/TLS session object
230 * \param get_params GET parameters in the HTTP request.
231 * \param path_vars Path variables extracted from the request.
232 * \param headers HTTP headers.
233 * \param body
234 * \param[out] response Response to the HTTP request.
235 */
236 static void ast_ari_events_claim_channel_cb(
237 struct ast_tcptls_session_instance *ser,
238 struct ast_variable *get_params, struct ast_variable *path_vars,
239 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
240 {
241 struct ast_ari_events_claim_channel_args args = {};
242 struct ast_variable *i;
243 #if defined(AST_DEVMODE)
244 int is_valid;
245 int code;
246 #endif /* AST_DEVMODE */
247
248 for (i = get_params; i; i = i->next) {
249 if (strcmp(i->name, "channelId") == 0) {
250 args.channel_id = (i->value);
251 } else
252 if (strcmp(i->name, "application") == 0) {
253 args.application = (i->value);
254 } else
255 {}
256 }
257 if (ast_ari_events_claim_channel_parse_body(body, &args)) {
258 ast_ari_response_alloc_failed(response);
259 goto fin;
260 }
261 ast_ari_events_claim_channel(headers, &args, response);
262 #if defined(AST_DEVMODE)
263 code = response->response_code;
264
265 switch (code) {
266 case 0: /* Implementation is still a stub, or the code wasn't set */
267 is_valid = response->message == NULL;
268 break;
269 case 500: /* Internal Server Error */
270 case 501: /* Not Implemented */
271 case 404: /* Channel not found or not in broadcast state. */
272 case 409: /* Channel has already been claimed by another application. */
273 is_valid = 1;
274 break;
275 default:
276 if (200 <= code && code <= 299) {
277 is_valid = ast_ari_validate_void(
278 response->message);
279 } else {
280 ast_log(LOG_ERROR, "Invalid error response %d for /events/claim\n", code);
281 is_valid = 0;
282 }
283 }
284
285 if (!is_valid) {
286 ast_log(LOG_ERROR, "Response validation failed for /events/claim\n");
287 ast_ari_response_error(response, 500,
288 "Internal Server Error", "Response validation failed");
289 }
290 #endif /* AST_DEVMODE */
291
292 fin: __attribute__((unused))
293 return;
294 }
295
296 /*! \brief REST handler for /api-docs/events.json */
297 static struct stasis_rest_handlers events_user_eventName = {
298 .path_segment = "eventName",
299 .is_wildcard = 1,
300 .callbacks = {
301 [AST_HTTP_POST] = ast_ari_events_user_event_cb,
302 },
303 .num_children = 0,
304 .children = { }
305 };
306 /*! \brief REST handler for /api-docs/events.json */
307 static struct stasis_rest_handlers events_user = {
308 .path_segment = "user",
309 .callbacks = {
310 },
311 .num_children = 1,
312 .children = { &events_user_eventName, }
313 };
314 /*! \brief REST handler for /api-docs/events.json */
315 static struct stasis_rest_handlers events_claim = {
316 .path_segment = "claim",
317 .callbacks = {
318 [AST_HTTP_POST] = ast_ari_events_claim_channel_cb,
319 },
320 .num_children = 0,
321 .children = { }
322 };
323 /*! \brief REST handler for /api-docs/events.json */
324 static struct stasis_rest_handlers events = {
325 .path_segment = "events",
326 .callbacks = {
327 },
328 .num_children = 2,
329 .children = { &events_user,&events_claim, }
330 };
331
332 static int unload_module(void)
333 {
334 ast_ari_remove_handler(&events);
335 return 0;
336 }
337
338 static int load_module(void)
339 {
340 int res = 0;
341
342 events.is_websocket = 1;
343
344 res |= ast_ari_add_handler(&events);
345 if (res) {
346 unload_module();
347 return AST_MODULE_LOAD_DECLINE;
348 }
349
350 return AST_MODULE_LOAD_SUCCESS;
351 }
352
353 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - WebSocket resource",
354 .support_level = AST_MODULE_SUPPORT_CORE,
355 .load = load_module,
356 .unload = unload_module,
357 .requires = "res_ari,res_ari_model,res_stasis,res_http_websocket",
358 );