Blame SOURCES/liblognorm-2.0.0-rhbz1565219-add-skipempty.patch

596e81
From eb2299a0897577048205e4d8a331168d82ce09d0 Mon Sep 17 00:00:00 2001
596e81
From: Noriko Hosoi <nhosoi@momo7.localdomain>
596e81
Date: Thu, 26 Jul 2018 17:18:38 -0700
596e81
Subject: [PATCH] Add a parameter skipempty to the json field type.
596e81
596e81
If skipempty is set as follows, empty json objects are dropped from
596e81
the parsed result.
596e81
  %field_name:json:skipempty%
596e81
596e81
If any parameter other than "skipempty" is given ("bogus" in this
596e81
example), an error message "invalid flag for JSON parser: bogus"
596e81
is issued.
596e81
---
596e81
 src/parser.c                  | 127 ++++++++++++++++++++++++++++++++++++++++++
596e81
 src/parser.h                  |   2 +-
596e81
 src/pdag.c                    |   2 +-
596e81
 3 files changed, 129 insertions(+), 2 deletions(-)
596e81
596e81
diff --git a/src/parser.c b/src/parser.c
596e81
index 77407c6..6736c6f 100644
596e81
--- a/src/parser.c
596e81
+++ b/src/parser.c
596e81
@@ -2325,6 +2325,85 @@ PARSER_Parse(v2IPTables)
596e81
 	return r;
596e81
 }
596e81
 
596e81
+/*
596e81
+ * Delete children of the given object if it has children and they are empty.
596e81
+ *
596e81
+ * return 0 if object is not empty
596e81
+ * return 1 if object is empty
596e81
+ * return < 0 if error
596e81
+ *
596e81
+ * Caller should do this:
596e81
+ * if (jsonSkipEmpty(obj) > 0) {
596e81
+ *     json_object_put(obj);
596e81
+ *     obj = NULL;
596e81
+ * }
596e81
+ * or otherwise not use obj if jsonSkipEmpty returns > 0.
596e81
+ */
596e81
+static int
596e81
+jsonSkipEmpty(struct json_object *__restrict__ json)
596e81
+{
596e81
+	int rc = 0;
596e81
+	struct json_object *val = NULL;
596e81
+
596e81
+	if(json == NULL) {
596e81
+		rc = 1;
596e81
+		goto finalize_it;
596e81
+	}
596e81
+
596e81
+	switch (json_object_get_type(json)) {
596e81
+	case json_type_string:
596e81
+		rc = json_object_get_string_len(json) == 0;
596e81
+		break;
596e81
+	case json_type_array:
596e81
+	{
596e81
+		int i;
596e81
+		int arrayLen = json_object_array_length(json);
596e81
+		for (i = 0 ; i < arrayLen ; ++i) {
596e81
+			val = json_object_array_get_idx(json, i);
596e81
+			if ((rc = jsonSkipEmpty(val)) > 0) {
596e81
+				/* delete the empty item and reset the index and arrayLen */
596e81
+				json_object_array_del_idx(json, i--);
596e81
+				arrayLen = json_object_array_length(json);
596e81
+			} else if (rc < 0) {
596e81
+				goto finalize_it;
596e81
+			}
596e81
+		}
596e81
+		rc = json_object_array_length(json) == 0;
596e81
+		break;
596e81
+	}
596e81
+	case json_type_object:
596e81
+	{
596e81
+		struct json_object_iterator it = json_object_iter_begin(json);
596e81
+		struct json_object_iterator itEnd = json_object_iter_end(json);
596e81
+		while (!json_object_iter_equal(&it, &itEnd)) {
596e81
+			val = json_object_iter_peek_value(&it);
596e81
+			if ((rc = jsonSkipEmpty(val)) > 0) {
596e81
+				json_object_object_del(json, json_object_iter_peek_name(&it);;
596e81
+			} else if (rc < 0) {
596e81
+				goto finalize_it;
596e81
+			}
596e81
+			json_object_iter_next(&it);
596e81
+		}
596e81
+		rc = json_object_object_length(json) == 0;
596e81
+	}
596e81
+	case json_type_null:
596e81
+	case json_type_boolean:
596e81
+	case json_type_double:
596e81
+	case json_type_int:
596e81
+	default: break;
596e81
+	}
596e81
+finalize_it:
596e81
+	return rc;
596e81
+}
596e81
+
596e81
+/* 
596e81
+ * Parameters for field type json
596e81
+ *   skipempty - skips empty json objects.
596e81
+ *             - %field_name:json:skipempty%
596e81
+ */
596e81
+struct data_JSON {
596e81
+	int skipempty;
596e81
+};
596e81
 /**
596e81
  * Parse JSON. This parser tries to find JSON data inside a message.
596e81
  * If it finds valid JSON, it will extract it. Extra data after the
596e81
@@ -2340,6 +2419,7 @@ PARSER_Parse(v2IPTables)
596e81
 PARSER_Parse(JSON)
596e81
 	const size_t i = *offs;
596e81
 	struct json_tokener *tokener = NULL;
596e81
+	struct data_JSON *const data = (struct data_JSON*) pdata;
596e81
 
596e81
 	if(npb->str[i] != '{' && npb->str[i] != ']') {
596e81
 		/* this can't be json, see RFC4627, Sect. 2
596e81
@@ -2368,6 +2448,20 @@ PARSER_Parse(JSON)
596e81
 	if(value == NULL) {
596e81
 		json_object_put(json);
596e81
 	} else {
596e81
+		if (data && data->skipempty) {
596e81
+			int rc = jsonSkipEmpty(json);
596e81
+			if (rc < 0) {
596e81
+				json_object_put(json);
596e81
+				FAIL(LN_WRONGPARSER);
596e81
+			} else if (rc > 0) {
596e81
+				/* 
596e81
+				 * json value is empty.
596e81
+				 * E.g., {"message":""}, {"message":[]}, {"message":{}}
596e81
+				 */
596e81
+				json_object_put(json);
596e81
+				FAIL(0);
596e81
+			}
596e81
+		}
596e81
 		*value = json;
596e81
 	}
596e81
 
596e81
@@ -2376,7 +2470,40 @@ PARSER_Parse(JSON)
596e81
 		json_tokener_free(tokener);
596e81
 	return r;
596e81
 }
596e81
+PARSER_Construct(JSON)
596e81
+{
596e81
+	int r = 0;
596e81
+	struct json_object *ed;
596e81
+	struct data_JSON *data = NULL;
596e81
+	char *flag;
596e81
 
596e81
+	if(json == NULL)
596e81
+		goto done;
596e81
+
596e81
+	if(json_object_object_get_ex(json, "extradata", &ed) == 0) {
596e81
+		/* No JSON parameter */
596e81
+		goto done;
596e81
+	}
596e81
+	data = (struct data_JSON*) calloc(1, sizeof(struct data_JSON));
596e81
+	flag = json_object_get_string(ed);
596e81
+	if (strcasecmp(flag, "skipempty") == 0) {
596e81
+		data->skipempty = 1;
596e81
+	} else {
596e81
+		ln_errprintf(ctx, 0, "invalid flag for JSON parser: %s", flag);
596e81
+		r = LN_BADCONFIG;
596e81
+		goto done;
596e81
+	}
596e81
+	*pdata = data;
596e81
+done:
596e81
+	if(r != 0) {
596e81
+		free(data);
596e81
+	}
596e81
+	return r;
596e81
+}
596e81
+PARSER_Destruct(JSON)
596e81
+{
596e81
+	free(pdata);
596e81
+}
596e81
 
596e81
 /* check if a char is valid inside a name of a NameValue list
596e81
  * The set of valid characters may be extended if there is good
596e81
diff --git a/src/parser.h b/src/parser.h
596e81
index 38be62d..5b4a821 100644
596e81
--- a/src/parser.h
596e81
+++ b/src/parser.h
596e81
@@ -70,7 +70,7 @@ PARSERDEF_NO_DATA(Time24hr);
596e81
 PARSERDEF_NO_DATA(Duration);
596e81
 PARSERDEF_NO_DATA(IPv4);
596e81
 PARSERDEF_NO_DATA(IPv6);
596e81
-PARSERDEF_NO_DATA(JSON);
596e81
+PARSERDEF(JSON);
596e81
 PARSERDEF_NO_DATA(CEESyslog);
596e81
 PARSERDEF_NO_DATA(v2IPTables);
596e81
 PARSERDEF_NO_DATA(CiscoInterfaceSpec);
596e81
diff --git a/src/pdag.c b/src/pdag.c
596e81
index 0768e99..9feb755 100644
596e81
--- a/src/pdag.c
596e81
+++ b/src/pdag.c
596e81
@@ -90,7 +90,7 @@ static struct ln_parser_info parser_lookup_table[] = {
596e81
 	PARSER_ENTRY_NO_DATA("duration", Duration, 16),
596e81
 	PARSER_ENTRY_NO_DATA("cisco-interface-spec", CiscoInterfaceSpec, 4),
596e81
 	PARSER_ENTRY_NO_DATA("name-value-list", NameValue, 8),
596e81
-	PARSER_ENTRY_NO_DATA("json", JSON, 4),
596e81
+	PARSER_ENTRY("json", JSON, 4),
596e81
 	PARSER_ENTRY_NO_DATA("cee-syslog", CEESyslog, 4),
596e81
 	PARSER_ENTRY_NO_DATA("mac48", MAC48, 16),
596e81
 	PARSER_ENTRY_NO_DATA("cef", CEF, 4),