Index: third_party/WebKit/Source/modules/fetch/MultipartParser.cpp |
diff --git a/third_party/WebKit/Source/modules/fetch/MultipartParser.cpp b/third_party/WebKit/Source/modules/fetch/MultipartParser.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3dea324d80d3bf6352dbd50e2cbfc78fa99d8785 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/fetch/MultipartParser.cpp |
@@ -0,0 +1,354 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "modules/fetch/MultipartParser.h" |
+ |
+#include "platform/HTTPNames.h" |
+#include "platform/network/HTTPParsers.h" |
+#include "public/platform/Platform.h" |
+ |
+#include <algorithm> |
+#include <utility> |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+constexpr char kCloseDelimiterSuffix[] = "--\r\n"; |
+constexpr size_t kCloseDelimiterSuffixSize = |
+ WTF_ARRAY_LENGTH(kCloseDelimiterSuffix) - 1u; |
+constexpr size_t kDashBoundaryOffset = 2u; // The length of "\r\n". |
+constexpr char kDelimiterSuffix[] = "\r\n"; |
+constexpr size_t kDelimiterSuffixSize = WTF_ARRAY_LENGTH(kDelimiterSuffix) - 1u; |
+ |
+bool octetsNeedDecoding(const HTTPHeaderMap& headerFields) { |
+ const AtomicString& encoding = |
+ headerFields.get(HTTPNames::Content_Transfer_Encoding); |
+ |
+ // Decoding is not needed if a transfer encoding is not used. |
+ if (encoding.isNull()) |
+ return false; |
+ |
+ // Decoding is not needed if a no-op transfer encoding is used. |
+ if (equalIgnoringCase(encoding, "binary") || |
+ equalIgnoringCase(encoding, "7bit") || |
+ equalIgnoringCase(encoding, "8bit")) { |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+MultipartParser::Matcher::Matcher() = default; |
+ |
+MultipartParser::Matcher::Matcher(const char* data, |
+ size_t numMatchedBytes, |
+ size_t size) |
+ : m_data(data), m_numMatchedBytes(numMatchedBytes), m_size(size) {} |
+ |
+bool MultipartParser::Matcher::match(const char* first, const char* last) { |
+ while (first < last) { |
+ if (!match(*first++)) |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+void MultipartParser::Matcher::setNumMatchedBytes(size_t numMatchedBytes) { |
+ DCHECK_LE(numMatchedBytes, m_size); |
+ m_numMatchedBytes = numMatchedBytes; |
+} |
+ |
+MultipartParser::MultipartParser(Vector<char> boundary, Client* client) |
+ : m_client(client), |
+ m_delimiter(std::move(boundary)), |
+ m_state(State::ParsingPreamble) { |
+ // The delimiter consists of "\r\n" and a dash boundary which consists of |
+ // "--" and a boundary. |
+ m_delimiter.prepend("\r\n--", 4u); |
+ m_matcher = delimiterMatcher(kDashBoundaryOffset); |
+} |
+ |
+bool MultipartParser::appendData(const char* bytes, size_t size) { |
+ DCHECK_NE(State::Finished, m_state); |
+ DCHECK_NE(State::Cancelled, m_state); |
+ |
+ const char* const bytesEnd = bytes + size; |
+ |
+ while (bytes < bytesEnd) { |
+ switch (m_state) { |
+ case State::ParsingPreamble: |
+ // Parse either a preamble and a delimiter or a dash boundary. |
+ parseDelimiter(&bytes, bytesEnd); |
+ if (!m_matcher.isMatchComplete() && bytes < bytesEnd) { |
+ // Parse a preamble data (by ignoring it) and then a delimiter. |
+ m_matcher.setNumMatchedBytes(0u); |
+ parseDataAndDelimiter(&bytes, bytesEnd); |
+ } |
+ if (m_matcher.isMatchComplete()) { |
+ // Prepare for a delimiter suffix. |
+ m_matcher = delimiterSuffixMatcher(); |
+ m_state = State::ParsingDelimiterSuffix; |
+ } |
+ break; |
+ |
+ case State::ParsingDelimiterSuffix: |
+ // Parse transport padding and "\r\n" after a delimiter. |
+ // This state can be reached after either a preamble or part |
+ // octets are parsed. |
+ if (m_matcher.numMatchedBytes() == 0u) |
+ parseTransportPadding(&bytes, bytesEnd); |
+ while (bytes < bytesEnd) { |
+ if (!m_matcher.match(*bytes++)) |
+ return false; |
+ if (m_matcher.isMatchComplete()) { |
+ // Prepare for part header fields. |
+ m_state = State::ParsingPartHeaderFields; |
+ break; |
+ } |
+ } |
+ break; |
+ |
+ case State::ParsingPartHeaderFields: { |
+ // Parse part header fields (which ends with "\r\n") and an empty |
+ // line (which also ends with "\r\n"). |
+ // This state can be reached after a delimiter and a delimiter |
+ // suffix after either a preamble or part octets are parsed. |
+ HTTPHeaderMap headerFields; |
+ if (parseHeaderFields(&bytes, bytesEnd, &headerFields)) { |
+ // Decoding is not implemented. |
+ if (octetsNeedDecoding(headerFields)) |
yhirano
2017/01/11 08:54:03
Is this needed? With the current spec we end up ig
e_hakkinen
2017/05/02 23:38:56
Yes, let's remove this.
|
+ return false; |
+ // Prepare for part octets. |
+ m_matcher = delimiterMatcher(); |
+ m_state = State::ParsingPartOctets; |
+ m_client->partHeaderFieldsInMultipartReceived(headerFields); |
+ } |
+ break; |
+ } |
+ |
+ case State::ParsingPartOctets: { |
+ // Parse part octets and a delimiter. |
+ // This state can be reached only after part header fields are |
+ // parsed. |
+ const size_t numInitiallyMatchedBytes = m_matcher.numMatchedBytes(); |
+ const char* octetsBegin = bytes; |
+ parseDelimiter(&bytes, bytesEnd); |
+ if (!m_matcher.isMatchComplete() && bytes < bytesEnd) { |
+ if (m_matcher.numMatchedBytes() >= numInitiallyMatchedBytes && |
+ numInitiallyMatchedBytes > 0u) { |
+ // Since the matched bytes did not form a complete |
+ // delimiter, the matched bytes turned out to be octet |
+ // bytes instead of being delimiter bytes. Additionally, |
+ // some of the matched bytes are from the previous call and |
+ // are therefore not in the range [octetsBegin, bytesEnd[. |
+ m_client->partDataInMultipartReceived(m_matcher.data(), |
+ m_matcher.numMatchedBytes()); |
+ if (m_state != State::ParsingPartOctets) |
+ break; |
+ octetsBegin = bytes; |
+ } |
+ m_matcher.setNumMatchedBytes(0u); |
+ parseDataAndDelimiter(&bytes, bytesEnd); |
+ const char* const octetsEnd = bytes - m_matcher.numMatchedBytes(); |
+ if (octetsBegin < octetsEnd) { |
+ m_client->partDataInMultipartReceived( |
+ octetsBegin, static_cast<size_t>(octetsEnd - octetsBegin)); |
+ if (m_state != State::ParsingPartOctets) |
+ break; |
+ } |
+ } |
+ if (m_matcher.isMatchComplete()) { |
+ m_state = State::ParsingDelimiterOrCloseDelimiterSuffix; |
+ m_client->partDataInMultipartFullyReceived(); |
+ } |
+ break; |
+ } |
+ |
+ case State::ParsingDelimiterOrCloseDelimiterSuffix: |
+ // Determine whether this is a delimiter suffix or a close |
+ // delimiter suffix. |
+ // This state can be reached only after part octets are parsed. |
+ if (*bytes == '-') { |
+ // Prepare for a close delimiter suffix. |
+ m_matcher = closeDelimiterSuffixMatcher(); |
+ m_state = State::ParsingCloseDelimiterSuffix; |
+ } else { |
+ // Prepare for a delimiter suffix. |
+ m_matcher = delimiterSuffixMatcher(); |
+ m_state = State::ParsingDelimiterSuffix; |
+ } |
+ break; |
+ |
+ case State::ParsingCloseDelimiterSuffix: |
+ // Parse "--", transport padding and "\r\n" after a delimiter |
+ // (a delimiter and "--" constitute a close delimiter). |
+ // This state can be reached only after part octets are parsed. |
+ for (;;) { |
+ if (m_matcher.numMatchedBytes() == 2u) |
+ parseTransportPadding(&bytes, bytesEnd); |
+ if (bytes >= bytesEnd) |
+ break; |
+ if (!m_matcher.match(*bytes++)) |
+ return false; |
+ if (m_matcher.isMatchComplete()) { |
+ // Prepare for an epilogue. |
+ m_state = State::ParsingEpilogue; |
+ break; |
+ } |
+ } |
+ break; |
+ |
+ case State::ParsingEpilogue: |
+ // Parse an epilogue (by ignoring it). |
+ // This state can be reached only after a delimiter and a close |
+ // delimiter suffix after part octets are parsed. |
+ return true; |
+ |
+ case State::Cancelled: |
+ case State::Finished: |
+ // The client changed the state. |
+ return false; |
+ } |
+ } |
+ |
+ DCHECK_EQ(bytesEnd, bytes); |
+ |
+ return true; |
+} |
+ |
+void MultipartParser::cancel() { |
+ m_state = State::Cancelled; |
+} |
+ |
+bool MultipartParser::finish() { |
+ DCHECK_NE(State::Cancelled, m_state); |
+ DCHECK_NE(State::Finished, m_state); |
+ |
+ const State initialState = m_state; |
+ m_state = State::Finished; |
+ |
+ switch (initialState) { |
+ case State::ParsingPartOctets: |
+ if (m_matcher.numMatchedBytes() > 0u) { |
+ // Since the matched bytes did not form a complete delimiter, |
+ // the matched bytes turned out to be octet bytes instead of being |
+ // delimiter bytes. |
+ m_client->partDataInMultipartReceived(m_matcher.data(), |
+ m_matcher.numMatchedBytes()); |
+ } |
+ return false; |
+ case State::ParsingCloseDelimiterSuffix: |
+ // Require a full close delimiter consisting of a delimiter and "--" |
+ // but ignore missing or partial "\r\n" after that. |
+ return m_matcher.numMatchedBytes() >= 2u; |
+ case State::ParsingEpilogue: |
+ return true; |
+ default: |
+ return false; |
+ } |
+} |
+ |
+MultipartParser::Matcher MultipartParser::closeDelimiterSuffixMatcher() const { |
+ return Matcher(kCloseDelimiterSuffix, 0u, kCloseDelimiterSuffixSize); |
+} |
+ |
+MultipartParser::Matcher MultipartParser::delimiterMatcher( |
+ size_t numAlreadyMatchedBytes) const { |
+ return Matcher(m_delimiter.data(), numAlreadyMatchedBytes, |
+ m_delimiter.size()); |
+} |
+ |
+MultipartParser::Matcher MultipartParser::delimiterSuffixMatcher() const { |
+ return Matcher(kDelimiterSuffix, 0u, kDelimiterSuffixSize); |
+} |
+ |
+void MultipartParser::parseDataAndDelimiter(const char** bytesPointer, |
+ const char* bytesEnd) { |
+ DCHECK_EQ(0u, m_matcher.numMatchedBytes()); |
+ |
+ // Search for a complete delimiter within the bytes. |
+ const char* delimiterBegin = std::search( |
+ *bytesPointer, bytesEnd, m_delimiter.begin(), m_delimiter.end()); |
+ if (delimiterBegin != bytesEnd) { |
+ // A complete delimiter was found. The bytes before that are octet |
+ // bytes. |
+ const char* const delimiterEnd = delimiterBegin + m_delimiter.size(); |
+ const bool matched = m_matcher.match(delimiterBegin, delimiterEnd); |
+ DCHECK(matched); |
+ DCHECK(m_matcher.isMatchComplete()); |
+ *bytesPointer = delimiterEnd; |
+ } else { |
+ // Search for a partial delimiter in the end of the bytes. |
+ const size_t size = static_cast<size_t>(bytesEnd - *bytesPointer); |
+ for (delimiterBegin = bytesEnd - std::min(m_delimiter.size() - 1u, size); |
+ delimiterBegin < bytesEnd; ++delimiterBegin) { |
+ if (m_matcher.match(delimiterBegin, bytesEnd)) |
+ break; |
+ m_matcher.setNumMatchedBytes(0u); |
+ } |
+ // If a partial delimiter was found in the end of bytes, the bytes |
+ // before the partial delimiter are definitely octets bytes and |
+ // the partial delimiter bytes are buffered for now. |
+ // If a partial delimiter was not found in the end of bytes, all bytes |
+ // are definitely octets bytes. |
+ // In all cases, all bytes are parsed now. |
+ *bytesPointer = bytesEnd; |
+ } |
+ |
+ DCHECK(m_matcher.isMatchComplete() || *bytesPointer == bytesEnd); |
+} |
+ |
+void MultipartParser::parseDelimiter(const char** bytesPointer, |
+ const char* bytesEnd) { |
+ DCHECK(!m_matcher.isMatchComplete()); |
+ while (*bytesPointer < bytesEnd && m_matcher.match(*(*bytesPointer))) { |
+ ++(*bytesPointer); |
+ if (m_matcher.isMatchComplete()) |
+ break; |
+ } |
+} |
+ |
+bool MultipartParser::parseHeaderFields(const char** bytesPointer, |
+ const char* bytesEnd, |
+ HTTPHeaderMap* headerFields) { |
+ // Combine the current bytes with buffered header bytes if needed. |
+ const char* headerBytes = *bytesPointer; |
+ size_t headerSize = static_cast<size_t>(bytesEnd - *bytesPointer); |
+ if (!m_bufferedHeaderBytes.isEmpty()) { |
+ m_bufferedHeaderBytes.append(headerBytes, headerSize); |
+ headerBytes = m_bufferedHeaderBytes.data(); |
+ headerSize = m_bufferedHeaderBytes.size(); |
+ } |
+ |
+ size_t end = 0u; |
+ if (!parseMultipartFormHeadersFromBody(headerBytes, headerSize, headerFields, |
+ &end)) { |
+ // Store the current header bytes for the next call unless that has |
+ // already been done. |
+ if (m_bufferedHeaderBytes.isEmpty()) |
+ m_bufferedHeaderBytes.append(headerBytes, headerSize); |
+ *bytesPointer = bytesEnd; |
+ return false; |
+ } |
+ m_bufferedHeaderBytes.clear(); |
+ *bytesPointer = bytesEnd - (headerSize - end); |
+ |
+ return true; |
+} |
+ |
+void MultipartParser::parseTransportPadding(const char** bytesPointer, |
+ const char* bytesEnd) const { |
+ while (*bytesPointer < bytesEnd && |
+ (*(*bytesPointer) == '\t' || *(*bytesPointer) == ' ')) |
+ ++(*bytesPointer); |
+} |
+ |
+DEFINE_TRACE(MultipartParser) { |
+ visitor->trace(m_client); |
+} |
+ |
+} // namespace blink |