OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/fetch/MultipartImageResourceParser.h" | 5 #include "core/fetch/MultipartImageResourceParser.h" |
6 | 6 |
7 #include "public/platform/Platform.h" | 7 #include "public/platform/Platform.h" |
8 #include "public/platform/WebURLResponse.h" | 8 #include "public/platform/WebURLResponse.h" |
9 #include "wtf/NotFound.h" | 9 #include "wtf/NotFound.h" |
10 #include "wtf/text/WTFString.h" | 10 #include "wtf/text/WTFString.h" |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 | 13 |
14 namespace blink { | 14 namespace blink { |
15 | 15 |
16 MultipartImageResourceParser::MultipartImageResourceParser(const ResourceRespons
e& response, const Vector<char>& boundary, Client* client) | 16 MultipartImageResourceParser::MultipartImageResourceParser(const ResourceRespons
e& response, const Vector<char>& boundary, Client* client) |
17 : m_originalResponse(response) | 17 : m_originalResponse(response) |
18 , m_boundary(boundary) | 18 , m_boundary(boundary) |
19 , m_client(client) | 19 , m_client(client) |
20 { | 20 { |
21 // Some servers report a boundary prefixed with "--". See https://crbug.com
/5786. | 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] != '-') | 22 if (m_boundary.size() < 2 || m_boundary[0] != '-' || m_boundary[1] != '-') |
23 m_boundary.prepend("--", 2); | 23 m_boundary.prepend("--", 2); |
24 } | 24 } |
25 | 25 |
26 void MultipartImageResourceParser::appendData(const char* bytes, size_t size) | 26 void MultipartImageResourceParser::appendData(const char* bytes, size_t size) |
27 { | 27 { |
| 28 ASSERT(!isCancelled()); |
28 // m_sawLastBoundary means that we've already received the final boundary | 29 // 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 // token. The server should stop sending us data at this point, but if it |
30 // does, we just throw it away. | 31 // does, we just throw it away. |
31 if (m_sawLastBoundary) | 32 if (m_sawLastBoundary) |
32 return; | 33 return; |
33 m_data.append(bytes, size); | 34 m_data.append(bytes, size); |
34 | 35 |
35 if (m_isParsingTop) { | 36 if (m_isParsingTop) { |
36 // Eat leading \r\n | 37 // Eat leading \r\n |
37 size_t pos = pushOverLine(m_data, 0); | 38 size_t pos = pushOverLine(m_data, 0); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 97 |
97 // We can now throw out data up through the boundary | 98 // We can now throw out data up through the boundary |
98 size_t offset = pushOverLine(m_data, boundaryEndPosition); | 99 size_t offset = pushOverLine(m_data, boundaryEndPosition); |
99 m_data.remove(0, boundaryEndPosition + offset); | 100 m_data.remove(0, boundaryEndPosition + offset); |
100 | 101 |
101 // Ok, back to parsing headers | 102 // Ok, back to parsing headers |
102 if (!parseHeaders()) { | 103 if (!parseHeaders()) { |
103 m_isParsingHeaders = true; | 104 m_isParsingHeaders = true; |
104 break; | 105 break; |
105 } | 106 } |
| 107 if (isCancelled()) |
| 108 return; |
106 } | 109 } |
107 | 110 |
108 // At this point, we should send over any data we have, but keep enough data | 111 // 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. | 112 // buffered to handle a boundary that may have been truncated. |
110 if (!m_isParsingHeaders && m_data.size() > m_boundary.size()) { | 113 if (!m_isParsingHeaders && m_data.size() > m_boundary.size()) { |
111 // If the last character is a new line character, go ahead and just send | 114 // 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. | 115 // everything we have buffered. This matches an optimization in Gecko. |
113 size_t sendLength = m_data.size() - m_boundary.size(); | 116 size_t sendLength = m_data.size() - m_boundary.size(); |
114 if (m_data.last() == '\n') | 117 if (m_data.last() == '\n') |
115 sendLength = m_data.size(); | 118 sendLength = m_data.size(); |
116 m_client->multipartDataReceived(m_data.data(), sendLength); | 119 m_client->multipartDataReceived(m_data.data(), sendLength); |
117 m_data.remove(0, sendLength); | 120 m_data.remove(0, sendLength); |
118 } | 121 } |
119 } | 122 } |
120 | 123 |
121 void MultipartImageResourceParser::finish() | 124 void MultipartImageResourceParser::finish() |
122 { | 125 { |
123 ASSERT(!isCancelled()); | 126 ASSERT(!isCancelled()); |
| 127 if (m_sawLastBoundary) |
| 128 return; |
124 // If we have any pending data and we're not in a header, go ahead and send | 129 // If we have any pending data and we're not in a header, go ahead and send |
125 // it to the client. | 130 // it to the client. |
126 if (!m_isParsingHeaders && !m_data.isEmpty() && !m_sawLastBoundary) | 131 if (!m_isParsingHeaders && !m_data.isEmpty()) |
127 m_client->multipartDataReceived(m_data.data(), m_data.size()); | 132 m_client->multipartDataReceived(m_data.data(), m_data.size()); |
128 m_data.clear(); | 133 m_data.clear(); |
129 m_sawLastBoundary = true; | 134 m_sawLastBoundary = true; |
130 } | 135 } |
131 | 136 |
132 size_t MultipartImageResourceParser::pushOverLine(const Vector<char>& data, size
_t pos) | 137 size_t MultipartImageResourceParser::pushOverLine(const Vector<char>& data, size
_t pos) |
133 { | 138 { |
134 size_t offset = 0; | 139 size_t offset = 0; |
135 // TODO(yhirano): This function has two problems. Fix them. | 140 // TODO(yhirano): This function has two problems. Fix them. |
136 // 1. It eats "\n\n". | 141 // 1. It eats "\n\n". |
(...skipping 14 matching lines...) Expand all Loading... |
151 // does. See netwerk/streamconv/converters/nsMultiMixedConv.cpp. | 156 // does. See netwerk/streamconv/converters/nsMultiMixedConv.cpp. |
152 WebURLResponse response(m_originalResponse.url()); | 157 WebURLResponse response(m_originalResponse.url()); |
153 for (const auto& header : m_originalResponse.httpHeaderFields()) | 158 for (const auto& header : m_originalResponse.httpHeaderFields()) |
154 response.addHTTPHeaderField(header.key, header.value); | 159 response.addHTTPHeaderField(header.key, header.value); |
155 | 160 |
156 size_t end = 0; | 161 size_t end = 0; |
157 if (!Platform::current()->parseMultipartHeadersFromBody(m_data.data(), m_dat
a.size(), &response, &end)) | 162 if (!Platform::current()->parseMultipartHeadersFromBody(m_data.data(), m_dat
a.size(), &response, &end)) |
158 return false; | 163 return false; |
159 m_data.remove(0, end); | 164 m_data.remove(0, end); |
160 | 165 |
161 // To avoid recording every multipart load as a separate visit in | 166 bool isFirstPart = m_isFirstPart; |
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 m_isFirstPart = false; |
167 // Send the response! | 168 // Send the response! |
168 m_client->onePartInMultipartReceived(response.toResourceResponse()); | 169 m_client->onePartInMultipartReceived(response.toResourceResponse(), isFirstP
art); |
169 | 170 |
170 return true; | 171 return true; |
171 } | 172 } |
172 | 173 |
173 // Boundaries are supposed to be preceeded with --, but it looks like gecko | 174 // Boundaries are supposed to be preceeded with --, but it looks like gecko |
174 // doesn't require the dashes to exist. See nsMultiMixedConv::FindToken. | 175 // doesn't require the dashes to exist. See nsMultiMixedConv::FindToken. |
175 size_t MultipartImageResourceParser::findBoundary(const Vector<char>& data, Vect
or<char>* boundary) | 176 size_t MultipartImageResourceParser::findBoundary(const Vector<char>& data, Vect
or<char>* boundary) |
176 { | 177 { |
177 auto it = std::search(data.data(), data.data() + data.size(), boundary->data
(), boundary->data() + boundary->size()); | 178 auto it = std::search(data.data(), data.data() + data.size(), boundary->data
(), boundary->data() + boundary->size()); |
178 if (it == data.data() + data.size()) | 179 if (it == data.data() + data.size()) |
(...skipping 13 matching lines...) Expand all Loading... |
192 } | 193 } |
193 return boundaryPosition; | 194 return boundaryPosition; |
194 } | 195 } |
195 | 196 |
196 DEFINE_TRACE(MultipartImageResourceParser) | 197 DEFINE_TRACE(MultipartImageResourceParser) |
197 { | 198 { |
198 visitor->trace(m_client); | 199 visitor->trace(m_client); |
199 } | 200 } |
200 | 201 |
201 } // namespace blink | 202 } // namespace blink |
OLD | NEW |