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