1 /*
2 *
3 * Copyright 2018 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 <assert.h>
22 #include <limits.h>
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28
29 #include "src/core/ext/filters/http/client_authority_filter.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/slice/slice_internal.h"
33 #include "src/core/lib/slice/slice_string_helpers.h"
34 #include "src/core/lib/surface/call.h"
35 #include "src/core/lib/surface/channel_init.h"
36 #include "src/core/lib/surface/channel_stack_type.h"
37 #include "src/core/lib/transport/static_metadata.h"
38
39 namespace {
40
41 struct call_data {
42 grpc_linked_mdelem authority_storage;
43 grpc_core::CallCombiner* call_combiner;
44 };
45
46 struct channel_data {
47 grpc_core::ManagedMemorySlice default_authority;
48 grpc_mdelem default_authority_mdelem;
49 };
50
client_authority_start_transport_stream_op_batch(grpc_call_element * elem,grpc_transport_stream_op_batch * batch)51 void client_authority_start_transport_stream_op_batch(
52 grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
53 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
54 call_data* calld = static_cast<call_data*>(elem->call_data);
55 // Handle send_initial_metadata.
56 // If the initial metadata doesn't already contain :authority, add it.
57 if (batch->send_initial_metadata &&
58 batch->payload->send_initial_metadata.send_initial_metadata->idx.named
59 .authority == nullptr) {
60 grpc_error* error = grpc_metadata_batch_add_head(
61 batch->payload->send_initial_metadata.send_initial_metadata,
62 &calld->authority_storage,
63 GRPC_MDELEM_REF(chand->default_authority_mdelem), GRPC_BATCH_AUTHORITY);
64 if (error != GRPC_ERROR_NONE) {
65 grpc_transport_stream_op_batch_finish_with_failure(batch, error,
66 calld->call_combiner);
67 return;
68 }
69 }
70 // Pass control down the stack.
71 grpc_call_next_op(elem, batch);
72 }
73
74 /* Constructor for call_data */
client_authority_init_call_elem(grpc_call_element * elem,const grpc_call_element_args * args)75 grpc_error* client_authority_init_call_elem(
76 grpc_call_element* elem, const grpc_call_element_args* args) {
77 call_data* calld = static_cast<call_data*>(elem->call_data);
78 calld->call_combiner = args->call_combiner;
79 return GRPC_ERROR_NONE;
80 }
81
82 /* Destructor for call_data */
client_authority_destroy_call_elem(grpc_call_element *,const grpc_call_final_info *,grpc_closure *)83 void client_authority_destroy_call_elem(
84 grpc_call_element* /*elem*/, const grpc_call_final_info* /*final_info*/,
85 grpc_closure* /*ignored*/) {}
86
87 /* Constructor for channel_data */
client_authority_init_channel_elem(grpc_channel_element * elem,grpc_channel_element_args * args)88 grpc_error* client_authority_init_channel_elem(
89 grpc_channel_element* elem, grpc_channel_element_args* args) {
90 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
91 const grpc_arg* default_authority_arg =
92 grpc_channel_args_find(args->channel_args, GRPC_ARG_DEFAULT_AUTHORITY);
93 if (default_authority_arg == nullptr) {
94 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
95 "GRPC_ARG_DEFAULT_AUTHORITY channel arg. not found. Note that direct "
96 "channels must explicitly specify a value for this argument.");
97 }
98 const char* default_authority_str =
99 grpc_channel_arg_get_string(default_authority_arg);
100 if (default_authority_str == nullptr) {
101 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
102 "GRPC_ARG_DEFAULT_AUTHORITY channel arg. must be a string");
103 }
104 chand->default_authority =
105 grpc_core::ManagedMemorySlice(default_authority_str);
106 chand->default_authority_mdelem = grpc_mdelem_create(
107 GRPC_MDSTR_AUTHORITY, chand->default_authority, nullptr);
108 GPR_ASSERT(!args->is_last);
109 return GRPC_ERROR_NONE;
110 }
111
112 /* Destructor for channel data */
client_authority_destroy_channel_elem(grpc_channel_element * elem)113 void client_authority_destroy_channel_elem(grpc_channel_element* elem) {
114 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
115 grpc_slice_unref_internal(chand->default_authority);
116 GRPC_MDELEM_UNREF(chand->default_authority_mdelem);
117 }
118 } // namespace
119
120 const grpc_channel_filter grpc_client_authority_filter = {
121 client_authority_start_transport_stream_op_batch,
122 grpc_channel_next_op,
123 sizeof(call_data),
124 client_authority_init_call_elem,
125 grpc_call_stack_ignore_set_pollset_or_pollset_set,
126 client_authority_destroy_call_elem,
127 sizeof(channel_data),
128 client_authority_init_channel_elem,
129 client_authority_destroy_channel_elem,
130 grpc_channel_next_get_info,
131 "authority"};
132
add_client_authority_filter(grpc_channel_stack_builder * builder,void * arg)133 static bool add_client_authority_filter(grpc_channel_stack_builder* builder,
134 void* arg) {
135 const grpc_channel_args* channel_args =
136 grpc_channel_stack_builder_get_channel_arguments(builder);
137 const grpc_arg* disable_client_authority_filter_arg = grpc_channel_args_find(
138 channel_args, GRPC_ARG_DISABLE_CLIENT_AUTHORITY_FILTER);
139 if (disable_client_authority_filter_arg != nullptr) {
140 const bool is_client_authority_filter_disabled =
141 grpc_channel_arg_get_bool(disable_client_authority_filter_arg, false);
142 if (is_client_authority_filter_disabled) {
143 return true;
144 }
145 }
146 return grpc_channel_stack_builder_prepend_filter(
147 builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
148 }
149
grpc_client_authority_filter_init(void)150 void grpc_client_authority_filter_init(void) {
151 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, INT_MAX,
152 add_client_authority_filter,
153 (void*)&grpc_client_authority_filter);
154 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX,
155 add_client_authority_filter,
156 (void*)&grpc_client_authority_filter);
157 }
158
grpc_client_authority_filter_shutdown(void)159 void grpc_client_authority_filter_shutdown(void) {}
160