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

Side by Side Diff: chrome/browser/extensions/api/web_request/post_data_parser.h

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

Powered by Google App Engine
This is Rietveld 408576698