Asterisk - The Open Source Telephony Project  21.4.1
Macros | Functions | Variables
res_ari_events.c File Reference

WebSocket resource. More...

#include "asterisk.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/stasis_app.h"
#include "ari/resource_events.h"
#include "asterisk/http_websocket.h"

Go to the source code of this file.

Macros

#define MAX_VALS   128
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
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, const char *session_id)
 
static void ast_ari_events_event_websocket_ws_established_cb (struct ast_websocket *ws_session, struct ast_variable *get_params, struct ast_variable *headers)
 
static void ast_ari_events_user_event_cb (struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 Parameter parsing callback for /events/user/{eventName}. More...
 
int ast_ari_events_user_event_parse_body (struct ast_json *body, struct ast_ari_events_user_event_args *args)
 Body parsing function for /events/user/{eventName}. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RESTful API module - WebSocket resource" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari,res_ari_model,res_stasis,res_http_websocket", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct stasis_rest_handlers events
 REST handler for /api-docs/events.json.
 
static struct stasis_rest_handlers events_user
 REST handler for /api-docs/events.json.
 
static struct stasis_rest_handlers events_user_eventName
 REST handler for /api-docs/events.json.
 

Detailed Description

WebSocket resource.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file res_ari_events.c.

Function Documentation

static void ast_ari_events_user_event_cb ( struct ast_tcptls_session_instance ser,
struct ast_variable get_params,
struct ast_variable path_vars,
struct ast_variable headers,
struct ast_json body,
struct ast_ari_response response 
)
static

Parameter parsing callback for /events/user/{eventName}.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 292 of file res_ari_events.c.

References ast_ari_events_user_event_args::application, ast_ari_events_user_event(), ast_ari_response_alloc_failed(), ast_ari_response_error(), ast_ari_validate_void(), ast_malloc, ast_strdup, ast_ari_events_user_event_args::event_name, ast_ari_response::message, ast_variable::name, ast_variable::next, ast_ari_response::response_code, ast_ari_events_user_event_args::source, ast_ari_events_user_event_args::source_count, ast_ari_events_user_event_args::source_parse, ast_variable::value, and ast_ari_events_user_event_args::variables.

296 {
297  struct ast_ari_events_user_event_args args = {};
298  struct ast_variable *i;
299 #if defined(AST_DEVMODE)
300  int is_valid;
301  int code;
302 #endif /* AST_DEVMODE */
303 
304  for (i = get_params; i; i = i->next) {
305  if (strcmp(i->name, "application") == 0) {
306  args.application = (i->value);
307  } else
308  if (strcmp(i->name, "source") == 0) {
309  /* Parse comma separated list */
310  char *vals[MAX_VALS];
311  size_t j;
312 
313  args.source_parse = ast_strdup(i->value);
314  if (!args.source_parse) {
316  goto fin;
317  }
318 
319  if (strlen(args.source_parse) == 0) {
320  /* ast_app_separate_args can't handle "" */
321  args.source_count = 1;
322  vals[0] = args.source_parse;
323  } else {
324  args.source_count = ast_app_separate_args(
325  args.source_parse, ',', vals,
326  ARRAY_LEN(vals));
327  }
328 
329  if (args.source_count == 0) {
331  goto fin;
332  }
333 
334  if (args.source_count >= MAX_VALS) {
335  ast_ari_response_error(response, 400,
336  "Bad Request",
337  "Too many values for source");
338  goto fin;
339  }
340 
341  args.source = ast_malloc(sizeof(*args.source) * args.source_count);
342  if (!args.source) {
344  goto fin;
345  }
346 
347  for (j = 0; j < args.source_count; ++j) {
348  args.source[j] = (vals[j]);
349  }
350  } else
351  {}
352  }
353  for (i = path_vars; i; i = i->next) {
354  if (strcmp(i->name, "eventName") == 0) {
355  args.event_name = (i->value);
356  } else
357  {}
358  }
359  args.variables = body;
360  ast_ari_events_user_event(headers, &args, response);
361 #if defined(AST_DEVMODE)
362  code = response->response_code;
363 
364  switch (code) {
365  case 0: /* Implementation is still a stub, or the code wasn't set */
366  is_valid = response->message == NULL;
367  break;
368  case 500: /* Internal Server Error */
369  case 501: /* Not Implemented */
370  case 404: /* Application does not exist. */
371  case 422: /* Event source not found. */
372  case 400: /* Invalid even tsource URI or userevent data. */
373  is_valid = 1;
374  break;
375  default:
376  if (200 <= code && code <= 299) {
377  is_valid = ast_ari_validate_void(
378  response->message);
379  } else {
380  ast_log(LOG_ERROR, "Invalid error response %d for /events/user/{eventName}\n", code);
381  is_valid = 0;
382  }
383  }
384 
385  if (!is_valid) {
386  ast_log(LOG_ERROR, "Response validation failed for /events/user/{eventName}\n");
387  ast_ari_response_error(response, 500,
388  "Internal Server Error", "Response validation failed");
389  }
390 #endif /* AST_DEVMODE */
391 
392 fin: __attribute__((unused))
393  ast_free(args.source_parse);
394  ast_free(args.source);
395  return;
396 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
int ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
int response_code
Definition: ari.h:99
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
struct ast_json * message
Definition: ari.h:94
void ast_ari_events_user_event(struct ast_variable *headers, struct ast_ari_events_user_event_args *args, struct ast_ari_response *response)
Generate a user event.
int ast_ari_events_user_event_parse_body ( struct ast_json body,
struct ast_ari_events_user_event_args args 
)

Body parsing function for /events/user/{eventName}.

Parameters
bodyThe JSON body from which to parse parameters.
[out]argsThe args structure to parse into.
Return values
zeroon success
non-zeroon failure

Definition at line 241 of file res_ari_events.c.

References ast_ari_events_user_event_args::application, ast_json_array_get(), ast_json_array_size(), ast_json_object_get(), ast_json_string_get(), ast_json_typeof(), ast_malloc, ast_ari_events_user_event_args::source, and ast_ari_events_user_event_args::source_count.

Referenced by ast_ari_events_user_event().

244 {
245  struct ast_json *field;
246  /* Parse query parameters out of it */
247  field = ast_json_object_get(body, "application");
248  if (field) {
249  args->application = ast_json_string_get(field);
250  }
251  field = ast_json_object_get(body, "source");
252  if (field) {
253  /* If they were silly enough to both pass in a query param and a
254  * JSON body, free up the query value.
255  */
256  ast_free(args->source);
257  if (ast_json_typeof(field) == AST_JSON_ARRAY) {
258  /* Multiple param passed as array */
259  size_t i;
260  args->source_count = ast_json_array_size(field);
261  args->source = ast_malloc(sizeof(*args->source) * args->source_count);
262 
263  if (!args->source) {
264  return -1;
265  }
266 
267  for (i = 0; i < args->source_count; ++i) {
268  args->source[i] = ast_json_string_get(ast_json_array_get(field, i));
269  }
270  } else {
271  /* Multiple param passed as single value */
272  args->source_count = 1;
273  args->source = ast_malloc(sizeof(*args->source) * args->source_count);
274  if (!args->source) {
275  return -1;
276  }
277  args->source[0] = ast_json_string_get(field);
278  }
279  }
280  return 0;
281 }
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
enum ast_json_type ast_json_typeof(const struct ast_json *value)
Get the type of value.
Definition: json.c:78
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:366
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:370