Skip to content

Commit a3876e3

Browse files
committed
Avoid unvanted content parse (whitespaces between tags)
1 parent d228ea6 commit a3876e3

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/request_body_processor/xml.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ XMLNodes::XMLNodes(Transaction *transaction)
4242
node_depth(0),
4343
currpath(""),
4444
currval(""),
45+
currval_is_set(false),
4546
m_transaction(transaction)
4647
{}
4748

@@ -68,20 +69,21 @@ class MSCSAXHandler {
6869
// note, the condition should always be true because there is always a pseudo root element: 'xml'
6970
if (xml_data->nodes.size() > 1) {
7071
xml_data->currpath.append(".");
71-
xml_data->nodes[xml_data->nodes.size()-1]->has_child = true;
72+
xml_data->nodes[xml_data->nodes.size()-2]->has_child = true;
7273
}
7374
xml_data->currpath.append(name);
7475
// set the current value empty
7576
// this is necessary because if there is any text between the tags (new line, etc)
7677
// it will be added to the current value
7778
xml_data->currval = "";
79+
xml_data->currval_is_set = false;
7880
}
7981

8082
void onEndElement(void * ctx, const xmlChar *localname) {
8183
std::string name = reinterpret_cast<const char*>(localname);
8284
XMLNodes* xml_data = static_cast<XMLNodes*>(ctx);
8385
const std::shared_ptr<NodeData>& nd = xml_data->nodes[xml_data->nodes.size()-1];
84-
if (nd->has_child == true) {
86+
if (nd->has_child == false) {
8587
// check the return value
8688
// if false, then stop parsing
8789
// this means the number of arguments reached the limit
@@ -97,6 +99,7 @@ class MSCSAXHandler {
9799
xml_data->nodes.pop_back();
98100
xml_data->node_depth--;
99101
xml_data->currval = "";
102+
xml_data->currval_is_set = false;
100103
}
101104

102105
void onCharacters(void *ctx, const xmlChar *ch, int len) {
@@ -106,7 +109,12 @@ class MSCSAXHandler {
106109
// libxml2 SAX parser will call this function multiple times
107110
// during the parsing of a single node, if the value has multibyte
108111
// characters, so we need to concatenate the values
109-
xml_data->currval += content;
112+
if (xml_data->currval_is_set == false) {
113+
xml_data->currval = content;
114+
xml_data->currval_is_set = true;
115+
} else {
116+
xml_data->currval += content;
117+
}
110118
}
111119
};
112120

src/request_body_processor/xml.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class XMLNodes {
5454
unsigned long int node_depth;
5555
std::string currpath;
5656
std::string currval;
57+
bool currval_is_set;
5758
Transaction *m_transaction;
5859
// need to store context - this is the same as in xml_data
5960
// need to stop parsing if the number of arguments reached the limit

0 commit comments

Comments
 (0)