| 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/loader/resource/MultipartImageResourceParser.h" | 5 #include "core/loader/resource/MultipartImageResourceParser.h" |
| 6 | 6 |
| 7 #include "platform/network/HTTPParsers.h" | 7 #include "platform/network/HTTPParsers.h" |
| 8 #include "wtf/NotFound.h" | 8 #include "wtf/NotFound.h" |
| 9 #include "wtf/text/WTFString.h" | 9 #include "wtf/text/WTFString.h" |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 MultipartImageResourceParser::MultipartImageResourceParser( | 15 MultipartImageResourceParser::MultipartImageResourceParser( |
| 16 const ResourceResponse& response, | 16 const ResourceResponse& response, |
| 17 const Vector<char>& boundary, | 17 const Vector<char>& boundary, |
| 18 Client* client) | 18 Client* client) |
| 19 : m_originalResponse(response), m_boundary(boundary), m_client(client) { | 19 : m_originalResponse(response), m_boundary(boundary), m_client(client) { |
| 20 // Some servers report a boundary prefixed with "--". See | 20 // Some servers report a boundary prefixed with "--". See |
| 21 // https://crbug.com/5786. | 21 // 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.push_front("--", 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 DCHECK(!isCancelled()); | 27 DCHECK(!isCancelled()); |
| 28 // m_sawLastBoundary means that we've already received the final boundary | 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 | 29 // token. The server should stop sending us data at this point, but if it |
| 30 // does, we just throw it away. | 30 // does, we just throw it away. |
| 31 if (m_sawLastBoundary) | 31 if (m_sawLastBoundary) |
| 32 return; | 32 return; |
| 33 m_data.append(bytes, size); | 33 m_data.append(bytes, size); |
| 34 | 34 |
| 35 if (m_isParsingTop) { | 35 if (m_isParsingTop) { |
| 36 // Eat leading \r\n | 36 // Eat leading \r\n |
| 37 size_t pos = skippableLength(m_data, 0); | 37 size_t pos = skippableLength(m_data, 0); |
| 38 // +2 for "--" | 38 // +2 for "--" |
| 39 if (m_data.size() < m_boundary.size() + 2 + pos) { | 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 | 40 // We don't have enough data yet to make a boundary token. Just wait |
| 41 // until the next chunk of data arrives. | 41 // until the next chunk of data arrives. |
| 42 return; | 42 return; |
| 43 } | 43 } |
| 44 if (pos) | 44 if (pos) |
| 45 m_data.remove(0, pos); | 45 m_data.remove(0, pos); |
| 46 | 46 |
| 47 // Some servers don't send a boundary token before the first chunk of | 47 // Some servers don't send a boundary token before the first chunk of |
| 48 // data. We handle this case anyway (Gecko does too). | 48 // data. We handle this case anyway (Gecko does too). |
| 49 if (0 != memcmp(m_data.data(), m_boundary.data(), m_boundary.size())) { | 49 if (0 != memcmp(m_data.data(), m_boundary.data(), m_boundary.size())) { |
| 50 m_data.prepend("\n", 1); | 50 m_data.push_front("\n", 1); |
| 51 m_data.prependVector(m_boundary); | 51 m_data.prependVector(m_boundary); |
| 52 } | 52 } |
| 53 m_isParsingTop = false; | 53 m_isParsingTop = false; |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Headers | 56 // Headers |
| 57 if (m_isParsingHeaders) { | 57 if (m_isParsingHeaders) { |
| 58 if (!parseHeaders()) { | 58 if (!parseHeaders()) { |
| 59 // Get more data before trying again. | 59 // Get more data before trying again. |
| 60 return; | 60 return; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 return boundaryPosition; | 180 return boundaryPosition; |
| 181 } | 181 } |
| 182 | 182 |
| 183 DEFINE_TRACE(MultipartImageResourceParser) { | 183 DEFINE_TRACE(MultipartImageResourceParser) { |
| 184 visitor->trace(m_client); | 184 visitor->trace(m_client); |
| 185 } | 185 } |
| 186 | 186 |
| 187 } // namespace blink | 187 } // namespace blink |
| OLD | NEW |