#include "config.h"
#if ENABLE(MHTML)
#include "MHTMLParser.h"
#include "MHTMLArchive.h"
#include "MIMEHeader.h"
#include "MIMETypeRegistry.h"
#include "QuotedPrintable.h"
#include <wtf/text/Base64.h>
namespace WebCore {
static bool skipLinesUntilBoundaryFound(SharedBufferChunkReader& lineReader, const String& boundary)
{
String line;
while (!(line = lineReader.nextChunkAsUTF8StringWithLatin1Fallback()).isNull()) {
if (line == boundary)
return true;
}
return false;
}
MHTMLParser::MHTMLParser(SharedBuffer* data)
: m_lineReader(data, "\r\n")
{
}
RefPtr<MHTMLArchive> MHTMLParser::parseArchive()
{
return parseArchiveWithHeader(MIMEHeader::parseHeader(m_lineReader).get());
}
RefPtr<MHTMLArchive> MHTMLParser::parseArchiveWithHeader(MIMEHeader* header)
{
if (!header) {
LOG_ERROR("Failed to parse MHTML part: no header.");
return nullptr;
}
auto archive = MHTMLArchive::create();
if (!header->isMultipart()) {
bool endOfArchiveReached = false;
RefPtr<ArchiveResource> resource = parseNextPart(*header, String(), String(), endOfArchiveReached);
if (!resource)
return nullptr;
archive->setMainResource(resource.releaseNonNull());
return WTFMove(archive);
}
skipLinesUntilBoundaryFound(m_lineReader, header->endOfPartBoundary());
bool endOfArchive = false;
while (!endOfArchive) {
RefPtr<MIMEHeader> resourceHeader = MIMEHeader::parseHeader(m_lineReader);
if (!resourceHeader) {
LOG_ERROR("Failed to parse MHTML, invalid MIME header.");
return nullptr;
}
if (resourceHeader->contentType() == "multipart/alternative") {
RefPtr<MHTMLArchive> subframeArchive = parseArchiveWithHeader(resourceHeader.get());
if (!subframeArchive) {
LOG_ERROR("Failed to parse MHTML subframe.");
return nullptr;
}
bool endOfPartReached = skipLinesUntilBoundaryFound(m_lineReader, header->endOfPartBoundary());
ASSERT_UNUSED(endOfPartReached, endOfPartReached);
if (subframeArchive->mainResource())
addResourceToArchive(subframeArchive->mainResource(), archive.ptr());
archive->addSubframeArchive(subframeArchive.releaseNonNull());
continue;
}
RefPtr<ArchiveResource> resource = parseNextPart(*resourceHeader, header->endOfPartBoundary(), header->endOfDocumentBoundary(), endOfArchive);
if (!resource) {
LOG_ERROR("Failed to parse MHTML part.");
return nullptr;
}
addResourceToArchive(resource.get(), archive.ptr());
}
return WTFMove(archive);
}
void MHTMLParser::addResourceToArchive(ArchiveResource* resource, MHTMLArchive* archive)
{
const String& mimeType = resource->mimeType();
if (!MIMETypeRegistry::isSupportedNonImageMIMEType(mimeType) || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) || mimeType == "text/css") {
m_resources.append(resource);
return;
}
if (!archive->mainResource()) {
archive->setMainResource(*resource);
m_frames.append(archive);
return;
}
RefPtr<MHTMLArchive> subframe = MHTMLArchive::create();
subframe->setMainResource(*resource);
m_frames.append(subframe);
}
RefPtr<ArchiveResource> MHTMLParser::parseNextPart(const MIMEHeader& mimeHeader, const String& endOfPartBoundary, const String& endOfDocumentBoundary, bool& endOfArchiveReached)
{
ASSERT(endOfPartBoundary.isEmpty() == endOfDocumentBoundary.isEmpty());
RefPtr<SharedBuffer> content = SharedBuffer::create();
const bool checkBoundary = !endOfPartBoundary.isEmpty();
bool endOfPartReached = false;
if (mimeHeader.contentTransferEncoding() == MIMEHeader::Binary) {
if (!checkBoundary) {
LOG_ERROR("Binary contents requires end of part");
return nullptr;
}
m_lineReader.setSeparator(endOfPartBoundary.utf8().data());
Vector<char> part;
if (!m_lineReader.nextChunk(part)) {
LOG_ERROR("Binary contents requires end of part");
return nullptr;
}
content->append(WTFMove(part));
m_lineReader.setSeparator("\r\n");
Vector<char> nextChars;
if (m_lineReader.peek(nextChars, 2) != 2) {
LOG_ERROR("Invalid seperator.");
return nullptr;
}
endOfPartReached = true;
ASSERT(nextChars.size() == 2);
endOfArchiveReached = (nextChars[0] == '-' && nextChars[1] == '-');
if (!endOfArchiveReached) {
String line = m_lineReader.nextChunkAsUTF8StringWithLatin1Fallback();
if (!line.isEmpty()) {
LOG_ERROR("No CRLF at end of binary section.");
return nullptr;
}
}
} else {
String line;
while (!(line = m_lineReader.nextChunkAsUTF8StringWithLatin1Fallback()).isNull()) {
endOfArchiveReached = (line == endOfDocumentBoundary);
if (checkBoundary && (line == endOfPartBoundary || endOfArchiveReached)) {
endOfPartReached = true;
break;
}
content->append(line.utf8().data(), line.length());
if (mimeHeader.contentTransferEncoding() == MIMEHeader::QuotedPrintable) {
content->append("\r\n", 2);
}
}
}
if (!endOfPartReached && checkBoundary) {
LOG_ERROR("No bounday found for MHTML part.");
return nullptr;
}
Vector<char> data;
switch (mimeHeader.contentTransferEncoding()) {
case MIMEHeader::Base64:
if (!base64Decode(content->data(), content->size(), data)) {
LOG_ERROR("Invalid base64 content for MHTML part.");
return nullptr;
}
break;
case MIMEHeader::QuotedPrintable:
quotedPrintableDecode(content->data(), content->size(), data);
break;
case MIMEHeader::SevenBit:
case MIMEHeader::Binary:
data.append(content->data(), content->size());
break;
default:
LOG_ERROR("Invalid encoding for MHTML part.");
return nullptr;
}
RefPtr<SharedBuffer> contentBuffer = SharedBuffer::create(WTFMove(data));
URL location = URL(URL(), mimeHeader.contentLocation());
return ArchiveResource::create(WTFMove(contentBuffer), location, mimeHeader.contentType(), mimeHeader.charset(), String());
}
size_t MHTMLParser::frameCount() const
{
return m_frames.size();
}
MHTMLArchive* MHTMLParser::frameAt(size_t index) const
{
return m_frames[index].get();
}
size_t MHTMLParser::subResourceCount() const
{
return m_resources.size();
}
ArchiveResource* MHTMLParser::subResourceAt(size_t index) const
{
return m_resources[index].get();
}
}
#endif