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