Fix for bug in MailMessage reported on forum

(http://pocoproject.org/forum/viewtopic.php?f=12&t=6106&sid=9283682f915ea148d86813ea140cd3c7).
MailMessage was not handling encoding when no Content-Transfer-Encodign
header was not present for MultiPart messages, resulting on an
Exception. His fix checks if the header is present before getting is
value, and defaults to 7bit encoding as in
http://www.w3.org/Protocols/rfc1341/5_Content-Transfer-Encoding.html
This commit is contained in:
Cristian Thiago Moecke 2014-02-22 13:41:42 -03:00
parent 0ad18f3e80
commit 6b168771e0
3 changed files with 79 additions and 7 deletions

View File

@ -104,13 +104,16 @@ namespace
{ {
MailMessage::ContentTransferEncoding cte = MailMessage::ENCODING_7BIT; MailMessage::ContentTransferEncoding cte = MailMessage::ENCODING_7BIT;
std::string enc = header[MailMessage::HEADER_CONTENT_TRANSFER_ENCODING]; if(header.has(MailMessage::HEADER_CONTENT_TRANSFER_ENCODING))
if (enc == MailMessage::CTE_8BIT) {
cte = MailMessage::ENCODING_8BIT; std::string enc = header[MailMessage::HEADER_CONTENT_TRANSFER_ENCODING];
else if (enc == MailMessage::CTE_QUOTED_PRINTABLE) if (enc == MailMessage::CTE_8BIT)
cte = MailMessage::ENCODING_QUOTED_PRINTABLE; cte = MailMessage::ENCODING_8BIT;
else if (enc == MailMessage::CTE_BASE64) else if (enc == MailMessage::CTE_QUOTED_PRINTABLE)
cte = MailMessage::ENCODING_BASE64; cte = MailMessage::ENCODING_QUOTED_PRINTABLE;
else if (enc == MailMessage::CTE_BASE64)
cte = MailMessage::ENCODING_BASE64;
}
NameValueCollection::ConstIterator it = header.begin(); NameValueCollection::ConstIterator it = header.begin();
NameValueCollection::ConstIterator end = header.end(); NameValueCollection::ConstIterator end = header.end();

View File

@ -358,6 +358,31 @@ void MailMessageTest::testReadQP()
} }
void MailMessageTest::testReadDefaultTransferEncoding()
{
std::istringstream istr(
"Content-Type: text/plain\r\n"
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
"From: poco@appinf.com\r\n"
"Subject: Test Message\r\n"
"To: John Doe <john.doe@no.where>\r\n"
"\r\n"
"Hello, world!\r\n"
"This is a test for the MailMessage class.\r\n"
);
MailMessage message;
message.read(istr);
assert (message.getSender() == "poco@appinf.com");
assert (message.getContentType() == "text/plain");
assert (message.getContent() ==
"Hello, world!\r\n"
"This is a test for the MailMessage class.\r\n"
);
}
void MailMessageTest::testRead8Bit() void MailMessageTest::testRead8Bit()
{ {
std::istringstream istr( std::istringstream istr(
@ -425,6 +450,46 @@ void MailMessageTest::testReadMultiPart()
assert (handler.disp()[1] == "attachment; filename=sample.dat"); assert (handler.disp()[1] == "attachment; filename=sample.dat");
} }
void MailMessageTest::testReadMultiPartDefaultTransferEncoding()
{
std::istringstream istr(
"Content-Type: multipart/mixed; boundary=MIME_boundary_01234567\r\n"
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
"From: poco@appinf.com\r\n"
"Mime-Version: 1.0\r\n"
"Subject: Test Message\r\n"
"To: John Doe <john.doe@no.where>\r\n"
"\r\n"
"\r\n"
"--MIME_boundary_01234567\r\n"
"Content-Disposition: inline\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello World!\r\n"
"\r\n"
"--MIME_boundary_01234567\r\n"
"Content-Disposition: attachment; filename=sample.dat\r\n"
"Content-Transfer-Encoding: base64\r\n"
"Content-Type: application/octet-stream; name=sample\r\n"
"\r\n"
"VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
"--MIME_boundary_01234567--\r\n"
);
StringPartHandler handler;
MailMessage message;
message.read(istr, handler);
assert (handler.data().size() == 2);
assert (handler.data()[0] == "Hello World!\r\n");
assert (handler.type()[0] == "text/plain");
assert (handler.disp()[0] == "inline");
assert (handler.data()[1] == "This is some binary data. Really.");
assert (handler.type()[1] == "application/octet-stream; name=sample");
assert (handler.disp()[1] == "attachment; filename=sample.dat");
}
void MailMessageTest::testReadWriteMultiPart() void MailMessageTest::testReadWriteMultiPart()
{ {
@ -573,8 +638,10 @@ CppUnit::Test* MailMessageTest::suite()
CppUnit_addTest(pSuite, MailMessageTest, testWriteManyRecipients); CppUnit_addTest(pSuite, MailMessageTest, testWriteManyRecipients);
CppUnit_addTest(pSuite, MailMessageTest, testWriteMultiPart); CppUnit_addTest(pSuite, MailMessageTest, testWriteMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadQP); CppUnit_addTest(pSuite, MailMessageTest, testReadQP);
CppUnit_addTest(pSuite, MailMessageTest, testReadDefaultTransferEncoding);
CppUnit_addTest(pSuite, MailMessageTest, testRead8Bit); CppUnit_addTest(pSuite, MailMessageTest, testRead8Bit);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPart); CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPartDefaultTransferEncoding);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPart); CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPartStore); CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPartStore);
CppUnit_addTest(pSuite, MailMessageTest, testEncodeWord); CppUnit_addTest(pSuite, MailMessageTest, testEncodeWord);

View File

@ -53,9 +53,11 @@ public:
void testWriteMultiPart(); void testWriteMultiPart();
void testReadWriteMultiPart(); void testReadWriteMultiPart();
void testReadWriteMultiPartStore(); void testReadWriteMultiPartStore();
void testReadDefaultTransferEncoding();
void testReadQP(); void testReadQP();
void testRead8Bit(); void testRead8Bit();
void testReadMultiPart(); void testReadMultiPart();
void testReadMultiPartDefaultTransferEncoding();
void testEncodeWord(); void testEncodeWord();
void setUp(); void setUp();