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

Unified 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: Comments addressed, unit tests split 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 side-by-side diff with in-line comments
Download patch
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..c9e1d5d5750a2ce72d19f9699112082e0ac166cc
--- /dev/null
+++ b/chrome/browser/extensions/api/web_request/post_data_parser.h
@@ -0,0 +1,144 @@
+// 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 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 {
+ return key_;
+ }
+ const std::string& get_val() const {
+ return val_;
+ }
+ void Reset();
+ void SetKey(const char* s, size_t n);
+ 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
+ // on failure.
+ static scoped_ptr<PostDataParser> CreatePostDataParser(
+ net::URLRequest* request);
+ // Creates a correct parser instance based on the |value| of "Content-Type"
+ // URLRequest headers. If |value| is NULL, it defaults to
+ // "application/x-www-form-urlencoded". Returns NULL on failure.
+ static scoped_ptr<PostDataParser> CreatePostDataParser(
vabr (Chromium) 2012/07/16 15:40:51 I separated the two steps in the original CreatePo
+ const std::string* value);
battre 2012/07/16 17:39:45 can you rename |value| to content_type?
vabr (Chromium) 2012/07/17 11:11:11 Done.
+ // 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 Lines {kBoundary, kEndBoundary, kDisposition, kEmpty, kOther};
battre 2012/07/16 17:39:45 nit: rename to LineType?
vabr (Chromium) 2012/07/17 11:11:11 Done.
+ // 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 States {kInit, kHeadStart, kHead, kHeadRead, kBody, kFinal, kError};
battre 2012/07/16 17:39:45 nit: rename to State?
vabr (Chromium) 2012/07/17 11:11:11 Done.
+ // 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_.
+ Lines 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_;
+ States state_;
+ Lines line_type_;
+ DISALLOW_COPY_AND_ASSIGN(PostDataParserMultipart);
+};
+
+} // namespace extensions
+
+#endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_POST_DATA_PARSER_H_

Powered by Google App Engine
This is Rietveld 408576698