Skip to content

Commit

Permalink
Implementation for issue #38097
Browse files Browse the repository at this point in the history
Signed-off-by: Sheldon <shaoxt@gmail.com>
  • Loading branch information
shaoxt committed Jan 17, 2025
1 parent 2487368 commit e5d1001
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 20 additions & 2 deletions source/extensions/filters/http/compressor/compressor_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()) &&
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions source/extensions/filters/http/compressor/compressor_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e5d1001

Please sign in to comment.