OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MultipartParser_h | |
6 #define MultipartParser_h | |
7 | |
8 #include "modules/ModulesExport.h" | |
9 #include "platform/heap/Handle.h" | |
10 #include "platform/network/ResourceResponse.h" | |
11 #include "wtf/Vector.h" | |
12 | |
13 namespace blink { | |
14 | |
15 // This class parses a multipart message which is supplied (possible in chunks) | |
16 // to MultipartParser::appendData which parses the data and passes resulting | |
17 // part header fields and data to Client. | |
18 // | |
19 // - MultipartParser::appendData does not accept base64, quoted-printable nor | |
20 // otherwise transfer encoded multipart message parts (no-op transfer | |
21 // encodings "binary", "7bit" and "8bit" are OK). | |
22 // - If MultipartParser::cancel() is called, Client's methods will not be | |
23 // called anymore. | |
24 class MODULES_EXPORT MultipartParser final : public GarbageCollectedFinalized<Mu ltipartParser> { | |
25 WTF_MAKE_NONCOPYABLE(MultipartParser); | |
26 | |
27 public: | |
28 // Client recieves parsed part header fields and data. | |
29 class MODULES_EXPORT Client : public GarbageCollectedMixin { | |
30 public: | |
31 virtual ~Client() = default; | |
32 // The method is called whenever header fields of a part are parsed. | |
33 virtual void partHeaderFieldsInMultipartReceived(const ResourceResponse& ) = 0; | |
34 // The method is called whenever some data of a part is parsed which | |
35 // can happen zero or more times per each part. It always holds that | |
36 // |size| > 0. | |
37 virtual void partDataInMultipartReceived(const char* bytes, size_t) = 0; | |
38 // The method is called whenever all data of a complete part is parsed. | |
39 virtual void partDataInMultipartFullyReceived() = 0; | |
40 DEFINE_INLINE_VIRTUAL_TRACE() {} | |
41 }; | |
42 | |
43 MultipartParser(Vector<char> boundary, Client*); | |
44 bool appendData(const char* bytes, size_t); | |
45 void cancel(); | |
46 bool finish(); | |
47 | |
48 bool isCancelled() const { return m_state == Cancelled; } | |
49 | |
50 DECLARE_TRACE(); | |
51 | |
52 private: | |
53 class Matcher { | |
54 public: | |
55 Matcher(); | |
56 Matcher(const char* data, size_t numMatchedBytes, size_t); | |
57 | |
58 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.
| |
59 bool match(const char* first, const char* last); | |
60 bool isMatchComplete() const { return m_numMatchedBytes == m_size; } | |
61 size_t numMatchedBytes() const { return m_numMatchedBytes; } | |
62 void setNumMatchedBytes(size_t); | |
63 | |
64 const char* data() const { return m_data; } | |
65 | |
66 private: | |
67 const char* m_data = nullptr; | |
68 size_t m_numMatchedBytes = 0u; | |
69 size_t m_size = 0u; | |
70 }; | |
71 | |
72 Matcher closeDelimiterSuffixMatcher() const; | |
73 Matcher delimiterMatcher(size_t numAlreadyMatchedBytes = 0u) const; | |
74 Matcher delimiterSuffixMatcher() const; | |
75 | |
76 void parseDataAndDelimiter(const char** bytesPointer, const char* bytesEnd); | |
77 void parseDelimiter(const char** bytesPointer, const char* bytesEnd); | |
78 bool parseHeaderFields(const char** bytesPointer, const char* bytesEnd, WebU RLResponse*); | |
79 void parseTransportPadding(const char** bytesPointer, const char* bytesEnd) const; | |
80 | |
81 Matcher m_matcher; | |
82 Vector<char> m_bufferedHeaderBytes; | |
83 Member<Client> m_client; | |
84 Vector<char> m_delimiter; | |
85 | |
86 enum State { | |
yhirano
2016/09/26 10:59:34
Can you use enum class instead?
e_hakkinen
2016/09/28 15:15:05
Done.
| |
87 ParsingPreamble, | |
88 ParsingDelimiterSuffix, | |
89 ParsingPartHeaderFields, | |
90 ParsingPartOctets, | |
91 ParsingDelimiterOrCloseDelimiterSuffix, | |
92 ParsingCloseDelimiterSuffix, | |
93 ParsingEpilogue, | |
94 Cancelled, | |
95 Finished | |
96 } m_state; | |
97 }; | |
98 | |
99 } // namespace blink | |
100 | |
101 #endif // MultipartParser_h | |
OLD | NEW |