diff --git a/include/envoy/http/header_map.h b/include/envoy/http/header_map.h index fe18b2528e8e..4a2cb2499f1b 100644 --- a/include/envoy/http/header_map.h +++ b/include/envoy/http/header_map.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #include "envoy/common/pure.h" @@ -467,12 +466,10 @@ class HeaderMap { virtual void remove(const LowerCaseString& key) PURE; /** - * Remove all instances of headers with key matching the supplied regex. - * @param key_matcher supplies the regex to match header keys against. Note - * that this will be matching against LowerCaseString, so should be using - * lowercase characters. + * Remove all instances of headers where the key begins with the supplied prefix. + * @param prefix supplies the prefix to match header keys against. */ - virtual void remove(const std::regex& key_matcher) PURE; + virtual void removePrefix(const LowerCaseString& prefix) PURE; /** * @return the number of headers in the map. diff --git a/source/common/http/header_map_impl.cc b/source/common/http/header_map_impl.cc index b7eeacf9ff6a..c6c86cbe2e39 100644 --- a/source/common/http/header_map_impl.cc +++ b/source/common/http/header_map_impl.cc @@ -9,6 +9,8 @@ #include "common/common/utility.h" #include "common/singleton/const_singleton.h" +#include "absl/strings/match.h" + namespace Envoy { namespace Http { @@ -464,14 +466,14 @@ void HeaderMapImpl::remove(const LowerCaseString& key) { } } -void HeaderMapImpl::remove(const std::regex& regex) { +void HeaderMapImpl::removePrefix(const LowerCaseString& prefix) { headers_.remove_if([&](const HeaderEntryImpl& entry) { - absl::string_view key = entry.key().getStringView(); - bool to_remove = std::regex_search(key.begin(), key.end(), regex); + bool to_remove = absl::StartsWith(entry.key().getStringView(), prefix.get()); if (to_remove) { // If this header should be removed, make sure any references in the // static lookup table are cleared as well. - StaticLookupEntry::EntryCb cb = ConstSingleton::get().find(key.data()); + StaticLookupEntry::EntryCb cb = + ConstSingleton::get().find(entry.key().c_str()); if (cb) { StaticLookupResponse ref_lookup_response = cb(*this); if (ref_lookup_response.entry_) { diff --git a/source/common/http/header_map_impl.h b/source/common/http/header_map_impl.h index d607df7a2b07..97c52267a8d5 100644 --- a/source/common/http/header_map_impl.h +++ b/source/common/http/header_map_impl.h @@ -68,7 +68,7 @@ class HeaderMapImpl : public HeaderMap { void iterateReverse(ConstIterateCb cb, void* context) const override; Lookup lookup(const LowerCaseString& key, const HeaderEntry** entry) const override; void remove(const LowerCaseString& key) override; - void remove(const std::regex& regex) override; + void removePrefix(const LowerCaseString& key) override; size_t size() const override { return headers_.size(); } protected: diff --git a/test/common/http/header_map_impl_test.cc b/test/common/http/header_map_impl_test.cc index 073642e8fee2..52dcd7287bff 100644 --- a/test/common/http/header_map_impl_test.cc +++ b/test/common/http/header_map_impl_test.cc @@ -365,16 +365,8 @@ TEST(HeaderMapImplTest, RemoveRegex) { headers.addReference(key4, "value"); headers.addReference(key5, "value"); - // Trying to remove upper case strings from LowerCaseStrings will not work. - headers.remove(std::regex("^X-Prefix-")); - EXPECT_NE(nullptr, headers.get(key1)); - EXPECT_NE(nullptr, headers.get(key2)); - EXPECT_NE(nullptr, headers.get(key3)); - EXPECT_NE(nullptr, headers.get(key4)); - EXPECT_NE(nullptr, headers.get(key5)); - // Test removing the first header, middle headers, and the end header. - headers.remove(std::regex("^x-prefix-")); + headers.removePrefix(LowerCaseString("x-prefix-")); EXPECT_EQ(nullptr, headers.get(key1)); EXPECT_NE(nullptr, headers.get(key2)); EXPECT_EQ(nullptr, headers.get(key3)); @@ -382,7 +374,7 @@ TEST(HeaderMapImplTest, RemoveRegex) { EXPECT_EQ(nullptr, headers.get(key5)); // Remove all headers. - headers.remove(std::regex(".*")); + headers.removePrefix(LowerCaseString("")); EXPECT_EQ(nullptr, headers.get(key2)); EXPECT_EQ(nullptr, headers.get(key4)); @@ -390,7 +382,7 @@ TEST(HeaderMapImplTest, RemoveRegex) { headers.insertContentLength().value(5); EXPECT_STREQ("5", headers.ContentLength()->value().c_str()); EXPECT_EQ(1UL, headers.size()); - headers.remove(std::regex("content")); + headers.removePrefix(LowerCaseString("content")); EXPECT_EQ(nullptr, headers.ContentLength()); }