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

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