1 //
2 //
3 // Copyright 2016 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 "test/cpp/util/proto_reflection_descriptor_database.h"
20
21 #include <vector>
22
23 #include <grpc/support/log.h>
24
25 #include "src/core/lib/gprpp/crash.h"
26
27 using grpc::reflection::v1alpha::ErrorResponse;
28 using grpc::reflection::v1alpha::ListServiceResponse;
29 using grpc::reflection::v1alpha::ServerReflection;
30 using grpc::reflection::v1alpha::ServerReflectionRequest;
31 using grpc::reflection::v1alpha::ServerReflectionResponse;
32
33 namespace grpc {
34
ProtoReflectionDescriptorDatabase(std::unique_ptr<ServerReflection::Stub> stub)35 ProtoReflectionDescriptorDatabase::ProtoReflectionDescriptorDatabase(
36 std::unique_ptr<ServerReflection::Stub> stub)
37 : stub_(std::move(stub)) {}
38
ProtoReflectionDescriptorDatabase(const std::shared_ptr<grpc::ChannelInterface> & channel)39 ProtoReflectionDescriptorDatabase::ProtoReflectionDescriptorDatabase(
40 const std::shared_ptr<grpc::ChannelInterface>& channel)
41 : stub_(ServerReflection::NewStub(channel)) {}
42
~ProtoReflectionDescriptorDatabase()43 ProtoReflectionDescriptorDatabase::~ProtoReflectionDescriptorDatabase() {
44 if (stream_) {
45 stream_->WritesDone();
46 Status status = stream_->Finish();
47 if (!status.ok()) {
48 if (status.error_code() == StatusCode::UNIMPLEMENTED) {
49 fprintf(stderr,
50 "Reflection request not implemented; "
51 "is the ServerReflection service enabled?\n");
52 } else {
53 fprintf(stderr,
54 "ServerReflectionInfo rpc failed. Error code: %d, message: %s, "
55 "debug info: %s\n",
56 static_cast<int>(status.error_code()),
57 status.error_message().c_str(),
58 ctx_.debug_error_string().c_str());
59 }
60 }
61 }
62 }
63
FindFileByName(const string & filename,protobuf::FileDescriptorProto * output)64 bool ProtoReflectionDescriptorDatabase::FindFileByName(
65 const string& filename, protobuf::FileDescriptorProto* output) {
66 if (cached_db_.FindFileByName(filename, output)) {
67 return true;
68 }
69
70 if (known_files_.find(filename) != known_files_.end()) {
71 return false;
72 }
73
74 ServerReflectionRequest request;
75 request.set_file_by_filename(filename);
76 ServerReflectionResponse response;
77
78 if (!DoOneRequest(request, response)) {
79 return false;
80 }
81
82 if (response.message_response_case() ==
83 ServerReflectionResponse::MessageResponseCase::kFileDescriptorResponse) {
84 AddFileFromResponse(response.file_descriptor_response());
85 } else if (response.message_response_case() ==
86 ServerReflectionResponse::MessageResponseCase::kErrorResponse) {
87 const ErrorResponse& error = response.error_response();
88 if (error.error_code() == StatusCode::NOT_FOUND) {
89 gpr_log(GPR_INFO, "NOT_FOUND from server for FindFileByName(%s)",
90 filename.c_str());
91 } else {
92 gpr_log(GPR_INFO,
93 "Error on FindFileByName(%s)\n\tError code: %d\n"
94 "\tError Message: %s",
95 filename.c_str(), error.error_code(),
96 error.error_message().c_str());
97 }
98 } else {
99 gpr_log(
100 GPR_INFO,
101 "Error on FindFileByName(%s) response type\n"
102 "\tExpecting: %d\n\tReceived: %d",
103 filename.c_str(),
104 ServerReflectionResponse::MessageResponseCase::kFileDescriptorResponse,
105 response.message_response_case());
106 }
107
108 return cached_db_.FindFileByName(filename, output);
109 }
110
FindFileContainingSymbol(const string & symbol_name,protobuf::FileDescriptorProto * output)111 bool ProtoReflectionDescriptorDatabase::FindFileContainingSymbol(
112 const string& symbol_name, protobuf::FileDescriptorProto* output) {
113 if (cached_db_.FindFileContainingSymbol(symbol_name, output)) {
114 return true;
115 }
116
117 if (missing_symbols_.find(symbol_name) != missing_symbols_.end()) {
118 return false;
119 }
120
121 ServerReflectionRequest request;
122 request.set_file_containing_symbol(symbol_name);
123 ServerReflectionResponse response;
124
125 if (!DoOneRequest(request, response)) {
126 return false;
127 }
128
129 if (response.message_response_case() ==
130 ServerReflectionResponse::MessageResponseCase::kFileDescriptorResponse) {
131 AddFileFromResponse(response.file_descriptor_response());
132 } else if (response.message_response_case() ==
133 ServerReflectionResponse::MessageResponseCase::kErrorResponse) {
134 const ErrorResponse& error = response.error_response();
135 if (error.error_code() == StatusCode::NOT_FOUND) {
136 missing_symbols_.insert(symbol_name);
137 gpr_log(GPR_INFO,
138 "NOT_FOUND from server for FindFileContainingSymbol(%s)",
139 symbol_name.c_str());
140 } else {
141 gpr_log(GPR_INFO,
142 "Error on FindFileContainingSymbol(%s)\n"
143 "\tError code: %d\n\tError Message: %s",
144 symbol_name.c_str(), error.error_code(),
145 error.error_message().c_str());
146 }
147 } else {
148 gpr_log(
149 GPR_INFO,
150 "Error on FindFileContainingSymbol(%s) response type\n"
151 "\tExpecting: %d\n\tReceived: %d",
152 symbol_name.c_str(),
153 ServerReflectionResponse::MessageResponseCase::kFileDescriptorResponse,
154 response.message_response_case());
155 }
156 return cached_db_.FindFileContainingSymbol(symbol_name, output);
157 }
158
FindFileContainingExtension(const string & containing_type,int field_number,protobuf::FileDescriptorProto * output)159 bool ProtoReflectionDescriptorDatabase::FindFileContainingExtension(
160 const string& containing_type, int field_number,
161 protobuf::FileDescriptorProto* output) {
162 if (cached_db_.FindFileContainingExtension(containing_type, field_number,
163 output)) {
164 return true;
165 }
166
167 if (missing_extensions_.find(containing_type) != missing_extensions_.end() &&
168 missing_extensions_[containing_type].find(field_number) !=
169 missing_extensions_[containing_type].end()) {
170 gpr_log(GPR_INFO, "nested map.");
171 return false;
172 }
173
174 ServerReflectionRequest request;
175 request.mutable_file_containing_extension()->set_containing_type(
176 containing_type);
177 request.mutable_file_containing_extension()->set_extension_number(
178 field_number);
179 ServerReflectionResponse response;
180
181 if (!DoOneRequest(request, response)) {
182 return false;
183 }
184
185 if (response.message_response_case() ==
186 ServerReflectionResponse::MessageResponseCase::kFileDescriptorResponse) {
187 AddFileFromResponse(response.file_descriptor_response());
188 } else if (response.message_response_case() ==
189 ServerReflectionResponse::MessageResponseCase::kErrorResponse) {
190 const ErrorResponse& error = response.error_response();
191 if (error.error_code() == StatusCode::NOT_FOUND) {
192 if (missing_extensions_.find(containing_type) ==
193 missing_extensions_.end()) {
194 missing_extensions_[containing_type] = {};
195 }
196 missing_extensions_[containing_type].insert(field_number);
197 gpr_log(GPR_INFO,
198 "NOT_FOUND from server for FindFileContainingExtension(%s, %d)",
199 containing_type.c_str(), field_number);
200 } else {
201 gpr_log(GPR_INFO,
202 "Error on FindFileContainingExtension(%s, %d)\n"
203 "\tError code: %d\n\tError Message: %s",
204 containing_type.c_str(), field_number, error.error_code(),
205 error.error_message().c_str());
206 }
207 } else {
208 gpr_log(
209 GPR_INFO,
210 "Error on FindFileContainingExtension(%s, %d) response type\n"
211 "\tExpecting: %d\n\tReceived: %d",
212 containing_type.c_str(), field_number,
213 ServerReflectionResponse::MessageResponseCase::kFileDescriptorResponse,
214 response.message_response_case());
215 }
216
217 return cached_db_.FindFileContainingExtension(containing_type, field_number,
218 output);
219 }
220
FindAllExtensionNumbers(const string & extendee_type,std::vector<int> * output)221 bool ProtoReflectionDescriptorDatabase::FindAllExtensionNumbers(
222 const string& extendee_type, std::vector<int>* output) {
223 if (cached_extension_numbers_.find(extendee_type) !=
224 cached_extension_numbers_.end()) {
225 *output = cached_extension_numbers_[extendee_type];
226 return true;
227 }
228
229 ServerReflectionRequest request;
230 request.set_all_extension_numbers_of_type(extendee_type);
231 ServerReflectionResponse response;
232
233 if (!DoOneRequest(request, response)) {
234 return false;
235 }
236
237 if (response.message_response_case() ==
238 ServerReflectionResponse::MessageResponseCase::
239 kAllExtensionNumbersResponse) {
240 auto number = response.all_extension_numbers_response().extension_number();
241 *output = std::vector<int>(number.begin(), number.end());
242 cached_extension_numbers_[extendee_type] = *output;
243 return true;
244 } else if (response.message_response_case() ==
245 ServerReflectionResponse::MessageResponseCase::kErrorResponse) {
246 const ErrorResponse& error = response.error_response();
247 if (error.error_code() == StatusCode::NOT_FOUND) {
248 gpr_log(GPR_INFO, "NOT_FOUND from server for FindAllExtensionNumbers(%s)",
249 extendee_type.c_str());
250 } else {
251 gpr_log(GPR_INFO,
252 "Error on FindAllExtensionNumbersExtension(%s)\n"
253 "\tError code: %d\n\tError Message: %s",
254 extendee_type.c_str(), error.error_code(),
255 error.error_message().c_str());
256 }
257 }
258 return false;
259 }
260
GetServices(std::vector<std::string> * output)261 bool ProtoReflectionDescriptorDatabase::GetServices(
262 std::vector<std::string>* output) {
263 ServerReflectionRequest request;
264 request.set_list_services("");
265 ServerReflectionResponse response;
266
267 if (!DoOneRequest(request, response)) {
268 return false;
269 }
270
271 if (response.message_response_case() ==
272 ServerReflectionResponse::MessageResponseCase::kListServicesResponse) {
273 const ListServiceResponse& ls_response = response.list_services_response();
274 for (int i = 0; i < ls_response.service_size(); ++i) {
275 (*output).push_back(ls_response.service(i).name());
276 }
277 return true;
278 } else if (response.message_response_case() ==
279 ServerReflectionResponse::MessageResponseCase::kErrorResponse) {
280 const ErrorResponse& error = response.error_response();
281 gpr_log(GPR_INFO,
282 "Error on GetServices()\n\tError code: %d\n"
283 "\tError Message: %s",
284 error.error_code(), error.error_message().c_str());
285 } else {
286 gpr_log(
287 GPR_INFO,
288 "Error on GetServices() response type\n\tExpecting: %d\n\tReceived: %d",
289 ServerReflectionResponse::MessageResponseCase::kListServicesResponse,
290 response.message_response_case());
291 }
292 return false;
293 }
294
295 protobuf::FileDescriptorProto
ParseFileDescriptorProtoResponse(const std::string & byte_fd_proto)296 ProtoReflectionDescriptorDatabase::ParseFileDescriptorProtoResponse(
297 const std::string& byte_fd_proto) {
298 protobuf::FileDescriptorProto file_desc_proto;
299 file_desc_proto.ParseFromString(byte_fd_proto);
300 return file_desc_proto;
301 }
302
AddFileFromResponse(const grpc::reflection::v1alpha::FileDescriptorResponse & response)303 void ProtoReflectionDescriptorDatabase::AddFileFromResponse(
304 const grpc::reflection::v1alpha::FileDescriptorResponse& response) {
305 for (int i = 0; i < response.file_descriptor_proto_size(); ++i) {
306 const protobuf::FileDescriptorProto file_proto =
307 ParseFileDescriptorProtoResponse(response.file_descriptor_proto(i));
308 if (known_files_.find(file_proto.name()) == known_files_.end()) {
309 known_files_.insert(file_proto.name());
310 cached_db_.Add(file_proto);
311 }
312 }
313 }
314
315 std::shared_ptr<ProtoReflectionDescriptorDatabase::ClientStream>
GetStream()316 ProtoReflectionDescriptorDatabase::GetStream() {
317 if (!stream_) {
318 stream_ = stub_->ServerReflectionInfo(&ctx_);
319 }
320 return stream_;
321 }
322
DoOneRequest(const ServerReflectionRequest & request,ServerReflectionResponse & response)323 bool ProtoReflectionDescriptorDatabase::DoOneRequest(
324 const ServerReflectionRequest& request,
325 ServerReflectionResponse& response) {
326 bool success = false;
327 stream_mutex_.lock();
328 if (GetStream()->Write(request) && GetStream()->Read(&response)) {
329 success = true;
330 }
331 stream_mutex_.unlock();
332 return success;
333 }
334
335 } // namespace grpc
336