summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--net/http/http_util.cc14
-rw-r--r--net/http/http_util.h8
-rw-r--r--net/http/http_util_unittest.cc12
3 files changed, 34 insertions, 0 deletions
diff --git a/net/http/http_util.cc b/net/http/http_util.cc
index 811d7b0..72c7737e 100644
--- a/net/http/http_util.cc
+++ b/net/http/http_util.cc
@@ -642,6 +642,20 @@ bool HttpUtil::HeadersIterator::GetNext() {
return false;
}
+bool HttpUtil::HeadersIterator::AdvanceTo(const char* name) {
+ DCHECK(name != NULL);
+ DCHECK_EQ(0, StringToLowerASCII<std::string>(name).compare(name))
+ << "the header name must be in all lower case";
+
+ while (GetNext()) {
+ if (LowerCaseEqualsASCII(name_begin_, name_end_, name)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
HttpUtil::ValuesIterator::ValuesIterator(
string::const_iterator values_begin,
string::const_iterator values_end,
diff --git a/net/http/http_util.h b/net/http/http_util.h
index ad7e671..fcf762d9 100644
--- a/net/http/http_util.h
+++ b/net/http/http_util.h
@@ -159,6 +159,14 @@ class HttpUtil {
// header name and values.
bool GetNext();
+ // Iterates through the list of headers, starting with the current position
+ // and looks for the specified header. Note that the name _must_ be
+ // lower cased.
+ // If the header was found, the return value will be true and the current
+ // position points to the header. If the return value is false, the
+ // current position will be at the end of the headers.
+ bool AdvanceTo(const char* lowercase_name);
+
std::string::const_iterator name_begin() const {
return name_begin_;
}
diff --git a/net/http/http_util_unittest.cc b/net/http/http_util_unittest.cc
index 11e3fa3..80413ac 100644
--- a/net/http/http_util_unittest.cc
+++ b/net/http/http_util_unittest.cc
@@ -93,6 +93,18 @@ TEST(HttpUtilTest, HeadersIterator_MalformedLine) {
EXPECT_FALSE(it.GetNext());
}
+TEST(HttpUtilTest, HeadersIterator_AdvanceTo) {
+ std::string headers = "foo: 1\r\n: 2\r\n3\r\nbar: 4";
+
+ HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n");
+ EXPECT_TRUE(it.AdvanceTo("foo"));
+ EXPECT_EQ("foo", it.name());
+ EXPECT_TRUE(it.AdvanceTo("bar"));
+ EXPECT_EQ("bar", it.name());
+ EXPECT_FALSE(it.AdvanceTo("blat"));
+ EXPECT_FALSE(it.GetNext()); // should be at end of headers
+}
+
TEST(HttpUtilTest, ValuesIterator) {
std::string values = " must-revalidate, no-cache=\"foo, bar\"\t, private ";