Index: third_party/WebKit/Source/modules/fetch/MultipartParser.h |
diff --git a/third_party/WebKit/Source/modules/fetch/MultipartParser.h b/third_party/WebKit/Source/modules/fetch/MultipartParser.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..77a8a61f08046074e997c1cf36eb3e0b5d37fad4 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/fetch/MultipartParser.h |
@@ -0,0 +1,113 @@ |
+// 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. |
+ |
+#ifndef MultipartParser_h |
+#define MultipartParser_h |
+ |
+#include "modules/ModulesExport.h" |
+#include "platform/heap/Handle.h" |
+#include "platform/network/HTTPHeaderMap.h" |
+#include "wtf/Assertions.h" |
+#include "wtf/Vector.h" |
+ |
+namespace blink { |
+ |
+// This class parses a multipart message which is supplied (possible in chunks) |
+// to MultipartParser::appendData which parses the data and passes resulting |
+// part header fields and data to Client. |
+// |
+// - MultipartParser::appendData does not accept base64, quoted-printable nor |
+// otherwise transfer encoded multipart message parts (no-op transfer |
+// encodings "binary", "7bit" and "8bit" are OK). |
+// - If MultipartParser::cancel() is called, Client's methods will not be |
+// called anymore. |
+class MODULES_EXPORT MultipartParser final |
+ : public GarbageCollectedFinalized<MultipartParser> { |
+ WTF_MAKE_NONCOPYABLE(MultipartParser); |
+ |
+ public: |
+ // Client recieves parsed part header fields and data. |
+ class MODULES_EXPORT Client : public GarbageCollectedMixin { |
+ public: |
+ virtual ~Client() = default; |
+ // The method is called whenever header fields of a part are parsed. |
+ virtual void partHeaderFieldsInMultipartReceived( |
+ const HTTPHeaderMap& headerFields) = 0; |
+ // The method is called whenever some data of a part is parsed which |
+ // can happen zero or more times per each part. It always holds that |
+ // |size| > 0. |
+ virtual void partDataInMultipartReceived(const char* bytes, size_t) = 0; |
+ // The method is called whenever all data of a complete part is parsed. |
+ virtual void partDataInMultipartFullyReceived() = 0; |
+ DEFINE_INLINE_VIRTUAL_TRACE() {} |
+ }; |
+ |
+ MultipartParser(Vector<char> boundary, Client*); |
+ bool appendData(const char* bytes, size_t); |
+ void cancel(); |
+ bool finish(); |
+ |
+ bool isCancelled() const { return m_state == State::Cancelled; } |
+ |
+ DECLARE_TRACE(); |
+ |
+ private: |
+ class Matcher { |
+ public: |
+ Matcher(); |
+ Matcher(const char* data, size_t numMatchedBytes, size_t); |
+ |
+ bool match(char value) { |
+ DCHECK_LT(m_numMatchedBytes, m_size); |
+ if (value != m_data[m_numMatchedBytes]) |
+ return false; |
+ ++m_numMatchedBytes; |
+ return true; |
+ } |
+ bool match(const char* first, const char* last); |
+ bool isMatchComplete() const { return m_numMatchedBytes == m_size; } |
+ size_t numMatchedBytes() const { return m_numMatchedBytes; } |
+ void setNumMatchedBytes(size_t); |
+ |
+ const char* data() const { return m_data; } |
+ |
+ private: |
+ const char* m_data = nullptr; |
+ size_t m_numMatchedBytes = 0u; |
+ size_t m_size = 0u; |
+ }; |
+ |
+ Matcher closeDelimiterSuffixMatcher() const; |
+ Matcher delimiterMatcher(size_t numAlreadyMatchedBytes = 0u) const; |
+ Matcher delimiterSuffixMatcher() const; |
+ |
+ void parseDataAndDelimiter(const char** bytesPointer, const char* bytesEnd); |
+ void parseDelimiter(const char** bytesPointer, const char* bytesEnd); |
+ bool parseHeaderFields(const char** bytesPointer, |
+ const char* bytesEnd, |
+ HTTPHeaderMap* headerFields); |
+ void parseTransportPadding(const char** bytesPointer, |
+ const char* bytesEnd) const; |
+ |
+ Matcher m_matcher; |
+ Vector<char> m_bufferedHeaderBytes; |
+ Member<Client> m_client; |
+ Vector<char> m_delimiter; |
+ |
+ enum class State { |
+ ParsingPreamble, |
+ ParsingDelimiterSuffix, |
+ ParsingPartHeaderFields, |
+ ParsingPartOctets, |
+ ParsingDelimiterOrCloseDelimiterSuffix, |
+ ParsingCloseDelimiterSuffix, |
+ ParsingEpilogue, |
+ Cancelled, |
+ Finished |
+ } m_state; |
+}; |
+ |
+} // namespace blink |
+ |
+#endif // MultipartParser_h |