[svn-commits] dlee: branch dlee/ASTERISK-22685-json-body r402970 - in /team/dlee/ASTERISK-2...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Nov 21 13:45:21 CST 2013


Author: dlee
Date: Thu Nov 21 13:45:20 2013
New Revision: 402970

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=402970
Log:
Properly parse array values from JSON supplied params

Modified:
    team/dlee/ASTERISK-22685-json-body/res/res_ari_applications.c
    team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c
    team/dlee/ASTERISK-22685-json-body/res/res_ari_bridges.c
    team/dlee/ASTERISK-22685-json-body/rest-api-templates/param_parsing.mustache

Modified: team/dlee/ASTERISK-22685-json-body/res/res_ari_applications.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/res/res_ari_applications.c?view=diff&rev=402970&r1=402969&r2=402970
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/res/res_ari_applications.c (original)
+++ team/dlee/ASTERISK-22685-json-body/res/res_ari_applications.c Thu Nov 21 13:45:20 2013
@@ -251,28 +251,35 @@
 	}
 	/* Parse query parameters out of it */
 	field = ast_json_object_get(body, "eventSource");
-	if (field && ast_json_typeof(field) == AST_JSON_ARRAY) {
-		size_t i;
-		args.event_source_count = ast_json_array_size(field);
-		ast_free(args.event_source); /* In case it was set above */
-		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 {
-		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);
+	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_application_subscribe(headers, &args, response);
 #if defined(AST_DEVMODE)
@@ -401,28 +408,35 @@
 	}
 	/* Parse query parameters out of it */
 	field = ast_json_object_get(body, "eventSource");
-	if (field && ast_json_typeof(field) == AST_JSON_ARRAY) {
-		size_t i;
-		args.event_source_count = ast_json_array_size(field);
-		ast_free(args.event_source); /* In case it was set above */
-		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 {
-		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);
+	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_application_unsubscribe(headers, &args, response);
 #if defined(AST_DEVMODE)

Modified: team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c?view=diff&rev=402970&r1=402969&r2=402970
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c (original)
+++ team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c Thu Nov 21 13:45:20 2013
@@ -135,28 +135,35 @@
 	}
 	/* Parse query parameters out of it */
 	field = ast_json_object_get(body, "only");
-	if (field && ast_json_typeof(field) == AST_JSON_ARRAY) {
-		size_t i;
-		args.only_count = ast_json_array_size(field);
-		ast_free(args.only); /* In case it was set above */
-		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 {
-		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);
+	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_get_asterisk_info(headers, &args, response);
 #if defined(AST_DEVMODE)

Modified: team/dlee/ASTERISK-22685-json-body/res/res_ari_bridges.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/res/res_ari_bridges.c?view=diff&rev=402970&r1=402969&r2=402970
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/res/res_ari_bridges.c (original)
+++ team/dlee/ASTERISK-22685-json-body/res/res_ari_bridges.c Thu Nov 21 13:45:20 2013
@@ -392,28 +392,35 @@
 	}
 	/* Parse query parameters out of it */
 	field = ast_json_object_get(body, "channel");
-	if (field && ast_json_typeof(field) == AST_JSON_ARRAY) {
-		size_t i;
-		args.channel_count = ast_json_array_size(field);
-		ast_free(args.channel); /* In case it was set above */
-		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 {
-		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);
+	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) {
@@ -547,28 +554,35 @@
 	}
 	/* Parse query parameters out of it */
 	field = ast_json_object_get(body, "channel");
-	if (field && ast_json_typeof(field) == AST_JSON_ARRAY) {
-		size_t i;
-		args.channel_count = ast_json_array_size(field);
-		ast_free(args.channel); /* In case it was set above */
-		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 {
-		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);
+	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_remove_channel_from_bridge(headers, &args, response);
 #if defined(AST_DEVMODE)

Modified: team/dlee/ASTERISK-22685-json-body/rest-api-templates/param_parsing.mustache
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/rest-api-templates/param_parsing.mustache?view=diff&rev=402970&r1=402969&r2=402970
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/rest-api-templates/param_parsing.mustache (original)
+++ team/dlee/ASTERISK-22685-json-body/rest-api-templates/param_parsing.mustache Thu Nov 21 13:45:20 2013
@@ -107,36 +107,41 @@
 	/* Parse query parameters out of it */
 {{#query_parameters}}
 	field = ast_json_object_get(body, "{{name}}");
+	if (field) {
 {{^allow_multiple}}
-	if (field) {
 		args.{{c_name}} = {{json_convert}}(field);
-	}
 {{/allow_multiple}}
 {{#allow_multiple}}
-	if (field && ast_json_typeof(field) == AST_JSON_ARRAY) {
-		size_t i;
-		args.{{c_name}}_count = ast_json_array_size(field);
-		ast_free(args.{{c_name}}); /* In case it was set above */
-		args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
+		/* If they were silly enough to both pass in a query param and a
+		 * JSON body, free up the query value.
+		 */
+		ast_free(args.{{c_name}});
+		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+			/* Multiple param passed as array */
+			size_t i;
+			args.{{c_name}}_count = ast_json_array_size(field);
+			args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
 
-		if (!args.{{c_name}}) {
-			ast_ari_response_alloc_failed(response);
-			goto fin;
+			if (!args.{{c_name}}) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			for (i = 0; i < args.{{c_name}}_count; ++i) {
+				args.{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i));
+			}
+		} else {
+			/* Multiple param passed as single value */
+			args.{{c_name}}_count = 1;
+			args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
+			if (!args.{{c_name}}) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+			args.{{c_name}}[0] = {{json_convert}}(field);
 		}
-
-		for (i = 0; i < args.{{c_name}}_count; ++i) {
-			args.{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i));
-		}
-	} else {
-		args.{{c_name}}_count = 1;
-		args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
-		if (!args.{{c_name}}) {
-			ast_ari_response_alloc_failed(response);
-			goto fin;
-		}
-		args.{{c_name}}[0] = {{json_convert}}(field);
+{{/allow_multiple}}
 	}
-{{/allow_multiple}}
 {{/query_parameters}}
 {{/body_parameter}}
 {{/parse_body}}




More information about the svn-commits mailing list