OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "modules/fetch/MultipartParser.h" | |
6 | |
7 #include "platform/HTTPNames.h" | |
8 #include "public/platform/Platform.h" | |
9 | |
10 #include <algorithm> | |
11 #include <utility> | |
12 | |
13 namespace blink { | |
14 | |
15 namespace { | |
16 | |
17 constexpr char kCloseDelimiterSuffix[] = "--\r\n"; | |
18 constexpr size_t kCloseDelimiterSuffixSize = WTF_ARRAY_LENGTH(kCloseDelimiterSuf fix) - 1u; | |
19 constexpr size_t kDashBoundaryOffset = 2u; // The length of "--". | |
20 constexpr char kDelimiterSuffix[] = "\r\n"; | |
21 constexpr size_t kDelimiterSuffixSize = WTF_ARRAY_LENGTH(kDelimiterSuffix) - 1u; | |
22 | |
23 } // namespace | |
24 | |
25 MultipartParser::Buffer::Buffer() = default; | |
26 | |
27 MultipartParser::Buffer::Buffer(const char* first, const char* last) | |
28 : Buffer(first, static_cast<size_t>(last - first), static_cast<size_t>(last - first)) {} | |
29 | |
30 MultipartParser::Buffer::Buffer(const char* data, size_t size, size_t capacity) | |
31 : m_capacity(capacity), m_data(data), m_size(size) {} | |
32 | |
33 bool MultipartParser::Buffer::appendIfExpected(char value) | |
34 { | |
35 DCHECK_LT(m_size, m_capacity); | |
36 if (value != m_data[m_size]) | |
37 return false; | |
38 ++m_size; | |
39 return true; | |
40 } | |
41 | |
42 bool MultipartParser::Buffer::appendIfExpected(const char* first, const char* la st) | |
43 { | |
44 while (first < last) { | |
45 if (!appendIfExpected(*first++)) | |
46 return false; | |
47 } | |
48 return true; | |
49 } | |
50 | |
51 void MultipartParser::Buffer::clear() | |
52 { | |
53 m_size = 0u; | |
54 } | |
55 | |
56 MultipartParser::MultipartParser(Vector<char> boundary, Client* client) | |
57 : m_client(client) | |
58 , m_delimiter(std::move(boundary)) | |
59 , m_state(ParsingPreamble) | |
60 { | |
61 // The delimiter consists of "\r\n" and a dash boundary which consists of | |
62 // "--" and a boundary. | |
63 m_delimiter.prepend("\r\n--", 4u); | |
64 m_bufferedBytes = delimiterBuffer(kDashBoundaryOffset); | |
65 } | |
66 | |
67 bool MultipartParser::appendData(const char* bytes, size_t size) | |
68 { | |
69 DCHECK_NE(Finished, m_state); | |
70 DCHECK_NE(Cancelled, m_state); | |
71 | |
72 const char* bytesEnd = bytes + size; | |
73 | |
74 while (bytes < bytesEnd) { | |
75 switch (m_state) { | |
76 case ParsingPreamble: { | |
77 // Parse either a preamble and a delimiter or a dash boundary. | |
78 Buffer unusedPreambleData; | |
79 if (parseDataAndDelimiter(&bytes, bytesEnd, &unusedPreambleData)) | |
80 m_state = ParsingDelimiterSuffix; | |
81 break; | |
82 } | |
83 | |
84 case ParsingDelimiterSuffix: | |
85 // Parse transport padding and "\r\n" after a delimiter. | |
86 // This can happen after either a preamble or part octets. | |
87 if (m_bufferedBytes.empty()) { | |
88 m_bufferedBytes = delimiterSuffixBuffer(); | |
89 parseTransportPadding(&bytes, bytesEnd); | |
90 } | |
91 while (m_state == ParsingDelimiterSuffix && bytes < bytesEnd) { | |
92 if (!m_bufferedBytes.appendIfExpected(*bytes++)) { | |
93 m_state = Failed; | |
yhirano
2016/09/20 09:49:53
If bytes == bytesEnd this function returns true. I
e_hakkinen
2016/09/20 21:59:29
True. Fixed.
| |
94 } else if (m_bufferedBytes.size() == kDelimiterSuffixSize) { | |
yhirano
2016/09/20 09:49:53
How about having isComplete method rather than com
e_hakkinen
2016/09/20 21:59:29
Done.
| |
95 m_bufferedBytes.clear(); | |
96 m_state = ParsingPartHeaderFields; | |
97 } | |
98 } | |
99 break; | |
100 | |
101 case ParsingPartHeaderFields: { | |
102 // Parse part header fields (which ends with "\r\n") and an empty | |
103 // line (which also ends with "\r\n"). | |
104 // This can happen after a delimiter and a delimiter suffix which | |
105 // can happen after either a preamble or part octets. | |
106 WebURLResponse response; | |
107 if (parseHeaderFields(&bytes, bytesEnd, &response)) { | |
108 m_bufferedBytes = delimiterBuffer(); | |
109 m_state = ParsingPartOctets; | |
110 m_client->partHeaderFieldsInMultipartReceived(response.toResourc eResponse()); | |
111 } | |
112 break; | |
113 } | |
114 | |
115 case ParsingPartOctets: { | |
116 // Parse either a non-empty part octets and a delimiter or an empty | |
117 // part octets and a dash boundary. | |
118 // This can happen only after part header fields. | |
119 Buffer octetsData; | |
120 if (parseDataAndDelimiter(&bytes, bytesEnd, &octetsData)) | |
121 m_state = ParsingDelimiterOrCloseDelimiterSuffix; | |
122 if (!octetsData.empty()) | |
123 m_client->partDataInMultipartReceived(octetsData.data(), octetsD ata.size()); | |
124 if (m_state == ParsingDelimiterOrCloseDelimiterSuffix) | |
125 m_client->partDataInMultipartFullyReceived(); | |
126 break; | |
127 } | |
128 | |
129 case ParsingDelimiterOrCloseDelimiterSuffix: | |
130 // Determine whether this is a delimiter suffix or a close | |
131 // delimiter suffix. | |
132 // This can happen only after part octets. | |
133 m_state = *bytes == '-' ? ParsingCloseDelimiterSuffix : ParsingDelim iterSuffix; | |
134 break; | |
135 | |
136 case ParsingCloseDelimiterSuffix: | |
137 // Parse "--", transport padding and "\r\n" after a delimiter | |
138 // (a delimiter and "--" constitute a close delimiter). | |
139 // This can happen only after part octets. | |
140 if (m_bufferedBytes.empty()) | |
141 m_bufferedBytes = closeDelimiterSuffixBuffer(); | |
142 while (m_state == ParsingCloseDelimiterSuffix && bytes < bytesEnd) { | |
143 if (m_bufferedBytes.size() == 2u && parseTransportPadding(&bytes , bytesEnd)) | |
yhirano
2016/09/20 09:49:53
What do you think about the following pattern?
wh
e_hakkinen
2016/09/20 21:59:29
I am equally fine with that. Done.
| |
144 continue; | |
145 if (!m_bufferedBytes.appendIfExpected(*bytes++)) { | |
146 m_state = Failed; | |
147 } else if (m_bufferedBytes.size() == kCloseDelimiterSuffixSize) { | |
148 m_bufferedBytes.clear(); | |
149 m_state = ParsingEpilogue; | |
150 } | |
151 } | |
152 break; | |
153 | |
154 case ParsingEpilogue: | |
155 // Parse an epilogue (by ignoring it). | |
156 // This can happen only after a delimiter and a close delimiter | |
157 // suffix which can happen only after part octets. | |
158 return true; | |
159 | |
160 case Cancelled: | |
161 case Finished: | |
162 // The client changed the state. | |
163 return true; | |
164 | |
165 case Failed: | |
166 // Keep failing. | |
167 return false; | |
168 } | |
169 } | |
170 | |
171 DCHECK_EQ(bytesEnd, bytes); | |
172 | |
173 return true; | |
174 } | |
175 | |
176 void MultipartParser::cancel() | |
177 { | |
178 m_state = Cancelled; | |
179 } | |
180 | |
181 bool MultipartParser::finish() | |
182 { | |
183 DCHECK_NE(Cancelled, m_state); | |
184 | |
185 State initialState = m_state; | |
186 | |
187 if (m_state == ParsingPartOctets && !m_bufferedBytes.empty()) { | |
188 // The end of append bytes looked like a delimiter but was not a full | |
189 // one, after all. Treat the those bytes as part of part octets. | |
190 m_client->partDataInMultipartReceived(m_bufferedBytes.data(), m_buffered Bytes.size()); | |
191 m_bufferedBytes.clear(); | |
192 } | |
193 m_state = Finished; | |
194 | |
195 switch (initialState) { | |
196 case ParsingCloseDelimiterSuffix: | |
197 // Require a full close delimiter consisting of a delimiter and "--" | |
198 // but ignore missing or partial "\r\n" after that. | |
199 return m_bufferedBytes.size() >= 2u; | |
200 case ParsingEpilogue: | |
201 case Finished: | |
202 return true; | |
203 default: | |
204 return false; | |
205 } | |
206 } | |
207 | |
208 MultipartParser::Buffer MultipartParser::closeDelimiterSuffixBuffer(size_t size) const | |
209 { | |
210 return Buffer(kCloseDelimiterSuffix, size, kCloseDelimiterSuffixSize); | |
211 } | |
212 | |
213 MultipartParser::Buffer MultipartParser::delimiterBuffer(size_t size) const | |
214 { | |
215 return Buffer(m_delimiter.data(), size, m_delimiter.size()); | |
216 } | |
217 | |
218 MultipartParser::Buffer MultipartParser::delimiterSuffixBuffer(size_t size) cons t | |
219 { | |
220 return Buffer(kDelimiterSuffix, size, kDelimiterSuffixSize); | |
221 } | |
222 | |
223 bool MultipartParser::parseDataAndDelimiter(const char** bytesPointer, const cha r* bytesEnd, Buffer* data) | |
224 { | |
225 bool hasInitiallyBufferedBytes = !m_bufferedBytes.empty(); | |
226 const char* bytesBegin = *bytesPointer; | |
227 | |
228 // Continue parsing a partial delimiter in the beginning of bytes. | |
229 while (*bytesPointer < bytesEnd && m_bufferedBytes.appendIfExpected(*(*bytes Pointer))) { | |
230 // The partial delimiter continued. | |
231 ++(*bytesPointer); | |
232 if (m_bufferedBytes.size() == m_delimiter.size()) { | |
233 // The partial delimiter turned out to be a complete one. | |
234 // Therefore, the buffered bytes are delimiter bytes. | |
235 m_bufferedBytes.clear(); | |
yhirano
2016/09/20 09:49:53
Can you move this statement (and the similar ones)
e_hakkinen
2016/09/20 21:59:29
Done.
| |
236 *data = Buffer(); | |
237 return true; // A delimiter is parsed. | |
238 } | |
239 } | |
240 | |
241 if (*bytesPointer == bytesEnd) { | |
242 // More bytes are needed. All bytes are buffered for now. | |
243 *data = Buffer(); | |
244 return false; | |
245 } | |
246 | |
247 if (!m_bufferedBytes.empty()) { | |
248 // The partial delimiter did not turn out to be a complete one. | |
249 // Therefore, the buffered bytes are data bytes. | |
250 if (hasInitiallyBufferedBytes) { | |
251 // Not all buffered bytes are in the range [bytesBegin, bytesEnd[ | |
252 // thus the buffered bytes must be returned separately. | |
253 *data = std::move(m_bufferedBytes); | |
254 m_bufferedBytes = delimiterBuffer(); | |
255 return false; | |
256 } | |
257 // The fact the some bytes are now buffered can be ignored as they are | |
258 // also in the range [bytesBegin, bytesEnd[. There is no need to reset | |
yhirano
2016/09/20 09:49:53
]
e_hakkinen
2016/09/20 21:59:29
*bytesEnd does not belong to the range.
| |
259 // |*bytesPointer| to |bytesBegin| as (partial) delimiters cannot | |
260 // overlap. | |
261 } | |
262 | |
263 // Search for a complete delimiter within the remaining bytes. | |
264 const char* delimiterBegin = std::search(*bytesPointer, bytesEnd, m_delimite r.begin(), m_delimiter.end()); | |
265 if (delimiterBegin != bytesEnd) { | |
266 // A complete delimiter was found. The bytes before that are data | |
267 // bytes. | |
268 const char* delimiterEnd = delimiterBegin + m_delimiter.size(); | |
269 *bytesPointer = delimiterEnd; | |
270 *data = Buffer(bytesBegin, delimiterBegin); | |
271 m_bufferedBytes.clear(); | |
272 return true; // A delimiter is parsed. | |
273 } | |
274 | |
275 // Search for a partial delimiter in the end of the remaining bytes. | |
276 size_t remainingBytesSize = static_cast<size_t>(bytesEnd - *bytesPointer); | |
277 delimiterBegin = bytesEnd - std::min(m_delimiter.size() - 1u, remainingBytes Size); | |
278 for (;;) { | |
279 m_bufferedBytes = delimiterBuffer(); | |
280 if (m_bufferedBytes.appendIfExpected(delimiterBegin, bytesEnd)) | |
281 break; | |
282 DCHECK_LT(delimiterBegin, bytesEnd); | |
283 ++delimiterBegin; | |
284 } | |
285 | |
286 // A partial delimiter (consisting of zero or more bytes) was found and | |
287 // the bytes are buffered for now. The bytes before the partial delimiter | |
288 // are definitely data bytes and can thus be returned. | |
289 *bytesPointer = bytesEnd; | |
290 *data = Buffer(bytesBegin, delimiterBegin); | |
291 return false; | |
292 } | |
293 | |
294 bool MultipartParser::parseHeaderFields(const char** bytesPointer, const char* b ytesEnd, WebURLResponse* response) | |
295 { | |
296 // Combine the current bytes with buffered header bytes if needed. | |
297 const char* headerBytes = *bytesPointer; | |
298 size_t headerSize = static_cast<size_t>(bytesEnd - *bytesPointer); | |
299 if (!m_bufferedHeaderBytes.isEmpty()) { | |
300 m_bufferedHeaderBytes.append(headerBytes, headerSize); | |
301 headerBytes = m_bufferedHeaderBytes.data(); | |
302 headerSize = m_bufferedHeaderBytes.size(); | |
303 } | |
304 | |
305 size_t end = 0u; | |
306 if (!Platform::current()->parseMultipartHeadersFromBody(headerBytes, headerS ize, response, &end)) { | |
307 // Store the current header bytes for the next call unless that has | |
308 // already been done. | |
309 if (m_bufferedHeaderBytes.isEmpty()) | |
310 m_bufferedHeaderBytes.append(headerBytes, headerSize); | |
311 *bytesPointer = bytesEnd; | |
312 return false; | |
313 } | |
314 m_bufferedHeaderBytes.clear(); | |
315 *bytesPointer = bytesEnd - (headerSize - end); | |
316 | |
317 const AtomicString& encoding = response->toResourceResponse().httpHeaderFiel ds().get(HTTPNames::Content_Transfer_Encoding); | |
318 if (!encoding.isNull()) { | |
yhirano
2016/09/20 09:49:53
Why do you need this condition?
e_hakkinen
2016/09/20 21:59:29
It is needed in order to handle the common case in
| |
319 if (!(equalIgnoringCase(encoding, "binary") | |
320 || equalIgnoringCase(encoding, "7bit") | |
321 || equalIgnoringCase(encoding, "8bit"))) { | |
322 // Decoding is not implemented. | |
323 m_state = Failed; | |
324 return false; | |
325 } | |
326 } | |
327 | |
328 return true; | |
329 } | |
330 | |
331 bool MultipartParser::parseTransportPadding(const char** bytesPointer, const cha r* bytesEnd) const | |
332 { | |
333 const char* bytesBegin = *bytesPointer; | |
334 while (*bytesPointer < bytesEnd && (*(*bytesPointer) == '\t' || *(*bytesPoin ter) == ' ')) | |
335 ++(*bytesPointer); | |
336 return *bytesPointer > bytesBegin; | |
337 } | |
338 | |
339 DEFINE_TRACE(MultipartParser) | |
340 { | |
341 visitor->trace(m_client); | |
342 } | |
343 | |
344 } // namespace blink | |
OLD | NEW |