diff --git a/include/grpc++/channel_arguments.h b/include/grpc++/channel_arguments.h index 68f24cde4af..7b17830a86a 100644 --- a/include/grpc++/channel_arguments.h +++ b/include/grpc++/channel_arguments.h @@ -59,8 +59,9 @@ class ChannelArguments { void SetSslTargetNameOverride(const grpc::string& name); // TODO(yangg) add flow control options - // Set the compression level for the channel. - void SetCompressionLevel(grpc_compression_level level); + // Set the compression algorithm for the channel. + void _Experimental_SetCompressionAlgorithm( + grpc_compression_algorithm algorithm); // Generic channel argument setters. Only for advanced use cases. void SetInt(const grpc::string& key, int value); diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index 4bad20de42b..6b8d7211d1a 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -110,15 +110,12 @@ class ClientContext { creds_ = creds; } - grpc_compression_level get_compression_level() const { - return compression_level_; - } - void set_compression_level(grpc_compression_level level); - - grpc_compression_algorithm get_compression_algorithm() const { + grpc_compression_algorithm _experimental_get_compression_algorithm() const { return compression_algorithm_; } - void set_compression_algorithm(grpc_compression_algorithm algorithm); + + void _experimental_set_compression_algorithm( + grpc_compression_algorithm algorithm); std::shared_ptr auth_context() const; @@ -179,7 +176,6 @@ class ClientContext { std::multimap recv_initial_metadata_; std::multimap trailing_metadata_; - grpc_compression_level compression_level_; grpc_compression_algorithm compression_algorithm_; }; diff --git a/include/grpc/compression.h b/include/grpc/compression.h index dd7e1d0a125..913e553ba96 100644 --- a/include/grpc/compression.h +++ b/include/grpc/compression.h @@ -39,7 +39,7 @@ extern "C" { #endif /** To be used in channel arguments */ -#define GRPC_COMPRESSION_LEVEL_ARG "grpc.compression_level" +#define GRPC_COMPRESSION_ALGORITHM_ARG "grpc.compression_algorithm" /* The various compression algorithms supported by GRPC */ typedef enum { diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c index d45898f2f44..c430b56fa2d 100644 --- a/src/core/channel/channel_args.c +++ b/src/core/channel/channel_args.c @@ -124,25 +124,25 @@ int grpc_channel_args_is_census_enabled(const grpc_channel_args *a) { return 0; } -grpc_compression_level grpc_channel_args_get_compression_level( +grpc_compression_algorithm grpc_channel_args_get_compression_algorithm( const grpc_channel_args *a) { size_t i; if (a == NULL) return 0; for (i = 0; i < a->num_args; ++i) { if (a->args[i].type == GRPC_ARG_INTEGER && - !strcmp(GRPC_COMPRESSION_LEVEL_ARG, a->args[i].key)) { + !strcmp(GRPC_COMPRESSION_ALGORITHM_ARG, a->args[i].key)) { return a->args[i].value.integer; break; } } - return GRPC_COMPRESS_LEVEL_NONE; + return GRPC_COMPRESS_NONE; } -grpc_channel_args *grpc_channel_args_set_compression_level( - grpc_channel_args *a, grpc_compression_level level) { +grpc_channel_args *grpc_channel_args_set_compression_algorithm( + grpc_channel_args *a, grpc_compression_algorithm algorithm) { grpc_arg tmp; tmp.type = GRPC_ARG_INTEGER; - tmp.key = GRPC_COMPRESSION_LEVEL_ARG; - tmp.value.integer = level; + tmp.key = GRPC_COMPRESSION_ALGORITHM_ARG; + tmp.value.integer = algorithm; return grpc_channel_args_copy_and_add(a, &tmp, 1); } diff --git a/src/core/channel/channel_args.h b/src/core/channel/channel_args.h index 17321010c51..7e6ddd3997a 100644 --- a/src/core/channel/channel_args.h +++ b/src/core/channel/channel_args.h @@ -57,14 +57,14 @@ void grpc_channel_args_destroy(grpc_channel_args *a); * is specified in channel args, otherwise returns 0. */ int grpc_channel_args_is_census_enabled(const grpc_channel_args *a); -/** Returns the compression level set in \a a. */ -grpc_compression_level grpc_channel_args_get_compression_level( +/** Returns the compression algorithm set in \a a. */ +grpc_compression_algorithm grpc_channel_args_get_compression_algorithm( const grpc_channel_args *a); /** Returns a channel arg instance with compression enabled. If \a a is - * non-NULL, its args are copied. N.B. GRPC_COMPRESS_LEVEL_NONE disables - * compression for the channel. */ -grpc_channel_args *grpc_channel_args_set_compression_level( - grpc_channel_args *a, grpc_compression_level level); + * non-NULL, its args are copied. N.B. GRPC_COMPRESS_NONE disables compression + * for the channel. */ +grpc_channel_args *grpc_channel_args_set_compression_algorithm( + grpc_channel_args *a, grpc_compression_algorithm algorithm); #endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H */ diff --git a/src/core/channel/compress_filter.c b/src/core/channel/compress_filter.c index 93b9b203e52..3d85ed41c5c 100644 --- a/src/core/channel/compress_filter.c +++ b/src/core/channel/compress_filter.c @@ -31,6 +31,26 @@ * */ +/** Compression filter for outgoing data. + * + * Compression settings may come from: + * - Channel configuration, as established at channel creation time. + * - The metadata accompanying the outgoing data to be compressed. This is + * taken as a request only. We may choose not to honor it. The metadata key + * is given by \a GRPC_COMPRESS_REQUEST_ALGORITHM_KEY. + * + * Compression can be disabled for concrete messages (for instance in order to + * prevent CRIME/BEAST type attacks) by having the GRPC_WRITE_NO_COMPRESS set in + * the BEGIN_MESSAGE flags. + * + * The attempted compression mechanism is added to the resulting initial + * metadata under the'grpc-encoding' key. + * + * If compression is actually performed, BEGIN_MESSAGE's flag is modified to + * incorporate GRPC_WRITE_INTERNAL_COMPRESS. Otherwise, and regardless of the + * aforementioned 'grpc-encoding' metadata value, data will pass through + * uncompressed. */ + #include #include @@ -277,11 +297,9 @@ static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, int is_first, int is_last) { channel_data *channeld = elem->channel_data; grpc_compression_algorithm algo_idx; - const grpc_compression_level clevel = - grpc_channel_args_get_compression_level(args); channeld->default_compression_algorithm = - grpc_compression_algorithm_for_level(clevel); + grpc_channel_args_get_compression_algorithm(args); channeld->mdstr_request_compression_algorithm_key = grpc_mdstr_from_string(mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY); diff --git a/src/core/channel/compress_filter.h b/src/core/channel/compress_filter.h index 3a196eb7bf0..8d9c3ba697d 100644 --- a/src/core/channel/compress_filter.h +++ b/src/core/channel/compress_filter.h @@ -40,11 +40,7 @@ /** Message-level compression filter. * - * See for the available compression levels. - * - * Use grpc_channel_args_set_compression_level and - * grpc_channel_args_get_compression_level to interact with the compression - * settings for a channel. + * See for the available compression settings. * * grpc_op instances of type GRPC_OP_SEND_MESSAGE can have the bit specified by * the GRPC_WRITE_NO_COMPRESS mask in order to disable compression in an diff --git a/src/cpp/client/channel_arguments.cc b/src/cpp/client/channel_arguments.cc index b271650673c..92ac5ea6fd6 100644 --- a/src/cpp/client/channel_arguments.cc +++ b/src/cpp/client/channel_arguments.cc @@ -37,8 +37,9 @@ namespace grpc { -void ChannelArguments::SetCompressionLevel(grpc_compression_level level) { - SetInt(GRPC_COMPRESSION_LEVEL_ARG, level); +void ChannelArguments::_Experimental_SetCompressionAlgorithm( + grpc_compression_algorithm algorithm) { + SetInt(GRPC_COMPRESSION_ALGORITHM_ARG, algorithm); } void ChannelArguments::SetInt(const grpc::string& key, int value) { diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc index cc5f51d618f..c81f06186aa 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -79,13 +79,7 @@ void ClientContext::set_call(grpc_call* call, } } -void ClientContext::set_compression_level(grpc_compression_level level) { - const grpc_compression_algorithm algorithm_for_level = - grpc_compression_algorithm_for_level(level); - set_compression_algorithm(algorithm_for_level); -} - -void ClientContext::set_compression_algorithm( +void ClientContext::_experimental_set_compression_algorithm( grpc_compression_algorithm algorithm) { char* algorithm_name = NULL; if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) { diff --git a/test/core/end2end/fixtures/chttp2_fullstack_compression.c b/test/core/end2end/fixtures/chttp2_fullstack_compression.c index 19658ed38f3..0a9a3122968 100644 --- a/test/core/end2end/fixtures/chttp2_fullstack_compression.c +++ b/test/core/end2end/fixtures/chttp2_fullstack_compression.c @@ -80,8 +80,8 @@ void chttp2_init_client_fullstack_compression(grpc_end2end_test_fixture *f, if (ffd->client_args_compression != NULL) { grpc_channel_args_destroy(ffd->client_args_compression); } - ffd->client_args_compression = grpc_channel_args_set_compression_level( - client_args, GRPC_COMPRESS_LEVEL_HIGH); + ffd->client_args_compression = grpc_channel_args_set_compression_algorithm( + client_args, GRPC_COMPRESS_GZIP); f->client = grpc_channel_create(ffd->localaddr, ffd->client_args_compression); } @@ -91,8 +91,8 @@ void chttp2_init_server_fullstack_compression(grpc_end2end_test_fixture *f, if (ffd->server_args_compression != NULL) { grpc_channel_args_destroy(ffd->server_args_compression); } - ffd->server_args_compression = grpc_channel_args_set_compression_level( - server_args, GRPC_COMPRESS_LEVEL_HIGH); + ffd->server_args_compression = grpc_channel_args_set_compression_algorithm( + server_args, GRPC_COMPRESS_GZIP); if (f->server) { grpc_server_destroy(f->server); } diff --git a/test/core/end2end/tests/request_with_compressed_payload.c b/test/core/end2end/tests/request_with_compressed_payload.c index 0c1b065bd80..2599f796d22 100644 --- a/test/core/end2end/tests/request_with_compressed_payload.c +++ b/test/core/end2end/tests/request_with_compressed_payload.c @@ -104,7 +104,7 @@ static void end_test(grpc_end2end_test_fixture *f) { static void request_with_payload_template( grpc_end2end_test_config config, const char *test_name, gpr_uint32 send_flags_bitmask, - grpc_compression_level requested_compression_level, + grpc_compression_algorithm requested_compression_algorithm, grpc_compression_algorithm expected_compression_algorithm, grpc_metadata *client_metadata) { grpc_call *c; @@ -133,10 +133,10 @@ static void request_with_payload_template( request_payload_slice = gpr_slice_from_copied_string(str); request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1); - client_args = grpc_channel_args_set_compression_level( - NULL, requested_compression_level); - server_args = grpc_channel_args_set_compression_level( - NULL, requested_compression_level); + client_args = grpc_channel_args_set_compression_algorithm( + NULL, requested_compression_algorithm); + server_args = grpc_channel_args_set_compression_algorithm( + NULL, requested_compression_algorithm); f = begin_test(config, test_name, client_args, server_args); cqv = cq_verifier_create(f.cq); @@ -256,7 +256,7 @@ static void test_invoke_request_with_exceptionally_uncompressed_payload( grpc_end2end_test_config config) { request_with_payload_template( config, "test_invoke_request_with_exceptionally_uncompressed_payload", - GRPC_WRITE_NO_COMPRESS, GRPC_COMPRESS_LEVEL_HIGH, GRPC_COMPRESS_NONE, + GRPC_WRITE_NO_COMPRESS, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_NONE, NULL); } @@ -264,16 +264,14 @@ static void test_invoke_request_with_uncompressed_payload( grpc_end2end_test_config config) { request_with_payload_template( config, "test_invoke_request_with_uncompressed_payload", 0, - GRPC_COMPRESS_LEVEL_NONE, - grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE), NULL); + GRPC_COMPRESS_NONE, GRPC_COMPRESS_NONE, NULL); } static void test_invoke_request_with_compressed_payload( grpc_end2end_test_config config) { request_with_payload_template( config, "test_invoke_request_with_compressed_payload", 0, - GRPC_COMPRESS_LEVEL_HIGH, - grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH), NULL); + GRPC_COMPRESS_GZIP, GRPC_COMPRESS_GZIP, NULL); } static void test_invoke_request_with_compressed_payload_md_override( @@ -296,19 +294,17 @@ static void test_invoke_request_with_compressed_payload_md_override( /* Channel default NONE, call override to GZIP */ request_with_payload_template( config, "test_invoke_request_with_compressed_payload_md_override_1", 0, - GRPC_COMPRESS_LEVEL_NONE, GRPC_COMPRESS_GZIP, &gzip_compression_override); + GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, &gzip_compression_override); /* Channel default DEFLATE, call override to GZIP */ request_with_payload_template( config, "test_invoke_request_with_compressed_payload_md_override_2", 0, - grpc_compression_level_for_algorithm(GRPC_COMPRESS_DEFLATE), - GRPC_COMPRESS_GZIP, &gzip_compression_override); + GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP, &gzip_compression_override); /* Channel default DEFLATE, call override to NONE */ request_with_payload_template( config, "test_invoke_request_with_compressed_payload_md_override_3", 0, - grpc_compression_level_for_algorithm(GRPC_COMPRESS_DEFLATE), - GRPC_COMPRESS_NONE, &none_compression_override); + GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_NONE, &none_compression_override); } void grpc_end2end_tests(grpc_end2end_test_config config) { diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index c086d66a7c3..b3523b8330b 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -271,7 +271,7 @@ static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub, for (int i = 0; i < num_rpcs; ++i) { ClientContext context; - context.set_compression_level(GRPC_COMPRESS_LEVEL_HIGH); + context._experimental_set_compression_algorithm(GRPC_COMPRESS_GZIP); Status s = stub->Echo(&context, request, &response); EXPECT_EQ(response.message(), request.message()); EXPECT_TRUE(s.ok()); diff --git a/test/cpp/end2end/generic_end2end_test.cc b/test/cpp/end2end/generic_end2end_test.cc index e9d86cc9f75..8fe0d6886a4 100644 --- a/test/cpp/end2end/generic_end2end_test.cc +++ b/test/cpp/end2end/generic_end2end_test.cc @@ -227,7 +227,7 @@ TEST_F(GenericEnd2endTest, SimpleBidiStreaming) { GenericServerContext srv_ctx; GenericServerAsyncReaderWriter srv_stream(&srv_ctx); - cli_ctx.set_compression_level(GRPC_COMPRESS_LEVEL_HIGH); + cli_ctx._experimental_set_compression_algorithm(GRPC_COMPRESS_GZIP); send_request.set_message("Hello"); std::unique_ptr cli_stream = generic_stub_->Call(&cli_ctx, kMethodName, &cli_cq_, tag(1));