diff options
author | David Garcia Quintas <dgq@google.com> | 2015-08-24 12:11:26 -0700 |
---|---|---|
committer | David Garcia Quintas <dgq@google.com> | 2015-08-24 12:11:26 -0700 |
commit | d09bae5b488cc235b11d4c2446accaf3160322c7 (patch) | |
tree | 8851e37b9123b8a97fab8aab8976276f5ee00768 /src/core | |
parent | c43648f250dd6cb0f086e2366e468372a6de26ae (diff) | |
parent | cb30410b1063845cc3a0205741a72c9c96e07638 (diff) |
Merge branch 'compression-accept-encoding' of github.com:dgquintas/grpc into compression-accept-encoding
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/channel/channel_args.c | 48 | ||||
-rw-r--r-- | src/core/channel/compress_filter.c | 9 |
2 files changed, 36 insertions, 21 deletions
diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c index 7d97b79553..55b4cb6170 100644 --- a/src/core/channel/channel_args.c +++ b/src/core/channel/channel_args.c @@ -148,44 +148,58 @@ grpc_channel_args *grpc_channel_args_set_compression_algorithm( return grpc_channel_args_copy_and_add(a, &tmp, 1); } -/** Returns the compression algorithm's enabled states bitset from \a a. If not - * found, return a biset will all algorithms enabled */ -static gpr_uint32 find_compression_algorithm_states_bitset( - const grpc_channel_args *a) { - gpr_uint32 states_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; +/** Returns 1 if the argument for compression algorithm's enabled states bitset + * was found in \a a, returning the arg's value in \a states. Otherwise, returns + * 0. */ +static int find_compression_algorithm_states_bitset( + const grpc_channel_args *a, int **states_arg) { if (a != NULL) { size_t i; for (i = 0; i < a->num_args; ++i) { if (a->args[i].type == GRPC_ARG_INTEGER && !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) { - states_bitset = a->args[i].value.integer; - break; + *states_arg = &a->args[i].value.integer; + return 1; /* GPR_TRUE */ } } } - return states_bitset; + return 0; /* GPR_FALSE */ } grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( grpc_channel_args *a, grpc_compression_algorithm algorithm, int state) { - gpr_uint32 states_bitset = find_compression_algorithm_states_bitset(a); - grpc_arg tmp; + int *states_arg; + grpc_channel_args *result = a; + const int states_arg_found = + find_compression_algorithm_states_bitset(a, &states_arg); + + if (!states_arg_found) { + /* create a new arg */ + grpc_arg tmp; + tmp.type = GRPC_ARG_INTEGER; + tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG; + states_arg = &tmp.value.integer; + result = grpc_channel_args_copy_and_add(a, &tmp, 1); + } + /* update either the new arg's value or the already present one */ if (state != 0) { - GPR_BITSET(&states_bitset, algorithm); + GPR_BITSET(states_arg, algorithm); } else { - GPR_BITCLEAR(&states_bitset, algorithm); + GPR_BITCLEAR(states_arg, algorithm); } - tmp.type = GRPC_ARG_INTEGER; - tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG; - tmp.value.integer = states_bitset; - return grpc_channel_args_copy_and_add(a, &tmp, 1); + return result; } int grpc_channel_args_compression_algorithm_get_states( const grpc_channel_args *a) { - return find_compression_algorithm_states_bitset(a); + int *states_arg; + if (find_compression_algorithm_states_bitset(a, &states_arg)) { + return *states_arg; + } else { + return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */ + } } diff --git a/src/core/channel/compress_filter.c b/src/core/channel/compress_filter.c index 8e9ab30ceb..39efc08ab5 100644 --- a/src/core/channel/compress_filter.c +++ b/src/core/channel/compress_filter.c @@ -306,6 +306,7 @@ static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, 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; @@ -344,15 +345,15 @@ static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, GRPC_MDSTR_REF(channeld->mdstr_outgoing_compression_algorithm_key), grpc_mdstr_from_string(mdctx, algorithm_name, 0)); if (algo_idx > 0) { - supported_algorithms_names[algo_idx - 1] = algorithm_name; + 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, GPR_ARRAY_SIZE(supported_algorithms_names), - ", ", &accept_encoding_str_len); + 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), |