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

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

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.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..f44def473be655bcfaff3a856ae77304bb81bf6f
--- /dev/null
+++ b/third_party/WebKit/Source/modules/fetch/MultipartParser.h
@@ -0,0 +1,101 @@
+// 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/ResourceResponse.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 ResourceResponse&) = 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 == Cancelled; }
+
+ DECLARE_TRACE();
+
+private:
+ class Matcher {
+ public:
+ Matcher();
+ Matcher(const char* data, size_t numMatchedBytes, size_t);
+
+ bool match(char value);
horo 2016/09/27 10:45:50 I think we should make this function inline. http
e_hakkinen 2016/09/28 15:15:05 Done.
+ 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, WebURLResponse*);
+ 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 State {
yhirano 2016/09/26 10:59:34 Can you use enum class instead?
e_hakkinen 2016/09/28 15:15:05 Done.
+ ParsingPreamble,
+ ParsingDelimiterSuffix,
+ ParsingPartHeaderFields,
+ ParsingPartOctets,
+ ParsingDelimiterOrCloseDelimiterSuffix,
+ ParsingCloseDelimiterSuffix,
+ ParsingEpilogue,
+ Cancelled,
+ Finished
+ } m_state;
+};
+
+} // namespace blink
+
+#endif // MultipartParser_h

Powered by Google App Engine
This is Rietveld 408576698