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