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

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, 9 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/text/WTFString.h"
11
12 #include <algorithm>
13
14 namespace blink {
15
16 MultipartImageResourceParser::MultipartImageResourceParser(const ResourceRespons e& response, const Vector<char>& boundary, Client* client)
17 : m_originalResponse(response)
18 , m_boundary(boundary)
19 , m_client(client)
20 {
21 // Some servers report a boundary prefixed with "--". See https://crbug.com /5786.
22 if (m_boundary.size() < 2 || m_boundary[0] != '-' || m_boundary[1] != '-')
23 m_boundary.prepend("--", 2);
24 }
25
26 void MultipartImageResourceParser::appendData(const char* bytes, size_t size)
27 {
28 // m_sawLastBoundary means that we've already received the final boundary
29 // token. The server should stop sending us data at this point, but if it
30 // does, we just throw it away.
31 if (m_sawLastBoundary)
32 return;
33 m_data.append(bytes, size);
34
35 if (m_isParsingTop) {
36 // Eat leading \r\n
37 size_t pos = pushOverLine(m_data, 0);
38 if (pos)
39 m_data.remove(0, pos);
40
41 if (m_data.size() < m_boundary.size() + 2) {
42 // We don't have enough data yet to make a boundary token. Just
43 // wait until the next chunk of data arrives.
44 return;
45 }
46
47 // Some servers don't send a boundary token before the first chunk of
48 // data. We handle this case anyway (Gecko does too).
49 if (0 != memcmp(m_data.data(), m_boundary.data(), m_boundary.size())) {
50 m_data.prepend("\n", 1);
51 m_data.prependVector(m_boundary);
52 }
53 m_isParsingTop = false;
54 }
55
56 // Headers
57 if (m_isParsingHeaders) {
58 // Eat leading \r\n
59 size_t pos = pushOverLine(m_data, 0);
60 if (pos)
61 m_data.remove(0, pos);
62
63 if (!parseHeaders()) {
64 // Get more data before trying again.
65 return;
66 }
67 // Successfully parsed headers.
68 m_isParsingHeaders = false;
69 if (isCancelled())
70 return;
71 }
72
73 size_t boundaryPosition;
74 while ((boundaryPosition = findBoundary(m_data, &m_boundary)) != kNotFound) {
75 // Strip out trailing \r\n characters in the buffer preceding the
76 // boundary on the same lines as does Firefox.
77 size_t dataSize = boundaryPosition;
78 if (boundaryPosition > 0 && m_data[boundaryPosition - 1] == '\n') {
79 dataSize--;
80 if (boundaryPosition > 1 && m_data[boundaryPosition - 2] == '\r') {
81 dataSize--;
82 }
83 }
84 if (dataSize) {
85 m_client->multipartDataReceived(m_data.data(), dataSize);
86 if (isCancelled())
87 return;
88 }
89 size_t boundaryEndPosition = boundaryPosition + m_boundary.size();
90 if (boundaryEndPosition < m_data.size() && '-' == m_data[boundaryEndPosi tion]) {
91 // This was the last boundary so we can stop processing.
92 m_sawLastBoundary = true;
93 m_data.clear();
94 return;
95 }
96
97 // We can now throw out data up through the boundary
98 size_t offset = pushOverLine(m_data, boundaryEndPosition);
99 m_data.remove(0, boundaryEndPosition + offset);
100
101 // Ok, back to parsing headers
102 if (!parseHeaders()) {
103 m_isParsingHeaders = true;
104 break;
105 }
106 }
107
108 // At this point, we should send over any data we have, but keep enough data
109 // buffered to handle a boundary that may have been truncated.
110 if (!m_isParsingHeaders && m_data.size() > m_boundary.size()) {
111 // If the last character is a new line character, go ahead and just send
112 // everything we have buffered. This matches an optimization in Gecko.
113 size_t sendLength = m_data.size() - m_boundary.size();
114 if (m_data.last() == '\n')
115 sendLength = m_data.size();
116 m_client->multipartDataReceived(m_data.data(), sendLength);
117 m_data.remove(0, sendLength);
118 }
119 }
120
121 void MultipartImageResourceParser::finish()
122 {
123 ASSERT(!isCancelled());
124 // If we have any pending data and we're not in a header, go ahead and send
125 // it to the client.
126 if (!m_isParsingHeaders && !m_data.isEmpty() && !m_sawLastBoundary)
127 m_client->multipartDataReceived(m_data.data(), m_data.size());
128 m_data.clear();
129 m_sawLastBoundary = true;
130 }
131
132 size_t MultipartImageResourceParser::pushOverLine(const Vector<char>& data, size _t pos)
133 {
134 size_t offset = 0;
135 // TODO(yhirano): This function has two problems. Fix them.
136 // 1. It eats "\n\n".
137 // 2. When the incoming data is not sufficient (i.e. data[pos] == '\r'
138 // && data.size() == pos + 1), it should notify the caller.
139 if (pos < data.size() && (data[pos] == '\r' || data[pos] == '\n')) {
140 ++offset;
141 if (pos + 1 < data.size() && data[pos + 1] == '\n')
142 ++offset;
143 }
144 return offset;
145 }
146
147 bool MultipartImageResourceParser::parseHeaders()
148 {
149 // Create a WebURLResponse based on the original set of headers + the
150 // replacement headers. We only replace the same few headers that gecko
151 // does. See netwerk/streamconv/converters/nsMultiMixedConv.cpp.
152 WebURLResponse response(m_originalResponse.url());
153 for (const auto& header : m_originalResponse.httpHeaderFields())
154 response.addHTTPHeaderField(header.key, header.value);
155
156 size_t end = 0;
157 if (!Platform::current()->parseMultipartHeadersFromBody(m_data.data(), m_dat a.size(), &response, &end))
158 return false;
159 m_data.remove(0, end);
160
161 // To avoid recording every multipart load as a separate visit in
162 // the history database, we want to keep track of whether the response
163 // is part of a multipart payload. We do want to record the first visit,
164 // so we only set isMultipartPayload to true after the first visit.
165 response.setIsMultipartPayload(!m_isFirstPart);
166 m_isFirstPart = false;
167 // Send the response!
168 m_client->onePartInMultipartReceived(response.toResourceResponse());
169
170 return true;
171 }
172
173 // Boundaries are supposed to be preceeded with --, but it looks like gecko
174 // doesn't require the dashes to exist. See nsMultiMixedConv::FindToken.
175 size_t MultipartImageResourceParser::findBoundary(const Vector<char>& data, Vect or<char>* boundary)
176 {
177 auto it = std::search(data.data(), data.data() + data.size(), boundary->data (), boundary->data() + boundary->size());
178 if (it == data.data() + data.size())
179 return kNotFound;
180
181 size_t boundaryPosition = it - data.data();
182 // Back up over -- for backwards compat
183 // TODO(tc): Don't we only want to do this once? Gecko code doesn't
184 // seem to care.
185 if (boundaryPosition >= 2) {
186 if (data[boundaryPosition - 1] == '-' && data[boundaryPosition - 2] == ' -') {
187 boundaryPosition -= 2;
188 Vector<char> v(2, '-');
189 v.appendVector(*boundary);
190 *boundary = v;
191 }
192 }
193 return boundaryPosition;
194 }
195
196 DEFINE_TRACE(MultipartImageResourceParser)
197 {
198 visitor->trace(m_client);
199 }
200
201 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698