utility::stream::size_type body::getGeneratedSize(const generationContext& ctx)
{
	// MIME-Multipart
	if (getPartCount() != 0)
	{
		utility::stream::size_type size = 0;

		// Size of parts and boundaries
		for (size_t p = 0 ; p < getPartCount() ; ++p)
		{
			size += 100;  // boundary, CRLF...
			size += getPartAt(p)->getGeneratedSize(ctx);
		}

		// Size of prolog/epilog text
		const text prologText = getActualPrologText(ctx);

		if (!prologText.isEmpty())
		{
			std::ostringstream oss;
			utility::outputStreamAdapter osa(oss);

			prologText.encodeAndFold(ctx, osa, 0,
				NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE);

			size += oss.str().size();
		}

		const text epilogText = getActualEpilogText(ctx);

		if (!epilogText.isEmpty())
		{
			std::ostringstream oss;
			utility::outputStreamAdapter osa(oss);

			epilogText.encodeAndFold(ctx, osa, 0,
				NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE);

			size += oss.str().size();
		}

		return size;
	}
	// Simple body
	else
	{
		ref <utility::encoder::encoder> srcEncoder = m_contents->getEncoding().getEncoder();
		ref <utility::encoder::encoder> dstEncoder = getEncoding().getEncoder();

		return dstEncoder->getEncodedSize(srcEncoder->getDecodedSize(m_contents->getLength()));
	}
}
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());
	}
}
Beispiel #3
0
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());
	}
}