diff --git a/source/common/http/hash_policy.cc b/source/common/http/hash_policy.cc index 23b80e707457..a12d5730a085 100644 --- a/source/common/http/hash_policy.cc +++ b/source/common/http/hash_policy.cc @@ -88,7 +88,7 @@ class CookieHashMethod : public HashMethodImplBase { const HashPolicy::AddCookieCallback add_cookie, const StreamInfo::FilterStateSharedPtr) const override { absl::optional hash; - absl::string_view value = Utility::parseCookieValue(headers, key_); + std::string value = Utility::parseCookieValue(headers, key_); if (value.empty() && ttl_.has_value()) { value = add_cookie(key_, path_, ttl_.value()); hash = HashUtil::xxHash64(value); diff --git a/source/common/http/utility.cc b/source/common/http/utility.cc index e59cdd616108..83bddeff13c4 100644 --- a/source/common/http/utility.cc +++ b/source/common/http/utility.cc @@ -288,14 +288,14 @@ forEachCookie(const HeaderMap& headers, const LowerCaseString& cookie_header, } } -absl::string_view parseCookie(const HeaderMap& headers, const absl::string_view key, - const LowerCaseString& cookie) { - absl::string_view value; +static std::string parseCookie(const HeaderMap& headers, const absl::string_view key, + const LowerCaseString& cookie) { + std::string value; // Iterate over each cookie & return if its value is not empty. forEachCookie(headers, cookie, [&key, &value](absl::string_view k, absl::string_view v) -> bool { if (key == k) { - value = v; + value = std::string{v}; return false; } @@ -329,17 +329,15 @@ Utility::parseCookies(const RequestHeaderMap& headers, return cookies; } -absl::InlinedVector Utility::parseCookieValues(const HeaderMap& headers, - const absl::string_view key, - const size_t max_vals) { - absl::InlinedVector ret; +absl::InlinedVector Utility::parseCookieValues(const HeaderMap& headers, + const absl::string_view key, + const size_t max_vals) { + absl::InlinedVector ret; forEachCookie(headers, Http::Headers::get().Cookie, [&ret, &key, &max_vals](absl::string_view k, absl::string_view v) -> bool { if (k == key) { - ret.push_back(v); - // max_vals == 0 => infinity, so the condition above will never be true - // in this case. + ret.emplace_back(v); if (ret.size() == max_vals) { return false; } @@ -506,13 +504,13 @@ std::string Utility::replaceQueryString(const HeaderString& path, return new_path; } -absl::string_view Utility::parseCookieValue(const HeaderMap& headers, const absl::string_view key) { +std::string Utility::parseCookieValue(const HeaderMap& headers, const absl::string_view key) { // TODO(wbpcode): Modify the headers parameter type to 'RequestHeaderMap'. return parseCookie(headers, key, Http::Headers::get().Cookie); } -absl::string_view Utility::parseSetCookieValue(const Http::HeaderMap& headers, - const absl::string_view key) { +std::string Utility::parseSetCookieValue(const Http::HeaderMap& headers, + const absl::string_view key) { return parseCookie(headers, key, Http::Headers::get().SetCookie); } diff --git a/source/common/http/utility.h b/source/common/http/utility.h index e4f41027da1c..b5892f1d557b 100644 --- a/source/common/http/utility.h +++ b/source/common/http/utility.h @@ -278,7 +278,7 @@ std::string replaceQueryString(const HeaderString& path, const QueryParams& para * @return absl::string_view the parsed cookie value, or a default constructed absl::string_view if * it doesn't exist. **/ -absl::string_view parseCookieValue(const HeaderMap& headers, const absl::string_view key); +std::string parseCookieValue(const HeaderMap& headers, const absl::string_view key); /** * Parse cookies from header into a map. @@ -301,9 +301,9 @@ absl::flat_hash_map parseCookies(const RequestHeaderMa * Parse a particular value out of a set-cookie * @param headers supplies the headers to get the set-cookie from. * @param key the key for the particular set-cookie value to return - * @return absl::string_view the parsed set-cookie value, or "" if none exists + * @return std::string the parsed set-cookie value, or "" if none exists **/ -absl::string_view parseSetCookieValue(const HeaderMap& headers, const absl::string_view key); +std::string parseSetCookieValue(const HeaderMap& headers, const absl::string_view key); /** * Parse particular value(s) out of a cookie. The difference with @@ -314,12 +314,13 @@ absl::string_view parseSetCookieValue(const HeaderMap& headers, const absl::stri * * @param headers supplies the headers to get the cookie from. * @param key the key for the particular cookie value to return. - * @param max_vals the maximum number of values to return. 0 means all of them. - * @return absl::InlinedVector a vector of - * absl::string_view objects containing the extracted values. + * @param max_vals the maximum number of values to return. + * @return absl::InlinedVector a vector of + * std::string objects containing the extracted values. **/ -absl::InlinedVector -parseCookieValues(const HeaderMap& headers, const absl::string_view key, const size_t max_vals = 0); +absl::InlinedVector +parseCookieValues(const HeaderMap& headers, const absl::string_view key, + const size_t max_vals = std::numeric_limits::max()); /** * Produce the value for a Set-Cookie header with the given parameters. diff --git a/source/extensions/filters/http/header_to_metadata/header_to_metadata_filter.cc b/source/extensions/filters/http/header_to_metadata/header_to_metadata_filter.cc index 107e7db4052b..88b558b3d064 100644 --- a/source/extensions/filters/http/header_to_metadata/header_to_metadata_filter.cc +++ b/source/extensions/filters/http/header_to_metadata/header_to_metadata_filter.cc @@ -33,9 +33,9 @@ absl::optional HeaderValueSelector::extract(Http::HeaderMap& map) c // Extract the value of the key from the cookie header. absl::optional CookieValueSelector::extract(Http::HeaderMap& map) const { - std::string value(Envoy::Http::Utility::parseCookieValue(map, cookie_)); + std::string value = Envoy::Http::Utility::parseCookieValue(map, cookie_); if (!value.empty()) { - return absl::optional(value); + return absl::optional(std::move(value)); } return absl::nullopt; } diff --git a/source/extensions/http/stateful_session/cookie/cookie.h b/source/extensions/http/stateful_session/cookie/cookie.h index 2186a03c0ca7..ecc883f37638 100644 --- a/source/extensions/http/stateful_session/cookie/cookie.h +++ b/source/extensions/http/stateful_session/cookie/cookie.h @@ -53,7 +53,7 @@ class CookieBasedSessionStateFactory : public Envoy::Http::SessionStateFactory { private: absl::optional parseAddress(const Envoy::Http::RequestHeaderMap& headers) const { - const absl::string_view cookie_value = Envoy::Http::Utility::parseCookieValue(headers, name_); + const std::string cookie_value = Envoy::Http::Utility::parseCookieValue(headers, name_); std::string address = Envoy::Base64::decode(cookie_value); return !address.empty() ? absl::make_optional(std::move(address)) : absl::nullopt; diff --git a/test/common/http/matching/inputs_test.cc b/test/common/http/matching/inputs_test.cc index 9234423e2e5d..982894bd0cd1 100644 --- a/test/common/http/matching/inputs_test.cc +++ b/test/common/http/matching/inputs_test.cc @@ -71,7 +71,6 @@ TEST(HttpRequestCookiesDataInput, Idempotence) { data.onRequestHeaders(request_headers); EXPECT_EQ(input.get(data).data_, "foo,bar"); - EXPECT_EQ(input.get(data).data_, "foo,bar"); } TEST(MatchingData, HttpResponseHeadersDataInput) { diff --git a/test/common/http/utility_test.cc b/test/common/http/utility_test.cc index a40639006fea..b06413aff735 100644 --- a/test/common/http/utility_test.cc +++ b/test/common/http/utility_test.cc @@ -694,7 +694,7 @@ TEST(HttpUtility, TestParseCookie) { {"cookie", "key2=value2; key3=value3"}}; absl::string_view key{"token"}; - absl::string_view value = Utility::parseCookieValue(headers, key); + std::string value = Utility::parseCookieValue(headers, key); EXPECT_EQ(value, "abc123"); } @@ -716,7 +716,7 @@ TEST(HttpUtility, TestParseSetCookie) { {"set-cookie", "key2=value2; key3=value3"}}; std::string key{"token"}; - absl::string_view value = Utility::parseSetCookieValue(headers, key); + std::string value = Utility::parseSetCookieValue(headers, key); EXPECT_EQ(value, "abc123"); }