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