diff options
-rw-r--r-- | chrome/browser/extensions/api/web_request/web_request_api_helpers.cc | 3 | ||||
-rw-r--r-- | chrome/browser/extensions/api/web_request/web_request_api_unittest.cc | 10 | ||||
-rw-r--r-- | net/http/http_response_headers.cc | 72 | ||||
-rw-r--r-- | net/http/http_response_headers.h | 14 | ||||
-rw-r--r-- | net/http/http_response_headers_unittest.cc | 30 |
5 files changed, 68 insertions, 61 deletions
diff --git a/chrome/browser/extensions/api/web_request/web_request_api_helpers.cc b/chrome/browser/extensions/api/web_request/web_request_api_helpers.cc index 18d6f57..fad5eda 100644 --- a/chrome/browser/extensions/api/web_request/web_request_api_helpers.cc +++ b/chrome/browser/extensions/api/web_request/web_request_api_helpers.cc @@ -476,8 +476,7 @@ void MergeOnHeadersReceivedResponses( { for (i = (*delta)->deleted_response_headers.begin(); i != (*delta)->deleted_response_headers.end(); ++i) { - (*override_response_headers)->RemoveHeaderWithValue(i->first, - i->second); + (*override_response_headers)->RemoveHeaderLine(i->first, i->second); removed_headers.insert(ToLowerCase(*i)); } } diff --git a/chrome/browser/extensions/api/web_request/web_request_api_unittest.cc b/chrome/browser/extensions/api/web_request/web_request_api_unittest.cc index d5685a5..7797be5 100644 --- a/chrome/browser/extensions/api/web_request/web_request_api_unittest.cc +++ b/chrome/browser/extensions/api/web_request/web_request_api_unittest.cc @@ -928,7 +928,7 @@ TEST(ExtensionWebRequestHelpersTest, TestCalculateOnHeadersReceivedDelta) { char base_headers_string[] = "HTTP/1.0 200 OK\r\n" "Key1: Value1\r\n" - "Key2: Value2\r\n" + "Key2: Value2, Bar\r\n" "Key3: Value3\r\n" "\r\n"; scoped_refptr<net::HttpResponseHeaders> base_headers( @@ -954,7 +954,7 @@ TEST(ExtensionWebRequestHelpersTest, TestCalculateOnHeadersReceivedDelta) { ResponseHeader("Key4", "Value4"))); EXPECT_EQ(2u, delta->deleted_response_headers.size()); EXPECT_TRUE(Contains(delta->deleted_response_headers, - ResponseHeader("Key2", "Value2"))); + ResponseHeader("Key2", "Value2, Bar"))); EXPECT_TRUE(Contains(delta->deleted_response_headers, ResponseHeader("Key3", "Value3"))); } @@ -1289,7 +1289,7 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnHeadersReceivedResponses) { char base_headers_string[] = "HTTP/1.0 200 OK\r\n" "Key1: Value1\r\n" - "Key2: Value2\r\n" + "Key2: Value2, Foo\r\n" "\r\n"; scoped_refptr<net::HttpResponseHeaders> base_headers( new net::HttpResponseHeaders( @@ -1311,7 +1311,7 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnHeadersReceivedResponses) { linked_ptr<EventResponseDelta> d1( new EventResponseDelta("extid1", base::Time::FromInternalValue(2000))); d1->deleted_response_headers.push_back(ResponseHeader("KEY1", "Value1")); - d1->deleted_response_headers.push_back(ResponseHeader("KEY2", "Value2")); + d1->deleted_response_headers.push_back(ResponseHeader("KEY2", "Value2, Foo")); d1->added_response_headers.push_back(ResponseHeader("Key2", "Value3")); deltas.push_back(d1); deltas.sort(&InDecreasingExtensionInstallationTimeOrder); @@ -1340,7 +1340,7 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnHeadersReceivedResponses) { new EventResponseDelta("extid2", base::Time::FromInternalValue(1500))); // Note that we use a different capitalization of KeY2. This should not // matter. - d2->deleted_response_headers.push_back(ResponseHeader("KeY2", "Value2")); + d2->deleted_response_headers.push_back(ResponseHeader("KeY2", "Value2, Foo")); d2->added_response_headers.push_back(ResponseHeader("Key2", "Value4")); deltas.push_back(d2); deltas.sort(&InDecreasingExtensionInstallationTimeOrder); diff --git a/net/http/http_response_headers.cc b/net/http/http_response_headers.cc index 6fe8e2f..90766ba 100644 --- a/net/http/http_response_headers.cc +++ b/net/http/http_response_headers.cc @@ -311,42 +311,6 @@ void HttpResponseHeaders::MergeWithHeaders(const std::string& raw_headers, Parse(new_raw_headers); } -void HttpResponseHeaders::MergeWithHeadersWithValue( - const std::string& raw_headers, - const std::string& header_to_remove_name, - const std::string& header_to_remove_value) { - std::string header_to_remove_name_lowercase(header_to_remove_name); - StringToLowerASCII(&header_to_remove_name_lowercase); - - std::string new_raw_headers(raw_headers); - for (size_t i = 0; i < parsed_.size(); ++i) { - DCHECK(!parsed_[i].is_continuation()); - - // Locate the start of the next header. - size_t k = i; - while (++k < parsed_.size() && parsed_[k].is_continuation()) {} - --k; - - std::string name(parsed_[i].name_begin, parsed_[i].name_end); - StringToLowerASCII(&name); - std::string value(parsed_[i].value_begin, parsed_[i].value_end); - if (name != header_to_remove_name_lowercase || - value != header_to_remove_value) { - // It's ok to preserve this header in the final result. - new_raw_headers.append(parsed_[i].name_begin, parsed_[k].value_end); - new_raw_headers.push_back('\0'); - } - - i = k; - } - new_raw_headers.push_back('\0'); - - // Make this object hold the new data. - raw_headers_.clear(); - parsed_.clear(); - Parse(new_raw_headers); -} - void HttpResponseHeaders::RemoveHeader(const std::string& name) { // Copy up to the null byte. This just copies the status line. std::string new_raw_headers(raw_headers_.c_str()); @@ -359,13 +323,39 @@ void HttpResponseHeaders::RemoveHeader(const std::string& name) { MergeWithHeaders(new_raw_headers, to_remove); } -void HttpResponseHeaders::RemoveHeaderWithValue(const std::string& name, - const std::string& value) { - // Copy up to the null byte. This just copies the status line. - std::string new_raw_headers(raw_headers_.c_str()); +void HttpResponseHeaders::RemoveHeaderLine(const std::string& name, + const std::string& value) { + std::string name_lowercase(name); + StringToLowerASCII(&name_lowercase); + + std::string new_raw_headers(GetStatusLine()); + new_raw_headers.push_back('\0'); + + new_raw_headers.reserve(raw_headers_.size()); + + void* iter = NULL; + std::string old_header_name; + std::string old_header_value; + while (EnumerateHeaderLines(&iter, &old_header_name, &old_header_value)) { + std::string old_header_name_lowercase(name); + StringToLowerASCII(&old_header_name_lowercase); + + if (name_lowercase == old_header_name_lowercase && + value == old_header_value) + continue; + + new_raw_headers.append(old_header_name); + new_raw_headers.push_back(':'); + new_raw_headers.push_back(' '); + new_raw_headers.append(old_header_value); + new_raw_headers.push_back('\0'); + } new_raw_headers.push_back('\0'); - MergeWithHeadersWithValue(new_raw_headers, name, value); + // Make this object hold the new data. + raw_headers_.clear(); + parsed_.clear(); + Parse(new_raw_headers); } void HttpResponseHeaders::AddHeader(const std::string& header) { diff --git a/net/http/http_response_headers.h b/net/http/http_response_headers.h index f6a270a..d2586d2 100644 --- a/net/http/http_response_headers.h +++ b/net/http/http_response_headers.h @@ -65,9 +65,9 @@ class NET_EXPORT HttpResponseHeaders // Removes all instances of a particular header. void RemoveHeader(const std::string& name); - // Removes a particular header. The header name is compared + // Removes a particular header line. The header name is compared // case-insensitively. - void RemoveHeaderWithValue(const std::string& name, const std::string& value); + void RemoveHeaderLine(const std::string& name, const std::string& value); // Adds a particular header. |header| has to be a single header without any // EOL termination, just [<header-name>: <header-values>] @@ -321,16 +321,6 @@ class NET_EXPORT HttpResponseHeaders void MergeWithHeaders(const std::string& raw_headers, const HeaderSet& headers_to_remove); - // Replaces the current headers with the merged version of |raw_headers| and - // the current headers with out the header consisting of - // |header_to_remove_name| and |header_to_remove_value|. Note that - // |header_to_remove_name| is compared case-insensitively. - // Note that the header to remove is removed from the current headers (before - // the merge), not after the merge. - void MergeWithHeadersWithValue(const std::string& raw_headers, - const std::string& header_to_remove_name, - const std::string& header_to_remove_value); - // Adds the values from any 'cache-control: no-cache="foo,bar"' headers. void AddNonCacheableHeaders(HeaderSet* header_names) const; diff --git a/net/http/http_response_headers_unittest.cc b/net/http/http_response_headers_unittest.cc index 00608e2..981fa10 100644 --- a/net/http/http_response_headers_unittest.cc +++ b/net/http/http_response_headers_unittest.cc @@ -1685,7 +1685,35 @@ TEST(HttpResponseHeadersTest, RemoveIndividualHeader) { "Content-Length: 450\n" "Cache-control: max-age=10000\n" }, + { "HTTP/1.1 200 OK\n" + "connection: keep-alive \n" + "Foo: bar, baz\n" + "Foo: bar\n" + "Cache-control: max-age=10000\n", + + "Foo", + + "bar, baz", // Space in value. + "HTTP/1.1 200 OK\n" + "connection: keep-alive\n" + "Foo: bar\n" + "Cache-control: max-age=10000\n" + }, + { "HTTP/1.1 200 OK\n" + "connection: keep-alive \n" + "Foo: bar, baz\n" + "Cache-control: max-age=10000\n", + + "Foo", + + "baz", // Only partial match -> ignored. + + "HTTP/1.1 200 OK\n" + "connection: keep-alive\n" + "Foo: bar, baz\n" + "Cache-control: max-age=10000\n" + }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { @@ -1696,7 +1724,7 @@ TEST(HttpResponseHeadersTest, RemoveIndividualHeader) { std::string name(tests[i].to_remove_name); std::string value(tests[i].to_remove_value); - parsed->RemoveHeaderWithValue(name, value); + parsed->RemoveHeaderLine(name, value); std::string resulting_headers; parsed->GetNormalizedHeaders(&resulting_headers); |