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 "core/fetch/MultipartImageResourceParser.h" | |
6 | |
7 #include "platform/network/HTTPParsers.h" | |
8 #include "wtf/NotFound.h" | |
9 #include "wtf/text/WTFString.h" | |
10 | |
11 #include <algorithm> | |
12 | |
13 namespace blink { | |
14 | |
15 MultipartImageResourceParser::MultipartImageResourceParser( | |
16 const ResourceResponse& response, | |
17 const Vector<char>& boundary, | |
18 Client* client) | |
19 : m_originalResponse(response), m_boundary(boundary), m_client(client) { | |
20 // Some servers report a boundary prefixed with "--". See | |
21 // 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 DCHECK(!isCancelled()); | |
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 = skippableLength(m_data, 0); | |
38 // +2 for "--" | |
39 if (m_data.size() < m_boundary.size() + 2 + pos) { | |
40 // We don't have enough data yet to make a boundary token. Just wait | |
41 // until the next chunk of data arrives. | |
42 return; | |
43 } | |
44 if (pos) | |
45 m_data.remove(0, pos); | |
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 if (!parseHeaders()) { | |
59 // Get more data before trying again. | |
60 return; | |
61 } | |
62 // Successfully parsed headers. | |
63 m_isParsingHeaders = false; | |
64 if (isCancelled()) | |
65 return; | |
66 } | |
67 | |
68 size_t boundaryPosition; | |
69 while ((boundaryPosition = findBoundary(m_data, &m_boundary)) != kNotFound) { | |
70 // Strip out trailing \r\n characters in the buffer preceding the boundary | |
71 // on the same lines as does Firefox. | |
72 size_t dataSize = boundaryPosition; | |
73 if (boundaryPosition > 0 && m_data[boundaryPosition - 1] == '\n') { | |
74 dataSize--; | |
75 if (boundaryPosition > 1 && m_data[boundaryPosition - 2] == '\r') { | |
76 dataSize--; | |
77 } | |
78 } | |
79 if (dataSize) { | |
80 m_client->multipartDataReceived(m_data.data(), dataSize); | |
81 if (isCancelled()) | |
82 return; | |
83 } | |
84 size_t boundaryEndPosition = boundaryPosition + m_boundary.size(); | |
85 if (boundaryEndPosition < m_data.size() && | |
86 '-' == m_data[boundaryEndPosition]) { | |
87 // This was the last boundary so we can stop processing. | |
88 m_sawLastBoundary = true; | |
89 m_data.clear(); | |
90 return; | |
91 } | |
92 | |
93 // We can now throw out data up through the boundary | |
94 m_data.remove(0, boundaryEndPosition); | |
95 | |
96 // Ok, back to parsing headers | |
97 if (!parseHeaders()) { | |
98 m_isParsingHeaders = true; | |
99 break; | |
100 } | |
101 if (isCancelled()) | |
102 return; | |
103 } | |
104 | |
105 // At this point, we should send over any data we have, but keep enough data | |
106 // buffered to handle a boundary that may have been truncated. "+2" for CRLF, | |
107 // as we may ignore the last CRLF. | |
108 if (!m_isParsingHeaders && m_data.size() > m_boundary.size() + 2) { | |
109 size_t sendLength = m_data.size() - m_boundary.size() - 2; | |
110 m_client->multipartDataReceived(m_data.data(), sendLength); | |
111 m_data.remove(0, sendLength); | |
112 } | |
113 } | |
114 | |
115 void MultipartImageResourceParser::finish() { | |
116 DCHECK(!isCancelled()); | |
117 if (m_sawLastBoundary) | |
118 return; | |
119 // If we have any pending data and we're not in a header, go ahead and send | |
120 // it to the client. | |
121 if (!m_isParsingHeaders && !m_data.isEmpty()) | |
122 m_client->multipartDataReceived(m_data.data(), m_data.size()); | |
123 m_data.clear(); | |
124 m_sawLastBoundary = true; | |
125 } | |
126 | |
127 size_t MultipartImageResourceParser::skippableLength(const Vector<char>& data, | |
128 size_t pos) { | |
129 if (data.size() >= pos + 2 && data[pos] == '\r' && data[pos + 1] == '\n') | |
130 return 2; | |
131 if (data.size() >= pos + 1 && data[pos] == '\n') | |
132 return 1; | |
133 return 0; | |
134 } | |
135 | |
136 bool MultipartImageResourceParser::parseHeaders() { | |
137 // Eat leading \r\n | |
138 size_t pos = skippableLength(m_data, 0); | |
139 | |
140 // Create a ResourceResponse based on the original set of headers + the | |
141 // replacement headers. We only replace the same few headers that gecko does. | |
142 // See netwerk/streamconv/converters/nsMultiMixedConv.cpp. | |
143 ResourceResponse response; | |
144 response.setURL(m_originalResponse.url()); | |
145 for (const auto& header : m_originalResponse.httpHeaderFields()) | |
146 response.addHTTPHeaderField(header.key, header.value); | |
147 | |
148 size_t end = 0; | |
149 if (!parseMultipartHeadersFromBody(m_data.data() + pos, m_data.size() - pos, | |
150 &response, &end)) | |
151 return false; | |
152 m_data.remove(0, end + pos); | |
153 // Send the response! | |
154 m_client->onePartInMultipartReceived(response); | |
155 return true; | |
156 } | |
157 | |
158 // Boundaries are supposed to be preceeded with --, but it looks like gecko | |
159 // doesn't require the dashes to exist. See nsMultiMixedConv::FindToken. | |
160 size_t MultipartImageResourceParser::findBoundary(const Vector<char>& data, | |
161 Vector<char>* boundary) { | |
162 auto it = std::search(data.data(), data.data() + data.size(), | |
163 boundary->data(), boundary->data() + boundary->size()); | |
164 if (it == data.data() + data.size()) | |
165 return kNotFound; | |
166 | |
167 size_t boundaryPosition = it - data.data(); | |
168 // Back up over -- for backwards compat | |
169 // TODO(tc): Don't we only want to do this once? Gecko code doesn't seem to | |
170 // care. | |
171 if (boundaryPosition >= 2) { | |
172 if (data[boundaryPosition - 1] == '-' && | |
173 data[boundaryPosition - 2] == '-') { | |
174 boundaryPosition -= 2; | |
175 Vector<char> v(2, '-'); | |
176 v.appendVector(*boundary); | |
177 *boundary = v; | |
178 } | |
179 } | |
180 return boundaryPosition; | |
181 } | |
182 | |
183 DEFINE_TRACE(MultipartImageResourceParser) { | |
184 visitor->trace(m_client); | |
185 } | |
186 | |
187 } // namespace blink | |
OLD | NEW |