| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
ast_free(args->source); |
| 70 |
if (ast_json_typeof(field) == AST_JSON_ARRAY) { |
| 71 |
|
| 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 |
|
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
| 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 |
|
| 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 |
|
| 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: |
| 179 |
is_valid = response->message == NULL; |
| 180 |
break; |
| 181 |
case 500: |
| 182 |
case 501: |
| 183 |
case 404: |
| 184 |
case 422: |
| 185 |
case 400: |
| 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 |
| 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 |
|
| 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 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 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 |
| 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: |
| 267 |
is_valid = response->message == NULL; |
| 268 |
break; |
| 269 |
case 500: |
| 270 |
case 501: |
| 271 |
case 404: |
| 272 |
case 409: |
| 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 |
| 291 |
|
| 292 |
fin: __attribute__((unused)) |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
); |