XBOX::VError DecompressStream (XBOX::VStream& ioStream)
{
	XBOX::VError error = VE_COMP_LIBRARY_NOT_FOUND;

	if (XBOX::VComponentManager::IsComponentAvailable ((XBOX::CType)CZipComponent::Component_Type))
	{
		CZipComponent *zipComponent = (CZipComponent *)VComponentManager::RetainComponent ((CType)CZipComponent::Component_Type);

		if (zipComponent)
		{
			XBOX::VPtrStream decompressedStream;

			if ((XBOX::VE_OK == (error = ioStream.OpenReading())) && !decompressedStream.OpenWriting())
			{
				error = zipComponent->ExpandStream (&ioStream, &decompressedStream);
				ioStream.CloseReading();
				decompressedStream.CloseWriting();

				if ((XBOX::VE_OK == error) && (!decompressedStream.IsEmpty()))
				{
					if (XBOX::VE_OK == (error = ioStream.OpenWriting()))
					{
						ioStream.SetSize (0);
						ioStream.PutData (decompressedStream.GetDataPtr(), decompressedStream.GetDataSize());
						error = ioStream.CloseWriting();
					}
				}
			}

			zipComponent->Release();
		}
	}

	return error;
}
XBOX::VError CompressStream (XBOX::VStream& ioStream, HTTPCompressionMethod inMethod)
{
	if ((inMethod == COMPRESSION_NONE) || (inMethod > COMPRESSION_LAST_SUPPORTED_METHOD))
		return VE_INVALID_PARAMETER;

	XBOX::VError error = VE_COMP_LIBRARY_NOT_FOUND;

	if (VHTTPServer::GetZipComponentAvailable())
	{
		CZipComponent *zipComponent = VHTTPServer::RetainZipComponent();

		if (NULL != zipComponent)
		{
			XBOX::VPtrStream		compressedStream;
			EZipCompressionLevel	compressionLevel = ((inMethod == COMPRESSION_GZIP) || (inMethod == COMPRESSION_X_GZIP)) ? eCompressionLevel_GZip_BestSpeed : eCompressionLevel_BestSpeed;

			if ((XBOX::VE_OK == (error = ioStream.OpenReading())) && !compressedStream.OpenWriting())
			{
				error = zipComponent->CompressStream (&ioStream, compressionLevel, &compressedStream);
				ioStream.CloseReading();
				compressedStream.CloseWriting();

				if ((XBOX::VE_OK == error) && (!compressedStream.IsEmpty()))
				{
					if (XBOX::VE_OK == (error = ioStream.OpenWriting()))
					{
						ioStream.SetSize (0);
						ioStream.PutData (compressedStream.GetDataPtr(), compressedStream.GetDataSize());
						ioStream.CloseWriting();
					}
				}
			}

			XBOX::QuickReleaseRefCountable (zipComponent);
		}
	}

	return error;
}
Пример #3
0
XBOX::VError VMIMEMessage::ToStream (XBOX::VStream& outStream, sLONG inEncoding)
{
	XBOX::VError error = XBOX::VE_OK;

	if (XBOX::VE_OK == outStream.OpenWriting())
	{
		outStream.SetCarriageReturnMode(eCRM_CRLF);
		if (!fMIMEParts.empty())
		{
			XBOX::VString	string;
			XBOX::VString	charsetName;
			bool			bEncodeBody;	// Encode using base64.

			for (XBOX::VectorOfMIMEPart::const_iterator it = fMIMEParts.begin(); it != fMIMEParts.end(); ++it)
			{
				outStream.PutPrintf ("\r\n--%S\r\n", &fBoundary);

				string.FromCString ("Content-Type: ");
				string.AppendString ((*it)->GetMediaType());
 
				if (!(*it)->GetFileName().IsEmpty())
				{
					string.AppendCString ("; name=\"");
					if (!(*it)->GetName().IsEmpty())
						string.AppendString ((*it)->GetName());
					else
						string.AppendString ((*it)->GetFileName());

					string.AppendCString("\"\r\nContent-Disposition: ");
					string.AppendCString((*it)->IsInline() ? "inline; " : "attachment; ");					
					string.AppendCString("filename=\"");

					string.AppendString ((*it)->GetFileName());
					string.AppendCString ("\"\r\n");

					if (inEncoding == ENCODING_BINARY) {

						string.AppendCString ("Content-Transfer-Encoding: 8bit\r\n");
						bEncodeBody = false;

					} else {

						if ((inEncoding == ENCODING_BINARY_ONLY) && ((*it)->GetMediaTypeKind() == MIMETYPE_TEXT))
						{
							bEncodeBody = false;
						}
						else
						{
							string.AppendCString ("Content-Transfer-Encoding: base64\r\n");
							bEncodeBody = true;
						}
					}
				}
				else
				{
					if ((*it)->GetMediaTypeCharSet() != XBOX::VTC_UNKNOWN)
					{
						string.AppendCString ("; charset=\"");
						XBOX::VTextConverters::Get()->GetNameFromCharSet ((*it)->GetMediaTypeCharSet(), charsetName);
						string.AppendString (charsetName);
						string.AppendCString ("\"");
					}

					if (!(*it)->GetName().IsEmpty()) {

						string.AppendCString("; name=\"");
						string.AppendString((*it)->GetName());
						string.AppendCString("\"");

					}

					string.AppendCString ("\r\n");

					if ((*it)->IsInline())

						string.AppendCString("Content-Disposition: inline\r\n");
						
					if (inEncoding == ENCODING_7BIT || ((inEncoding == ENCODING_BINARY_ONLY) && ((*it)->GetMediaTypeKind() != MIMETYPE_TEXT))) {

						string.AppendCString ("Content-Transfer-Encoding: base64\r\n");
						bEncodeBody = true;

					} else {

						string.AppendCString("Content-Transfer-Encoding: 8bit\r\n");
						bEncodeBody = false;

					}
				}

				if ((*it)->GetContentID().GetLength()) {

					string.AppendCString("Content-ID: <");
					string.AppendString((*it)->GetContentID());
					string.AppendCString(">\r\n");

				}

				string.AppendCString ("\r\n");

				outStream.PutText (string);

				if (bEncodeBody)
				{
					XBOX::VMemoryBuffer<> buffer;
					if (XBOX::Base64Coder::Encode ((*it)->GetData().GetDataPtr(), (*it)->GetData().GetDataSize(), buffer, kBASE64_QUADS_PER_LINE))
					{
						outStream.PutData (buffer.GetDataPtr(), buffer.GetDataSize());
					}
				}
				else
				{
					outStream.PutData ((*it)->GetData().GetDataPtr(), (*it)->GetData().GetDataSize());
				}
			}

			outStream.PutPrintf ("\r\n--%S--\r\n", &fBoundary);
		}

		outStream.CloseWriting();
	}

	return error;
}