initial import
[vuplus_webkit] / Source / WebCore / platform / network / MIMEHeader.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "MIMEHeader.h"
33
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>
41
42 namespace WebCore {
43
44 typedef HashMap<String, String> KeyValueMap;
45
46 static KeyValueMap retrieveKeyValuePairs(WebCore::SharedBufferChunkReader* buffer)
47 {
48     KeyValueMap keyValuePairs;
49     String line;
50     String key;
51     StringBuilder value;
52     while (!(line = buffer->nextChunkAsUTF8StringWithLatin1Fallback()).isNull()) {
53         if (line.isEmpty())
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));
58             continue;
59         }
60         // New key/value, store the previous one if any.
61         if (!key.isEmpty()) {
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());
65             key = String();
66             value.clear();
67         }
68         size_t semiColonIndex = line.find(':');
69         if (semiColonIndex == notFound) {
70             // This is not a key value pair, ignore.
71             continue;
72         }
73         key = line.substring(0, semiColonIndex).lower().stripWhiteSpace();
74         value.append(line.substring(semiColonIndex + 1));
75     }
76     // Store the last property if there is one.
77     if (!key.isEmpty())
78         keyValuePairs.set(key, value.toString().stripWhiteSpace());
79     return keyValuePairs;
80 }
81
82 PassRefPtr<MIMEHeader> MIMEHeader::parseHeader(SharedBufferChunkReader* buffer)
83 {
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();
93         else {
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.");
98                 return 0;
99             }
100             mimeHeader->m_endOfPartBoundary.insert("--", 0);
101             mimeHeader->m_endOfDocumentBoundary = mimeHeader->m_endOfPartBoundary;
102             mimeHeader->m_endOfDocumentBoundary.append("--");
103         }
104     }
105
106     mimeParametersIterator = keyValuePairs.find("content-transfer-encoding");
107     if (mimeParametersIterator != keyValuePairs.end())
108         mimeHeader->m_contentTransferEncoding = parseContentTransferEncoding(mimeParametersIterator->second);
109
110     mimeParametersIterator = keyValuePairs.find("content-location");
111     if (mimeParametersIterator != keyValuePairs.end())
112         mimeHeader->m_contentLocation = mimeParametersIterator->second;
113
114     return mimeHeader.release();
115 }
116
117 MIMEHeader::Encoding MIMEHeader::parseContentTransferEncoding(const String& text)
118 {
119     String encoding = text.stripWhiteSpace().lower();
120     if (encoding == "base64")
121         return Base64;
122     if (encoding == "quoted-printable")
123         return QuotedPrintable;
124     if (encoding == "7bit")
125         return SevenBit;
126     if (encoding == "binary")
127         return Binary;
128     LOG_ERROR("Unknown encoding '%s' found in MIME header.", text.ascii().data());
129     return Unknown;
130 }
131
132 MIMEHeader::MIMEHeader()
133     : m_contentTransferEncoding(Unknown)
134 {
135 }
136
137 }