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

Unified Diff: net/http/http_util.h

Issue 1811163002: Share link header parsing code between blink and content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@base-optional
Patch Set: rebase Created 4 years, 7 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
« no previous file with comments | « net/http/http_security_headers.cc ('k') | net/http/http_util.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/http/http_util.h
diff --git a/net/http/http_util.h b/net/http/http_util.h
index 458a7187441a253e2ad862785fb076824f1643c6..2b70e20d341778c3c7766c14cd4a00cbc3fbd3eb 100644
--- a/net/http/http_util.h
+++ b/net/http/http_util.h
@@ -10,6 +10,7 @@
#include <string>
#include <vector>
+#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_tokenizer.h"
#include "base/time/time.h"
@@ -123,6 +124,13 @@ class NET_EXPORT HttpUtil {
return IsToken(str.begin(), str.end());
}
+ // Whether the string is a valid |parmname| as defined in RFC 5987 Sec 3.2.1.
+ static bool IsParmName(std::string::const_iterator begin,
+ std::string::const_iterator end);
+ static bool IsParmName(const std::string& str) {
+ return IsParmName(str.begin(), str.end());
+ }
+
// RFC 2616 Sec 2.2:
// quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
// Unquote() strips the surrounding quotemarks off a string, and unescapes
@@ -134,6 +142,20 @@ class NET_EXPORT HttpUtil {
// Same as above.
static std::string Unquote(const std::string& str);
+ // Similar to Unquote(), but additionally validates that the string being
+ // unescaped actually is a valid quoted string. Returns false for an empty
+ // string, a string without quotes, a string with mismatched quotes, and
+ // a string with unescaped embeded quotes.
+ // In accordance with RFC 2616 this method only allows double quotes to
+ // enclose the string.
+ static bool StrictUnquote(std::string::const_iterator begin,
+ std::string::const_iterator end,
+ std::string* out) WARN_UNUSED_RESULT;
+
+ // Same as above.
+ static bool StrictUnquote(const std::string& str,
+ std::string* out) WARN_UNUSED_RESULT;
+
// The reverse of Unquote() -- escapes and surrounds with "
static std::string Quote(const std::string& str);
@@ -303,6 +325,12 @@ class NET_EXPORT HttpUtil {
ValuesIterator(const ValuesIterator& other);
~ValuesIterator();
+ // Set the characters to regard as quotes. By default, this includes both
+ // single and double quotes.
+ void set_quote_chars(const char* quotes) {
+ values_.set_quote_chars(quotes);
+ }
+
// Advances the iterator to the next value, if any. Returns true if there
// is a next value. Use value* methods to access the resultant value.
bool GetNext();
@@ -332,18 +360,26 @@ class NET_EXPORT HttpUtil {
// calls to GetNext() or after the NameValuePairsIterator is destroyed.
class NET_EXPORT NameValuePairsIterator {
public:
- // Whether or not values are optional. VALUES_OPTIONAL allows
- // e.g. name1=value1;name2;name3=value3, whereas VALUES_NOT_OPTIONAL
+ // Whether or not values are optional. Values::NOT_REQUIRED allows
+ // e.g. name1=value1;name2;name3=value3, whereas Vaues::REQUIRED
// will treat it as a parse error because name2 does not have a
// corresponding equals sign.
- enum OptionalValues { VALUES_OPTIONAL, VALUES_NOT_OPTIONAL };
+ enum class Values { NOT_REQUIRED, REQUIRED };
+
+ // Whether or not unmatched quotes should be considered a failure. By
+ // default this class is pretty lenient and does a best effort to parse
+ // values with mismatched quotes. When set to STRICT_QUOTES a value with
+ // mismatched or otherwise invalid quotes is considered a parse error.
+ enum class Quotes { STRICT_QUOTES, NOT_STRICT };
NameValuePairsIterator(std::string::const_iterator begin,
std::string::const_iterator end,
char delimiter,
- OptionalValues optional_values);
+ Values optional_values,
+ Quotes strict_quotes);
- // Treats values as not optional by default (VALUES_NOT_OPTIONAL).
+ // Treats values as not optional by default (Values::REQUIRED) and
+ // treats quotes as not strict.
NameValuePairsIterator(std::string::const_iterator begin,
std::string::const_iterator end,
char delimiter);
@@ -384,6 +420,8 @@ class NET_EXPORT HttpUtil {
value_end_); }
private:
+ bool IsQuote(char c) const;
+
HttpUtil::ValuesIterator props_;
bool valid_;
@@ -403,6 +441,11 @@ class NET_EXPORT HttpUtil {
// True if values are required for each name/value pair; false if a
// name is permitted to appear without a corresponding value.
bool values_optional_;
+
+ // True if quotes values are required to be properly quoted; false if
+ // mismatched quotes and other problems with quoted values should be more
+ // or less gracefully treated as valid.
+ bool strict_quotes_;
};
};
« no previous file with comments | « net/http/http_security_headers.cc ('k') | net/http/http_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698