diff --git a/src/envoy/utils/http_header_utils.cc b/src/envoy/utils/http_header_utils.cc index b3ab822cd..bf9edb37a 100644 --- a/src/envoy/utils/http_header_utils.cc +++ b/src/envoy/utils/http_header_utils.cc @@ -37,14 +37,17 @@ absl::string_view readHeaderEntry(const Envoy::Http::HeaderEntry* entry) { return Envoy::EMPTY_STRING; } -absl::string_view extractHeader(const Envoy::Http::HeaderMap& headers, - const Envoy::Http::LowerCaseString& header) { +std::string extractHeader(const Envoy::Http::HeaderMap& headers, + const Envoy::Http::LowerCaseString& header) { const auto result = Envoy::Http::HeaderUtility::getAllOfHeaderAsString(headers, header); if (!result.result().has_value()) { return Envoy::EMPTY_STRING; } - return result.result().value(); + // When the header has multiple values, result().value() points into the + // local result's backing string, so it must be copied out before result + // goes out of scope. + return std::string(result.result().value()); } bool handleHttpMethodOverride(Envoy::Http::RequestHeaderMap& headers) { diff --git a/src/envoy/utils/http_header_utils.h b/src/envoy/utils/http_header_utils.h index 4f15dad90..a69e36194 100644 --- a/src/envoy/utils/http_header_utils.h +++ b/src/envoy/utils/http_header_utils.h @@ -31,8 +31,8 @@ absl::string_view readHeaderEntry(const Envoy::Http::HeaderEntry* entry); // better performance. // For details, see // https://github.com/envoyproxy/envoy/blob/c5f4302325223765b660f0f366ce25bf8570e7a5/include/envoy/http/header_map.h#L271 -absl::string_view extractHeader(const Envoy::Http::HeaderMap& headers, - const Envoy::Http::LowerCaseString& header); +std::string extractHeader(const Envoy::Http::HeaderMap& headers, + const Envoy::Http::LowerCaseString& header); // Get the HTTP method to be used for the request. This method understands the // x-http-method-override header. If present, it will override the method