Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

MultipartReader ignores first part, if preamble is missing #160

Closed
jochenonline opened this Issue Apr 24, 2013 · 1 comment

Comments

Projects
None yet
2 participants

Although documentation sais "The message must not contain a preamble preceding the first encapsulation boundary." the first part is ignored, if the preamble is missing as in the following example. If a add a preamble before the frst part (any text is o.k.), the part is perfectly read.

Content-Type: multipart/mixed; boundary=MIME_boundary_227911F9473A3053
Date: Tue, 23 Apr 2013 10:41:31 GMT
From: john@doe.com
Mime-Version: 1.0
Subject: This is the subject
To: <me@doe.com>

--MIME_boundary_227911F9473A3053
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain

This is the Mailbody, which is ignored because there is no preamble.
--MIME_boundary_227911F9473A3053
Content-Disposition: attachment
Content-Transfer-Encoding: base64
Content-Type: text/plain; name=text.txt

RGllcyBpc3QgZGVyIEluaGFsdCAyenUxIHZvbiB0ZXh0LnR4dA==
--MIME_boundary_227911F9473A3053
Content-Disposition: attachment
Content-Transfer-Encoding: base64
Content-Type: text/plain; name=text2.txt

RGllcyBpc3QgZGVyIEluaGFsdCAyenUxIHZvbiB0ZXh0Mi50eHQ=
--MIME_boundary_227911F9473A3053--

The problem seems to be in this function

void MailMessage::readHeader(std::istream& istr)
{
    clear();
    MessageHeader::read(istr);
    istr.get(); // \r
    istr.get(); // \n
}

Because MessageHeader::read(istr) has only pushed back the \n from the last read line, the first istr.get() does not read \r but \n. And the second istr.get() does not read \n but the first - of the boundary line, which has the effect that this boundary is not detected in findFirstBoundary() because the first - is missing.

The corrected function looks like this:

void MailMessage::readHeader(std::istream& istr)
{
    clear();
    MessageHeader::read(istr);
    istr.get(); // \r or \n
    char ch = (char)istr.get(); // \n (potentially)
    if( ch != '\n' ) {
        // ooops, we have read the first char of the next line...
        istr.putback(ch);
    }
}

Alternatively you can omit the second istr.get() totally, but this way it seemed safer for me.

@ghost ghost assigned aleks-f Apr 26, 2013

aleks-f added a commit that referenced this issue Apr 28, 2013

GH160: MultipartReader ignores first part, if preamble is missing
fixed GH #160: MultipartReader ignores first part, if preamble is
missing

@aleks-f aleks-f closed this Apr 28, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment