3 * Copyright 2018 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>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
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"
42 grpc_linked_mdelem authority_storage;
43 grpc_core::CallCombiner* call_combiner;
47 grpc_core::ManagedMemorySlice default_authority;
48 grpc_mdelem default_authority_mdelem;
51 void 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 auto* initial_metadata =
57 batch->payload->send_initial_metadata.send_initial_metadata;
58 // If the initial metadata doesn't already contain :authority, add it.
59 if (batch->send_initial_metadata &&
60 initial_metadata->idx.named.authority == nullptr) {
61 grpc_error* error = grpc_metadata_batch_add_head(
62 initial_metadata, &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);
70 // Pass control down the stack.
71 grpc_call_next_op(elem, batch);
74 /* Constructor for call_data */
75 grpc_error* init_call_elem(grpc_call_element* elem,
76 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;
82 /* Destructor for call_data */
83 void destroy_call_elem(grpc_call_element* elem,
84 const grpc_call_final_info* final_info,
85 grpc_closure* ignored) {}
87 /* Constructor for channel_data */
88 grpc_error* init_channel_elem(grpc_channel_element* elem,
89 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.");
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");
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;
112 /* Destructor for channel data */
113 void 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);
120 const grpc_channel_filter grpc_client_authority_filter = {
121 authority_start_transport_stream_op_batch,
122 grpc_channel_next_op,
125 grpc_call_stack_ignore_set_pollset_or_pollset_set,
127 sizeof(channel_data),
129 destroy_channel_elem,
130 grpc_channel_next_get_info,
133 static bool add_client_authority_filter(grpc_channel_stack_builder* builder,
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) {
146 return grpc_channel_stack_builder_prepend_filter(
147 builder, static_cast<const grpc_channel_filter*>(arg), nullptr, nullptr);
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);
159 void grpc_client_authority_filter_shutdown(void) {}