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

Unified Diff: third_party/WebKit/Source/modules/fetch/MultipartParser.cpp

Issue 2292763002: [Fetch API] Implement Request.formData and Response.formData. (Closed)
Patch Set: Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
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..712aeb10308d5b069ec469dab2adcac53e606442
--- /dev/null
+++ b/third_party/WebKit/Source/modules/fetch/MultipartParser.cpp
@@ -0,0 +1,358 @@
+// 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 "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 "--".
+constexpr char kDelimiterSuffix[] = "\r\n";
+constexpr size_t kDelimiterSuffixSize = WTF_ARRAY_LENGTH(kDelimiterSuffix) - 1u;
+
+bool octetsNeedDecoding(const WebURLResponse& response)
+{
+ const AtomicString& encoding = response.toResourceResponse().httpHeaderField(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(char value)
+{
+ DCHECK_LT(m_numMatchedBytes, m_size);
+ if (value != m_data[m_numMatchedBytes])
+ return false;
+ ++m_numMatchedBytes;
+ return true;
+}
+
+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(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(Finished, m_state);
+ DCHECK_NE(Cancelled, m_state);
+
+ const char* bytesEnd = bytes + size;
+
+ while (bytes < bytesEnd) {
+ switch (m_state) {
+ case 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 = ParsingDelimiterSuffix;
+ }
+ break;
+
+ case 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 = ParsingPartHeaderFields;
+ break;
+ }
+ }
+ break;
+
+ case 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.
+ WebURLResponse response;
+ if (parseHeaderFields(&bytes, bytesEnd, &response)) {
+ // Decoding is not implemented.
+ if (octetsNeedDecoding(response))
+ return false;
+ // Prepare for part octets.
+ m_matcher = delimiterMatcher();
+ m_state = ParsingPartOctets;
+ m_client->partHeaderFieldsInMultipartReceived(response.toResourceResponse());
+ }
+ break;
+ }
+
+ case ParsingPartOctets: {
+ // Parse part octets and a delimiter.
+ // This state can be reached only after part header fields are
+ // parsed.
+ size_t numInitiallyMatchedBytes = m_matcher.numMatchedBytes();
horo 2016/09/27 10:45:50 nit: const
e_hakkinen 2016/09/28 15:15:04 Done.
+ 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 != ParsingPartOctets)
+ break;
+ octetsBegin = bytes;
+ }
+ m_matcher.setNumMatchedBytes(0u);
+ parseDataAndDelimiter(&bytes, bytesEnd);
+ const char* octetsEnd = bytes - m_matcher.numMatchedBytes();
+ if (octetsBegin < octetsEnd) {
+ m_client->partDataInMultipartReceived(octetsBegin, static_cast<size_t>(octetsEnd - octetsBegin));
+ if (m_state != ParsingPartOctets)
+ break;
+ }
+ }
+ if (m_matcher.isMatchComplete()) {
+ m_state = ParsingDelimiterOrCloseDelimiterSuffix;
+ m_client->partDataInMultipartFullyReceived();
+ }
+ break;
+ }
+
+ case 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 = ParsingCloseDelimiterSuffix;
+ } else {
+ // Prepare for a delimiter suffix.
+ m_matcher = delimiterSuffixMatcher();
+ m_state = ParsingDelimiterSuffix;
+ }
+ break;
+
+ case 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 = ParsingEpilogue;
+ break;
+ }
+ }
+ break;
+
+ case 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 Cancelled:
+ case Finished:
+ // The client changed the state.
+ return false;
+ }
+ }
+
+ DCHECK_EQ(bytesEnd, bytes);
+
+ return true;
+}
+
+void MultipartParser::cancel()
+{
+ m_state = Cancelled;
+}
+
+bool MultipartParser::finish()
+{
+ DCHECK_NE(Cancelled, m_state);
+
horo 2016/09/27 10:45:50 Is it allowed to call MultipartParser::finish() mu
e_hakkinen 2016/09/28 15:15:05 Maybe it is easier to find the success path correc
+ State initialState = m_state;
horo 2016/09/27 10:45:50 nit: const
e_hakkinen 2016/09/28 15:15:04 Done.
+ m_state = Finished;
+
+ switch (initialState) {
+ case 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 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 ParsingEpilogue:
+ case Finished:
+ 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* delimiterEnd = delimiterBegin + m_delimiter.size();
+ bool matched = m_matcher.match(delimiterBegin, delimiterEnd);
horo 2016/09/27 10:45:50 nit: const
e_hakkinen 2016/09/28 15:15:04 Done.
+ DCHECK(matched);
+ DCHECK(m_matcher.isMatchComplete());
+ *bytesPointer = delimiterEnd;
+ } else {
+ // Search for a partial delimiter in the end of the bytes.
+ 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, WebURLResponse* response)
+{
+ // 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 (!Platform::current()->parseMultipartHeadersFromBody(headerBytes, headerSize, response, &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

Powered by Google App Engine
This is Rietveld 408576698