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"
36 #include "ParsedContentType.h"
37 #include "SharedBufferChunkReader.h"
38 #include <wtf/HashMap.h>
39 #include <wtf/text/CString.h>
40 #include <wtf/text/StringBuilder.h>
41 #include <wtf/text/StringConcatenate.h>
42 #include <wtf/text/StringHash.h>
46 typedef HashMap<String, String> KeyValueMap;
48 static KeyValueMap retrieveKeyValuePairs(WebCore::SharedBufferChunkReader& buffer)
50 KeyValueMap keyValuePairs;
54 while (!(line = buffer.nextChunkAsUTF8StringWithLatin1Fallback()).isNull()) {
56 break; // Empty line means end of key/value section.
57 if (line[0] == '\t') {
58 ASSERT(!key.isEmpty());
59 value.append(line.substring(1));
62 // New key/value, store the previous one if any.
64 if (keyValuePairs.find(key) != keyValuePairs.end())
65 LOG_ERROR("Key duplicate found in MIME header. Key is '%s', previous value replaced.", key.ascii().data());
66 keyValuePairs.add(key, value.toString().stripWhiteSpace());
70 size_t semiColonIndex = line.find(':');
71 if (semiColonIndex == notFound) {
72 // This is not a key value pair, ignore.
75 key = line.substring(0, semiColonIndex).convertToASCIILowercase().stripWhiteSpace();
76 value.append(line.substring(semiColonIndex + 1));
78 // Store the last property if there is one.
80 keyValuePairs.set(key, value.toString().stripWhiteSpace());
84 RefPtr<MIMEHeader> MIMEHeader::parseHeader(SharedBufferChunkReader& buffer)
86 auto mimeHeader = adoptRef(*new MIMEHeader);
87 KeyValueMap keyValuePairs = retrieveKeyValuePairs(buffer);
88 KeyValueMap::iterator mimeParametersIterator = keyValuePairs.find("content-type");
89 if (mimeParametersIterator != keyValuePairs.end()) {
90 ParsedContentType parsedContentType(mimeParametersIterator->value);
91 mimeHeader->m_contentType = parsedContentType.mimeType();
92 if (!mimeHeader->isMultipart())
93 mimeHeader->m_charset = parsedContentType.charset().stripWhiteSpace();
95 mimeHeader->m_multipartType = parsedContentType.parameterValueForName("type");
96 mimeHeader->m_endOfPartBoundary = parsedContentType.parameterValueForName("boundary");
97 if (mimeHeader->m_endOfPartBoundary.isNull()) {
98 LOG_ERROR("No boundary found in multipart MIME header.");
101 mimeHeader->m_endOfPartBoundary = "--" + mimeHeader->m_endOfPartBoundary;
102 mimeHeader->m_endOfDocumentBoundary = mimeHeader->m_endOfPartBoundary + "--";
106 mimeParametersIterator = keyValuePairs.find("content-transfer-encoding");
107 if (mimeParametersIterator != keyValuePairs.end())
108 mimeHeader->m_contentTransferEncoding = parseContentTransferEncoding(mimeParametersIterator->value);
110 mimeParametersIterator = keyValuePairs.find("content-location");
111 if (mimeParametersIterator != keyValuePairs.end())
112 mimeHeader->m_contentLocation = mimeParametersIterator->value;
114 return WTFMove(mimeHeader);
117 MIMEHeader::Encoding MIMEHeader::parseContentTransferEncoding(const String& text)
119 String encoding = text.stripWhiteSpace();
120 if (equalLettersIgnoringASCIICase(encoding, "base64"))
122 if (equalLettersIgnoringASCIICase(encoding, "quoted-printable"))
123 return QuotedPrintable;
124 if (equalLettersIgnoringASCIICase(encoding, "7bit"))
126 if (equalLettersIgnoringASCIICase(encoding, "binary"))
128 LOG_ERROR("Unknown encoding '%s' found in MIME header.", text.ascii().data());
132 MIMEHeader::MIMEHeader()
133 : m_contentTransferEncoding(Unknown)