Skip to content

Commit

Permalink
Poco::Net::MailMessage::read stucks while reading message without fin…
Browse files Browse the repository at this point in the history
…al multipart boundary (pocoproject#2401)
  • Loading branch information
Aleksandr Voronkov authored and avoronkov committed Aug 6, 2018
1 parent 823cfab commit 4f80bb9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Net/src/MessageHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ void MessageHeader::read(std::istream& istr, RecipientList* pRecipients)
++fields;

}
istr.putback(static_cast<char>(ch));
if (istr.good() && ch != eof)
{
istr.putback(static_cast<char>(ch));
}
}


Expand Down
4 changes: 4 additions & 0 deletions Net/src/MultipartReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ int MultipartStreamBuf::readFromDevice(char* buffer, std::streamsize length)
*buffer++ = (char) buf.sbumpc(); ++n;
ch = buf.sgetc();
}
if (ch == eof)
{
_lastPart = true;
}
return n;
}

Expand Down
50 changes: 50 additions & 0 deletions Net/testsuite/src/MailMessageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include "Poco/Timestamp.h"
#include "Poco/FileStream.h"
#include "Poco/String.h"
#include "Poco/TemporaryFile.h"
#include <sstream>
#include <fstream>
#include <vector>


Expand All @@ -36,6 +38,7 @@ using Poco::Timestamp;
using Poco::FileInputStream;
using Poco::replaceInPlace;
using Poco::icompare;
using Poco::TemporaryFile;


namespace
Expand Down Expand Up @@ -540,6 +543,52 @@ void MailMessageTest::testReadMultiPartDefaultTransferEncoding()
assertTrue (handler.disp()[1] == "attachment; filename=sample.dat");
}

void MailMessageTest::testReadMultiPartNoFinalBoundaryFromFile()
{
std::string data(
"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-Transfer-Encoding: 8bit\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"
);

// The problem #2401 occurs during reading message from file.
// (For stringstreams it works fine.)
TemporaryFile file;
std::ofstream(file.path()) << data;
std::ifstream istr(file.path());

StringPartHandler handler;
MailMessage message;
message.read(istr, handler);

assertTrue (handler.data().size() == 2);
assertTrue (handler.data()[0] == "Hello World!\r\n");
assertTrue (handler.type()[0] == "text/plain");
assertTrue (handler.disp()[0] == "inline");

assertTrue (handler.data()[1] == "This is some binary data. Really.");
assertTrue (handler.type()[1] == "application/octet-stream; name=sample");
assertTrue (handler.disp()[1] == "attachment; filename=sample.dat");
}


void MailMessageTest::testReadWriteMultiPart()
{
Expand Down Expand Up @@ -839,6 +888,7 @@ CppUnit::Test* MailMessageTest::suite()
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPartWithAttachmentNames);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPartDefaultTransferEncoding);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPartNoFinalBoundaryFromFile);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPartStore);
CppUnit_addTest(pSuite, MailMessageTest, testEncodeWord);
Expand Down
1 change: 1 addition & 0 deletions Net/testsuite/src/MailMessageTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MailMessageTest: public CppUnit::TestCase
void testReadMultiPart();
void testReadMultiPartWithAttachmentNames();
void testReadMultiPartDefaultTransferEncoding();
void testReadMultiPartNoFinalBoundaryFromFile();
void testEncodeWord();
void testDecodeWord();

Expand Down

0 comments on commit 4f80bb9

Please sign in to comment.