res_ari_events.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  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. * !!!!! DO NOT EDIT !!!!!
  21. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  22. * This file is generated by a mustache template. Please see the original
  23. * template in rest-api-templates/res_ari_resource.c.mustache
  24. */
  25. /*! \file
  26. *
  27. * \brief WebSocket resource
  28. *
  29. * \author David M. Lee, II <dlee@digium.com>
  30. */
  31. /*** MODULEINFO
  32. <depend type="module">res_ari</depend>
  33. <depend type="module">res_ari_model</depend>
  34. <depend type="module">res_stasis</depend>
  35. <depend type="module">res_http_websocket</depend>
  36. <support_level>core</support_level>
  37. ***/
  38. #include "asterisk.h"
  39. #include "asterisk/app.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/stasis_app.h"
  42. #include "ari/resource_events.h"
  43. #if defined(AST_DEVMODE)
  44. #include "ari/ari_model_validators.h"
  45. #endif
  46. #include "asterisk/http_websocket.h"
  47. #define MAX_VALS 128
  48. static int ast_ari_events_event_websocket_ws_attempted_cb(struct ast_tcptls_session_instance *ser,
  49. struct ast_variable *get_params, struct ast_variable *headers, const char *session_id)
  50. {
  51. struct ast_ari_events_event_websocket_args args = {};
  52. int res = 0;
  53. RAII_VAR(struct ast_ari_response *, response, NULL, ast_free);
  54. struct ast_variable *i;
  55. response = ast_calloc(1, sizeof(*response));
  56. if (!response) {
  57. ast_log(LOG_ERROR, "Failed to create response.\n");
  58. goto fin;
  59. }
  60. for (i = get_params; i; i = i->next) {
  61. if (strcmp(i->name, "app") == 0) {
  62. /* Parse comma separated list */
  63. char *vals[MAX_VALS];
  64. size_t j;
  65. args.app_parse = ast_strdup(i->value);
  66. if (!args.app_parse) {
  67. ast_ari_response_alloc_failed(response);
  68. goto fin;
  69. }
  70. if (strlen(args.app_parse) == 0) {
  71. /* ast_app_separate_args can't handle "" */
  72. args.app_count = 1;
  73. vals[0] = args.app_parse;
  74. } else {
  75. args.app_count = ast_app_separate_args(
  76. args.app_parse, ',', vals,
  77. ARRAY_LEN(vals));
  78. }
  79. if (args.app_count == 0) {
  80. ast_ari_response_alloc_failed(response);
  81. goto fin;
  82. }
  83. if (args.app_count >= MAX_VALS) {
  84. ast_ari_response_error(response, 400,
  85. "Bad Request",
  86. "Too many values for app");
  87. goto fin;
  88. }
  89. args.app = ast_malloc(sizeof(*args.app) * args.app_count);
  90. if (!args.app) {
  91. ast_ari_response_alloc_failed(response);
  92. goto fin;
  93. }
  94. for (j = 0; j < args.app_count; ++j) {
  95. args.app[j] = (vals[j]);
  96. }
  97. } else
  98. if (strcmp(i->name, "subscribeAll") == 0) {
  99. args.subscribe_all = ast_true(i->value);
  100. } else
  101. {}
  102. }
  103. res = ast_ari_websocket_events_event_websocket_attempted(ser, headers, &args, session_id);
  104. fin: __attribute__((unused))
  105. if (!response) {
  106. ast_http_error(ser, 500, "Server Error", "Memory allocation error");
  107. res = -1;
  108. } else if (response->response_code != 0) {
  109. /* Param parsing failure */
  110. RAII_VAR(char *, msg, NULL, ast_json_free);
  111. if (response->message) {
  112. msg = ast_json_dump_string(response->message);
  113. } else {
  114. ast_log(LOG_ERROR, "Missing response message\n");
  115. }
  116. if (msg) {
  117. ast_http_error(ser, response->response_code, response->response_text, msg);
  118. }
  119. res = -1;
  120. }
  121. ast_free(args.app_parse);
  122. ast_free(args.app);
  123. return res;
  124. }
  125. static void ast_ari_events_event_websocket_ws_established_cb(struct ast_websocket *ws_session,
  126. struct ast_variable *get_params, struct ast_variable *headers)
  127. {
  128. struct ast_ari_events_event_websocket_args args = {};
  129. RAII_VAR(struct ast_ari_response *, response, NULL, ast_free);
  130. struct ast_variable *i;
  131. RAII_VAR(struct ast_websocket *, s, ws_session, ast_websocket_unref);
  132. RAII_VAR(struct ast_ari_websocket_session *, session, NULL, ao2_cleanup);
  133. SCOPED_MODULE_USE(ast_module_info->self);
  134. response = ast_calloc(1, sizeof(*response));
  135. if (!response) {
  136. ast_log(LOG_ERROR, "Failed to create response.\n");
  137. goto fin;
  138. }
  139. #if defined(AST_DEVMODE)
  140. session = ast_ari_websocket_session_create(ws_session,
  141. ast_ari_validate_message_fn());
  142. #else
  143. session = ast_ari_websocket_session_create(ws_session, NULL);
  144. #endif
  145. if (!session) {
  146. ast_log(LOG_ERROR, "Failed to create ARI session\n");
  147. goto fin;
  148. }
  149. for (i = get_params; i; i = i->next) {
  150. if (strcmp(i->name, "app") == 0) {
  151. /* Parse comma separated list */
  152. char *vals[MAX_VALS];
  153. size_t j;
  154. args.app_parse = ast_strdup(i->value);
  155. if (!args.app_parse) {
  156. ast_ari_response_alloc_failed(response);
  157. goto fin;
  158. }
  159. if (strlen(args.app_parse) == 0) {
  160. /* ast_app_separate_args can't handle "" */
  161. args.app_count = 1;
  162. vals[0] = args.app_parse;
  163. } else {
  164. args.app_count = ast_app_separate_args(
  165. args.app_parse, ',', vals,
  166. ARRAY_LEN(vals));
  167. }
  168. if (args.app_count == 0) {
  169. ast_ari_response_alloc_failed(response);
  170. goto fin;
  171. }
  172. if (args.app_count >= MAX_VALS) {
  173. ast_ari_response_error(response, 400,
  174. "Bad Request",
  175. "Too many values for app");
  176. goto fin;
  177. }
  178. args.app = ast_malloc(sizeof(*args.app) * args.app_count);
  179. if (!args.app) {
  180. ast_ari_response_alloc_failed(response);
  181. goto fin;
  182. }
  183. for (j = 0; j < args.app_count; ++j) {
  184. args.app[j] = (vals[j]);
  185. }
  186. } else
  187. if (strcmp(i->name, "subscribeAll") == 0) {
  188. args.subscribe_all = ast_true(i->value);
  189. } else
  190. {}
  191. }
  192. ast_ari_websocket_events_event_websocket_established(session, headers, &args);
  193. fin: __attribute__((unused))
  194. if (response && response->response_code != 0) {
  195. /* Param parsing failure */
  196. RAII_VAR(char *, msg, NULL, ast_json_free);
  197. if (response->message) {
  198. msg = ast_json_dump_string(response->message);
  199. } else {
  200. ast_log(LOG_ERROR, "Missing response message\n");
  201. }
  202. if (msg) {
  203. ast_websocket_write(ws_session,
  204. AST_WEBSOCKET_OPCODE_TEXT, msg, strlen(msg));
  205. }
  206. }
  207. ast_free(args.app_parse);
  208. ast_free(args.app);
  209. }
  210. int ast_ari_events_user_event_parse_body(
  211. struct ast_json *body,
  212. struct ast_ari_events_user_event_args *args)
  213. {
  214. struct ast_json *field;
  215. /* Parse query parameters out of it */
  216. field = ast_json_object_get(body, "application");
  217. if (field) {
  218. args->application = ast_json_string_get(field);
  219. }
  220. field = ast_json_object_get(body, "source");
  221. if (field) {
  222. /* If they were silly enough to both pass in a query param and a
  223. * JSON body, free up the query value.
  224. */
  225. ast_free(args->source);
  226. if (ast_json_typeof(field) == AST_JSON_ARRAY) {
  227. /* Multiple param passed as array */
  228. size_t i;
  229. args->source_count = ast_json_array_size(field);
  230. args->source = ast_malloc(sizeof(*args->source) * args->source_count);
  231. if (!args->source) {
  232. return -1;
  233. }
  234. for (i = 0; i < args->source_count; ++i) {
  235. args->source[i] = ast_json_string_get(ast_json_array_get(field, i));
  236. }
  237. } else {
  238. /* Multiple param passed as single value */
  239. args->source_count = 1;
  240. args->source = ast_malloc(sizeof(*args->source) * args->source_count);
  241. if (!args->source) {
  242. return -1;
  243. }
  244. args->source[0] = ast_json_string_get(field);
  245. }
  246. }
  247. return 0;
  248. }
  249. /*!
  250. * \brief Parameter parsing callback for /events/user/{eventName}.
  251. * \param ser TCP/TLS session object
  252. * \param get_params GET parameters in the HTTP request.
  253. * \param path_vars Path variables extracted from the request.
  254. * \param headers HTTP headers.
  255. * \param body
  256. * \param[out] response Response to the HTTP request.
  257. */
  258. static void ast_ari_events_user_event_cb(
  259. struct ast_tcptls_session_instance *ser,
  260. struct ast_variable *get_params, struct ast_variable *path_vars,
  261. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  262. {
  263. struct ast_ari_events_user_event_args args = {};
  264. struct ast_variable *i;
  265. #if defined(AST_DEVMODE)
  266. int is_valid;
  267. int code;
  268. #endif /* AST_DEVMODE */
  269. for (i = get_params; i; i = i->next) {
  270. if (strcmp(i->name, "application") == 0) {
  271. args.application = (i->value);
  272. } else
  273. if (strcmp(i->name, "source") == 0) {
  274. /* Parse comma separated list */
  275. char *vals[MAX_VALS];
  276. size_t j;
  277. args.source_parse = ast_strdup(i->value);
  278. if (!args.source_parse) {
  279. ast_ari_response_alloc_failed(response);
  280. goto fin;
  281. }
  282. if (strlen(args.source_parse) == 0) {
  283. /* ast_app_separate_args can't handle "" */
  284. args.source_count = 1;
  285. vals[0] = args.source_parse;
  286. } else {
  287. args.source_count = ast_app_separate_args(
  288. args.source_parse, ',', vals,
  289. ARRAY_LEN(vals));
  290. }
  291. if (args.source_count == 0) {
  292. ast_ari_response_alloc_failed(response);
  293. goto fin;
  294. }
  295. if (args.source_count >= MAX_VALS) {
  296. ast_ari_response_error(response, 400,
  297. "Bad Request",
  298. "Too many values for source");
  299. goto fin;
  300. }
  301. args.source = ast_malloc(sizeof(*args.source) * args.source_count);
  302. if (!args.source) {
  303. ast_ari_response_alloc_failed(response);
  304. goto fin;
  305. }
  306. for (j = 0; j < args.source_count; ++j) {
  307. args.source[j] = (vals[j]);
  308. }
  309. } else
  310. {}
  311. }
  312. for (i = path_vars; i; i = i->next) {
  313. if (strcmp(i->name, "eventName") == 0) {
  314. args.event_name = (i->value);
  315. } else
  316. {}
  317. }
  318. args.variables = body;
  319. ast_ari_events_user_event(headers, &args, response);
  320. #if defined(AST_DEVMODE)
  321. code = response->response_code;
  322. switch (code) {
  323. case 0: /* Implementation is still a stub, or the code wasn't set */
  324. is_valid = response->message == NULL;
  325. break;
  326. case 500: /* Internal Server Error */
  327. case 501: /* Not Implemented */
  328. case 404: /* Application does not exist. */
  329. case 422: /* Event source not found. */
  330. case 400: /* Invalid even tsource URI or userevent data. */
  331. is_valid = 1;
  332. break;
  333. default:
  334. if (200 <= code && code <= 299) {
  335. is_valid = ast_ari_validate_void(
  336. response->message);
  337. } else {
  338. ast_log(LOG_ERROR, "Invalid error response %d for /events/user/{eventName}\n", code);
  339. is_valid = 0;
  340. }
  341. }
  342. if (!is_valid) {
  343. ast_log(LOG_ERROR, "Response validation failed for /events/user/{eventName}\n");
  344. ast_ari_response_error(response, 500,
  345. "Internal Server Error", "Response validation failed");
  346. }
  347. #endif /* AST_DEVMODE */
  348. fin: __attribute__((unused))
  349. ast_free(args.source_parse);
  350. ast_free(args.source);
  351. return;
  352. }
  353. /*! \brief REST handler for /api-docs/events.json */
  354. static struct stasis_rest_handlers events_user_eventName = {
  355. .path_segment = "eventName",
  356. .is_wildcard = 1,
  357. .callbacks = {
  358. [AST_HTTP_POST] = ast_ari_events_user_event_cb,
  359. },
  360. .num_children = 0,
  361. .children = { }
  362. };
  363. /*! \brief REST handler for /api-docs/events.json */
  364. static struct stasis_rest_handlers events_user = {
  365. .path_segment = "user",
  366. .callbacks = {
  367. },
  368. .num_children = 1,
  369. .children = { &events_user_eventName, }
  370. };
  371. /*! \brief REST handler for /api-docs/events.json */
  372. static struct stasis_rest_handlers events = {
  373. .path_segment = "events",
  374. .callbacks = {
  375. },
  376. .num_children = 1,
  377. .children = { &events_user, }
  378. };
  379. static int unload_module(void)
  380. {
  381. ast_ari_remove_handler(&events);
  382. ao2_cleanup(events.ws_server);
  383. events.ws_server = NULL;
  384. ast_ari_websocket_events_event_websocket_dtor();
  385. return 0;
  386. }
  387. static int load_module(void)
  388. {
  389. int res = 0;
  390. struct ast_websocket_protocol *protocol;
  391. if (ast_ari_websocket_events_event_websocket_init() == -1) {
  392. return AST_MODULE_LOAD_DECLINE;
  393. }
  394. events.ws_server = ast_websocket_server_create();
  395. if (!events.ws_server) {
  396. ast_ari_websocket_events_event_websocket_dtor();
  397. return AST_MODULE_LOAD_DECLINE;
  398. }
  399. protocol = ast_websocket_sub_protocol_alloc("ari");
  400. if (!protocol) {
  401. ao2_ref(events.ws_server, -1);
  402. events.ws_server = NULL;
  403. ast_ari_websocket_events_event_websocket_dtor();
  404. return AST_MODULE_LOAD_DECLINE;
  405. }
  406. protocol->session_attempted = ast_ari_events_event_websocket_ws_attempted_cb;
  407. protocol->session_established = ast_ari_events_event_websocket_ws_established_cb;
  408. res |= ast_websocket_server_add_protocol2(events.ws_server, protocol);
  409. res |= ast_ari_add_handler(&events);
  410. if (res) {
  411. unload_module();
  412. return AST_MODULE_LOAD_DECLINE;
  413. }
  414. return AST_MODULE_LOAD_SUCCESS;
  415. }
  416. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - WebSocket resource",
  417. .support_level = AST_MODULE_SUPPORT_CORE,
  418. .load = load_module,
  419. .unload = unload_module,
  420. .requires = "res_ari,res_ari_model,res_stasis,res_http_websocket",
  421. );