ARI: REST over Websocket

This commit adds the ability to make ARI REST requests over the same
websocket used to receive events.

For full details on how to use the new capability, visit...

https://docs.asterisk.org/Configuration/Interfaces/Asterisk-REST-Interface-ARI/ARI-REST-over-WebSocket/

Changes:

* Added utilities to http.c:
  * ast_get_http_method_from_string().
  * ast_http_parse_post_form().
* Added utilities to json.c:
  * ast_json_nvp_array_to_ast_variables().
  * ast_variables_to_json_nvp_array().
* Added definitions for new events to carry REST responses.
* Created res/ari/ari_websocket_requests.c to house the new request handlers.
* Moved non-event specific code out of res/ari/resource_events.c into
  res/ari/ari_websockets.c
* Refactored res/res_ari.c to move non-http code out of ast_ari_callback()
  (which is http specific) and into ast_ari_invoke() so it can be shared
  between both the http and websocket transports.

UpgradeNote: This commit adds the ability to make ARI REST requests over the same
websocket used to receive events.
See https://docs.asterisk.org/Configuration/Interfaces/Asterisk-REST-Interface-ARI/ARI-REST-over-WebSocket/

(cherry picked from commit 6bc055416b)
This commit is contained in:
George Joseph
2025-03-12 15:58:51 -06:00
committed by Asterisk Development Team
parent 6a7038e2c5
commit 64aeb20724
20 changed files with 2154 additions and 1300 deletions

View File

@@ -861,6 +861,83 @@ enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_v
return AST_JSON_TO_AST_VARS_CODE_SUCCESS;
}
enum ast_json_nvp_ast_vars_code ast_json_nvp_array_to_ast_variables(
struct ast_json *json_variables, struct ast_variable **variables)
{
struct ast_variable *tail = NULL;
int i = 0;
size_t len = json_variables ? ast_json_array_size(json_variables) : 0;
if (len == 0) {
return AST_JSON_NVP_AST_VARS_CODE_NO_INPUT;
}
for (i = 0; i < len; i++) {
struct ast_variable *new_var;
struct ast_json *json_value;
struct ast_json *json_key;
const char *key;
const char *value;
json_value = ast_json_array_get(json_variables, i);
if (!json_value || ast_json_is_null(json_value) || ast_json_typeof(json_value) != AST_JSON_OBJECT) {
/* Error: Only objects allowed */
return AST_JSON_NVP_AST_VARS_CODE_INVALID_TYPE;
}
json_key = ast_json_object_get(json_value, "name");
if (!json_key || ast_json_is_null(json_key) || ast_json_typeof(json_key) != AST_JSON_STRING) {
/* Error: Only strings allowed */
return AST_JSON_NVP_AST_VARS_CODE_INVALID_TYPE;
}
key = ast_json_string_get(json_key);
json_key = ast_json_object_get(json_value, "value");
if (!json_key || ast_json_is_null(json_key) || ast_json_typeof(json_key) != AST_JSON_STRING) {
/* Error: Only strings allowed */
return AST_JSON_NVP_AST_VARS_CODE_INVALID_TYPE;
}
value = ast_json_string_get(json_key);
new_var = ast_variable_new(key, value, "");
if (!new_var) {
/* Error: OOM */
return AST_JSON_NVP_AST_VARS_CODE_OOM;
}
tail = ast_variable_list_append_hint(variables, tail, new_var);
}
return AST_JSON_NVP_AST_VARS_CODE_SUCCESS;
}
struct ast_json *ast_variables_to_json_nvp_array(struct ast_variable *variables)
{
struct ast_variable *v = NULL;
struct ast_json *json_variables = ast_json_array_create();
if (!variables || !json_variables) {
return NULL;
}
for (v = variables; v; v = v->next) {
struct ast_json *obj = ast_json_pack("{s: s, s: s}",
"name", v->name,
"value", v->value);
if (!obj) {
ast_json_unref(json_variables);
return NULL;
}
if (ast_json_array_append(json_variables, obj)) {
ast_json_unref(json_variables);
ast_json_unref(obj);
return NULL;
}
}
return json_variables;
}
struct ast_json *ast_json_channel_vars(struct varshead *channelvars)
{
struct ast_json *ret;