[asterisk-commits] dlee: trunk r403177 - in /trunk: ./ include/asterisk/ main/ res/ rest-api-tem...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Nov 27 09:48:42 CST 2013


Author: dlee
Date: Wed Nov 27 09:48:39 2013
New Revision: 403177

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=403177
Log:
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/

Modified:
    trunk/   (props changed)
    trunk/include/asterisk/ari.h
    trunk/include/asterisk/http.h
    trunk/main/http.c
    trunk/res/res_ari.c
    trunk/res/res_ari_applications.c
    trunk/res/res_ari_asterisk.c
    trunk/res/res_ari_bridges.c
    trunk/res/res_ari_channels.c
    trunk/res/res_ari_device_states.c
    trunk/res/res_ari_endpoints.c
    trunk/res/res_ari_playbacks.c
    trunk/res/res_ari_recordings.c
    trunk/res/res_ari_sounds.c
    trunk/rest-api-templates/asterisk_processor.py
    trunk/rest-api-templates/param_parsing.mustache
    trunk/rest-api-templates/res_ari_resource.c.mustache
    trunk/rest-api-templates/swagger_model.py
    trunk/tests/test_ari.c

Propchange: trunk/
------------------------------------------------------------------------------
--- branch-12-merged (original)
+++ branch-12-merged Wed Nov 27 09:48:39 2013
@@ -1,1 +1,1 @@
-/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-402993,403016,403022,403069,403082,403094,403117,403119,403131,403134,403160,403173
+/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-403175

Modified: trunk/include/asterisk/ari.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/ari.h?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/include/asterisk/ari.h (original)
+++ trunk/include/asterisk/ari.h Wed Nov 27 09:48:39 2013
@@ -50,15 +50,16 @@
 
 /*!
  * \brief Callback type for RESTful method handlers.
+ * \param ser TCP/TLS session object
  * \param get_params GET parameters from the HTTP request.
  * \param path_vars Path variables from any wildcard path segments.
  * \param headers HTTP headers from the HTTP requiest.
  * \param[out] response The RESTful response.
  */
-typedef void (*stasis_rest_callback)(struct ast_variable *get_params,
-				     struct ast_variable *path_vars,
-				     struct ast_variable *headers,
-				     struct ast_ari_response *response);
+typedef void (*stasis_rest_callback)(
+	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);
 
 /*!
  * \brief Handler for a single RESTful path segment.

Modified: trunk/include/asterisk/http.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/http.h?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/include/asterisk/http.h (original)
+++ trunk/include/asterisk/http.h Wed Nov 27 09:48:39 2013
@@ -212,5 +212,17 @@
  */
 struct ast_variable *ast_http_get_post_vars(struct ast_tcptls_session_instance *ser, struct ast_variable *headers);
 
+struct ast_json;
+
+/*!\brief Get JSON from client Request Entity-Body, if content type is
+ *        application/json.
+ * \param ser TCP/TLS session object
+ * \param headers List of HTTP headers
+ * \return Parsed JSON content body
+ * \return \c NULL on error, if no content, or if different content type.
+ * \since 12
+ */
+struct ast_json *ast_http_get_json(
+	struct ast_tcptls_session_instance *ser, struct ast_variable *headers);
 
 #endif /* _ASTERISK_SRV_H */

Modified: trunk/main/http.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/http.c?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/main/http.c (original)
+++ trunk/main/http.c Wed Nov 27 09:48:39 2013
@@ -65,6 +65,7 @@
 #include "asterisk/_private.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/netsock2.h"
+#include "asterisk/json.h"
 
 #define MAX_PREFIX 80
 #define DEFAULT_PORT 8088
@@ -607,6 +608,91 @@
 
 #define MAX_POST_CONTENT 1025
 
+static const char *get_content_type(struct ast_variable *headers)
+{
+	struct ast_variable *v;
+
+	for (v = headers; v; v = v->next) {
+		if (strcasecmp(v->name, "Content-Type") == 0) {
+			return v->value;
+		}
+	}
+
+	/* Missing content type; assume empty string */
+	return "";
+}
+
+static int get_content_length(struct ast_variable *headers)
+{
+	struct ast_variable *v;
+
+	for (v = headers; v; v = v->next) {
+		if (!strcasecmp(v->name, "Content-Length")) {
+			return atoi(v->value);
+		}
+	}
+
+	/* Missing content length; assume zero */
+	return 0;
+}
+
+struct ast_json *ast_http_get_json(
+	struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
+{
+	int content_length = 0;
+	int res;
+	struct ast_json *body;
+	RAII_VAR(char *, buf, NULL, ast_free);
+
+	/* Use errno to distinguish errors from no body */
+	errno = 0;
+
+	if (strcasecmp(get_content_type(headers), "application/json") != 0) {
+		/* Content type is not JSON */
+		return NULL;
+	}
+
+	content_length = get_content_length(headers);
+
+	if (content_length <= 0) {
+		/* No content (or streaming content). */
+		return NULL;
+	}
+
+	if (content_length > MAX_POST_CONTENT - 1) {
+		ast_log(LOG_WARNING,
+			"Excessively long HTTP content. (%d > %d)\n",
+			content_length, MAX_POST_CONTENT);
+		errno = EFBIG;
+		return NULL;
+	}
+
+	buf = ast_malloc(content_length);
+	if (!buf) {
+		/* Malloc sets ENOMEM */
+		return NULL;
+	}
+
+	res = fread(buf, 1, content_length, ser->f);
+	if (res < content_length) {
+		/* Error, distinguishable by ferror() or feof(), but neither
+		 * is good. Treat either one as I/O error */
+		ast_log(LOG_WARNING, "Short HTTP request body (%d < %d)\n",
+			res, content_length);
+		errno = EIO;
+		return NULL;
+	}
+
+	body = ast_json_load_buf(buf, content_length, NULL);
+	if (body == NULL) {
+		/* Failed to parse JSON; treat as an I/O error */
+		errno = EIO;
+		return NULL;
+	}
+
+	return body;
+}
+
 /*
  * get post variables from client Request Entity-Body, if content type is
  * application/x-www-form-urlencoded
@@ -623,21 +709,12 @@
 	/* Use errno to distinguish errors from no params */
 	errno = 0;
 
-	for (v = headers; v; v = v->next) {
-		if (!strcasecmp(v->name, "Content-Type")) {
-			if (strcasecmp(v->value, "application/x-www-form-urlencoded")) {
-				return NULL;
-			}
-			break;
-		}
-	}
-
-	for (v = headers; v; v = v->next) {
-		if (!strcasecmp(v->name, "Content-Length")) {
-			content_length = atoi(v->value);
-			break;
-		}
-	}
+	if (strcasecmp(get_content_type(headers), "application/x-www-form-urlencoded") != 0) {
+		/* Content type is not form data */
+		return NULL;
+	}
+
+	content_length = get_content_length(headers);
 
 	if (content_length <= 0) {
 		return NULL;

Modified: trunk/res/res_ari.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_ari.c?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/res/res_ari.c (original)
+++ trunk/res/res_ari.c Wed Nov 27 09:48:39 2013
@@ -539,7 +539,7 @@
 		return;
 	}
 
-	callback(get_params, path_vars, headers, response);
+	callback(ser, get_params, path_vars, headers, response);
 	if (response->message == NULL && response->response_code == 0) {
 		/* Really should not happen */
 		ast_log(LOG_ERROR, "ARI %s %s not implemented\n",

Modified: trunk/res/res_ari_applications.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_ari_applications.c?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/res/res_ari_applications.c (original)
+++ trunk/res/res_ari_applications.c Wed Nov 27 09:48:39 2013
@@ -59,10 +59,12 @@
  * \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 @@
  * \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 @@
  * \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;
@@ -226,6 +233,53 @@
 			args.application_name = (i->value);
 		} 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)
@@ -272,11 +326,14 @@
  * \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;
@@ -333,6 +390,53 @@
 			args.application_name = (i->value);
 		} 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)

Modified: trunk/res/res_ari_asterisk.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_ari_asterisk.c?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/res/res_ari_asterisk.c (original)
+++ trunk/res/res_ari_asterisk.c Wed Nov 27 09:48:39 2013
@@ -59,11 +59,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_asterisk_get_info_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_asterisk_get_info_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;
@@ -114,6 +117,53 @@
 			}
 		} 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, "only");
+	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.only);
+		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+			/* Multiple param passed as array */
+			size_t i;
+			args.only_count = ast_json_array_size(field);
+			args.only = ast_malloc(sizeof(*args.only) * args.only_count);
+
+			if (!args.only) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			for (i = 0; i < args.only_count; ++i) {
+				args.only[i] = ast_json_string_get(ast_json_array_get(field, i));
+			}
+		} else {
+			/* Multiple param passed as single value */
+			args.only_count = 1;
+			args.only = ast_malloc(sizeof(*args.only) * args.only_count);
+			if (!args.only) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+			args.only[0] = ast_json_string_get(field);
+		}
 	}
 	ast_ari_asterisk_get_info(headers, &args, response);
 #if defined(AST_DEVMODE)
@@ -157,11 +207,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_asterisk_get_global_var_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_asterisk_get_global_var_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;
@@ -172,6 +225,26 @@
 			args.variable = (i->value);
 		} 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, "variable");
+	if (field) {
+		args.variable = ast_json_string_get(field);
 	}
 	ast_ari_asterisk_get_global_var(headers, &args, response);
 #if defined(AST_DEVMODE)
@@ -214,11 +287,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_asterisk_set_global_var_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_asterisk_set_global_var_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;
@@ -232,6 +308,30 @@
 			args.value = (i->value);
 		} 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, "variable");
+	if (field) {
+		args.variable = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "value");
+	if (field) {
+		args.value = ast_json_string_get(field);
 	}
 	ast_ari_asterisk_set_global_var(headers, &args, response);
 #if defined(AST_DEVMODE)

Modified: trunk/res/res_ari_bridges.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_ari_bridges.c?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/res/res_ari_bridges.c (original)
+++ trunk/res/res_ari_bridges.c Wed Nov 27 09:48:39 2013
@@ -59,10 +59,12 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_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_bridges_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,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_create_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_bridges_create_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;
@@ -123,6 +128,26 @@
 			args.type = (i->value);
 		} 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, "type");
+	if (field) {
+		args.type = ast_json_string_get(field);
 	}
 	ast_ari_bridges_create(headers, &args, response);
 #if defined(AST_DEVMODE)
@@ -164,11 +189,13 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_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_bridges_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;
@@ -221,11 +248,13 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_destroy_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_bridges_destroy_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;
@@ -278,11 +307,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_add_channel_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_bridges_add_channel_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;
@@ -343,6 +375,57 @@
 		} 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, "channel");
+	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.channel);
+		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+			/* Multiple param passed as array */
+			size_t i;
+			args.channel_count = ast_json_array_size(field);
+			args.channel = ast_malloc(sizeof(*args.channel) * args.channel_count);
+
+			if (!args.channel) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			for (i = 0; i < args.channel_count; ++i) {
+				args.channel[i] = ast_json_string_get(ast_json_array_get(field, i));
+			}
+		} else {
+			/* Multiple param passed as single value */
+			args.channel_count = 1;
+			args.channel = ast_malloc(sizeof(*args.channel) * args.channel_count);
+			if (!args.channel) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+			args.channel[0] = ast_json_string_get(field);
+		}
+	}
+	field = ast_json_object_get(body, "role");
+	if (field) {
+		args.role = ast_json_string_get(field);
+	}
 	ast_ari_bridges_add_channel(headers, &args, response);
 #if defined(AST_DEVMODE)
 	code = response->response_code;
@@ -389,11 +472,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_remove_channel_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_bridges_remove_channel_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;
@@ -451,6 +537,53 @@
 		} 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, "channel");
+	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.channel);
+		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+			/* Multiple param passed as array */
+			size_t i;
+			args.channel_count = ast_json_array_size(field);
+			args.channel = ast_malloc(sizeof(*args.channel) * args.channel_count);
+
+			if (!args.channel) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			for (i = 0; i < args.channel_count; ++i) {
+				args.channel[i] = ast_json_string_get(ast_json_array_get(field, i));
+			}
+		} else {
+			/* Multiple param passed as single value */
+			args.channel_count = 1;
+			args.channel = ast_malloc(sizeof(*args.channel) * args.channel_count);
+			if (!args.channel) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+			args.channel[0] = ast_json_string_get(field);
+		}
+	}
 	ast_ari_bridges_remove_channel(headers, &args, response);
 #if defined(AST_DEVMODE)
 	code = response->response_code;
@@ -497,11 +630,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_start_moh_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_bridges_start_moh_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;
@@ -518,6 +654,26 @@
 			args.bridge_id = (i->value);
 		} 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, "mohClass");
+	if (field) {
+		args.moh_class = ast_json_string_get(field);
 	}
 	ast_ari_bridges_start_moh(headers, &args, response);
 #if defined(AST_DEVMODE)
@@ -561,11 +717,13 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_stop_moh_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_bridges_stop_moh_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;
@@ -619,11 +777,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_play_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_bridges_play_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;
@@ -649,6 +810,38 @@
 			args.bridge_id = (i->value);
 		} 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, "media");
+	if (field) {
+		args.media = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "lang");
+	if (field) {
+		args.lang = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "offsetms");
+	if (field) {
+		args.offsetms = ast_json_integer_get(field);
+	}
+	field = ast_json_object_get(body, "skipms");
+	if (field) {
+		args.skipms = ast_json_integer_get(field);
 	}
 	ast_ari_bridges_play(headers, &args, response);
 #if defined(AST_DEVMODE)
@@ -692,11 +885,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_bridges_record_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_bridges_record_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;
@@ -731,6 +927,50 @@
 			args.bridge_id = (i->value);
 		} 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, "name");
+	if (field) {
+		args.name = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "format");
+	if (field) {
+		args.format = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "maxDurationSeconds");
+	if (field) {
+		args.max_duration_seconds = ast_json_integer_get(field);
+	}
+	field = ast_json_object_get(body, "maxSilenceSeconds");
+	if (field) {
+		args.max_silence_seconds = ast_json_integer_get(field);
+	}
+	field = ast_json_object_get(body, "ifExists");
+	if (field) {
+		args.if_exists = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "beep");
+	if (field) {
+		args.beep = ast_json_is_true(field);
+	}
+	field = ast_json_object_get(body, "terminateOn");
+	if (field) {
+		args.terminate_on = ast_json_string_get(field);
 	}
 	ast_ari_bridges_record(headers, &args, response);
 #if defined(AST_DEVMODE)

Modified: trunk/res/res_ari_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_ari_channels.c?view=diff&rev=403177&r1=403176&r2=403177
==============================================================================
--- trunk/res/res_ari_channels.c (original)
+++ trunk/res/res_ari_channels.c Wed Nov 27 09:48:39 2013
@@ -59,10 +59,12 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_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_channels_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,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_originate_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_channels_originate_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;
@@ -145,6 +150,54 @@
 		} 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, "endpoint");
+	if (field) {
+		args.endpoint = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "extension");
+	if (field) {
+		args.extension = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "context");
+	if (field) {
+		args.context = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "priority");
+	if (field) {
+		args.priority = ast_json_integer_get(field);
+	}
+	field = ast_json_object_get(body, "app");
+	if (field) {
+		args.app = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "appArgs");
+	if (field) {
+		args.app_args = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "callerId");
+	if (field) {
+		args.caller_id = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "timeout");
+	if (field) {
+		args.timeout = ast_json_integer_get(field);
+	}
 	ast_ari_channels_originate(headers, &args, response);
 #if defined(AST_DEVMODE)
 	code = response->response_code;
@@ -186,11 +239,13 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_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_channels_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;
@@ -243,11 +298,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_hangup_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_channels_hangup_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;
@@ -265,6 +323,26 @@
 		} 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, "reason");
+	if (field) {
+		args.reason = ast_json_string_get(field);
+	}
 	ast_ari_channels_hangup(headers, &args, response);
 #if defined(AST_DEVMODE)
 	code = response->response_code;
@@ -307,11 +385,14 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_continue_in_dialplan_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_channels_continue_in_dialplan_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;
@@ -335,6 +416,34 @@
 		} 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, "context");
+	if (field) {
+		args.context = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "extension");
+	if (field) {
+		args.extension = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "priority");
+	if (field) {
+		args.priority = ast_json_integer_get(field);
+	}
 	ast_ari_channels_continue_in_dialplan(headers, &args, response);
 #if defined(AST_DEVMODE)
 	code = response->response_code;
@@ -377,11 +486,13 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_answer_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_channels_answer_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;
@@ -435,11 +546,13 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_ring_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_channels_ring_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;
@@ -493,11 +606,13 @@
  * \param[out] response Response to the HTTP request.
  */
 static void ast_ari_channels_ring_stop_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_channels_ring_stop_args args = {};

[... 1150 lines stripped ...]



More information about the asterisk-commits mailing list