[icinga-checkins] icinga.org: icinga-core/dev/cgis: classic ui: Fixed Invalid json data if field value is multiline #2263

git at icinga.org git at icinga.org
Fri Feb 24 17:28:38 CET 2012


Module: icinga-core
Branch: dev/cgis
Commit: b79a32186afc1cc16a96a54877a8fe7991f0e089
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=b79a32186afc1cc16a96a54877a8fe7991f0e089

Author: Ricardo Bartels <ricardo at bitchbrothers.com>
Date:   Thu Jan 12 18:48:28 2012 +0100

classic ui: Fixed Invalid json data if field value is multiline #2263

refs: #2263

json_encode now also escapes newline

---

 Changelog      |    3 ++-
 cgi/cgiutils.c |    7 +++++++
 cgi/extinfo.c  |   12 ++++++++----
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/Changelog b/Changelog
index 753e77a..b1f5c68 100644
--- a/Changelog
+++ b/Changelog
@@ -5,10 +5,11 @@ Icinga 1.6.x Change Log
 Thanks to all contributers, testers and developers. Please read AUTHORS and THANKS for a detailed list :-)
 
 
-1.7.0 - 01/03/2012
+1.6.2 - ??/??/2012
 
 FIXES
 * classic ui: fixed invalid json output for tac.cgi #2160
+* classic ui: Fixed Invalid json data if field value is multiline #2263
 
 
 1.6.1 - 02/12/2011
diff --git a/cgi/cgiutils.c b/cgi/cgiutils.c
index 33b4909..f897e55 100644
--- a/cgi/cgiutils.c
+++ b/cgi/cgiutils.c
@@ -2983,9 +2983,16 @@ char *json_encode(char *input) {
 
 	for (i = 0, j = 0; i < len; i++) {
 
+		/* escape quotes */
 		if ((char)input[i] == (char)'"') {
 			encoded_string[j++] = '\\';
 			encoded_string[j++] = input[i];
+
+		/* escape newlines */
+		} else if ((char)input[i] == (char)'\n') {
+			encoded_string[j++] = '\\';
+			encoded_string[j++] = 'n';
+
 		} else
 			encoded_string[j++] = input[i];
 	}
diff --git a/cgi/extinfo.c b/cgi/extinfo.c
index 62273c8..984695b 100644
--- a/cgi/extinfo.c
+++ b/cgi/extinfo.c
@@ -1391,9 +1391,11 @@ void show_host_info(void) {
 				printf("\"status_duration\": \"%s\",\n", state_duration);
 			printf("\"status_duration_in_seconds\": %lu,\n", (unsigned long)ts_state_duration);
 			if (temp_hoststatus->long_plugin_output != NULL)
-				printf("\"status_information\": \"%s %s\",\n", json_encode(temp_hoststatus->long_plugin_output), json_encode(temp_hoststatus->plugin_output));
-			else
+				printf("\"status_information\": \"%s\\n%s\",\n", json_encode(temp_hoststatus->plugin_output), json_encode(temp_hoststatus->long_plugin_output));
+			else if (temp_hoststatus->plugin_output != NULL)
 				printf("\"status_information\": \"%s\",\n", json_encode(temp_hoststatus->plugin_output));
+			else
+				printf("\"status_information\": null,\n");
 			if (temp_hoststatus->perf_data == NULL)
 				printf("\"performance_data\": null,\n");
 			else
@@ -1806,9 +1808,11 @@ void show_service_info(void) {
 				printf("\"status_duration\": \"%s\",\n", state_duration);
 			printf("\"status_duration_in_seconds\": %lu,\n", (unsigned long)ts_state_duration);
 			if (temp_svcstatus->long_plugin_output != NULL)
-				printf("\"status_information\": \"%s %s\",\n", json_encode(temp_svcstatus->long_plugin_output), json_encode(temp_svcstatus->plugin_output));
-			else
+				printf("\"status_information\": \"%s\\n%s\",\n", json_encode(temp_svcstatus->plugin_output), json_encode(temp_svcstatus->long_plugin_output));
+			else if (temp_svcstatus->plugin_output != NULL)
 				printf("\"status_information\": \"%s\",\n", json_encode(temp_svcstatus->plugin_output));
+			else
+				printf("\"status_information\": null,\n");
 			if (temp_svcstatus->perf_data == NULL)
 				printf("\"performance_data\": null,\n");
 			else





More information about the icinga-checkins mailing list