Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_POST_DATA_PARSER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_POST_DATA_PARSER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/string_piece.h" | |
| 14 | |
| 15 namespace net { | |
| 16 class URLRequest; | |
| 17 } | |
| 18 | |
| 19 namespace extensions { | |
| 20 | |
| 21 // Interface for parsers for the POST data. | |
| 22 class PostDataParser { | |
| 23 public: | |
| 24 class Result { | |
| 25 public: | |
| 26 Result(); | |
| 27 ~Result(); | |
| 28 const std::string& get_key() const { | |
| 29 return key_; | |
| 30 } | |
| 31 const std::string& get_val() const { | |
| 32 return val_; | |
| 33 } | |
| 34 void Reset(); | |
| 35 void SetKey(const char* s, size_t n); | |
| 36 void SetKey(const std::string& str); | |
| 37 void SetVal(const char* s, size_t n); | |
| 38 void SetVal(const std::string& str); | |
| 39 | |
| 40 private: | |
| 41 std::string key_; | |
| 42 std::string val_; | |
| 43 }; | |
| 44 // Creates a correct parser instance based on the |request|. Returns NULL | |
| 45 // on failure. | |
| 46 static scoped_ptr<PostDataParser> CreatePostDataParser( | |
| 47 net::URLRequest* request); | |
| 48 // Creates a correct parser instance based on the |value| of "Content-Type" | |
| 49 // URLRequest headers. If |value| is NULL, it defaults to | |
| 50 // "application/x-www-form-urlencoded". Returns NULL on failure. | |
| 51 static scoped_ptr<PostDataParser> CreatePostDataParser( | |
|
vabr (Chromium)
2012/07/16 15:40:51
I separated the two steps in the original CreatePo
| |
| 52 const std::string* value); | |
|
battre
2012/07/16 17:39:45
can you rename |value| to content_type?
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
| 53 // No member variables, so the destructor can be safely inlined. | |
| 54 virtual ~PostDataParser(); | |
| 55 // Returns true if there was some data, it was well formed and all was read. | |
| 56 virtual bool AllDataReadOK() = 0; | |
| 57 // Returns the next key-value pair as |result|. After SetSource has succeeded, | |
| 58 // this allows to iterate over all pairs in the source. | |
| 59 // Returns true as long as a new pair was successfully found. | |
| 60 virtual bool GetNextPair(Result* result) = 0; | |
| 61 // Sets the |source| of the data to be parsed. The ownership is let with the | |
| 62 // caller and the source should live until |this| dies or |this->SetSource()| | |
| 63 // is called again, whatever comes sooner. Returns true on success. | |
| 64 virtual bool SetSource(const std::vector<char>* source) = 0; | |
| 65 | |
| 66 protected: | |
| 67 PostDataParser() {} | |
| 68 | |
| 69 private: | |
| 70 DISALLOW_COPY_AND_ASSIGN(PostDataParser); | |
| 71 }; | |
| 72 | |
| 73 // Parses URLencoded forms, see | |
| 74 // http://www.w3.org/TR/REC-html40-971218/interact/forms.html#h-17.13.4.1 . | |
| 75 class PostDataParserUrlEncoded : public PostDataParser { | |
| 76 public: | |
| 77 PostDataParserUrlEncoded(); | |
| 78 virtual ~PostDataParserUrlEncoded(); | |
| 79 // Implementation of PostDataParser. | |
| 80 virtual bool AllDataReadOK() OVERRIDE; | |
| 81 virtual bool GetNextPair(Result* result) OVERRIDE; | |
| 82 virtual bool SetSource(const std::vector<char>* source) OVERRIDE; | |
| 83 | |
| 84 private: | |
| 85 const std::vector<char>* source_; | |
| 86 std::vector<char>::const_iterator offset_; | |
| 87 DISALLOW_COPY_AND_ASSIGN(PostDataParserUrlEncoded); | |
| 88 }; | |
| 89 | |
| 90 // Parses forms encoded as multipart, see RFC2388. | |
| 91 class PostDataParserMultipart : public PostDataParser { | |
| 92 public: | |
| 93 explicit PostDataParserMultipart(const std::string& boundary_separator); | |
| 94 virtual ~PostDataParserMultipart(); | |
| 95 // Implementation of PostDataParser. | |
| 96 virtual bool AllDataReadOK() OVERRIDE; | |
| 97 virtual bool GetNextPair(Result* result) OVERRIDE; | |
| 98 virtual bool SetSource(const std::vector<char>* source) OVERRIDE; | |
| 99 | |
| 100 private: | |
| 101 // Note on implementation: | |
| 102 // This parser reads the source line by line. There are four types of lines: | |
| 103 // - "Boundary" line, separating entries. | |
| 104 // - "End boundary" line, ends the data. | |
| 105 // - "Content-Disposition" line, containing the "key" for |result|. | |
| 106 // - Empty lines. | |
| 107 // - Other non-empty lines. | |
| 108 enum Lines {kBoundary, kEndBoundary, kDisposition, kEmpty, kOther}; | |
|
battre
2012/07/16 17:39:45
nit: rename to LineType?
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
| 109 // The parser uses the following 7-state automaton to check for structure: | |
| 110 // kInit --Boundary--> kHeadStart, kInit --not(Boundary)--> kError | |
| 111 // kHeadStart --Disposition--> kHeadRead, | |
| 112 // kHeadStart --not(Disposition)--> kHead | |
| 113 // kHead --Disposition--> kHeadRead, kHead --not(Disposition)-->kHead | |
| 114 // kHeadRead --Empty-->kBody, kHeadRead --not(Empty)--> kHeadRead | |
| 115 // kBody --Boundary--> kHeadStart, kBody --EndBoundary--> kFinal, | |
| 116 // kBody --not(Boundary OR EndBoundary)--> kBody | |
| 117 enum States {kInit, kHeadStart, kHead, kHeadRead, kBody, kFinal, kError}; | |
|
battre
2012/07/16 17:39:45
nit: rename to State?
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
| 118 // One-step of the automaton, based on |state_| and |line_type_|. | |
| 119 // This calls SeekNextLine() and returns it return value. | |
| 120 bool DoStep(); | |
| 121 // Determine the |line_type_| of the current line_start_. | |
| 122 Lines GetLineType(); | |
| 123 // Read one more line from |source_|, update line pointers. | |
| 124 bool SeekNextLine(); | |
| 125 // Extracts "key" and possibly "val" from a DISP line. Returns success. | |
| 126 bool ParseHead(Result* result, bool* val_extracted); | |
| 127 // We parse the first |length_| bytes from |source_|. | |
| 128 const char* source_; | |
| 129 size_t length_; | |
| 130 // Offset of the current and next line from |source_|: | |
| 131 // [line_start_]...line... [line_end_]EOL [next_line_]...line... | |
| 132 size_t line_start_; // Points at the first character of the current line. | |
| 133 size_t line_end_; // Points right beyond the last character of the line. | |
| 134 size_t next_line_; // First char. of the next line, or beyond source length. | |
| 135 const std::string boundary_; | |
| 136 const std::string final_boundary_; | |
| 137 States state_; | |
| 138 Lines line_type_; | |
| 139 DISALLOW_COPY_AND_ASSIGN(PostDataParserMultipart); | |
| 140 }; | |
| 141 | |
| 142 } // namespace extensions | |
| 143 | |
| 144 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_POST_DATA_PARSER_H_ | |
| OLD | NEW |