diff options
author | erikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-22 16:04:49 +0000 |
---|---|---|
committer | erikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-22 16:04:49 +0000 |
commit | 213b99ad9d66e3c039bbb90c19d78603975c1be9 (patch) | |
tree | 0347413f86c4aecf437241e6e6ec6127d481ebc4 /net/http/http_util.cc | |
parent | ab5b32246b6fcda4cf76023c1813f1c21c45b373 (diff) | |
download | chromium_src-213b99ad9d66e3c039bbb90c19d78603975c1be9.zip chromium_src-213b99ad9d66e3c039bbb90c19d78603975c1be9.tar.gz chromium_src-213b99ad9d66e3c039bbb90c19d78603975c1be9.tar.bz2 |
Refactor net::HttpUtil::NameValuePairsIterator to relieve clients of the need to think about quoted values while also avoiding a string copy if the value is unquoted.
The iterator now holds a (normally empty) string member that it uses only if the currently accessed value is quoted. In this case, the value_begin and value_end iterators point into this string (holding the unquoted value) as opposed to the original buffer (holding the quoted value). The value is only unquoted if it is accessed.
As a result, the interface is simplified to not expose whether the current value is quoted. This simplifies the work of all clients. Furthermore, this implementation is optimized to only construct a string if it is required, whereas most clients previously (for simplicity) constructed a new string whether or not it was required. They will therefore benefit from a slight increase in efficiency.
BUG=52601
TEST=net_unittests / HttpUtilTest.NameValuePairs*, HttpAuthTest.*
Review URL: http://codereview.chromium.org/3777012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63514 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http/http_util.cc')
-rw-r--r-- | net/http/http_util.cc | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/net/http/http_util.cc b/net/http/http_util.cc index 5c7a4fc..641c43c 100644 --- a/net/http/http_util.cc +++ b/net/http/http_util.cc @@ -828,12 +828,12 @@ bool HttpUtil::NameValuePairsIterator::GetNext() { // Scan for the equals sign. std::string::const_iterator equals = std::find(value_begin_, value_end_, '='); if (equals == value_end_ || equals == value_begin_) - return valid_ = false; // Malformed + return valid_ = false; // Malformed, no equals sign // Verify that the equals sign we found wasn't inside of quote marks. for (std::string::const_iterator it = value_begin_; it != equals; ++it) { if (HttpUtil::IsQuote(*it)) - return valid_ = false; // Malformed + return valid_ = false; // Malformed, quote appears before equals sign } name_begin_ = value_begin_; @@ -843,25 +843,28 @@ bool HttpUtil::NameValuePairsIterator::GetNext() { TrimLWS(&name_begin_, &name_end_); TrimLWS(&value_begin_, &value_end_); value_is_quoted_ = false; - if (value_begin_ != value_end_ && HttpUtil::IsQuote(*value_begin_)) { + unquoted_value_.clear(); + + if (value_begin_ == value_end_) + return valid_ = false; // Malformed, value is empty + + if (HttpUtil::IsQuote(*value_begin_)) { // Trim surrounding quotemarks off the value - if (*value_begin_ != *(value_end_ - 1) || value_begin_ + 1 == value_end_) + if (*value_begin_ != *(value_end_ - 1) || value_begin_ + 1 == value_end_) { // NOTE: This is not as graceful as it sounds: // * quoted-pairs will no longer be unquoted // (["\"hello] should give ["hello]). // * Does not detect when the final quote is escaped // (["value\"] should give [value"]) ++value_begin_; // Gracefully recover from mismatching quotes. - else + } else { value_is_quoted_ = true; + // Do not store iterators into this. See declaration of unquoted_value_. + unquoted_value_ = HttpUtil::Unquote(value_begin_, value_end_); + } } return true; } -// If value() has quotemarks, unquote it. -std::string HttpUtil::NameValuePairsIterator::unquoted_value() const { - return HttpUtil::Unquote(value_begin_, value_end_); -} - } // namespace net |