res_ari_events.c 12 KB

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