Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / ext / filters / http / http_filters_plugin.cc
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include <string.h>
22
23 #include "src/core/ext/filters/http/client/http_client_filter.h"
24 #include "src/core/ext/filters/http/message_compress/message_compress_filter.h"
25 #include "src/core/ext/filters/http/server/http_server_filter.h"
26 #include "src/core/lib/channel/channel_stack_builder.h"
27 #include "src/core/lib/surface/call.h"
28 #include "src/core/lib/surface/channel_init.h"
29 #include "src/core/lib/transport/transport_impl.h"
30
31 typedef struct {
32   const grpc_channel_filter* filter;
33   const char* control_channel_arg;
34 } optional_filter;
35
36 static optional_filter compress_filter = {
37     &grpc_message_compress_filter, GRPC_ARG_ENABLE_PER_MESSAGE_COMPRESSION};
38
39 static bool is_building_http_like_transport(
40     grpc_channel_stack_builder* builder) {
41   grpc_transport* t = grpc_channel_stack_builder_get_transport(builder);
42   return t != nullptr && strstr(t->vtable->name, "http");
43 }
44
45 static bool maybe_add_optional_filter(grpc_channel_stack_builder* builder,
46                                       void* arg) {
47   if (!is_building_http_like_transport(builder)) return true;
48   optional_filter* filtarg = static_cast<optional_filter*>(arg);
49   const grpc_channel_args* channel_args =
50       grpc_channel_stack_builder_get_channel_arguments(builder);
51   bool enable = grpc_channel_arg_get_bool(
52       grpc_channel_args_find(channel_args, filtarg->control_channel_arg),
53       !grpc_channel_args_want_minimal_stack(channel_args));
54   return enable ? grpc_channel_stack_builder_prepend_filter(
55                       builder, filtarg->filter, nullptr, nullptr)
56                 : true;
57 }
58
59 static bool maybe_add_required_filter(grpc_channel_stack_builder* builder,
60                                       void* arg) {
61   return is_building_http_like_transport(builder)
62              ? grpc_channel_stack_builder_prepend_filter(
63                    builder, static_cast<const grpc_channel_filter*>(arg),
64                    nullptr, nullptr)
65              : true;
66 }
67
68 void grpc_http_filters_init(void) {
69   grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
70                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
71                                    maybe_add_optional_filter, &compress_filter);
72   grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
73                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
74                                    maybe_add_optional_filter, &compress_filter);
75   grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
76                                    GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
77                                    maybe_add_optional_filter, &compress_filter);
78   grpc_channel_init_register_stage(
79       GRPC_CLIENT_SUBCHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
80       maybe_add_required_filter, (void*)&grpc_http_client_filter);
81   grpc_channel_init_register_stage(
82       GRPC_CLIENT_DIRECT_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
83       maybe_add_required_filter, (void*)&grpc_http_client_filter);
84   grpc_channel_init_register_stage(
85       GRPC_SERVER_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
86       maybe_add_required_filter, (void*)&grpc_http_server_filter);
87 }
88
89 void grpc_http_filters_shutdown(void) {}