From 2487368848035a1afc202c3ec8d60a66d56e190f Mon Sep 17 00:00:00 2001 From: Raven Black Date: Fri, 17 Jan 2025 13:43:25 -0500 Subject: [PATCH 1/8] Remove incorrect/outdated doc for explicit_http_config (#38090) Commit Message: Remove incorrect/outdated doc for explicit_http_config Additional Description: Per #38064, this docstring became incorrect with #14362 so should be removed. Risk Level: None, doc-only. Testing: n/a Docs Changes: Yes it is. Release Notes: n/a Platform Specific Features: n/a Signed-off-by: Raven Black Signed-off-by: Sheldon --- .../extensions/upstreams/http/v3/http_protocol_options.proto | 1 - 1 file changed, 1 deletion(-) diff --git a/api/envoy/extensions/upstreams/http/v3/http_protocol_options.proto b/api/envoy/extensions/upstreams/http/v3/http_protocol_options.proto index ca4cb81fe033..7e3ed435edd3 100644 --- a/api/envoy/extensions/upstreams/http/v3/http_protocol_options.proto +++ b/api/envoy/extensions/upstreams/http/v3/http_protocol_options.proto @@ -140,7 +140,6 @@ message HttpProtocolOptions { option (validate.required) = true; // To explicitly configure either HTTP/1 or HTTP/2 (but not both!) use ``explicit_http_config``. - // If the ``explicit_http_config`` is empty, HTTP/1.1 is used. ExplicitHttpConfig explicit_http_config = 3; // This allows switching on protocol based on what protocol the downstream From e5d10019c43d2f0030b79a256d9c1887e53e75e3 Mon Sep 17 00:00:00 2001 From: Sheldon Date: Fri, 17 Jan 2025 12:03:05 -0800 Subject: [PATCH 2/8] Implementation for issue https://github.com/envoyproxy/envoy/issues/38097 Signed-off-by: Sheldon --- .../http/compressor/v3/compressor.proto | 5 +++++ .../http/compressor/compressor_filter.cc | 22 +++++++++++++++++-- .../http/compressor/compressor_filter.h | 3 +++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/api/envoy/extensions/filters/http/compressor/v3/compressor.proto b/api/envoy/extensions/filters/http/compressor/v3/compressor.proto index 6fe4b137da6e..4f4cce1767df 100644 --- a/api/envoy/extensions/filters/http/compressor/v3/compressor.proto +++ b/api/envoy/extensions/filters/http/compressor/v3/compressor.proto @@ -49,6 +49,11 @@ message Compressor { // Configuration for filter behavior on the request direction. message RequestDirectionConfig { CommonDirectionConfig common_config = 1; + + // Allow request body compression enabled by request header flag + // https://github.com/envoyproxy/envoy/issues/38097 + // If true, it checks the "X-Request-Compression" header to see whether the request needs to do compression or not + bool enable_on_x_header = 2; } // Configuration for filter behavior on the response direction. diff --git a/source/extensions/filters/http/compressor/compressor_filter.cc b/source/extensions/filters/http/compressor/compressor_filter.cc index 09a1d0c50493..f510ff7345db 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.cc +++ b/source/extensions/filters/http/compressor/compressor_filter.cc @@ -113,7 +113,8 @@ CompressorFilterConfig::RequestDirectionConfig::RequestDirectionConfig( const std::string& stats_prefix, Stats::Scope& scope, Runtime::Loader& runtime) : DirectionConfig(proto_config.request_direction_config().common_config(), stats_prefix + "request.", scope, runtime), - is_set_{proto_config.has_request_direction_config()} {} + is_set_{proto_config.has_request_direction_config(), + enable_on_x_header_(proto_config.request_direction_config().enable_on_x_header())} {} CompressorFilterConfig::ResponseDirectionConfig::ResponseDirectionConfig( const envoy::extensions::filters::http::compressor::v3::Compressor& proto_config, @@ -212,7 +213,7 @@ Http::FilterHeadersStatus CompressorFilter::decodeHeaders(Http::RequestHeaderMap const auto& request_config = config_->requestDirectionConfig(); - if (!end_stream && request_config.compressionEnabled() && !Http::Utility::isUpgrade(headers) && + if (!end_stream && requestCompressionEnabled(request_config, headers) && !Http::Utility::isUpgrade(headers) && request_config.isMinimumContentLength(headers) && request_config.isContentTypeAllowed(headers) && !headers.getInline(request_content_encoding_handle.handle()) && @@ -648,6 +649,23 @@ bool CompressorFilter::compressionEnabled( : config.compressionEnabled(); } +bool CompressorFilter::requestCompressionEnabled(const CompressorFilterConfig::RequestDirectionConfig& config, + const Http::RequestHeaderMap& headers) const { + if (config.compressionEnabled()) { + return config.compressionEnabled(); + } else if (config.enableOnXHeader()) { + Envoy::Http::HeaderMap::GetResult header_result = headers.get(Envoy::Http::LowerCaseString("X-Request-Compression")); + if (header_result.size() > 0) { // Check if the header exists + std::string compressionRequired = std::string(header_result.operator[](0)->value().getStringView()); + ENVOY_LOG(debug, "X-Request-Compression {}", compressionRequired); + if (absl::EqualsIgnoreCase(compressionRequired, "true")) { + return true; + } + } + } + return false; +} + bool CompressorFilter::removeAcceptEncodingHeader( const CompressorFilterConfig::ResponseDirectionConfig& config, const CompressorPerRouteFilterConfig* per_route_config) const { diff --git a/source/extensions/filters/http/compressor/compressor_filter.h b/source/extensions/filters/http/compressor/compressor_filter.h index f63903195606..6a71b72329fb 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.h +++ b/source/extensions/filters/http/compressor/compressor_filter.h @@ -107,8 +107,11 @@ class CompressorFilterConfig { bool compressionEnabled() const override { return is_set_ && compression_enabled_.enabled(); } + bool enableOnXHeader() const { return enable_on_x_header_; } + private: const bool is_set_; + const bool enable_on_x_header_; }; class ResponseDirectionConfig : public DirectionConfig { From abb7eb6c9568c3411c096f7dffc7663739d225e6 Mon Sep 17 00:00:00 2001 From: Sheldon Date: Fri, 17 Jan 2025 12:53:40 -0800 Subject: [PATCH 3/8] Format the code Signed-off-by: Sheldon --- .../http/compressor/compressor_filter.cc | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/source/extensions/filters/http/compressor/compressor_filter.cc b/source/extensions/filters/http/compressor/compressor_filter.cc index f510ff7345db..cbf39dbb5ee2 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.cc +++ b/source/extensions/filters/http/compressor/compressor_filter.cc @@ -649,21 +649,24 @@ bool CompressorFilter::compressionEnabled( : config.compressionEnabled(); } -bool CompressorFilter::requestCompressionEnabled(const CompressorFilterConfig::RequestDirectionConfig& config, - const Http::RequestHeaderMap& headers) const { - if (config.compressionEnabled()) { - return config.compressionEnabled(); - } else if (config.enableOnXHeader()) { - Envoy::Http::HeaderMap::GetResult header_result = headers.get(Envoy::Http::LowerCaseString("X-Request-Compression")); - if (header_result.size() > 0) { // Check if the header exists - std::string compressionRequired = std::string(header_result.operator[](0)->value().getStringView()); - ENVOY_LOG(debug, "X-Request-Compression {}", compressionRequired); - if (absl::EqualsIgnoreCase(compressionRequired, "true")) { - return true; - } - } - } - return false; +bool CompressorFilter::requestCompressionEnabled( + const CompressorFilterConfig::RequestDirectionConfig& config, + const Http::RequestHeaderMap& headers) const { + if (config.compressionEnabled()) { + return config.compressionEnabled(); + } else if (config.enableOnXHeader()) { + Envoy::Http::HeaderMap::GetResult header_result = + headers.get(Envoy::Http::LowerCaseString("X-Request-Compression")); + if (header_result.size() > 0) { // Check if the header exists + std::string compressionRequired = + std::string(header_result.operator[](0)->value().getStringView()); + ENVOY_LOG(debug, "X-Request-Compression {}", compressionRequired); + if (absl::EqualsIgnoreCase(compressionRequired, "true")) { + return true; + } + } + } + return false; } bool CompressorFilter::removeAcceptEncodingHeader( From 32793f86f035f61470b4887176c1db3d7b5757cd Mon Sep 17 00:00:00 2001 From: Sheldon Date: Fri, 17 Jan 2025 13:13:10 -0800 Subject: [PATCH 4/8] Initial enable_on_x_header_ Signed-off-by: Sheldon --- .../extensions/filters/http/compressor/compressor_filter.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/extensions/filters/http/compressor/compressor_filter.cc b/source/extensions/filters/http/compressor/compressor_filter.cc index cbf39dbb5ee2..19346d439679 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.cc +++ b/source/extensions/filters/http/compressor/compressor_filter.cc @@ -113,8 +113,8 @@ CompressorFilterConfig::RequestDirectionConfig::RequestDirectionConfig( const std::string& stats_prefix, Stats::Scope& scope, Runtime::Loader& runtime) : DirectionConfig(proto_config.request_direction_config().common_config(), stats_prefix + "request.", scope, runtime), - is_set_{proto_config.has_request_direction_config(), - enable_on_x_header_(proto_config.request_direction_config().enable_on_x_header())} {} + enable_on_x_header_(proto_config.request_direction_config().enable_on_x_header()), + is_set_{proto_config.has_request_direction_config()} {} CompressorFilterConfig::ResponseDirectionConfig::ResponseDirectionConfig( const envoy::extensions::filters::http::compressor::v3::Compressor& proto_config, From 6c98b6a6e87518117b6ced0d30b46231b38404f5 Mon Sep 17 00:00:00 2001 From: Sheldon Date: Fri, 17 Jan 2025 13:21:34 -0800 Subject: [PATCH 5/8] Initial enable_on_x_header_ Signed-off-by: Sheldon --- source/extensions/filters/http/compressor/compressor_filter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/filters/http/compressor/compressor_filter.h b/source/extensions/filters/http/compressor/compressor_filter.h index 6a71b72329fb..132838cafad1 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.h +++ b/source/extensions/filters/http/compressor/compressor_filter.h @@ -110,8 +110,8 @@ class CompressorFilterConfig { bool enableOnXHeader() const { return enable_on_x_header_; } private: - const bool is_set_; const bool enable_on_x_header_; + const bool is_set_; }; class ResponseDirectionConfig : public DirectionConfig { From 8d9f01e753458a1803c3f5c5ce1c927b6db975c6 Mon Sep 17 00:00:00 2001 From: Sheldon Date: Fri, 17 Jan 2025 13:44:51 -0800 Subject: [PATCH 6/8] Missed the requestCompressionEnabled declaration Signed-off-by: Sheldon --- source/extensions/filters/http/compressor/compressor_filter.cc | 1 + source/extensions/filters/http/compressor/compressor_filter.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/source/extensions/filters/http/compressor/compressor_filter.cc b/source/extensions/filters/http/compressor/compressor_filter.cc index 19346d439679..372b7bea7b80 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.cc +++ b/source/extensions/filters/http/compressor/compressor_filter.cc @@ -649,6 +649,7 @@ bool CompressorFilter::compressionEnabled( : config.compressionEnabled(); } +// True if request compression is enabled. bool CompressorFilter::requestCompressionEnabled( const CompressorFilterConfig::RequestDirectionConfig& config, const Http::RequestHeaderMap& headers) const { diff --git a/source/extensions/filters/http/compressor/compressor_filter.h b/source/extensions/filters/http/compressor/compressor_filter.h index 132838cafad1..d416704fa81f 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.h +++ b/source/extensions/filters/http/compressor/compressor_filter.h @@ -203,6 +203,8 @@ class CompressorFilter : public Http::PassThroughFilter { private: bool compressionEnabled(const CompressorFilterConfig::ResponseDirectionConfig& config, const CompressorPerRouteFilterConfig* per_route_config) const; + bool requestCompressionEnabled(const CompressorFilterConfig::RequestDirectionConfig& config, + const Http::RequestHeaderMap& headers) const; bool removeAcceptEncodingHeader(const CompressorFilterConfig::ResponseDirectionConfig& config, const CompressorPerRouteFilterConfig* per_route_config) const; bool hasCacheControlNoTransform(Http::ResponseHeaderMap& headers) const; From 1b00244f6b7008a55b7124ccc2585c6303277c6d Mon Sep 17 00:00:00 2001 From: Sheldon Date: Fri, 17 Jan 2025 16:59:52 -0800 Subject: [PATCH 7/8] Fix syntax error Signed-off-by: Sheldon --- .../http/compressor/compressor_filter.cc | 38 +++++++++---------- .../http/compressor/compressor_filter.h | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/source/extensions/filters/http/compressor/compressor_filter.cc b/source/extensions/filters/http/compressor/compressor_filter.cc index 372b7bea7b80..773ea0faca33 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.cc +++ b/source/extensions/filters/http/compressor/compressor_filter.cc @@ -213,8 +213,8 @@ Http::FilterHeadersStatus CompressorFilter::decodeHeaders(Http::RequestHeaderMap const auto& request_config = config_->requestDirectionConfig(); - if (!end_stream && requestCompressionEnabled(request_config, headers) && !Http::Utility::isUpgrade(headers) && - request_config.isMinimumContentLength(headers) && + if (!end_stream && requestCompressionEnabled(request_config, headers) && + !Http::Utility::isUpgrade(headers) && request_config.isMinimumContentLength(headers) && request_config.isContentTypeAllowed(headers) && !headers.getInline(request_content_encoding_handle.handle()) && isTransferEncodingAllowed(headers)) { @@ -651,23 +651,23 @@ bool CompressorFilter::compressionEnabled( // True if request compression is enabled. bool CompressorFilter::requestCompressionEnabled( - const CompressorFilterConfig::RequestDirectionConfig& config, - const Http::RequestHeaderMap& headers) const { - if (config.compressionEnabled()) { - return config.compressionEnabled(); - } else if (config.enableOnXHeader()) { - Envoy::Http::HeaderMap::GetResult header_result = - headers.get(Envoy::Http::LowerCaseString("X-Request-Compression")); - if (header_result.size() > 0) { // Check if the header exists - std::string compressionRequired = - std::string(header_result.operator[](0)->value().getStringView()); - ENVOY_LOG(debug, "X-Request-Compression {}", compressionRequired); - if (absl::EqualsIgnoreCase(compressionRequired, "true")) { - return true; - } - } - } - return false; + const CompressorFilterConfig::RequestDirectionConfig& config, + const Http::RequestHeaderMap& headers) const { + if (config.compressionEnabled()) { + return config.compressionEnabled(); + } else if (config.enableOnXHeader()) { + Envoy::Http::HeaderMap::GetResult header_result = + headers.get(Envoy::Http::LowerCaseString("X-Request-Compression")); + if (header_result.size() > 0) { // Check if the header exists + std::string compressionRequired = + std::string(header_result.operator[](0)->value().getStringView()); + ENVOY_LOG(debug, "X-Request-Compression {}", compressionRequired); + if (absl::EqualsIgnoreCase(compressionRequired, "true")) { + return true; + } + } + } + return false; } bool CompressorFilter::removeAcceptEncodingHeader( diff --git a/source/extensions/filters/http/compressor/compressor_filter.h b/source/extensions/filters/http/compressor/compressor_filter.h index d416704fa81f..e6f26f5738db 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.h +++ b/source/extensions/filters/http/compressor/compressor_filter.h @@ -204,7 +204,7 @@ class CompressorFilter : public Http::PassThroughFilter { bool compressionEnabled(const CompressorFilterConfig::ResponseDirectionConfig& config, const CompressorPerRouteFilterConfig* per_route_config) const; bool requestCompressionEnabled(const CompressorFilterConfig::RequestDirectionConfig& config, - const Http::RequestHeaderMap& headers) const; + const Http::RequestHeaderMap& headers) const; bool removeAcceptEncodingHeader(const CompressorFilterConfig::ResponseDirectionConfig& config, const CompressorPerRouteFilterConfig* per_route_config) const; bool hasCacheControlNoTransform(Http::ResponseHeaderMap& headers) const; From b5223157d4d35d62f117b292da42a8b326dc9bb0 Mon Sep 17 00:00:00 2001 From: Sheldon Date: Fri, 17 Jan 2025 17:07:30 -0800 Subject: [PATCH 8/8] Remove ENVOY_LOG Signed-off-by: Sheldon --- source/extensions/filters/http/compressor/compressor_filter.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/source/extensions/filters/http/compressor/compressor_filter.cc b/source/extensions/filters/http/compressor/compressor_filter.cc index 773ea0faca33..c2d9a3b7244b 100644 --- a/source/extensions/filters/http/compressor/compressor_filter.cc +++ b/source/extensions/filters/http/compressor/compressor_filter.cc @@ -661,7 +661,6 @@ bool CompressorFilter::requestCompressionEnabled( if (header_result.size() > 0) { // Check if the header exists std::string compressionRequired = std::string(header_result.operator[](0)->value().getStringView()); - ENVOY_LOG(debug, "X-Request-Compression {}", compressionRequired); if (absl::EqualsIgnoreCase(compressionRequired, "true")) { return true; }