summaryrefslogtreecommitdiff
path: root/res/res_ari_applications.c
diff options
context:
space:
mode:
authorDavid M. Lee <dlee@digium.com>2013-11-27 15:48:39 +0000
committerDavid M. Lee <dlee@digium.com>2013-11-27 15:48:39 +0000
commitfccb427c88e6b6e0ae8627ffcdc06f4ae5077d34 (patch)
treef3aa4bd4148bd818be174ee8648c3d50300ceec7 /res/res_ari_applications.c
parentfd33969240111a9ad2fdd305461265b315ce13dd (diff)
ari:Add application/json parameter support
The patch allows ARI to parse request parameters from an incoming JSON request body, instead of requiring the request to come in as query parameters (which is just weird for POST and DELETE) or form parameters (which is okay, but a bit asymmetric given that all of our responses are JSON). For any operation that does _not_ have a parameter defined of type body (i.e. "paramType": "body" in the API declaration), if a request provides a request body with a Content type of "application/json", the provided JSON document is parsed and searched for parameters. The expected fields in the provided JSON document should match the query parameters defined for the operation. If the parameter has 'allowMultiple' set, then the field in the JSON document may optionally be an array of values. (closes issue ASTERISK-22685) Review: https://reviewboard.asterisk.org/r/2994/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@403177 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_ari_applications.c')
-rw-r--r--res/res_ari_applications.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/res/res_ari_applications.c b/res/res_ari_applications.c
index 565e3a7ab..1f021c41d 100644
--- a/res/res_ari_applications.c
+++ b/res/res_ari_applications.c
@@ -59,10 +59,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
* \param[out] response Response to the HTTP request.
*/
static void ast_ari_applications_list_cb(
+ struct ast_tcptls_session_instance *ser,
struct ast_variable *get_params, struct ast_variable *path_vars,
struct ast_variable *headers, struct ast_ari_response *response)
{
struct ast_ari_applications_list_args args = {};
+ RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
#if defined(AST_DEVMODE)
int is_valid;
int code;
@@ -108,11 +110,13 @@ fin: __attribute__((unused))
* \param[out] response Response to the HTTP request.
*/
static void ast_ari_applications_get_cb(
+ struct ast_tcptls_session_instance *ser,
struct ast_variable *get_params, struct ast_variable *path_vars,
struct ast_variable *headers, struct ast_ari_response *response)
{
struct ast_ari_applications_get_args args = {};
struct ast_variable *i;
+ RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
#if defined(AST_DEVMODE)
int is_valid;
int code;
@@ -165,11 +169,14 @@ fin: __attribute__((unused))
* \param[out] response Response to the HTTP request.
*/
static void ast_ari_applications_subscribe_cb(
+ struct ast_tcptls_session_instance *ser,
struct ast_variable *get_params, struct ast_variable *path_vars,
struct ast_variable *headers, struct ast_ari_response *response)
{
struct ast_ari_applications_subscribe_args args = {};
struct ast_variable *i;
+ RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
+ struct ast_json *field;
#if defined(AST_DEVMODE)
int is_valid;
int code;
@@ -227,6 +234,53 @@ static void ast_ari_applications_subscribe_cb(
} else
{}
}
+ /* Look for a JSON request entity */
+ body = ast_http_get_json(ser, headers);
+ if (!body) {
+ switch (errno) {
+ case EFBIG:
+ ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
+ goto fin;
+ case ENOMEM:
+ ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
+ goto fin;
+ case EIO:
+ ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
+ goto fin;
+ }
+ }
+ /* Parse query parameters out of it */
+ field = ast_json_object_get(body, "eventSource");
+ if (field) {
+ /* If they were silly enough to both pass in a query param and a
+ * JSON body, free up the query value.
+ */
+ ast_free(args.event_source);
+ if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+ /* Multiple param passed as array */
+ size_t i;
+ args.event_source_count = ast_json_array_size(field);
+ args.event_source = ast_malloc(sizeof(*args.event_source) * args.event_source_count);
+
+ if (!args.event_source) {
+ ast_ari_response_alloc_failed(response);
+ goto fin;
+ }
+
+ for (i = 0; i < args.event_source_count; ++i) {
+ args.event_source[i] = ast_json_string_get(ast_json_array_get(field, i));
+ }
+ } else {
+ /* Multiple param passed as single value */
+ args.event_source_count = 1;
+ args.event_source = ast_malloc(sizeof(*args.event_source) * args.event_source_count);
+ if (!args.event_source) {
+ ast_ari_response_alloc_failed(response);
+ goto fin;
+ }
+ args.event_source[0] = ast_json_string_get(field);
+ }
+ }
ast_ari_applications_subscribe(headers, &args, response);
#if defined(AST_DEVMODE)
code = response->response_code;
@@ -272,11 +326,14 @@ fin: __attribute__((unused))
* \param[out] response Response to the HTTP request.
*/
static void ast_ari_applications_unsubscribe_cb(
+ struct ast_tcptls_session_instance *ser,
struct ast_variable *get_params, struct ast_variable *path_vars,
struct ast_variable *headers, struct ast_ari_response *response)
{
struct ast_ari_applications_unsubscribe_args args = {};
struct ast_variable *i;
+ RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
+ struct ast_json *field;
#if defined(AST_DEVMODE)
int is_valid;
int code;
@@ -334,6 +391,53 @@ static void ast_ari_applications_unsubscribe_cb(
} else
{}
}
+ /* Look for a JSON request entity */
+ body = ast_http_get_json(ser, headers);
+ if (!body) {
+ switch (errno) {
+ case EFBIG:
+ ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
+ goto fin;
+ case ENOMEM:
+ ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
+ goto fin;
+ case EIO:
+ ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
+ goto fin;
+ }
+ }
+ /* Parse query parameters out of it */
+ field = ast_json_object_get(body, "eventSource");
+ if (field) {
+ /* If they were silly enough to both pass in a query param and a
+ * JSON body, free up the query value.
+ */
+ ast_free(args.event_source);
+ if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+ /* Multiple param passed as array */
+ size_t i;
+ args.event_source_count = ast_json_array_size(field);
+ args.event_source = ast_malloc(sizeof(*args.event_source) * args.event_source_count);
+
+ if (!args.event_source) {
+ ast_ari_response_alloc_failed(response);
+ goto fin;
+ }
+
+ for (i = 0; i < args.event_source_count; ++i) {
+ args.event_source[i] = ast_json_string_get(ast_json_array_get(field, i));
+ }
+ } else {
+ /* Multiple param passed as single value */
+ args.event_source_count = 1;
+ args.event_source = ast_malloc(sizeof(*args.event_source) * args.event_source_count);
+ if (!args.event_source) {
+ ast_ari_response_alloc_failed(response);
+ goto fin;
+ }
+ args.event_source[0] = ast_json_string_get(field);
+ }
+ }
ast_ari_applications_unsubscribe(headers, &args, response);
#if defined(AST_DEVMODE)
code = response->response_code;