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

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