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 | #if ENABLE(MHTML) |
35 | |
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> |
43 | |
44 | namespace WebCore { |
45 | |
46 | typedef HashMap<String, String> KeyValueMap; |
47 | |
48 | static KeyValueMap retrieveKeyValuePairs(WebCore::SharedBufferChunkReader& buffer) |
49 | { |
50 | KeyValueMap keyValuePairs; |
51 | String line; |
52 | String key; |
53 | StringBuilder value; |
54 | while (!(line = buffer.nextChunkAsUTF8StringWithLatin1Fallback()).isNull()) { |
55 | if (line.isEmpty()) |
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)); |
60 | continue; |
61 | } |
62 | // New key/value, store the previous one if any. |
63 | if (!key.isEmpty()) { |
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()); |
67 | key = String(); |
68 | value.clear(); |
69 | } |
70 | size_t semiColonIndex = line.find(':'); |
71 | if (semiColonIndex == notFound) { |
72 | // This is not a key value pair, ignore. |
73 | continue; |
74 | } |
75 | key = line.substring(0, semiColonIndex).convertToASCIILowercase().stripWhiteSpace(); |
76 | value.append(line.substring(semiColonIndex + 1)); |
77 | } |
78 | // Store the last property if there is one. |
79 | if (!key.isEmpty()) |
80 | keyValuePairs.set(key, value.toString().stripWhiteSpace()); |
81 | return keyValuePairs; |
82 | } |
83 | |
84 | RefPtr<MIMEHeader> MIMEHeader::(SharedBufferChunkReader& buffer) |
85 | { |
86 | auto = adoptRef(*new MIMEHeader); |
87 | KeyValueMap keyValuePairs = retrieveKeyValuePairs(buffer); |
88 | KeyValueMap::iterator mimeParametersIterator = keyValuePairs.find("content-type" ); |
89 | if (mimeParametersIterator != keyValuePairs.end()) { |
90 | String contentType, charset, multipartType, endOfPartBoundary; |
91 | if (auto parsedContentType = ParsedContentType::create(mimeParametersIterator->value)) { |
92 | contentType = parsedContentType->mimeType(); |
93 | charset = parsedContentType->charset().stripWhiteSpace(); |
94 | multipartType = parsedContentType->parameterValueForName("type" ); |
95 | endOfPartBoundary = parsedContentType->parameterValueForName("boundary" ); |
96 | } |
97 | mimeHeader->m_contentType = contentType; |
98 | if (!mimeHeader->isMultipart()) |
99 | mimeHeader->m_charset = charset; |
100 | else { |
101 | mimeHeader->m_multipartType = multipartType; |
102 | mimeHeader->m_endOfPartBoundary = endOfPartBoundary; |
103 | if (mimeHeader->m_endOfPartBoundary.isNull()) { |
104 | LOG_ERROR("No boundary found in multipart MIME header." ); |
105 | return nullptr; |
106 | } |
107 | mimeHeader->m_endOfPartBoundary = "--" + mimeHeader->m_endOfPartBoundary; |
108 | mimeHeader->m_endOfDocumentBoundary = mimeHeader->m_endOfPartBoundary + "--" ; |
109 | } |
110 | } |
111 | |
112 | mimeParametersIterator = keyValuePairs.find("content-transfer-encoding" ); |
113 | if (mimeParametersIterator != keyValuePairs.end()) |
114 | mimeHeader->m_contentTransferEncoding = parseContentTransferEncoding(mimeParametersIterator->value); |
115 | |
116 | mimeParametersIterator = keyValuePairs.find("content-location" ); |
117 | if (mimeParametersIterator != keyValuePairs.end()) |
118 | mimeHeader->m_contentLocation = mimeParametersIterator->value; |
119 | |
120 | return mimeHeader; |
121 | } |
122 | |
123 | MIMEHeader::Encoding MIMEHeader::(const String& text) |
124 | { |
125 | String encoding = text.stripWhiteSpace(); |
126 | if (equalLettersIgnoringASCIICase(encoding, "base64" )) |
127 | return Base64; |
128 | if (equalLettersIgnoringASCIICase(encoding, "quoted-printable" )) |
129 | return QuotedPrintable; |
130 | if (equalLettersIgnoringASCIICase(encoding, "7bit" )) |
131 | return SevenBit; |
132 | if (equalLettersIgnoringASCIICase(encoding, "binary" )) |
133 | return Binary; |
134 | LOG_ERROR("Unknown encoding '%s' found in MIME header." , text.ascii().data()); |
135 | return Unknown; |
136 | } |
137 | |
138 | MIMEHeader::() |
139 | : m_contentTransferEncoding(Unknown) |
140 | { |
141 | } |
142 | |
143 | } |
144 | |
145 | #endif |
146 | |