David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 34 | #include <assert.h> |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 37 | #include <grpc/compression.h> |
| 38 | #include <grpc/support/log.h> |
| 39 | #include <grpc/support/slice_buffer.h> |
| 40 | |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 41 | #include "src/core/channel/compress_filter.h" |
| 42 | #include "src/core/channel/channel_args.h" |
| 43 | #include "src/core/compression/message_compress.h" |
| 44 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 45 | typedef struct call_data { |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 46 | gpr_slice_buffer slices; /**< Buffers up input slices to be compressed */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 47 | grpc_linked_mdelem compression_algorithm_storage; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 48 | int remaining_slice_bytes; /**< Input data to be read, as per BEGIN_MESSAGE */ |
| 49 | int written_initial_metadata; /**< Already processed initial md? */ |
| 50 | /** Compression algorithm we'll try to use. It may be given by incoming |
| 51 | * metadata, or by the channel's default compression settings. */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 52 | grpc_compression_algorithm compression_algorithm; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 53 | /** If true, contents of \a compression_algorithm are authoritative */ |
| 54 | int has_compression_algorithm; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 55 | } call_data; |
| 56 | |
| 57 | typedef struct channel_data { |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 58 | /** Metadata key for the incoming (requested) compression algorithm */ |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 59 | grpc_mdstr *mdstr_request_compression_algorithm_key; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 60 | /** Metadata key for the outgoing (used) compression algorithm */ |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 61 | grpc_mdstr *mdstr_outgoing_compression_algorithm_key; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 62 | /** Precomputed metadata elements for all available compression algorithms */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 63 | grpc_mdelem *mdelem_compression_algorithms[GRPC_COMPRESS_ALGORITHMS_COUNT]; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 64 | /** The default, channel-level, compression algorithm */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 65 | grpc_compression_algorithm default_compression_algorithm; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 66 | } channel_data; |
| 67 | |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 68 | /** Compress \a slices in place using \a algorithm. Returns 1 if compression did |
| 69 | * actually happen, 0 otherwise (for example if the compressed output size was |
| 70 | * larger than the raw input). |
| 71 | * |
| 72 | * Returns 1 if the data was actually compress and 0 otherwise. */ |
| 73 | static int compress_send_sb(grpc_compression_algorithm algorithm, |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 74 | gpr_slice_buffer *slices) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 75 | int did_compress; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 76 | gpr_slice_buffer tmp; |
| 77 | gpr_slice_buffer_init(&tmp); |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 78 | did_compress = grpc_msg_compress(algorithm, slices, &tmp); |
David Garcia Quintas | 4e40336 | 2015-07-01 16:45:34 -0700 | [diff] [blame] | 79 | if (did_compress) { |
| 80 | gpr_slice_buffer_swap(slices, &tmp); |
| 81 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 82 | gpr_slice_buffer_destroy(&tmp); |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 83 | return did_compress; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 84 | } |
| 85 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 86 | /** For each \a md element from the incoming metadata, filter out the entry for |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 87 | * "grpc-encoding", using its value to populate the call data's |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 88 | * compression_algorithm field. */ |
| 89 | static grpc_mdelem* compression_md_filter(void *user_data, grpc_mdelem *md) { |
| 90 | grpc_call_element *elem = user_data; |
| 91 | call_data *calld = elem->call_data; |
| 92 | channel_data *channeld = elem->channel_data; |
| 93 | |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 94 | if (md->key == channeld->mdstr_request_compression_algorithm_key) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 95 | const char *md_c_str = grpc_mdstr_as_c_string(md->value); |
| 96 | if (!grpc_compression_algorithm_parse(md_c_str, |
| 97 | &calld->compression_algorithm)) { |
| 98 | gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'. Ignoring.", |
| 99 | md_c_str); |
| 100 | calld->compression_algorithm = GRPC_COMPRESS_NONE; |
| 101 | } |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 102 | calld->has_compression_algorithm = 1; |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | return md; |
| 107 | } |
| 108 | |
| 109 | static int skip_compression(channel_data *channeld, call_data *calld) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 110 | if (calld->has_compression_algorithm) { |
| 111 | if (calld->compression_algorithm == GRPC_COMPRESS_NONE) { |
| 112 | return 1; |
| 113 | } |
| 114 | return 0; /* we have an actual call-specific algorithm */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 115 | } |
| 116 | /* no per-call compression override */ |
| 117 | return channeld->default_compression_algorithm == GRPC_COMPRESS_NONE; |
| 118 | } |
| 119 | |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 120 | /** Assembles a new grpc_stream_op_buffer with the compressed slices, modifying |
| 121 | * the associated GRPC_OP_BEGIN_MESSAGE accordingly (new compressed length, |
| 122 | * flags indicating compression is in effect) and replaces \a send_ops with it. |
| 123 | * */ |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 124 | static void finish_compressed_sopb(grpc_stream_op_buffer *send_ops, |
| 125 | grpc_call_element *elem) { |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 126 | size_t i; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 127 | call_data *calld = elem->call_data; |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 128 | int new_slices_added = 0; /* GPR_FALSE */ |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 129 | grpc_metadata_batch metadata; |
| 130 | grpc_stream_op_buffer new_send_ops; |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 131 | grpc_sopb_init(&new_send_ops); |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 132 | |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 133 | for (i = 0; i < send_ops->nops; i++) { |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 134 | grpc_stream_op *sop = &send_ops->ops[i]; |
| 135 | switch (sop->type) { |
| 136 | case GRPC_OP_BEGIN_MESSAGE: |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 137 | grpc_sopb_add_begin_message( |
| 138 | &new_send_ops, calld->slices.length, |
| 139 | sop->data.begin_message.flags | GRPC_WRITE_INTERNAL_COMPRESS); |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 140 | break; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 141 | case GRPC_OP_SLICE: |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 142 | /* Once we reach the slices section of the original buffer, simply add |
| 143 | * all the new (compressed) slices. We obviously want to do this only |
| 144 | * once, hence the "new_slices_added" guard. */ |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 145 | if (!new_slices_added) { |
| 146 | size_t j; |
| 147 | for (j = 0; j < calld->slices.count; ++j) { |
| 148 | grpc_sopb_add_slice(&new_send_ops, |
| 149 | gpr_slice_ref(calld->slices.slices[j])); |
| 150 | } |
| 151 | new_slices_added = 1; /* GPR_TRUE */ |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 152 | } |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 153 | break; |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 154 | case GRPC_OP_METADATA: |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 155 | /* move the metadata to the new buffer. */ |
| 156 | grpc_metadata_batch_move(&metadata, &sop->data.metadata); |
| 157 | grpc_sopb_add_metadata(&new_send_ops, metadata); |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 158 | break; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 159 | case GRPC_NO_OP: |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 160 | break; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 163 | grpc_sopb_swap(send_ops, &new_send_ops); |
| 164 | grpc_sopb_destroy(&new_send_ops); |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 165 | } |
| 166 | |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 167 | /** Filter's "main" function, called for any incoming grpc_transport_stream_op |
| 168 | * instance that holds a non-zero number of send operations, accesible to this |
| 169 | * function in \a send_ops. */ |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 170 | static void process_send_ops(grpc_call_element *elem, |
| 171 | grpc_stream_op_buffer *send_ops) { |
| 172 | call_data *calld = elem->call_data; |
| 173 | channel_data *channeld = elem->channel_data; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 174 | size_t i; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 175 | int did_compress = 0; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 176 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 177 | for (i = 0; i < send_ops->nops; ++i) { |
| 178 | grpc_stream_op *sop = &send_ops->ops[i]; |
| 179 | switch (sop->type) { |
| 180 | case GRPC_OP_BEGIN_MESSAGE: |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 181 | /* buffer up slices until we've processed all the expected ones (as |
| 182 | * given by GRPC_OP_BEGIN_MESSAGE) */ |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 183 | calld->remaining_slice_bytes = sop->data.begin_message.length; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 184 | if (sop->data.begin_message.flags & GRPC_WRITE_NO_COMPRESS) { |
| 185 | calld->has_compression_algorithm = 1; /* GPR_TRUE */ |
| 186 | calld->compression_algorithm = GRPC_COMPRESS_NONE; |
| 187 | } |
| 188 | break; |
| 189 | case GRPC_OP_METADATA: |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 190 | if (!calld->written_initial_metadata) { |
| 191 | /* Parse incoming request for compression. If any, it'll be available |
| 192 | * at calld->compression_algorithm */ |
| 193 | grpc_metadata_batch_filter(&(sop->data.metadata), |
| 194 | compression_md_filter, elem); |
| 195 | if (!calld->has_compression_algorithm) { |
| 196 | /* If no algorithm was found in the metadata and we aren't |
| 197 | * exceptionally skipping compression, fall back to the channel |
| 198 | * default */ |
| 199 | calld->compression_algorithm = |
| 200 | channeld->default_compression_algorithm; |
| 201 | calld->has_compression_algorithm = 1; /* GPR_TRUE */ |
| 202 | } |
| 203 | grpc_metadata_batch_add_head( |
| 204 | &(sop->data.metadata), &calld->compression_algorithm_storage, |
| 205 | grpc_mdelem_ref(channeld->mdelem_compression_algorithms |
| 206 | [calld->compression_algorithm])); |
| 207 | calld->written_initial_metadata = 1; /* GPR_TRUE */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 208 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 209 | break; |
| 210 | case GRPC_OP_SLICE: |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 211 | if (skip_compression(channeld, calld)) continue; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 212 | GPR_ASSERT(calld->remaining_slice_bytes > 0); |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 213 | /* Increase input ref count, gpr_slice_buffer_add takes ownership. */ |
David Garcia Quintas | 4e40336 | 2015-07-01 16:45:34 -0700 | [diff] [blame] | 214 | gpr_slice_buffer_add(&calld->slices, gpr_slice_ref(sop->data.slice)); |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 215 | calld->remaining_slice_bytes -= GPR_SLICE_LENGTH(sop->data.slice); |
| 216 | if (calld->remaining_slice_bytes == 0) { |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 217 | did_compress = |
| 218 | compress_send_sb(calld->compression_algorithm, &calld->slices); |
| 219 | } |
| 220 | break; |
| 221 | case GRPC_NO_OP: |
David Garcia Quintas | a21e2c8 | 2015-07-10 00:15:36 -0700 | [diff] [blame] | 222 | break; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 226 | /* Modify the send_ops stream_op_buffer depending on whether compression was |
| 227 | * carried out */ |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 228 | if (did_compress) { |
| 229 | finish_compressed_sopb(send_ops, elem); |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 230 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* Called either: |
| 234 | - in response to an API call (or similar) from above, to send something |
| 235 | - a network event (or similar) from below, to receive something |
| 236 | op contains type and call direction information, in addition to the data |
| 237 | that is being sent or received. */ |
David Garcia Quintas | 17bb649 | 2015-07-08 15:16:22 -0700 | [diff] [blame] | 238 | static void compress_start_transport_stream_op(grpc_call_element *elem, |
| 239 | grpc_transport_stream_op *op) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 240 | if (op->send_ops && op->send_ops->nops > 0) { |
| 241 | process_send_ops(elem, op->send_ops); |
| 242 | } |
| 243 | |
| 244 | /* pass control down the stack */ |
| 245 | grpc_call_next_op(elem, op); |
| 246 | } |
| 247 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 248 | /* Constructor for call_data */ |
| 249 | static void init_call_elem(grpc_call_element *elem, |
| 250 | const void *server_transport_data, |
David Garcia Quintas | 17bb649 | 2015-07-08 15:16:22 -0700 | [diff] [blame] | 251 | grpc_transport_stream_op *initial_op) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 252 | /* grab pointers to our data from the call element */ |
| 253 | call_data *calld = elem->call_data; |
| 254 | |
| 255 | /* initialize members */ |
| 256 | gpr_slice_buffer_init(&calld->slices); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 257 | calld->has_compression_algorithm = 0; |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 258 | calld->written_initial_metadata = 0; /* GPR_FALSE */ |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 259 | |
| 260 | if (initial_op) { |
| 261 | if (initial_op->send_ops && initial_op->send_ops->nops > 0) { |
| 262 | process_send_ops(elem, initial_op->send_ops); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* Destructor for call_data */ |
| 268 | static void destroy_call_elem(grpc_call_element *elem) { |
| 269 | /* grab pointers to our data from the call element */ |
| 270 | call_data *calld = elem->call_data; |
| 271 | gpr_slice_buffer_destroy(&calld->slices); |
| 272 | } |
| 273 | |
| 274 | /* Constructor for channel_data */ |
David Garcia Quintas | 17bb649 | 2015-07-08 15:16:22 -0700 | [diff] [blame] | 275 | static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 276 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 277 | int is_first, int is_last) { |
| 278 | channel_data *channeld = elem->channel_data; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 279 | grpc_compression_algorithm algo_idx; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 280 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 281 | channeld->default_compression_algorithm = |
David Garcia Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 282 | grpc_channel_args_get_compression_algorithm(args); |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 283 | |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 284 | channeld->mdstr_request_compression_algorithm_key = |
| 285 | grpc_mdstr_from_string(mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY); |
| 286 | |
| 287 | channeld->mdstr_outgoing_compression_algorithm_key = |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 288 | grpc_mdstr_from_string(mdctx, "grpc-encoding"); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 289 | |
| 290 | for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 291 | char *algorith_name; |
| 292 | GPR_ASSERT(grpc_compression_algorithm_name(algo_idx, &algorith_name) != 0); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 293 | channeld->mdelem_compression_algorithms[algo_idx] = |
| 294 | grpc_mdelem_from_metadata_strings( |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 295 | mdctx, |
| 296 | grpc_mdstr_ref(channeld->mdstr_outgoing_compression_algorithm_key), |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 297 | grpc_mdstr_from_string(mdctx, algorith_name)); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 298 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 299 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 300 | GPR_ASSERT(!is_last); |
| 301 | } |
| 302 | |
| 303 | /* Destructor for channel data */ |
| 304 | static void destroy_channel_elem(grpc_channel_element *elem) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 305 | channel_data *channeld = elem->channel_data; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 306 | grpc_compression_algorithm algo_idx; |
| 307 | |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 308 | grpc_mdstr_unref(channeld->mdstr_request_compression_algorithm_key); |
| 309 | grpc_mdstr_unref(channeld->mdstr_outgoing_compression_algorithm_key); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 310 | for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 311 | ++algo_idx) { |
| 312 | grpc_mdelem_unref(channeld->mdelem_compression_algorithms[algo_idx]); |
| 313 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 314 | } |
| 315 | |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 316 | const grpc_channel_filter grpc_compress_filter = { |
| 317 | compress_start_transport_stream_op, |
| 318 | grpc_channel_next_op, |
| 319 | sizeof(call_data), |
| 320 | init_call_elem, |
| 321 | destroy_call_elem, |
| 322 | sizeof(channel_data), |
| 323 | init_channel_elem, |
| 324 | destroy_channel_elem, |
| 325 | "compress"}; |