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> |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/slice_buffer.h> |
| 41 | |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 42 | #include "src/core/channel/compress_filter.h" |
| 43 | #include "src/core/channel/channel_args.h" |
| 44 | #include "src/core/compression/message_compress.h" |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 45 | #include "src/core/support/string.h" |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 46 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 47 | typedef struct call_data { |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 48 | gpr_slice_buffer slices; /**< Buffers up input slices to be compressed */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 49 | grpc_linked_mdelem compression_algorithm_storage; |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 50 | grpc_linked_mdelem accept_encoding_storage; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 51 | int remaining_slice_bytes; /**< Input data to be read, as per BEGIN_MESSAGE */ |
| 52 | int written_initial_metadata; /**< Already processed initial md? */ |
| 53 | /** Compression algorithm we'll try to use. It may be given by incoming |
| 54 | * metadata, or by the channel's default compression settings. */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 55 | grpc_compression_algorithm compression_algorithm; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 56 | /** If true, contents of \a compression_algorithm are authoritative */ |
| 57 | int has_compression_algorithm; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 58 | } call_data; |
| 59 | |
| 60 | typedef struct channel_data { |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 61 | /** Metadata key for the incoming (requested) compression algorithm */ |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 62 | grpc_mdstr *mdstr_request_compression_algorithm_key; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 63 | /** Metadata key for the outgoing (used) compression algorithm */ |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 64 | grpc_mdstr *mdstr_outgoing_compression_algorithm_key; |
David Garcia Quintas | 39a6de2 | 2015-07-19 21:18:03 -0700 | [diff] [blame] | 65 | /** Metadata key for the accepted encodings */ |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 66 | grpc_mdstr *mdstr_compression_capabilities_key; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 67 | /** Precomputed metadata elements for all available compression algorithms */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 68 | grpc_mdelem *mdelem_compression_algorithms[GRPC_COMPRESS_ALGORITHMS_COUNT]; |
David Garcia Quintas | 39a6de2 | 2015-07-19 21:18:03 -0700 | [diff] [blame] | 69 | /** Precomputed metadata elements for the accepted encodings */ |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 70 | grpc_mdelem *mdelem_accept_encoding; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 71 | /** The default, channel-level, compression algorithm */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 72 | grpc_compression_algorithm default_compression_algorithm; |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 73 | /** Compression options for the channel */ |
| 74 | grpc_compression_options compression_options; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 75 | } channel_data; |
| 76 | |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 77 | /** Compress \a slices in place using \a algorithm. Returns 1 if compression did |
| 78 | * actually happen, 0 otherwise (for example if the compressed output size was |
| 79 | * larger than the raw input). |
| 80 | * |
| 81 | * Returns 1 if the data was actually compress and 0 otherwise. */ |
| 82 | static int compress_send_sb(grpc_compression_algorithm algorithm, |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 83 | gpr_slice_buffer *slices) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 84 | int did_compress; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 85 | gpr_slice_buffer tmp; |
| 86 | gpr_slice_buffer_init(&tmp); |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 87 | did_compress = grpc_msg_compress(algorithm, slices, &tmp); |
David Garcia Quintas | 4e40336 | 2015-07-01 16:45:34 -0700 | [diff] [blame] | 88 | if (did_compress) { |
| 89 | gpr_slice_buffer_swap(slices, &tmp); |
| 90 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 91 | gpr_slice_buffer_destroy(&tmp); |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 92 | return did_compress; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 95 | /** 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] | 96 | * "grpc-encoding", using its value to populate the call data's |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 97 | * compression_algorithm field. */ |
| 98 | static grpc_mdelem* compression_md_filter(void *user_data, grpc_mdelem *md) { |
| 99 | grpc_call_element *elem = user_data; |
| 100 | call_data *calld = elem->call_data; |
| 101 | channel_data *channeld = elem->channel_data; |
| 102 | |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 103 | if (md->key == channeld->mdstr_request_compression_algorithm_key) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 104 | const char *md_c_str = grpc_mdstr_as_c_string(md->value); |
David Garcia Quintas | 1c604fd | 2015-07-21 16:22:58 -0700 | [diff] [blame] | 105 | if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str), |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 106 | &calld->compression_algorithm)) { |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 107 | gpr_log(GPR_ERROR, |
| 108 | "Invalid compression algorithm: '%s' (unknown). Ignoring.", |
| 109 | md_c_str); |
| 110 | calld->compression_algorithm = GRPC_COMPRESS_NONE; |
| 111 | } |
| 112 | if (grpc_compression_options_is_algorithm_enabled( |
| 113 | &channeld->compression_options, calld->compression_algorithm) == 0) |
| 114 | { |
| 115 | gpr_log(GPR_ERROR, |
| 116 | "Invalid compression algorithm: '%s' (previously disabled). " |
| 117 | "Ignoring.", |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 118 | md_c_str); |
| 119 | calld->compression_algorithm = GRPC_COMPRESS_NONE; |
| 120 | } |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 121 | calld->has_compression_algorithm = 1; |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | return md; |
| 126 | } |
| 127 | |
| 128 | static int skip_compression(channel_data *channeld, call_data *calld) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 129 | if (calld->has_compression_algorithm) { |
| 130 | if (calld->compression_algorithm == GRPC_COMPRESS_NONE) { |
| 131 | return 1; |
| 132 | } |
| 133 | return 0; /* we have an actual call-specific algorithm */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 134 | } |
| 135 | /* no per-call compression override */ |
| 136 | return channeld->default_compression_algorithm == GRPC_COMPRESS_NONE; |
| 137 | } |
| 138 | |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 139 | /** Assembles a new grpc_stream_op_buffer with the compressed slices, modifying |
| 140 | * the associated GRPC_OP_BEGIN_MESSAGE accordingly (new compressed length, |
| 141 | * flags indicating compression is in effect) and replaces \a send_ops with it. |
| 142 | * */ |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 143 | static void finish_compressed_sopb(grpc_stream_op_buffer *send_ops, |
| 144 | grpc_call_element *elem) { |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 145 | size_t i; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 146 | call_data *calld = elem->call_data; |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 147 | int new_slices_added = 0; /* GPR_FALSE */ |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 148 | grpc_metadata_batch metadata; |
| 149 | grpc_stream_op_buffer new_send_ops; |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 150 | grpc_sopb_init(&new_send_ops); |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 151 | |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 152 | for (i = 0; i < send_ops->nops; i++) { |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 153 | grpc_stream_op *sop = &send_ops->ops[i]; |
| 154 | switch (sop->type) { |
| 155 | case GRPC_OP_BEGIN_MESSAGE: |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 156 | grpc_sopb_add_begin_message( |
| 157 | &new_send_ops, calld->slices.length, |
| 158 | sop->data.begin_message.flags | GRPC_WRITE_INTERNAL_COMPRESS); |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 159 | break; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 160 | case GRPC_OP_SLICE: |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 161 | /* Once we reach the slices section of the original buffer, simply add |
| 162 | * all the new (compressed) slices. We obviously want to do this only |
| 163 | * once, hence the "new_slices_added" guard. */ |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 164 | if (!new_slices_added) { |
| 165 | size_t j; |
| 166 | for (j = 0; j < calld->slices.count; ++j) { |
| 167 | grpc_sopb_add_slice(&new_send_ops, |
| 168 | gpr_slice_ref(calld->slices.slices[j])); |
| 169 | } |
| 170 | new_slices_added = 1; /* GPR_TRUE */ |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 171 | } |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 172 | break; |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 173 | case GRPC_OP_METADATA: |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 174 | /* move the metadata to the new buffer. */ |
| 175 | grpc_metadata_batch_move(&metadata, &sop->data.metadata); |
| 176 | grpc_sopb_add_metadata(&new_send_ops, metadata); |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 177 | break; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 178 | case GRPC_NO_OP: |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 179 | break; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
David Garcia Quintas | 20a3538 | 2015-07-10 17:48:22 -0700 | [diff] [blame] | 182 | grpc_sopb_swap(send_ops, &new_send_ops); |
| 183 | grpc_sopb_destroy(&new_send_ops); |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 184 | } |
| 185 | |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 186 | /** Filter's "main" function, called for any incoming grpc_transport_stream_op |
| 187 | * instance that holds a non-zero number of send operations, accesible to this |
| 188 | * function in \a send_ops. */ |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 189 | static void process_send_ops(grpc_call_element *elem, |
| 190 | grpc_stream_op_buffer *send_ops) { |
| 191 | call_data *calld = elem->call_data; |
| 192 | channel_data *channeld = elem->channel_data; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 193 | size_t i; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 194 | int did_compress = 0; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 195 | |
David Garcia Quintas | 518e3fe | 2015-07-21 15:49:42 -0700 | [diff] [blame] | 196 | /* In streaming calls, we need to reset the previously accumulated slices */ |
| 197 | gpr_slice_buffer_reset_and_unref(&calld->slices); |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 198 | for (i = 0; i < send_ops->nops; ++i) { |
| 199 | grpc_stream_op *sop = &send_ops->ops[i]; |
| 200 | switch (sop->type) { |
| 201 | case GRPC_OP_BEGIN_MESSAGE: |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 202 | /* buffer up slices until we've processed all the expected ones (as |
| 203 | * given by GRPC_OP_BEGIN_MESSAGE) */ |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 204 | calld->remaining_slice_bytes = sop->data.begin_message.length; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 205 | if (sop->data.begin_message.flags & GRPC_WRITE_NO_COMPRESS) { |
| 206 | calld->has_compression_algorithm = 1; /* GPR_TRUE */ |
| 207 | calld->compression_algorithm = GRPC_COMPRESS_NONE; |
| 208 | } |
| 209 | break; |
| 210 | case GRPC_OP_METADATA: |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 211 | if (!calld->written_initial_metadata) { |
| 212 | /* Parse incoming request for compression. If any, it'll be available |
| 213 | * at calld->compression_algorithm */ |
| 214 | grpc_metadata_batch_filter(&(sop->data.metadata), |
| 215 | compression_md_filter, elem); |
| 216 | if (!calld->has_compression_algorithm) { |
| 217 | /* If no algorithm was found in the metadata and we aren't |
| 218 | * exceptionally skipping compression, fall back to the channel |
| 219 | * default */ |
| 220 | calld->compression_algorithm = |
| 221 | channeld->default_compression_algorithm; |
| 222 | calld->has_compression_algorithm = 1; /* GPR_TRUE */ |
| 223 | } |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 224 | /* hint compression algorithm */ |
Craig Tiller | 6159c07 | 2015-07-22 17:01:54 -0700 | [diff] [blame] | 225 | grpc_metadata_batch_add_tail( |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 226 | &(sop->data.metadata), &calld->compression_algorithm_storage, |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 227 | GRPC_MDELEM_REF(channeld->mdelem_compression_algorithms |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 228 | [calld->compression_algorithm])); |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 229 | |
| 230 | /* convey supported compression algorithms */ |
| 231 | grpc_metadata_batch_add_head( |
| 232 | &(sop->data.metadata), &calld->accept_encoding_storage, |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 233 | GRPC_MDELEM_REF(channeld->mdelem_accept_encoding)); |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 234 | |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 235 | calld->written_initial_metadata = 1; /* GPR_TRUE */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 236 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 237 | break; |
| 238 | case GRPC_OP_SLICE: |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 239 | if (skip_compression(channeld, calld)) continue; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 240 | GPR_ASSERT(calld->remaining_slice_bytes > 0); |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 241 | /* Increase input ref count, gpr_slice_buffer_add takes ownership. */ |
David Garcia Quintas | 4e40336 | 2015-07-01 16:45:34 -0700 | [diff] [blame] | 242 | 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] | 243 | calld->remaining_slice_bytes -= GPR_SLICE_LENGTH(sop->data.slice); |
| 244 | if (calld->remaining_slice_bytes == 0) { |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 245 | did_compress = |
| 246 | compress_send_sb(calld->compression_algorithm, &calld->slices); |
| 247 | } |
| 248 | break; |
| 249 | case GRPC_NO_OP: |
David Garcia Quintas | a21e2c8 | 2015-07-10 00:15:36 -0700 | [diff] [blame] | 250 | break; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 254 | /* Modify the send_ops stream_op_buffer depending on whether compression was |
| 255 | * carried out */ |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 256 | if (did_compress) { |
| 257 | finish_compressed_sopb(send_ops, elem); |
David Garcia Quintas | a01f7a4 | 2015-07-08 00:16:50 -0700 | [diff] [blame] | 258 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* Called either: |
| 262 | - in response to an API call (or similar) from above, to send something |
| 263 | - a network event (or similar) from below, to receive something |
| 264 | op contains type and call direction information, in addition to the data |
| 265 | that is being sent or received. */ |
David Garcia Quintas | 17bb649 | 2015-07-08 15:16:22 -0700 | [diff] [blame] | 266 | static void compress_start_transport_stream_op(grpc_call_element *elem, |
| 267 | grpc_transport_stream_op *op) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 268 | if (op->send_ops && op->send_ops->nops > 0) { |
| 269 | process_send_ops(elem, op->send_ops); |
| 270 | } |
| 271 | |
| 272 | /* pass control down the stack */ |
| 273 | grpc_call_next_op(elem, op); |
| 274 | } |
| 275 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 276 | /* Constructor for call_data */ |
| 277 | static void init_call_elem(grpc_call_element *elem, |
| 278 | const void *server_transport_data, |
David Garcia Quintas | 17bb649 | 2015-07-08 15:16:22 -0700 | [diff] [blame] | 279 | grpc_transport_stream_op *initial_op) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 280 | /* grab pointers to our data from the call element */ |
| 281 | call_data *calld = elem->call_data; |
| 282 | |
| 283 | /* initialize members */ |
| 284 | gpr_slice_buffer_init(&calld->slices); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 285 | calld->has_compression_algorithm = 0; |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 286 | calld->written_initial_metadata = 0; /* GPR_FALSE */ |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 287 | |
| 288 | if (initial_op) { |
| 289 | if (initial_op->send_ops && initial_op->send_ops->nops > 0) { |
| 290 | process_send_ops(elem, initial_op->send_ops); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* Destructor for call_data */ |
| 296 | static void destroy_call_elem(grpc_call_element *elem) { |
| 297 | /* grab pointers to our data from the call element */ |
| 298 | call_data *calld = elem->call_data; |
| 299 | gpr_slice_buffer_destroy(&calld->slices); |
| 300 | } |
| 301 | |
| 302 | /* Constructor for channel_data */ |
David Garcia Quintas | 17bb649 | 2015-07-08 15:16:22 -0700 | [diff] [blame] | 303 | 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] | 304 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 305 | int is_first, int is_last) { |
| 306 | channel_data *channeld = elem->channel_data; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 307 | grpc_compression_algorithm algo_idx; |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 308 | const char* supported_algorithms_names[GRPC_COMPRESS_ALGORITHMS_COUNT-1]; |
| 309 | char *accept_encoding_str; |
| 310 | size_t accept_encoding_str_len; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 311 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 312 | grpc_compression_options_init(&channeld->compression_options); |
| 313 | channeld->compression_options.enabled_algorithms_bitset = |
| 314 | grpc_channel_args_compression_algorithm_get_states(args); |
| 315 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 316 | channeld->default_compression_algorithm = |
David Garcia Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 317 | grpc_channel_args_get_compression_algorithm(args); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 318 | /* Make sure the default isn't disabled. */ |
| 319 | GPR_ASSERT(grpc_compression_options_is_algorithm_enabled( |
| 320 | &channeld->compression_options, channeld->default_compression_algorithm)); |
| 321 | channeld->compression_options.default_compression_algorithm = |
| 322 | channeld->default_compression_algorithm; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 323 | |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 324 | channeld->mdstr_request_compression_algorithm_key = |
Craig Tiller | 6999c09 | 2015-07-22 08:14:12 -0700 | [diff] [blame] | 325 | grpc_mdstr_from_string(mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, 0); |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 326 | |
| 327 | channeld->mdstr_outgoing_compression_algorithm_key = |
Craig Tiller | 6999c09 | 2015-07-22 08:14:12 -0700 | [diff] [blame] | 328 | grpc_mdstr_from_string(mdctx, "grpc-encoding", 0); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 329 | |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 330 | channeld->mdstr_compression_capabilities_key = |
David Garcia Quintas | 10494fc | 2015-08-09 08:52:47 -0700 | [diff] [blame] | 331 | grpc_mdstr_from_string(mdctx, "grpc-accept-encoding", 0); |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 332 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 333 | for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) { |
Craig Tiller | 6999c09 | 2015-07-22 08:14:12 -0700 | [diff] [blame] | 334 | char *algorithm_name; |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 335 | /* skip disabled algorithms */ |
| 336 | if (grpc_compression_options_is_algorithm_enabled( |
| 337 | &channeld->compression_options, algo_idx) == 0) { |
| 338 | continue; |
| 339 | } |
Craig Tiller | 6999c09 | 2015-07-22 08:14:12 -0700 | [diff] [blame] | 340 | GPR_ASSERT(grpc_compression_algorithm_name(algo_idx, &algorithm_name) != 0); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 341 | channeld->mdelem_compression_algorithms[algo_idx] = |
| 342 | grpc_mdelem_from_metadata_strings( |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 343 | mdctx, |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 344 | GRPC_MDSTR_REF(channeld->mdstr_outgoing_compression_algorithm_key), |
Craig Tiller | 6999c09 | 2015-07-22 08:14:12 -0700 | [diff] [blame] | 345 | grpc_mdstr_from_string(mdctx, algorithm_name, 0)); |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 346 | if (algo_idx > 0) { |
David Garcia Quintas | 10494fc | 2015-08-09 08:52:47 -0700 | [diff] [blame] | 347 | supported_algorithms_names[algo_idx-1] = algorithm_name; |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 348 | } |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 349 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 350 | |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 351 | /* TODO(dgq): gpr_strjoin_sep could be made to work with statically allocated |
| 352 | * arrays, as to avoid the heap allocs */ |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 353 | accept_encoding_str = |
| 354 | gpr_strjoin_sep(supported_algorithms_names, |
| 355 | GPR_ARRAY_SIZE(supported_algorithms_names), |
| 356 | ", ", |
| 357 | &accept_encoding_str_len); |
| 358 | |
| 359 | channeld->mdelem_accept_encoding = |
| 360 | grpc_mdelem_from_metadata_strings( |
| 361 | mdctx, |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 362 | GRPC_MDSTR_REF(channeld->mdstr_compression_capabilities_key), |
David Garcia Quintas | 10494fc | 2015-08-09 08:52:47 -0700 | [diff] [blame] | 363 | grpc_mdstr_from_string(mdctx, accept_encoding_str, 0)); |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 364 | gpr_free(accept_encoding_str); |
| 365 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 366 | GPR_ASSERT(!is_last); |
| 367 | } |
| 368 | |
| 369 | /* Destructor for channel data */ |
| 370 | static void destroy_channel_elem(grpc_channel_element *elem) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 371 | channel_data *channeld = elem->channel_data; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 372 | grpc_compression_algorithm algo_idx; |
| 373 | |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 374 | GRPC_MDSTR_UNREF(channeld->mdstr_request_compression_algorithm_key); |
| 375 | GRPC_MDSTR_UNREF(channeld->mdstr_outgoing_compression_algorithm_key); |
| 376 | GRPC_MDSTR_UNREF(channeld->mdstr_compression_capabilities_key); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 377 | for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 378 | ++algo_idx) { |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 379 | GRPC_MDELEM_UNREF(channeld->mdelem_compression_algorithms[algo_idx]); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 380 | } |
David Garcia Quintas | c0a0901 | 2015-07-20 01:27:47 -0700 | [diff] [blame] | 381 | GRPC_MDELEM_UNREF(channeld->mdelem_accept_encoding); |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 382 | } |
| 383 | |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 384 | const grpc_channel_filter grpc_compress_filter = { |
| 385 | compress_start_transport_stream_op, |
| 386 | grpc_channel_next_op, |
| 387 | sizeof(call_data), |
| 388 | init_call_elem, |
| 389 | destroy_call_elem, |
| 390 | sizeof(channel_data), |
| 391 | init_channel_elem, |
| 392 | destroy_channel_elem, |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 393 | grpc_call_next_get_peer, |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 394 | "compress"}; |