Asterisk - The Open Source Telephony Project GIT-master-8f1982c
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 dependency graph for res_ari_events.c:

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 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 = AST_BUILDOPT_SUM, .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. More...
 
static struct stasis_rest_handlers events_user
 REST handler for /api-docs/events.json. More...
 
static struct stasis_rest_handlers events_user_eventName
 REST handler for /api-docs/events.json. More...
 

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.

Macro Definition Documentation

◆ MAX_VALS

#define MAX_VALS   128

Definition at line 52 of file res_ari_events.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 264 of file res_ari_events.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 264 of file res_ari_events.c.

◆ ast_ari_events_user_event_cb()

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 105 of file res_ari_events.c.

109{
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) {
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) {
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) {
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
205fin: __attribute__((unused))
206 ast_free(args.source_parse);
207 ast_free(args.source);
208 return;
209}
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:319
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:358
int ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
#define ast_app_separate_args(a, b, c, d)
#define LOG_ERROR
#define MAX_VALS
#define NULL
Definition: resample.c:96
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.
struct ast_json * message
Definition: ari.h:103
int response_code
Definition: ari.h:108
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
const char * args
#define ARRAY_LEN(a)
Definition: utils.h:666

References args, ARRAY_LEN, ast_app_separate_args, ast_ari_events_user_event(), ast_ari_response_alloc_failed(), ast_ari_response_error(), ast_ari_validate_void(), ast_free, ast_log, ast_malloc, ast_strdup, LOG_ERROR, MAX_VALS, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

◆ ast_ari_events_user_event_parse_body()

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 54 of file res_ari_events.c.

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}
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_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:370
@ AST_JSON_ARRAY
Definition: json.h:165
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
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, ...).

References args, ast_free, AST_JSON_ARRAY, ast_json_array_get(), ast_json_array_size(), ast_json_object_get(), ast_json_string_get(), ast_json_typeof(), and ast_malloc.

Referenced by ast_ari_events_user_event().

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 264 of file res_ari_events.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 244 of file res_ari_events.c.

245{
246 int res = 0;
247
249
251 if (res) {
254 }
255
257}
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:239
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct stasis_rest_handlers events
REST handler for /api-docs/events.json.
static int unload_module(void)
int is_websocket
Definition: ari.h:90

References ast_ari_add_handler(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, events, stasis_rest_handlers::is_websocket, and unload_module().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 238 of file res_ari_events.c.

239{
241 return 0;
242}
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:262

References ast_ari_remove_handler(), and events.

Referenced by load_module().

Variable Documentation

◆ __mod_info

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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari,res_ari_model,res_stasis,res_http_websocket", }
static

Definition at line 264 of file res_ari_events.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 264 of file res_ari_events.c.

◆ events

struct stasis_rest_handlers events
static

REST handler for /api-docs/events.json.

Definition at line 230 of file res_ari_events.c.

Referenced by load_module(), and unload_module().

◆ events_user

struct stasis_rest_handlers events_user
static

REST handler for /api-docs/events.json.

Definition at line 222 of file res_ari_events.c.

◆ events_user_eventName

struct stasis_rest_handlers events_user_eventName
static

REST handler for /api-docs/events.json.

Definition at line 212 of file res_ari_events.c.