summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdwin Mons <edwin.mons@isode.com>2019-05-13 12:34:28 (GMT)
committerEdwin Mons <edwin.mons@isode.com>2019-05-13 12:56:27 (GMT)
commit09d8ac653493a0bd16cb69664ca28fbfe3c61bbb (patch)
treeba3c073d809949187622d1b26abf958f7299b2e3
parent16f15b8d80f648b61e991b04800c23368dcd38a5 (diff)
downloadswift-09d8ac653493a0bd16cb69664ca28fbfe3c61bbb.zip
swift-09d8ac653493a0bd16cb69664ca28fbfe3c61bbb.tar.bz2
Add flag to signal final XML data
XML parse now takes an optional boolean that will signal that with the data that is fed into the parser, the document should now be complete. This will allow the parser to reject partial documents. Test-Information: Updated unit tests pass. Checked that default behaviour wasn't changed. Tested with various partial and complete documents. Change-Id: Ide7c2e47c49d5667f1febcb23da366e96d0dbc21
-rw-r--r--Swiften/Parser/ExpatParser.cpp6
-rw-r--r--Swiften/Parser/ExpatParser.h4
-rw-r--r--Swiften/Parser/LibXMLParser.cpp6
-rw-r--r--Swiften/Parser/LibXMLParser.h4
-rw-r--r--Swiften/Parser/UnitTest/XMLParserTest.cpp9
-rw-r--r--Swiften/Parser/XMLParser.h4
6 files changed, 21 insertions, 12 deletions
diff --git a/Swiften/Parser/ExpatParser.cpp b/Swiften/Parser/ExpatParser.cpp
index 8415c42..e4e66f2 100644
--- a/Swiften/Parser/ExpatParser.cpp
+++ b/Swiften/Parser/ExpatParser.cpp
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2010-2016 Isode Limited. 2 * Copyright (c) 2010-2019 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
@@ -82,11 +82,11 @@ ExpatParser::~ExpatParser() {
82 XML_ParserFree(p->parser_); 82 XML_ParserFree(p->parser_);
83} 83}
84 84
85bool ExpatParser::parse(const std::string& data) { 85bool ExpatParser::parse(const std::string& data, bool finalData) {
86 if (data.size() > std::numeric_limits<int>::max()) { 86 if (data.size() > std::numeric_limits<int>::max()) {
87 return false; 87 return false;
88 } 88 }
89 bool success = XML_Parse(p->parser_, data.c_str(), static_cast<int>(data.size()), false) == XML_STATUS_OK; 89 bool success = XML_Parse(p->parser_, data.c_str(), static_cast<int>(data.size()), finalData) == XML_STATUS_OK;
90 /*if (!success) { 90 /*if (!success) {
91 std::cout << "ERROR: " << XML_ErrorString(XML_GetErrorCode(p->parser_)) << " while parsing " << data << std::endl; 91 std::cout << "ERROR: " << XML_ErrorString(XML_GetErrorCode(p->parser_)) << " while parsing " << data << std::endl;
92 }*/ 92 }*/
diff --git a/Swiften/Parser/ExpatParser.h b/Swiften/Parser/ExpatParser.h
index 12df463..7583339 100644
--- a/Swiften/Parser/ExpatParser.h
+++ b/Swiften/Parser/ExpatParser.h
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2010-2016 Isode Limited. 2 * Copyright (c) 2010-2019 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
@@ -19,7 +19,7 @@ namespace Swift {
19 ExpatParser(XMLParserClient* client); 19 ExpatParser(XMLParserClient* client);
20 ~ExpatParser(); 20 ~ExpatParser();
21 21
22 bool parse(const std::string& data); 22 bool parse(const std::string& data, bool finalData = false);
23 23
24 void stopParser(); 24 void stopParser();
25 25
diff --git a/Swiften/Parser/LibXMLParser.cpp b/Swiften/Parser/LibXMLParser.cpp
index 5bd3737..c9f3a07 100644
--- a/Swiften/Parser/LibXMLParser.cpp
+++ b/Swiften/Parser/LibXMLParser.cpp
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2010-2018 Isode Limited. 2 * Copyright (c) 2010-2019 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
@@ -94,11 +94,11 @@ LibXMLParser::~LibXMLParser() {
94 } 94 }
95} 95}
96 96
97bool LibXMLParser::parse(const std::string& data) { 97bool LibXMLParser::parse(const std::string& data, bool finalData) {
98 if (data.size() > std::numeric_limits<int>::max()) { 98 if (data.size() > std::numeric_limits<int>::max()) {
99 return false; 99 return false;
100 } 100 }
101 if (xmlParseChunk(p->context_, data.c_str(), static_cast<int>(data.size()), false) == XML_ERR_OK) { 101 if (xmlParseChunk(p->context_, data.c_str(), static_cast<int>(data.size()), finalData) == XML_ERR_OK) {
102 return true; 102 return true;
103 } 103 }
104 xmlError* error = xmlCtxtGetLastError(p->context_); 104 xmlError* error = xmlCtxtGetLastError(p->context_);
diff --git a/Swiften/Parser/LibXMLParser.h b/Swiften/Parser/LibXMLParser.h
index 9f752ce..a863867 100644
--- a/Swiften/Parser/LibXMLParser.h
+++ b/Swiften/Parser/LibXMLParser.h
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2010-2016 Isode Limited. 2 * Copyright (c) 2010-2019 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
@@ -22,7 +22,7 @@ namespace Swift {
22 LibXMLParser(XMLParserClient* client); 22 LibXMLParser(XMLParserClient* client);
23 virtual ~LibXMLParser(); 23 virtual ~LibXMLParser();
24 24
25 bool parse(const std::string& data); 25 bool parse(const std::string& data, bool finalData = false);
26 26
27 private: 27 private:
28 static bool initialized; 28 static bool initialized;
diff --git a/Swiften/Parser/UnitTest/XMLParserTest.cpp b/Swiften/Parser/UnitTest/XMLParserTest.cpp
index b593aa7..9e9012b 100644
--- a/Swiften/Parser/UnitTest/XMLParserTest.cpp
+++ b/Swiften/Parser/UnitTest/XMLParserTest.cpp
@@ -217,6 +217,15 @@ class XMLParserTest : public CppUnit::TestFixture {
217 CPPUNIT_ASSERT_EQUAL(std::string("iq"), client_.events[1].data); 217 CPPUNIT_ASSERT_EQUAL(std::string("iq"), client_.events[1].data);
218 } 218 }
219 219
220 void testParse_CompleteDocument() {
221 ParserType testling(&client_);
222
223 CPPUNIT_ASSERT(!testling.parse("<iq", true));
224 CPPUNIT_ASSERT(!testling.parse("<iq>", true));
225 CPPUNIT_ASSERT(!testling.parse("<iq><child>foo</child>", true));
226 CPPUNIT_ASSERT(testling.parse("<iq><child>foo</child></iq>", true));
227 }
228
220 void testParse_WhitespaceInAttribute() { 229 void testParse_WhitespaceInAttribute() {
221 ParserType testling(&client_); 230 ParserType testling(&client_);
222 231
diff --git a/Swiften/Parser/XMLParser.h b/Swiften/Parser/XMLParser.h
index 8a73c3f..ad79b2d 100644
--- a/Swiften/Parser/XMLParser.h
+++ b/Swiften/Parser/XMLParser.h
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2010 Isode Limited. 2 * Copyright (c) 2010-2019 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
@@ -18,7 +18,7 @@ namespace Swift {
18 XMLParser(XMLParserClient* client); 18 XMLParser(XMLParserClient* client);
19 virtual ~XMLParser(); 19 virtual ~XMLParser();
20 20
21 virtual bool parse(const std::string& data) = 0; 21 virtual bool parse(const std::string& data, bool finalData = false) = 0;
22 22
23 XMLParserClient* getClient() const { 23 XMLParserClient* getClient() const {
24 return client_; 24 return client_;