1 //
2 //
3 // Copyright 2015 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 <stdlib.h>
20
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include "absl/strings/str_format.h"
28
29 #include <grpc/compression.h>
30 #include <grpc/grpc.h>
31 #include <grpc/impl/compression_types.h>
32 #include <grpc/status.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35 #include <grpc/support/time.h>
36 #include <grpcpp/channel.h>
37 #include <grpcpp/client_context.h>
38 #include <grpcpp/impl/interceptor_common.h>
39 #include <grpcpp/impl/sync.h>
40 #include <grpcpp/security/credentials.h>
41 #include <grpcpp/server_context.h>
42 #include <grpcpp/support/client_interceptor.h>
43
44 #include "src/core/lib/gprpp/crash.h"
45
46 namespace grpc {
47
48 class Channel;
49
50 class DefaultGlobalClientCallbacks final
51 : public ClientContext::GlobalCallbacks {
52 public:
~DefaultGlobalClientCallbacks()53 ~DefaultGlobalClientCallbacks() override {}
DefaultConstructor(ClientContext *)54 void DefaultConstructor(ClientContext* /*context*/) override {}
Destructor(ClientContext *)55 void Destructor(ClientContext* /*context*/) override {}
56 };
57
58 static DefaultGlobalClientCallbacks* g_default_client_callbacks =
59 new DefaultGlobalClientCallbacks();
60 static ClientContext::GlobalCallbacks* g_client_callbacks =
61 g_default_client_callbacks;
62
ClientContext()63 ClientContext::ClientContext()
64 : initial_metadata_received_(false),
65 wait_for_ready_(false),
66 wait_for_ready_explicitly_set_(false),
67 call_(nullptr),
68 call_canceled_(false),
69 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
70 census_context_(nullptr),
71 propagate_from_call_(nullptr),
72 compression_algorithm_(GRPC_COMPRESS_NONE),
73 initial_metadata_corked_(false) {
74 g_client_callbacks->DefaultConstructor(this);
75 }
76
~ClientContext()77 ClientContext::~ClientContext() {
78 if (call_) {
79 grpc_call_unref(call_);
80 call_ = nullptr;
81 }
82 g_client_callbacks->Destructor(this);
83 }
84
set_credentials(const std::shared_ptr<CallCredentials> & creds)85 void ClientContext::set_credentials(
86 const std::shared_ptr<CallCredentials>& creds) {
87 creds_ = creds;
88 // If call_ is set, we have already created the call, and set the call
89 // credentials. This should only be done before we have started the batch
90 // for sending initial metadata.
91 if (creds_ != nullptr && call_ != nullptr) {
92 if (!creds_->ApplyToCall(call_)) {
93 SendCancelToInterceptors();
94 grpc_call_cancel_with_status(call_, GRPC_STATUS_CANCELLED,
95 "Failed to set credentials to rpc.",
96 nullptr);
97 }
98 }
99 }
100
FromInternalServerContext(const grpc::ServerContextBase & context,PropagationOptions options)101 std::unique_ptr<ClientContext> ClientContext::FromInternalServerContext(
102 const grpc::ServerContextBase& context, PropagationOptions options) {
103 std::unique_ptr<ClientContext> ctx(new ClientContext);
104 ctx->propagate_from_call_ = context.call_.call;
105 ctx->propagation_options_ = options;
106 return ctx;
107 }
108
FromServerContext(const grpc::ServerContextBase & server_context,PropagationOptions options)109 std::unique_ptr<ClientContext> ClientContext::FromServerContext(
110 const grpc::ServerContextBase& server_context, PropagationOptions options) {
111 return FromInternalServerContext(server_context, options);
112 }
113
FromCallbackServerContext(const grpc::CallbackServerContext & server_context,PropagationOptions options)114 std::unique_ptr<ClientContext> ClientContext::FromCallbackServerContext(
115 const grpc::CallbackServerContext& server_context,
116 PropagationOptions options) {
117 return FromInternalServerContext(server_context, options);
118 }
119
AddMetadata(const std::string & meta_key,const std::string & meta_value)120 void ClientContext::AddMetadata(const std::string& meta_key,
121 const std::string& meta_value) {
122 send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
123 }
124
set_call(grpc_call * call,const std::shared_ptr<Channel> & channel)125 void ClientContext::set_call(grpc_call* call,
126 const std::shared_ptr<Channel>& channel) {
127 internal::MutexLock lock(&mu_);
128 GPR_ASSERT(call_ == nullptr);
129 call_ = call;
130 channel_ = channel;
131 if (creds_ && !creds_->ApplyToCall(call_)) {
132 // TODO(yashykt): should interceptors also see this status?
133 SendCancelToInterceptors();
134 grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
135 "Failed to set credentials to rpc.", nullptr);
136 }
137 if (call_canceled_) {
138 SendCancelToInterceptors();
139 grpc_call_cancel(call_, nullptr);
140 }
141 }
142
set_compression_algorithm(grpc_compression_algorithm algorithm)143 void ClientContext::set_compression_algorithm(
144 grpc_compression_algorithm algorithm) {
145 compression_algorithm_ = algorithm;
146 const char* algorithm_name = nullptr;
147 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
148 grpc_core::Crash(absl::StrFormat(
149 "Name for compression algorithm '%d' unknown.", algorithm));
150 }
151 GPR_ASSERT(algorithm_name != nullptr);
152 AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
153 }
154
TryCancel()155 void ClientContext::TryCancel() {
156 internal::MutexLock lock(&mu_);
157 if (call_) {
158 SendCancelToInterceptors();
159 grpc_call_cancel(call_, nullptr);
160 } else {
161 call_canceled_ = true;
162 }
163 }
164
SendCancelToInterceptors()165 void ClientContext::SendCancelToInterceptors() {
166 internal::CancelInterceptorBatchMethods cancel_methods;
167 for (size_t i = 0; i < rpc_info_.interceptors_.size(); i++) {
168 rpc_info_.RunInterceptor(&cancel_methods, i);
169 }
170 }
171
peer() const172 std::string ClientContext::peer() const {
173 std::string peer;
174 if (call_) {
175 char* c_peer = grpc_call_get_peer(call_);
176 peer = c_peer;
177 gpr_free(c_peer);
178 }
179 return peer;
180 }
181
SetGlobalCallbacks(GlobalCallbacks * client_callbacks)182 void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
183 GPR_ASSERT(g_client_callbacks == g_default_client_callbacks);
184 GPR_ASSERT(client_callbacks != nullptr);
185 GPR_ASSERT(client_callbacks != g_default_client_callbacks);
186 g_client_callbacks = client_callbacks;
187 }
188
189 } // namespace grpc
190