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

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

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: Adding an example value of formData to the docs Created 8 years, 5 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 #include "chrome/browser/extensions/api/web_request/post_data_parser.h"
6
7 #include "net/base/escape.h"
8 #include "net/url_request/url_request.h"
9
10 namespace {
11 const char kContentDisposition[] = "Content-Disposition:";
12 }
13
14 namespace extensions {
15
16 // Implementation of PostDataParser and PostDataParser::Result .
17
18 PostDataParser::Result::Result() {}
19 PostDataParser::Result::~Result() {}
20
21 void PostDataParser::Result::Reset() {
22 key_.erase();
23 val_.erase();
24 }
25
26 void PostDataParser::Result::SetKey(const char* s, size_t n) {
27 key_.replace(0, std::string::npos, s, n);
28 }
29
30 void PostDataParser::Result::SetVal(const char* s, size_t n) {
31 val_.replace(0, std::string::npos, s, n);
32 }
33
34 void PostDataParser::Result::SetKey(const std::string& str) {
35 key_ = str;
36 }
37
38 void PostDataParser::Result::SetVal(const std::string& str) {
39 val_ = str;
40 }
41
42 PostDataParser::~PostDataParser() {}
43
44 // static
45 scoped_ptr<PostDataParser> PostDataParser::CreatePostDataParser(
46 net::URLRequest* request) {
47 std::string value;
48 const bool found = request->extra_request_headers().GetHeader(
49 "Content-Type", &value);
50 return CreatePostDataParser(found ? &value : NULL);
51 }
52
53 // static
54 scoped_ptr<PostDataParser> PostDataParser::CreatePostDataParser(
55 const std::string* content_type_header) {
56 enum ParserChoice {kUrlEncoded, kMultipart, kError};
57 ParserChoice choice = kError;
58 std::string boundary;
59
60 if (content_type_header == NULL) {
61 choice = kUrlEncoded;
62 } else {
63 const std::string content_type(
64 content_type_header->substr(0, content_type_header->find(';')));
65 if (content_type == "application/x-www-form-urlencoded") {
66 choice = kUrlEncoded;
67 } else if (content_type == "multipart/form-data") {
68 const char kBoundaryString[] = "boundary=";
69 size_t offset = content_type_header->find(kBoundaryString);
70 if (offset == std::string::npos) {
71 // Malformed header.
72 return scoped_ptr<PostDataParser>();
73 }
74 offset += strlen(kBoundaryString);
75 boundary = content_type_header->substr(
76 offset, content_type_header->find(';', offset));
77 choice = kMultipart;
78 }
79 }
80 // Other cases are unparseable, including when |content_type| is "text/plain".
81
82 switch (choice) {
83 case kUrlEncoded:
84 return scoped_ptr<PostDataParser>(new PostDataParserUrlEncoded());
85 case kMultipart:
86 return scoped_ptr<PostDataParser>(new PostDataParserMultipart(boundary));
87 default: // In other words, case kError:
88 return scoped_ptr<PostDataParser>();
89 }
90 }
91
92 // Implementation of PostDataParserUrlEncoded.
93
94 PostDataParserUrlEncoded::PostDataParserUrlEncoded() : source_(NULL) {}
95
96 PostDataParserUrlEncoded::~PostDataParserUrlEncoded() {}
97
98 bool PostDataParserUrlEncoded::AllDataReadOK() {
99 return source_ != NULL && offset_ == source_->end();
100 }
101
102 bool PostDataParserUrlEncoded::GetNextPair(Result* result) {
103 result->Reset();
104 if (source_ == NULL)
105 return false;
106 if (offset_ == source_->end())
107 return false;
108 std::vector<char>::const_iterator seek = offset_;
109 // (*) Now we have |seek| >= |offset_| until the end of this function:
110 while (seek != source_->end() && *seek != '=')
111 ++seek;
112 if (seek == source_->end()) {
113 // This means the data is malformed.
114 offset_ = seek;
115 return false;
116 }
117 std::string encoded_key(&(*offset_), seek - offset_); // Safe, see (*).
118 const net::UnescapeRule::Type unescape_rules =
119 net::UnescapeRule::URL_SPECIAL_CHARS | net::UnescapeRule::CONTROL_CHARS |
120 net::UnescapeRule::SPACES | net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
121 result->SetKey(net::UnescapeURLComponent(encoded_key, unescape_rules));
122 offset_ = ++seek;
123 while (seek != source_->end() && *seek != '&')
124 ++seek;
125 std::string encoded_val(&(*offset_), seek - offset_); // Safe, see (*).
126 result->SetVal(net::UnescapeURLComponent(encoded_val, unescape_rules));
127 offset_ = (seek == source_->end()) ? seek : seek + 1;
128 return true;
129 }
130
131 bool PostDataParserUrlEncoded::SetSource(const std::vector<char>* source) {
132 if (source_ != NULL)
133 return false;
134 source_ = source;
135 offset_ = source_->begin();
136 return true;
137 }
138
139 // Implementation of PostDataParserMultipart.
140
141 PostDataParserMultipart::PostDataParserMultipart(
142 const std::string& boundary_separator)
143 : source_(NULL),
144 length_(0), // Dummy value.
145 line_start_(0), // Dummy value.
146 line_end_(0), // Dummy value.
147 next_line_(0), // Dummy value.
148 boundary_("--" + boundary_separator),
149 final_boundary_(boundary_ + "--"),
150 state_(kInit),
151 line_type_(kEmpty) // Dummy value.
152 {}
153
154 PostDataParserMultipart::~PostDataParserMultipart() {}
155
156 bool PostDataParserMultipart::AllDataReadOK() {
157 return source_ != NULL && next_line_ >= length_ && state_ == kFinal;
158 }
159
160 // This function reads one block of the data, between two boundaries.
161 // First it reads the header to learn the key, and possibly also the
162 // value, if this block is for a file input element.
163 // Otherwise it then reads the value from the body.
164 bool PostDataParserMultipart::GetNextPair(Result* result) {
165 result->Reset();
166 if (state_ == kError)
167 return false;
168 while (state_ != kHeadRead) {
169 if (!DoStep())
170 return false;
171 }
172 bool val_extracted = false;
173 bool name_parsed = ParseHead(result, &val_extracted);
174 while (state_ != kBody) {
175 if (!DoStep())
176 return false;
177 }
178 size_t val_start;
179 size_t val_end = 0; // Dummy value, replaced below, see (*).
180 // There may not be more to read from |source_| if the current result comes
181 // from a "file" input element. But then |result| is complete already.
182 if (!DoStep())
183 return val_extracted;
184 val_start = line_start_;
185 // (*) Now state_ == kBody, so val_end gets updated below.
186 while (state_ != kHeadStart && state_ != kFinal) {
187 val_end = line_end_;
188 if (!DoStep()) break;
189 }
190 if (name_parsed && !val_extracted) {
191 result->SetVal(source_ + val_start, val_end - val_start);
192 }
193 return name_parsed;
194 }
195
196 bool PostDataParserMultipart::SetSource(const std::vector<char>* source) {
197 if (state_ == kError)
198 return false;
199 if (source_ != NULL && next_line_ < length_)
200 return false;
201 source_ = &(source->front());
202 length_ = source->size();
203 next_line_ = 0;
204 return true;
205 }
206
207 bool PostDataParserMultipart::DoStep() {
208 if (!SeekNextLine())
209 return false;
210 switch (state_) {
211 case kInit:
212 if (line_type_ == kBoundary)
213 state_ = kHeadStart;
214 else
215 state_ = kError;
216 break;
217 case kHeadStart:
218 if (line_type_ == kDisposition)
219 state_ = kHeadRead;
220 else
221 state_ = kHead;
222 break;
223 case kHead:
224 if (line_type_ == kDisposition)
225 state_ = kHeadRead;
226 break;
227 case kHeadRead:
228 if (line_type_ == kEmpty)
229 state_ = kBody;
230 break;
231 case kBody:
232 if (line_type_ == kBoundary)
233 state_ = kHeadStart;
234 else if (line_type_ == kEndBoundary)
235 state_ = kFinal;
236 break;
237 case kFinal:
238 if (line_type_ != kEmpty)
239 state_ = kError;
240 case kError:
241 break;
242 }
243 return true;
244 }
245
246 PostDataParserMultipart::LineType PostDataParserMultipart::GetLineType() {
247 const size_t line_length = line_end_ - line_start_;
248 const base::StringPiece line(source_ + line_start_, line_length);
249 if (line == boundary_)
250 return kBoundary;
251 else if (line == final_boundary_)
252 return kEndBoundary;
253 else if (line.starts_with(kContentDisposition))
254 return kDisposition;
255 else if (line_start_ == line_end_)
256 return kEmpty;
257 else
258 return kOther;
259 }
260
261 // Contract: only to be called from DoStep().
262 bool PostDataParserMultipart::SeekNextLine() {
263 if (source_ == NULL || state_ == kError)
264 return false;
265 if (next_line_ >= length_)
266 return false;
267 line_start_ = next_line_;
268 size_t seek = line_start_;
269 while (seek < length_ && *(source_ + seek) != '\r')
270 ++seek;
271 line_end_ = seek;
272 line_type_ = GetLineType();
273 if ((seek+1) < length_ && strncmp(source_ + seek, "\r\n", 2) != 0)
274 return false;
275 next_line_ = seek + 2;
276 return true;
277 }
278
279 // Contract: line_type_ == kDisposition.
280 bool PostDataParserMultipart::ParseHead(Result* result, bool* val_extracted) {
281 DCHECK_EQ(kDisposition, line_type_);
282 base::StringPiece line(source_ + line_start_, line_end_ - line_start_);
283 const char kNameEquals[] = " name=\"";
284 const char kFilenameEquals[] = " filename=\"";
285 size_t key_offset = line.find(kNameEquals);
286 if (key_offset == base::StringPiece::npos)
287 return false;
288 key_offset += strlen(kNameEquals);
289 result->SetKey(source_ + line_start_ + key_offset,
290 line.find('"', key_offset) - key_offset);
291 size_t val_offset = line.find(kFilenameEquals);
292 if (val_offset == std::string::npos) {
293 *val_extracted = false;
294 } else {
295 *val_extracted = true;
296 val_offset += strlen(kFilenameEquals);
297 result->SetVal(source_ + line_start_ + val_offset,
298 line.find('"', val_offset) - val_offset);
299 }
300 return true;
301 }
302
303 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698