From a68e12235430901f71982ad2593268f38ea4b85b Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Fri, 11 Jul 2008 20:45:17 +0000 Subject: [PATCH] Recover from broken emails without a final boundary (Zarafa). --- AUTHORS | 3 +++ src/body.cpp | 23 ++++++++++++++++++++++- tests/parser/bodyPartTest.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 0a94013c..da6f0875 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,6 +8,9 @@ Project originator, design, core implementation. Pierre Thierry Patches for STL algorithms. +Zarafa +Miscellaneous patches. + Other contributors: - Stefan Uhrig diff --git a/src/body.cpp b/src/body.cpp index a6b74a76..b9180d74 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -186,8 +186,29 @@ void body::parse(const string& buffer, const string::size_type position, m_contents = vmime::create (); - if (partStart < end) + // Last part was not found: recover from missing boundary + if (!lastPart && pos == string::npos) + { + ref part = vmime::create (); + + try + { + part->parse(buffer, partStart, end); + } + catch (std::exception&) + { + throw; + } + + part->m_parent = m_part; + + m_parts.push_back(part); + } + // Treat remaining text as epilog + else if (partStart < end) + { m_epilogText = string(buffer.begin() + partStart, buffer.begin() + end); + } } // Treat the contents as 'simple' data else diff --git a/tests/parser/bodyPartTest.cpp b/tests/parser/bodyPartTest.cpp index 31c18879..d6fff5e8 100644 --- a/tests/parser/bodyPartTest.cpp +++ b/tests/parser/bodyPartTest.cpp @@ -33,6 +33,7 @@ VMIME_TEST_SUITE_BEGIN VMIME_TEST_LIST_BEGIN VMIME_TEST(testParse) VMIME_TEST(testGenerate) + VMIME_TEST(testParseMissingLastBoundary) VMIME_TEST_LIST_END @@ -43,6 +44,16 @@ VMIME_TEST_SUITE_BEGIN buffer.begin() + c.getParsedOffset() + c.getParsedLength()); } + static const vmime::string extractContents(const vmime::ref cts) + { + std::ostringstream oss; + vmime::utility::outputStreamAdapter os(oss); + + cts->extract(os); + + return oss.str(); + } + void testParse() { @@ -68,6 +79,23 @@ VMIME_TEST_SUITE_BEGIN VASSERT_EQ("6", "BODY", extractComponentString(str3, *p3.getBody())); } + void testParseMissingLastBoundary() + { + vmime::string str = + "Content-Type: multipart/mixed; boundary=\"MY-BOUNDARY\"" + "\r\n\r\n" + "--MY-BOUNDARY\r\nHEADER1\r\n\r\nBODY1" + "--MY-BOUNDARY\r\nHEADER2\r\n\r\nBODY2"; + + vmime::bodyPart p; + p.parse(str); + + VASSERT_EQ("count", 2, p.getBody()->getPartCount()); + + VASSERT_EQ("part1-body", "BODY1", extractContents(p.getBody()->getPartAt(0)->getBody()->getContents())); + VASSERT_EQ("part2-body", "BODY2", extractContents(p.getBody()->getPartAt(1)->getBody()->getContents())); + } + void testGenerate() { vmime::bodyPart p1;