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

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