-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 'xml' keyword in liblognorm.
- Loading branch information
Showing
9 changed files
with
218 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,11 @@ | |
#include <errno.h> | ||
#endif | ||
|
||
#ifdef FEATURE_XML | ||
#include <libxml/xmlmemory.h> | ||
#include <libxml/parser.h> | ||
#endif | ||
|
||
|
||
/* how should output values be formatted? */ | ||
enum FMT_MODE { | ||
|
@@ -75,6 +80,41 @@ hParseInt(const unsigned char **buf, size_t *lenBuf) | |
return i; | ||
} | ||
|
||
|
||
#ifdef FEATURE_XML | ||
/* Credits to https://github.com/katie-snow/xml2json-c | ||
This code is under GPL-3.0 License | ||
*/ | ||
static inline void | ||
xml2jsonc_convert_elements(xmlNode *anode, json_object *jobj) | ||
{ | ||
xmlNode *cur_node = NULL; | ||
json_object *cur_jobj = NULL; | ||
json_object *cur_jstr = NULL; | ||
|
||
for (cur_node = anode; cur_node; cur_node = cur_node->next) | ||
{ | ||
if (cur_node->type == XML_ELEMENT_NODE) | ||
{ | ||
if (xmlChildElementCount(cur_node) == 0) | ||
{ | ||
/* JSON string object */ | ||
cur_jobj = json_object_new_object(); | ||
cur_jstr = json_object_new_string((const char *)xmlNodeGetContent(cur_node)); | ||
json_object_object_add(jobj, (const char *)cur_node->name, cur_jstr); | ||
} | ||
else | ||
{ | ||
/* JSON object */ | ||
cur_jobj = json_object_new_object(); | ||
json_object_object_add(jobj, (const char *)cur_node->name, json_object_get(cur_jobj)); | ||
} | ||
} | ||
xml2jsonc_convert_elements(cur_node->children, cur_jobj); | ||
} | ||
} | ||
#endif /* #ifdef FEATURE_XML */ | ||
|
||
/* parser _parse interface | ||
* | ||
* All parsers receive | ||
|
@@ -2325,6 +2365,71 @@ PARSER_Parse(v2IPTables) | |
return r; | ||
} | ||
|
||
#ifdef FEATURE_XML | ||
/** | ||
* Parse XML. This parser tries to find XML data inside a message. | ||
* If it finds valid XML, it will extract it. | ||
* | ||
* Note: The XML Parser expects a string that begins with '<' and | ||
* ends with '>'. whitespace or any other character at the | ||
* beginning or at the end of the string will cause a parse failure | ||
* | ||
* Note: Is there is extra content after the XML content | ||
* the parser will fail. A hack consist of finding the | ||
* last '>' in the string and ignore the rest. | ||
* | ||
* added 2021-02-01 by [email protected] | ||
*/ | ||
PARSER_Parse(XML) | ||
xmlDocPtr doc = NULL; | ||
xmlNodePtr root_element = NULL; | ||
|
||
/* Find the last occurence of '>' in the string */ | ||
char * pch; | ||
pch=strrchr((const char *) npb->str + *offs, '>'); | ||
|
||
/* Truncate the string after the last occurence of '>' */ | ||
int newLen = pch - (npb->str + *offs) + 1; | ||
char *cstr = strndup(npb->str + *offs, newLen); | ||
CHKN(cstr); | ||
|
||
doc=xmlParseDoc((xmlChar*) cstr); | ||
free(cstr); | ||
|
||
/* Invalid XML string */ | ||
if (doc == NULL) { | ||
goto done; | ||
} | ||
|
||
/* Now convert XML document into JSON document */ | ||
root_element = xmlDocGetRootElement(doc); | ||
json_object *json = NULL; | ||
json = json_object_new_object(); | ||
xml2jsonc_convert_elements(root_element, json); | ||
|
||
if(json == NULL) | ||
goto done; | ||
|
||
/* parsing OK */ | ||
*parsed = newLen ; | ||
r = 0; | ||
|
||
if(value == NULL) { | ||
json_object_put(json); | ||
} else { | ||
*value = json; | ||
} | ||
|
||
done: | ||
if(doc != NULL) | ||
xmlFreeDoc(doc); | ||
xmlCleanupParser(); | ||
return r; | ||
} | ||
#endif /* #ifdef FEATURE_XML */ | ||
|
||
|
||
|
||
/** | ||
* Parse JSON. This parser tries to find JSON data inside a message. | ||
* If it finds valid JSON, it will extract it. Extra data after the | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
# added 2021-11-14 by Theo Bertin | ||
# This file is part of the liblognorm project, released under ASL 2.0 | ||
. $srcdir/exec.sh | ||
|
||
test_def $0 "XML field" | ||
add_rule 'version=2' | ||
add_rule 'rule=:%field:xml%' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note</note>' | ||
assert_output_json_eq '{ "field": { "note": "This is a simple note"} }' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note><one>first note</one><two>second note</two></note>' | ||
assert_output_json_eq '{ "field": { "note": { "one": "first note", "two": "second note" } } }' | ||
|
||
# execute '@cee: {"f1": "1", "f2": 2}' | ||
# assert_output_json_eq '{ "field": { "f1": "1", "f2": 2 } }' | ||
|
||
# execute '@cee: {"f1": "1", "f2": 2}' | ||
# assert_output_json_eq '{ "field": { "f1": "1", "f2": 2 } }' | ||
|
||
# | ||
# Things that MUST NOT work | ||
# | ||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note</note> ' # note the trailing space | ||
assert_output_json_eq '{ "originalmsg": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note<\/note> ", "unparsed-data": " " }' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note' | ||
assert_output_json_eq '{ "originalmsg": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note", "unparsed-data": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note" }' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note</note2>' | ||
assert_output_json_eq '{ "originalmsg": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note</note2>", "unparsed-data": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note</note2>" }' | ||
|
||
|
||
cleanup_tmp_files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
# added 2021-11-14 by Theo Bertin | ||
# This file is part of the liblognorm project, released under ASL 2.0 | ||
. $srcdir/exec.sh | ||
|
||
test_def $0 "XML field" | ||
add_rule 'version=2' | ||
add_rule 'rule=:%{"name":"field", "type":"xml"}%' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note</note>' | ||
assert_output_json_eq '{ "field": { "note": "This is a simple note"} }' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note><one>first note</one><two>second note</two></note>' | ||
assert_output_json_eq '{ "field": { "note": { "one": "first note", "two": "second note" } } }' | ||
|
||
# execute '@cee: {"f1": "1", "f2": 2}' | ||
# assert_output_json_eq '{ "field": { "f1": "1", "f2": 2 } }' | ||
|
||
# execute '@cee: {"f1": "1", "f2": 2}' | ||
# assert_output_json_eq '{ "field": { "f1": "1", "f2": 2 } }' | ||
|
||
# | ||
# Things that MUST NOT work | ||
# | ||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note</note> ' # note the trailing space | ||
assert_output_json_eq '{ "originalmsg": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note<\/note> ", "unparsed-data": " " }' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note' | ||
assert_output_json_eq '{ "originalmsg": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note", "unparsed-data": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note" }' | ||
|
||
execute '<?xml version="1.0" encoding="UTF-8"?><note>This is a simple note</note2>' | ||
assert_output_json_eq '{ "originalmsg": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note</note2>", "unparsed-data": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note>This is a simple note</note2>" }' | ||
|
||
|
||
cleanup_tmp_files | ||
|