Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: third_party/WebKit/Source/core/fetch/MultipartImageResourceParser.cpp

Issue 1693183002: Move multipart resource handling to core/fetch (1/2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@multipart-cleanup-preliminary
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "core/fetch/MultipartImageResourceParser.h"
6
7 #include "public/platform/Platform.h"
8 #include "public/platform/WebURLResponse.h"
9 #include "wtf/NotFound.h"
10 #include "wtf/StdLibExtras.h"
11 #include "wtf/text/WTFString.h"
12
13 namespace blink {
14
15 namespace {
16
17 // The list of response headers that we do not copy from the original
18 // response when generating a WebURLResponse for a MIME payload.
19 const char* kReplaceHeaders[] = {
20 "content-type",
21 "content-length",
22 "content-disposition",
23 "content-range",
24 "range",
25 "set-cookie"
26 };
27
28 size_t find(const char* s, size_t sSize, const char* pattern, size_t patternSize )
hiroshige 2016/02/25 18:10:24 I think it's better to use std::search() rather th
yhirano 2016/02/25 18:35:13 Oh, I didn't know the function. Thank you!
29 {
30 // Naive search
31 for (size_t i = 0; i + patternSize <= sSize; ++i) {
32 bool found = true;
33 for (size_t j = 0; found && j < patternSize; ++j)
34 found = (s[i + j] == pattern[j]);
35 if (found)
36 return i;
37 }
38 return kNotFound;
39 }
40
41 } // namespace
42
43 MultipartImageResourceParser::MultipartImageResourceParser(const ResourceRespons e& response, const Vector<char>& boundary, Client* client)
44 : m_originalResponse(response)
45 , m_boundary(boundary)
46 , m_client(client)
47 {
48 // Some servers report a boundary prefixed with "--". See https://crbug.com /5786.
49 if (m_boundary.size() < 2 || m_boundary[0] != '-' || m_boundary[1] != '-')
50 m_boundary.prepend("--", 2);
51 }
52
53 void MultipartImageResourceParser::addData(const char* bytes, size_t size)
54 {
55 // m_sawLastBoundary means that we've already received the final boundary
56 // token. The server should stop sending us data at this point, but if it
57 // does, we just throw it away.
58 if (m_sawLastBoundary)
59 return;
60 m_data.append(bytes, size);
61
62 if (m_isParsingTop) {
63 // Eat leading \r\n
64 int pos = pushOverLine(m_data, 0);
65 if (pos)
66 m_data.remove(0, pos);
67
68 if (m_data.size() < m_boundary.size() + 2) {
69 // We don't have enough data yet to make a boundary token. Just
70 // wait until the next chunk of data arrives.
71 return;
72 }
73
74 // Some servers don't send a boundary token before the first chunk of
75 // data. We handle this case anyway (Gecko does too).
76 if (0 != memcmp(m_data.data(), m_boundary.data(), m_boundary.size())) {
77 m_data.prepend("\n", 1);
78 const auto& boundary = m_boundary;
hiroshige 2016/02/25 18:10:24 nit: Why don't we do |m_data.prepend(m_boundary);|
yhirano 2016/02/25 18:35:13 It causes a strange compile error.
hiroshige 2016/02/25 21:46:31 I see. Probably this is WTF::Vector's problem.
79 m_data.prepend(boundary);
80 }
81 m_isParsingTop = false;
82 }
83 ASSERT(!m_isParsingTop);
84
85 // Headers
86 if (m_isParsingHeaders) {
87 // Eat leading \r\n
88 int pos = pushOverLine(m_data, 0);
89 if (pos)
90 m_data.remove(0, pos);
91
92 if (parseHeaders()) {
93 // Successfully parsed headers.
94 m_isParsingHeaders = false;
95 } else {
96 // Get more data before trying again.
97 return;
98 }
99 if (isCancelled())
hiroshige 2016/02/25 18:10:24 Should we place isCancelled() check here? In the c
hiroshige 2016/02/25 18:31:05 Er, I understood, parseHeaders() calls didReceiveR
yhirano 2016/02/25 18:35:13 This "parser" class parses incoming data and notif
100 return;
101 }
102 ASSERT(!m_isParsingHeaders);
103
104 size_t boundaryPosition;
105 while ((boundaryPosition = findBoundary(m_data, &m_boundary)) != kNotFound) {
106 // Strip out trailing \r\n characters in the buffer preceding the
107 // boundary on the same lines as Firefox.
108 size_t dataSize = boundaryPosition;
109 if (boundaryPosition > 0 && m_data[boundaryPosition - 1] == '\n') {
110 dataSize--;
111 if (boundaryPosition > 1 && m_data[boundaryPosition - 2] == '\r') {
112 dataSize--;
113 }
114 }
115 if (dataSize > 0) {
116 m_client->didReceiveData(m_data.data(), dataSize);
117 if (isCancelled())
118 return;
119 }
120 size_t boundaryEndPosition = boundaryPosition + m_boundary.size();
121 if (boundaryEndPosition < m_data.size() && '-' == m_data[boundaryEndPosi tion]) {
122 // This was the last boundary so we can stop processing.
123 m_sawLastBoundary = true;
124 m_data.clear();
125 return;
126 }
127
128 // We can now throw out data up through the boundary
129 int offset = pushOverLine(m_data, boundaryEndPosition);
130 m_data.remove(0, boundaryEndPosition + offset);
131
132 // Ok, back to parsing headers
133 if (!parseHeaders()) {
134 m_isParsingHeaders = true;
135 break;
136 }
137 }
138
139 // At this point, we should send over any data we have, but keep enough data
140 // buffered to handle a boundary that may have been truncated.
141 if (!m_isParsingHeaders && m_data.size() > m_boundary.size()) {
142 // If the last character is a new line character, go ahead and just send
143 // everything we have buffered. This matches an optimization in Gecko.
144 size_t sendLength = m_data.size() - m_boundary.size();
145 if (m_data.last() == '\n')
146 sendLength = m_data.size();
147 m_client->didReceiveData(m_data.data(), sendLength);
148 m_data.remove(0, sendLength);
149 }
150 }
151
152 void MultipartImageResourceParser::finish()
153 {
154 ASSERT(!isCancelled());
155 // If we have any pending data and we're not in a header, go ahead and send
156 // it to WebCore.
hiroshige 2016/02/25 18:10:24 nit: s/send it to WebCore/send it to Client/ (or s
yhirano 2016/02/25 18:35:13 Done.
157 if (!m_isParsingHeaders && !m_data.isEmpty() && !m_sawLastBoundary)
158 m_client->didReceiveData(m_data.data(), m_data.size());
159 m_data.clear();
160 m_sawLastBoundary = true;
161 }
162
163 int MultipartImageResourceParser::pushOverLine(const Vector<char>& data, size_t pos)
164 {
165 int offset = 0;
166 if (pos < data.size() && (data[pos] == '\r' || data[pos] == '\n')) {
167 ++offset;
168 if (pos + 1 < data.size() && data[pos + 1] == '\n')
169 ++offset;
170 }
171 return offset;
172 }
173
174 bool MultipartImageResourceParser::parseHeaders()
175 {
176 // Create a WebURLResponse based on the original set of headers + the
177 // replacement headers. We only replace the same few headers that gecko
178 // does. See netwerk/streamconv/converters/nsMultiMixedConv.cpp.
179 WebURLResponse response(m_originalResponse.url());
180
181 size_t end = 0;
182 if (!Platform::current()->parseAdditionalHeaders(m_data.data(), m_data.size( ), &response, &end))
183 return false;
184 m_data.remove(0, end);
185
186 // Copy the response headers from the original response.
187 for (const auto& header : m_originalResponse.httpHeaderFields()) {
188 bool forbidden = false;
189 for (size_t i = 0; !forbidden && i < WTF_ARRAY_LENGTH(kReplaceHeaders); ++i)
190 forbidden = equalIgnoringCase(header.key, kReplaceHeaders[i]);
191 if (!forbidden)
192 response.addHTTPHeaderField(header.key, header.value);
193 }
194
195 // To avoid recording every multipart load as a separate visit in
196 // the history database, we want to keep track of whether the response
197 // is part of a multipart payload. We do want to record the first visit,
198 // so we only set isMultipartPayload to true after the first visit.
199 response.setIsMultipartPayload(!m_isFirstPart);
200 m_isFirstPart = false;
201 // Send the response!
202 m_client->didReceiveResponse(response.toResourceResponse());
203
204 return true;
205 }
206
207 // Boundaries are supposed to be preceeded with --, but it looks like gecko
208 // doesn't require the dashes to exist. See nsMultiMixedConv::FindToken.
209 size_t MultipartImageResourceParser::findBoundary(const Vector<char>& data, Vect or<char>* boundary)
210 {
211 size_t boundaryPosition = find(data.data(), data.size(), boundary->data(), b oundary->size());
212 if (boundaryPosition != kNotFound) {
213 // Back up over -- for backwards compat
214 // TODO(tc): Don't we only want to do this once? Gecko code doesn't
215 // seem to care.
216 if (boundaryPosition >= 2) {
217 if ('-' == data[boundaryPosition - 1] && '-' == data[boundaryPositio n - 2]) {
218 boundaryPosition -= 2;
219 Vector<char> v(2, '-');
220 v.appendVector(*boundary);
221 *boundary = v;
222 }
223 }
224 }
225 return boundaryPosition;
226 }
227
228 DEFINE_TRACE(MultipartImageResourceParser)
229 {
230 visitor->trace(m_client);
231 }
232
233 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698