void body::initNewPart(shared_ptr <bodyPart> part) { // A part can be in only one body at the same time: if part is // already attached to a parent part, remove it from the current // parent part if (part->getParentPart()) part->getParentPart()->getBody()->removePart(part); if (m_part != NULL) { m_part->importChildPart(part); shared_ptr <header> hdr = m_part->getHeader(); // Check whether we have a boundary string shared_ptr <contentTypeField> ctf = hdr->findField <contentTypeField>(fields::CONTENT_TYPE); if (ctf) { if (ctf->hasBoundary()) { const string boundary = ctf->getBoundary(); if (boundary.empty() || !isValidBoundary(boundary)) ctf->setBoundary(generateRandomBoundaryString()); } else { // No "boundary" parameter: generate a random one. ctf->setBoundary(generateRandomBoundaryString()); } if (ctf->getValue <mediaType>()->getType() != mediaTypes::MULTIPART) { // Warning: multi-part body but the Content-Type is // not specified as "multipart/..." } } else { // No "Content-Type" field: create a new one and generate // a random boundary string. ctf = hdr->getField <contentTypeField>(fields::CONTENT_TYPE); ctf->setValue(mediaType(mediaTypes::MULTIPART, mediaTypes::MULTIPART_MIXED)); ctf->setBoundary(generateRandomBoundaryString()); } } }
void body::initNewPart(ref <bodyPart> part) { part->m_parent = m_part; ref <header> hdr = m_header.acquire(); if (hdr != NULL) { // Check whether we have a boundary string try { ref <contentTypeField> ctf = hdr->findField(fields::CONTENT_TYPE).dynamicCast <contentTypeField>(); try { const string boundary = ctf->getBoundary(); if (boundary.empty() || !isValidBoundary(boundary)) ctf->setBoundary(generateRandomBoundaryString()); } catch (exceptions::no_such_parameter&) { // No "boundary" parameter: generate a random one. ctf->setBoundary(generateRandomBoundaryString()); } if (ctf->getValue().dynamicCast <const mediaType>()->getType() != mediaTypes::MULTIPART) { // Warning: multi-part body but the Content-Type is // not specified as "multipart/..." } } catch (exceptions::no_such_field&) { // No "Content-Type" field: create a new one and generate // a random boundary string. ref <contentTypeField> ctf = hdr->getField(fields::CONTENT_TYPE).dynamicCast <contentTypeField>(); ctf->setValue(mediaType(mediaTypes::MULTIPART, mediaTypes::MULTIPART_MIXED)); ctf->setBoundary(generateRandomBoundaryString()); } } }
void body::generateImpl(utility::outputStream& os, const string::size_type maxLineLength, const string::size_type /* curLinePos */, string::size_type* newLinePos) const { // MIME-Multipart if (getPartCount() != 0) { string boundary; if (m_header.acquire() == NULL) { boundary = generateRandomBoundaryString(); } else { try { ref <const contentTypeField> ctf = m_header.acquire()->findField(fields::CONTENT_TYPE) .dynamicCast <const contentTypeField>(); boundary = ctf->getBoundary(); } catch (exceptions::no_such_field&) { // Warning: no content-type and no boundary string specified! boundary = generateRandomBoundaryString(); } catch (exceptions::no_such_parameter&) { // Warning: no boundary string specified! boundary = generateRandomBoundaryString(); } } const string& prologText = m_prologText.empty() ? (isRootPart() ? options::getInstance()->multipart.getPrologText() : NULL_STRING ) : m_prologText; const string& epilogText = m_epilogText.empty() ? (isRootPart() ? options::getInstance()->multipart.getEpilogText() : NULL_STRING ) : m_epilogText; if (!prologText.empty()) { text prolog(prologText, vmime::charset("us-ascii")); prolog.encodeAndFold(os, maxLineLength, 0, NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE); os << CRLF; } os << "--" << boundary; for (int p = 0 ; p < getPartCount() ; ++p) { os << CRLF; getPartAt(p)->generate(os, maxLineLength, 0); os << CRLF << "--" << boundary; } os << "--" << CRLF; if (!epilogText.empty()) { text epilog(epilogText, vmime::charset("us-ascii")); epilog.encodeAndFold(os, maxLineLength, 0, NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE); os << CRLF; } if (newLinePos) *newLinePos = 0; } // Simple body else { // Generate the contents m_contents->generate(os, getEncoding(), maxLineLength); } }
void body::generateImpl (const generationContext& ctx, utility::outputStream& os, const string::size_type /* curLinePos */, string::size_type* newLinePos) const { // MIME-Multipart if (getPartCount() != 0) { string boundary; if (m_header.acquire() == NULL) { boundary = generateRandomBoundaryString(); } else { try { ref <const contentTypeField> ctf = m_header.acquire()->findField(fields::CONTENT_TYPE) .dynamicCast <const contentTypeField>(); boundary = ctf->getBoundary(); } catch (exceptions::no_such_field&) { // Warning: no content-type and no boundary string specified! boundary = generateRandomBoundaryString(); } catch (exceptions::no_such_parameter&) { // Warning: no boundary string specified! boundary = generateRandomBoundaryString(); } } const text prologText = getActualPrologText(ctx); const text epilogText = getActualEpilogText(ctx); if (!prologText.isEmpty()) { prologText.encodeAndFold(ctx, os, 0, NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE); os << CRLF; } os << "--" << boundary; for (size_t p = 0 ; p < getPartCount() ; ++p) { os << CRLF; getPartAt(p)->generate(ctx, os, 0); os << CRLF << "--" << boundary; } os << "--" << CRLF; if (!epilogText.isEmpty()) { epilogText.encodeAndFold(ctx, os, 0, NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE); os << CRLF; } if (newLinePos) *newLinePos = 0; } // Simple body else { // Generate the contents ref <contentHandler> contents = m_contents->clone(); contents->setContentTypeHint(getContentType()); contents->generate(os, getEncoding(), ctx.getMaxLineLength()); } }
void body::generateImpl (const generationContext& ctx, utility::outputStream& os, const size_t /* curLinePos */, size_t* newLinePos) const { // MIME-Multipart if (getPartCount() != 0) { string boundary; if (!m_part) { boundary = generateRandomBoundaryString(); } else { // Use current boundary string, if specified. If no "Content-Type" field is // present, or the boundary is not specified, generate a random one shared_ptr <contentTypeField> ctf = m_part->getHeader()->findField <contentTypeField>(fields::CONTENT_TYPE); if (ctf) { if (ctf->hasBoundary()) { boundary = ctf->getBoundary(); } else { // No boundary string specified boundary = generateRandomBoundaryString(); } } else { // No Content-Type (and no boundary string specified) boundary = generateRandomBoundaryString(); } } const text prologText = getActualPrologText(ctx); const text epilogText = getActualEpilogText(ctx); if (!prologText.isEmpty()) { prologText.encodeAndFold(ctx, os, 0, NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE); os << CRLF; } os << "--" << boundary; for (size_t p = 0 ; p < getPartCount() ; ++p) { os << CRLF; getPartAt(p)->generate(ctx, os, 0); os << CRLF << "--" << boundary; } os << "--" << CRLF; if (!epilogText.isEmpty()) { epilogText.encodeAndFold(ctx, os, 0, NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE); os << CRLF; } if (newLinePos) *newLinePos = 0; } // Simple body else { // Generate the contents shared_ptr <contentHandler> contents = m_contents->clone(); contents->setContentTypeHint(getContentType()); contents->generate(os, getEncoding(), ctx.getMaxLineLength()); } }