Chromium Code Reviews| Index: chrome/browser/extensions/api/web_request/post_data_parser.h |
| diff --git a/chrome/browser/extensions/api/web_request/post_data_parser.h b/chrome/browser/extensions/api/web_request/post_data_parser.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0380b1fb64d6956a175abe0b0d3fcc19c8f99831 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/web_request/post_data_parser.h |
| @@ -0,0 +1,152 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_POST_DATA_PARSER_H_ |
| +#define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_POST_DATA_PARSER_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/string_piece.h" |
| + |
| +namespace base { |
| +class DictionaryValue; |
| +} |
| + |
| +namespace net { |
| +class URLRequest; |
| +} |
| + |
| +namespace extensions { |
| + |
| +// Interface for parsers for the POST data. |
| +class PostDataParser { |
| + public: |
| + class Result { |
| + public: |
| + Result(); |
| + ~Result(); |
| + 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.
|
| + return key_; |
| + } |
| + const std::string& get_val() const { |
| + return val_; |
| + } |
| + void Reset(); |
| + 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
|
| + void SetKey(const std::string& str); |
| + void SetVal(const char* s, size_t n); |
| + void SetVal(const std::string& str); |
| + |
| + private: |
| + std::string key_; |
| + std::string val_; |
| + }; |
| + // 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.
|
| + // on failure. |
| + static scoped_ptr<PostDataParser> CreatePostDataParser( |
| + const net::URLRequest* request); |
| + // Creates a correct parser instance based on the |content_type| of |
| + // "Content-Type" URLRequest headers. If |content_type| is NULL, it defaults |
| + // to "application/x-www-form-urlencoded". Returns NULL on failure. |
| + static scoped_ptr<PostDataParser> CreatePostDataParser( |
| + const std::string* content_type); |
| + // Tries to parse the POST data of |request| to extract encoded forms. |
| + // On success returns the parsed data, otherwise NULL. |
| + static scoped_ptr<base::DictionaryValue> ParseURLRequestData( |
| + const net::URLRequest* request); |
| + // No member variables, so the destructor can be safely inlined. |
| + virtual ~PostDataParser(); |
| + // Returns true if there was some data, it was well formed and all was read. |
| + virtual bool AllDataReadOK() = 0; |
| + // Returns the next key-value pair as |result|. After SetSource has succeeded, |
| + // this allows to iterate over all pairs in the source. |
| + // Returns true as long as a new pair was successfully found. |
| + virtual bool GetNextPair(Result* result) = 0; |
| + // Sets the |source| of the data to be parsed. The ownership is let with the |
| + // caller and the source should live until |this| dies or |this->SetSource()| |
| + // is called again, whatever comes sooner. Returns true on success. |
| + virtual bool SetSource(const std::vector<char>* source) = 0; |
| + |
| + protected: |
| + PostDataParser() {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(PostDataParser); |
| +}; |
| + |
| +// Parses URLencoded forms, see |
| +// http://www.w3.org/TR/REC-html40-971218/interact/forms.html#h-17.13.4.1 . |
| +class PostDataParserUrlEncoded : public PostDataParser { |
| + public: |
| + PostDataParserUrlEncoded(); |
| + virtual ~PostDataParserUrlEncoded(); |
| + // Implementation of PostDataParser. |
| + virtual bool AllDataReadOK() OVERRIDE; |
| + virtual bool GetNextPair(Result* result) OVERRIDE; |
| + virtual bool SetSource(const std::vector<char>* source) OVERRIDE; |
| + |
| + private: |
| + const std::vector<char>* source_; |
| + std::vector<char>::const_iterator offset_; |
| + DISALLOW_COPY_AND_ASSIGN(PostDataParserUrlEncoded); |
| +}; |
| + |
| +// Parses forms encoded as multipart, see RFC2388. |
| +class PostDataParserMultipart : public PostDataParser { |
| + public: |
| + explicit PostDataParserMultipart(const std::string& boundary_separator); |
| + virtual ~PostDataParserMultipart(); |
| + // Implementation of PostDataParser. |
| + virtual bool AllDataReadOK() OVERRIDE; |
| + virtual bool GetNextPair(Result* result) OVERRIDE; |
| + virtual bool SetSource(const std::vector<char>* source) OVERRIDE; |
| + |
| + private: |
| + // Note on implementation: |
| + // This parser reads the source line by line. There are four types of lines: |
| + // - "Boundary" line, separating entries. |
| + // - "End boundary" line, ends the data. |
| + // - "Content-Disposition" line, containing the "key" for |result|. |
| + // - Empty lines. |
| + // - Other non-empty lines. |
| + enum LineType {kBoundary, kEndBoundary, kDisposition, kEmpty, kOther}; |
| + // The parser uses the following 7-state automaton to check for structure: |
| + // kInit --Boundary--> kHeadStart, kInit --not(Boundary)--> kError |
| + // kHeadStart --Disposition--> kHeadRead, |
| + // kHeadStart --not(Disposition)--> kHead |
| + // kHead --Disposition--> kHeadRead, kHead --not(Disposition)-->kHead |
| + // kHeadRead --Empty-->kBody, kHeadRead --not(Empty)--> kHeadRead |
| + // kBody --Boundary--> kHeadStart, kBody --EndBoundary--> kFinal, |
| + // kBody --not(Boundary OR EndBoundary)--> kBody |
| + enum State {kInit, kHeadStart, kHead, kHeadRead, kBody, kFinal, kError}; |
| + // One-step of the automaton, based on |state_| and |line_type_|. |
| + // This calls SeekNextLine() and returns it return value. |
| + bool DoStep(); |
| + // Determine the |line_type_| of the current line_start_. |
| + LineType GetLineType(); |
| + // Read one more line from |source_|, update line pointers. |
| + bool SeekNextLine(); |
| + // Extracts "key" and possibly "val" from a DISP line. Returns success. |
| + bool ParseHead(Result* result, bool* val_extracted); |
| + // We parse the first |length_| bytes from |source_|. |
| + const char* source_; |
| + size_t length_; |
| + // Offset of the current and next line from |source_|: |
| + // [line_start_]...line... [line_end_]EOL [next_line_]...line... |
| + size_t line_start_; // Points at the first character of the current line. |
| + size_t line_end_; // Points right beyond the last character of the line. |
| + size_t next_line_; // First char. of the next line, or beyond source length. |
| + const std::string boundary_; |
| + const std::string final_boundary_; |
| + State state_; |
| + LineType line_type_; |
| + DISALLOW_COPY_AND_ASSIGN(PostDataParserMultipart); |
| +}; |
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_POST_DATA_PARSER_H_ |