3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
24 #include <grpc/compression.h>
26 #include "src/core/lib/compression/algorithm_metadata.h"
27 #include "src/core/lib/compression/compression_internal.h"
28 #include "src/core/lib/gpr/useful.h"
29 #include "src/core/lib/slice/slice_utils.h"
30 #include "src/core/lib/surface/api_trace.h"
31 #include "src/core/lib/transport/static_metadata.h"
33 int grpc_compression_algorithm_is_message(
34 grpc_compression_algorithm algorithm) {
35 return (algorithm >= GRPC_COMPRESS_DEFLATE && algorithm <= GRPC_COMPRESS_GZIP)
40 int grpc_compression_algorithm_is_stream(grpc_compression_algorithm algorithm) {
41 return (algorithm == GRPC_COMPRESS_STREAM_GZIP) ? 1 : 0;
44 int grpc_compression_algorithm_parse(grpc_slice name,
45 grpc_compression_algorithm* algorithm) {
46 if (grpc_slice_eq_static_interned(name, GRPC_MDSTR_IDENTITY)) {
47 *algorithm = GRPC_COMPRESS_NONE;
49 } else if (grpc_slice_eq_static_interned(name, GRPC_MDSTR_DEFLATE)) {
50 *algorithm = GRPC_COMPRESS_DEFLATE;
52 } else if (grpc_slice_eq_static_interned(name, GRPC_MDSTR_GZIP)) {
53 *algorithm = GRPC_COMPRESS_GZIP;
55 } else if (grpc_slice_eq_static_interned(name,
56 GRPC_MDSTR_STREAM_SLASH_GZIP)) {
57 *algorithm = GRPC_COMPRESS_STREAM_GZIP;
64 int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
66 GRPC_API_TRACE("grpc_compression_algorithm_name(algorithm=%d, name=%p)", 2,
67 ((int)algorithm, name));
69 case GRPC_COMPRESS_NONE:
72 case GRPC_COMPRESS_DEFLATE:
75 case GRPC_COMPRESS_GZIP:
78 case GRPC_COMPRESS_STREAM_GZIP:
79 *name = "stream/gzip";
81 case GRPC_COMPRESS_ALGORITHMS_COUNT:
87 grpc_compression_algorithm grpc_compression_algorithm_for_level(
88 grpc_compression_level level, uint32_t accepted_encodings) {
89 grpc_compression_algorithm algo;
90 if (level == GRPC_COMPRESS_LEVEL_NONE) {
91 return GRPC_COMPRESS_NONE;
92 } else if (level <= GRPC_COMPRESS_LEVEL_HIGH) {
93 // TODO(mxyan): Design algorithm to select from all algorithms, including
94 // stream compression algorithm
95 if (!grpc_compression_algorithm_from_message_stream_compression_algorithm(
97 grpc_message_compression_algorithm_for_level(
99 grpc_compression_bitset_to_message_bitset(accepted_encodings)),
100 static_cast<grpc_stream_compression_algorithm>(0))) {
101 gpr_log(GPR_ERROR, "Parse compression level error");
102 return GRPC_COMPRESS_NONE;
106 gpr_log(GPR_ERROR, "Unknown compression level: %d", level);
107 return GRPC_COMPRESS_NONE;
111 void grpc_compression_options_init(grpc_compression_options* opts) {
112 memset(opts, 0, sizeof(*opts));
113 /* all enabled by default */
114 opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
117 void grpc_compression_options_enable_algorithm(
118 grpc_compression_options* opts, grpc_compression_algorithm algorithm) {
119 GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm);
122 void grpc_compression_options_disable_algorithm(
123 grpc_compression_options* opts, grpc_compression_algorithm algorithm) {
124 GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm);
127 int grpc_compression_options_is_algorithm_enabled(
128 const grpc_compression_options* opts,
129 grpc_compression_algorithm algorithm) {
130 return grpc_compression_options_is_algorithm_enabled_internal(opts,
134 grpc_slice grpc_compression_algorithm_slice(
135 grpc_compression_algorithm algorithm) {
137 case GRPC_COMPRESS_NONE:
138 return GRPC_MDSTR_IDENTITY;
139 case GRPC_COMPRESS_DEFLATE:
140 return GRPC_MDSTR_DEFLATE;
141 case GRPC_COMPRESS_GZIP:
142 return GRPC_MDSTR_GZIP;
143 case GRPC_COMPRESS_STREAM_GZIP:
144 return GRPC_MDSTR_STREAM_SLASH_GZIP;
145 case GRPC_COMPRESS_ALGORITHMS_COUNT:
146 return grpc_empty_slice();
148 return grpc_empty_slice();
151 grpc_compression_algorithm grpc_compression_algorithm_from_slice(
152 const grpc_slice& str) {
153 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_IDENTITY))
154 return GRPC_COMPRESS_NONE;
155 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_DEFLATE))
156 return GRPC_COMPRESS_DEFLATE;
157 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP))
158 return GRPC_COMPRESS_GZIP;
159 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_STREAM_SLASH_GZIP))
160 return GRPC_COMPRESS_STREAM_GZIP;
161 return GRPC_COMPRESS_ALGORITHMS_COUNT;
164 grpc_mdelem grpc_compression_encoding_mdelem(
165 grpc_compression_algorithm algorithm) {
167 case GRPC_COMPRESS_NONE:
168 return GRPC_MDELEM_GRPC_ENCODING_IDENTITY;
169 case GRPC_COMPRESS_DEFLATE:
170 return GRPC_MDELEM_GRPC_ENCODING_DEFLATE;
171 case GRPC_COMPRESS_GZIP:
172 return GRPC_MDELEM_GRPC_ENCODING_GZIP;
173 case GRPC_COMPRESS_STREAM_GZIP:
174 return GRPC_MDELEM_GRPC_ENCODING_GZIP;