2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "MIMEHeader.h"
34 #include "ParsedContentType.h"
35 #include "SharedBufferChunkReader.h"
36 #include <wtf/HashMap.h>
37 #include <wtf/text/CString.h>
38 #include <wtf/text/StringBuilder.h>
39 #include <wtf/text/StringConcatenate.h>
40 #include <wtf/text/StringHash.h>
44 typedef HashMap<String, String> KeyValueMap;
46 static KeyValueMap retrieveKeyValuePairs(WebCore::SharedBufferChunkReader& buffer)
48 KeyValueMap keyValuePairs;
52 while (!(line = buffer.nextChunkAsUTF8StringWithLatin1Fallback()).isNull()) {
54 break; // Empty line means end of key/value section.
55 if (line[0] == '\t') {
56 ASSERT(!key.isEmpty());
57 value.append(line.substring(1));
60 // New key/value, store the previous one if any.
62 if (keyValuePairs.find(key) != keyValuePairs.end())
63 LOG_ERROR("Key duplicate found in MIME header. Key is '%s', previous value replaced.", key.ascii().data());
64 keyValuePairs.add(key, value.toString().stripWhiteSpace());
68 size_t semiColonIndex = line.find(':');
69 if (semiColonIndex == notFound) {
70 // This is not a key value pair, ignore.
73 key = line.substring(0, semiColonIndex).convertToASCIILowercase().stripWhiteSpace();
74 value.append(line.substring(semiColonIndex + 1));
76 // Store the last property if there is one.
78 keyValuePairs.set(key, value.toString().stripWhiteSpace());
82 RefPtr<MIMEHeader> MIMEHeader::parseHeader(SharedBufferChunkReader& buffer)
84 auto mimeHeader = adoptRef(*new MIMEHeader);
85 KeyValueMap keyValuePairs = retrieveKeyValuePairs(buffer);
86 KeyValueMap::iterator mimeParametersIterator = keyValuePairs.find("content-type");
87 if (mimeParametersIterator != keyValuePairs.end()) {
88 ParsedContentType parsedContentType(mimeParametersIterator->value);
89 mimeHeader->m_contentType = parsedContentType.mimeType();
90 if (!mimeHeader->isMultipart())
91 mimeHeader->m_charset = parsedContentType.charset().stripWhiteSpace();
93 mimeHeader->m_multipartType = parsedContentType.parameterValueForName("type");
94 mimeHeader->m_endOfPartBoundary = parsedContentType.parameterValueForName("boundary");
95 if (mimeHeader->m_endOfPartBoundary.isNull()) {
96 LOG_ERROR("No boundary found in multipart MIME header.");
99 mimeHeader->m_endOfPartBoundary = "--" + mimeHeader->m_endOfPartBoundary;
100 mimeHeader->m_endOfDocumentBoundary = mimeHeader->m_endOfPartBoundary + "--";
104 mimeParametersIterator = keyValuePairs.find("content-transfer-encoding");
105 if (mimeParametersIterator != keyValuePairs.end())
106 mimeHeader->m_contentTransferEncoding = parseContentTransferEncoding(mimeParametersIterator->value);
108 mimeParametersIterator = keyValuePairs.find("content-location");
109 if (mimeParametersIterator != keyValuePairs.end())
110 mimeHeader->m_contentLocation = mimeParametersIterator->value;
112 return WTFMove(mimeHeader);
115 MIMEHeader::Encoding MIMEHeader::parseContentTransferEncoding(const String& text)
117 String encoding = text.stripWhiteSpace();
118 if (equalLettersIgnoringASCIICase(encoding, "base64"))
120 if (equalLettersIgnoringASCIICase(encoding, "quoted-printable"))
121 return QuotedPrintable;
122 if (equalLettersIgnoringASCIICase(encoding, "7bit"))
124 if (equalLettersIgnoringASCIICase(encoding, "binary"))
126 LOG_ERROR("Unknown encoding '%s' found in MIME header.", text.ascii().data());
130 MIMEHeader::MIMEHeader()
131 : m_contentTransferEncoding(Unknown)