/* Constructor for channel_data */ static void init_channel_elem(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_channel_element_args *args) { channel_data *channeld = elem->channel_data; grpc_compression_algorithm algo_idx; grpc_compression_options_init(&channeld->compression_options); channeld->compression_options.enabled_algorithms_bitset = (uint32_t)grpc_channel_args_compression_algorithm_get_states( args->channel_args); channeld->default_compression_algorithm = grpc_channel_args_get_compression_algorithm(args->channel_args); /* Make sure the default isn't disabled. */ GPR_ASSERT(grpc_compression_options_is_algorithm_enabled( &channeld->compression_options, channeld->default_compression_algorithm)); channeld->compression_options.default_compression_algorithm = channeld->default_compression_algorithm; channeld->supported_compression_algorithms = 0; for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) { /* skip disabled algorithms */ if (grpc_compression_options_is_algorithm_enabled( &channeld->compression_options, algo_idx) == 0) { continue; } channeld->supported_compression_algorithms |= 1u << algo_idx; } GPR_ASSERT(!args->is_last); }
static void test_compression_enable_disable_algorithm(void) { grpc_compression_options options; grpc_compression_algorithm algorithm; gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm"); grpc_compression_options_init(&options); for (algorithm = GRPC_COMPRESS_NONE; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) { /* all algorithms are enabled by default */ GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options, algorithm) != 0); } /* disable one by one */ for (algorithm = GRPC_COMPRESS_NONE; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) { grpc_compression_options_disable_algorithm(&options, algorithm); GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options, algorithm) == 0); } /* re-enable one by one */ for (algorithm = GRPC_COMPRESS_NONE; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) { grpc_compression_options_enable_algorithm(&options, algorithm); GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options, algorithm) != 0); } }
/** For each \a md element from the incoming metadata, filter out the entry for * "grpc-encoding", using its value to populate the call data's * compression_algorithm field. */ static grpc_mdelem *compression_md_filter(void *user_data, grpc_mdelem *md) { grpc_call_element *elem = user_data; call_data *calld = elem->call_data; channel_data *channeld = elem->channel_data; if (md->key == GRPC_MDSTR_GRPC_INTERNAL_ENCODING_REQUEST) { const char *md_c_str = grpc_mdstr_as_c_string(md->value); if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str), &calld->compression_algorithm)) { gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s' (unknown). Ignoring.", md_c_str); calld->compression_algorithm = GRPC_COMPRESS_NONE; } if (grpc_compression_options_is_algorithm_enabled( &channeld->compression_options, calld->compression_algorithm) == 0) { gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s' (previously disabled). " "Ignoring.", md_c_str); calld->compression_algorithm = GRPC_COMPRESS_NONE; } calld->has_compression_algorithm = 1; return NULL; } return md; }
/* Indicates whether a given algorithm is enabled on this instance, given the * readable algorithm name. */ VALUE grpc_rb_compression_options_is_algorithm_enabled(VALUE self, VALUE algorithm_name) { grpc_rb_compression_options* wrapper = NULL; grpc_compression_algorithm internal_algorithm_value; TypedData_Get_Struct(self, grpc_rb_compression_options, &grpc_rb_compression_options_data_type, wrapper); grpc_rb_compression_options_algorithm_name_to_value_internal( &internal_algorithm_value, algorithm_name); if (grpc_compression_options_is_algorithm_enabled(wrapper->wrapped, internal_algorithm_value)) { return Qtrue; } return Qfalse; }
/* Gets a list of the disabled algorithms as readable names. * Returns an empty list if no algorithms have been disabled. */ VALUE grpc_rb_compression_options_get_disabled_algorithms(VALUE self) { VALUE disabled_algorithms = rb_ary_new(); grpc_compression_algorithm internal_value; grpc_rb_compression_options* wrapper = NULL; TypedData_Get_Struct(self, grpc_rb_compression_options, &grpc_rb_compression_options_data_type, wrapper); for (internal_value = GRPC_COMPRESS_NONE; internal_value < GRPC_COMPRESS_ALGORITHMS_COUNT; internal_value++) { if (!grpc_compression_options_is_algorithm_enabled(wrapper->wrapped, internal_value)) { rb_ary_push(disabled_algorithms, grpc_rb_compression_options_algorithm_value_to_name_internal( internal_value)); } } return disabled_algorithms; }
/* Constructor for channel_data */ static void init_channel_elem(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_channel *master, const grpc_channel_args *args, grpc_mdctx *mdctx, int is_first, int is_last) { channel_data *channeld = elem->channel_data; grpc_compression_algorithm algo_idx; const char *supported_algorithms_names[GRPC_COMPRESS_ALGORITHMS_COUNT - 1]; size_t supported_algorithms_idx = 0; char *accept_encoding_str; size_t accept_encoding_str_len; grpc_compression_options_init(&channeld->compression_options); channeld->compression_options.enabled_algorithms_bitset = (gpr_uint32)grpc_channel_args_compression_algorithm_get_states(args); channeld->default_compression_algorithm = grpc_channel_args_get_compression_algorithm(args); /* Make sure the default isn't disabled. */ GPR_ASSERT(grpc_compression_options_is_algorithm_enabled( &channeld->compression_options, channeld->default_compression_algorithm)); channeld->compression_options.default_compression_algorithm = channeld->default_compression_algorithm; channeld->mdstr_request_compression_algorithm_key = grpc_mdstr_from_string(mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, 0); channeld->mdstr_outgoing_compression_algorithm_key = grpc_mdstr_from_string(mdctx, "grpc-encoding", 0); channeld->mdstr_compression_capabilities_key = grpc_mdstr_from_string(mdctx, "grpc-accept-encoding", 0); for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) { char *algorithm_name; /* skip disabled algorithms */ if (grpc_compression_options_is_algorithm_enabled( &channeld->compression_options, algo_idx) == 0) { continue; } GPR_ASSERT(grpc_compression_algorithm_name(algo_idx, &algorithm_name) != 0); channeld->mdelem_compression_algorithms[algo_idx] = grpc_mdelem_from_metadata_strings( mdctx, GRPC_MDSTR_REF(channeld->mdstr_outgoing_compression_algorithm_key), grpc_mdstr_from_string(mdctx, algorithm_name, 0)); if (algo_idx > 0) { supported_algorithms_names[supported_algorithms_idx++] = algorithm_name; } } /* TODO(dgq): gpr_strjoin_sep could be made to work with statically allocated * arrays, as to avoid the heap allocs */ accept_encoding_str = gpr_strjoin_sep(supported_algorithms_names, supported_algorithms_idx, ",", &accept_encoding_str_len); channeld->mdelem_accept_encoding = grpc_mdelem_from_metadata_strings( mdctx, GRPC_MDSTR_REF(channeld->mdstr_compression_capabilities_key), grpc_mdstr_from_string(mdctx, accept_encoding_str, 0)); gpr_free(accept_encoding_str); GPR_ASSERT(!is_last); }