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 "ContentTypeParser.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).lower().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 PassRefPtr<MIMEHeader> MIMEHeader::parseHeader(SharedBufferChunkReader* buffer)
84 RefPtr<MIMEHeader> mimeHeader = adoptRef(new MIMEHeader);
85 KeyValueMap keyValuePairs = retrieveKeyValuePairs(buffer);
86 KeyValueMap::iterator mimeParametersIterator = keyValuePairs.find("content-type");
87 if (mimeParametersIterator != keyValuePairs.end()) {
88 // FIXME: make ContentTypeParser more flexible so we don't have to synthesize the "Content-Type:".
89 ContentTypeParser contentTypeParser(makeString("Content-Type:", mimeParametersIterator->second));
90 mimeHeader->m_contentType = contentTypeParser.mimeType();
91 if (!mimeHeader->isMultipart())
92 mimeHeader->m_charset = contentTypeParser.charset().stripWhiteSpace();
94 mimeHeader->m_multipartType = contentTypeParser.parameterValueForName("type");
95 mimeHeader->m_endOfPartBoundary = contentTypeParser.parameterValueForName("boundary");
96 if (mimeHeader->m_endOfPartBoundary.isNull()) {
97 LOG_ERROR("No boundary found in multipart MIME header.");
100 mimeHeader->m_endOfPartBoundary.insert("--", 0);
101 mimeHeader->m_endOfDocumentBoundary = mimeHeader->m_endOfPartBoundary;
102 mimeHeader->m_endOfDocumentBoundary.append("--");
106 mimeParametersIterator = keyValuePairs.find("content-transfer-encoding");
107 if (mimeParametersIterator != keyValuePairs.end())
108 mimeHeader->m_contentTransferEncoding = parseContentTransferEncoding(mimeParametersIterator->second);
110 mimeParametersIterator = keyValuePairs.find("content-location");
111 if (mimeParametersIterator != keyValuePairs.end())
112 mimeHeader->m_contentLocation = mimeParametersIterator->second;
114 return mimeHeader.release();
117 MIMEHeader::Encoding MIMEHeader::parseContentTransferEncoding(const String& text)
119 String encoding = text.stripWhiteSpace().lower();
120 if (encoding == "base64")
122 if (encoding == "quoted-printable")
123 return QuotedPrintable;
124 if (encoding == "7bit")
126 if (encoding == "binary")
128 LOG_ERROR("Unknown encoding '%s' found in MIME header.", text.ascii().data());
132 MIMEHeader::MIMEHeader()
133 : m_contentTransferEncoding(Unknown)