|
|
|
@ -23,58 +23,41 @@ |
|
|
|
|
|
|
|
|
|
#include <grpcpp/grpcpp.h> |
|
|
|
|
#include <grpcpp/support/interceptor.h> |
|
|
|
|
#include <grpcpp/support/sync_stream.h> |
|
|
|
|
|
|
|
|
|
// IWYU pragma: no_include "google/protobuf/descriptor.h"
|
|
|
|
|
// IWYU pragma: no_include <google/protobuf/descriptor.h>
|
|
|
|
|
|
|
|
|
|
using grpc::reflection::v1alpha::ErrorResponse; |
|
|
|
|
using grpc::reflection::v1alpha::ExtensionNumberResponse; |
|
|
|
|
using grpc::reflection::v1alpha::ExtensionRequest; |
|
|
|
|
using grpc::reflection::v1alpha::ListServiceResponse; |
|
|
|
|
using grpc::reflection::v1alpha::ServerReflectionRequest; |
|
|
|
|
using grpc::reflection::v1alpha::ServerReflectionResponse; |
|
|
|
|
using grpc::reflection::v1alpha::ServiceResponse; |
|
|
|
|
// IWYU pragma: no_include "src/proto/grpc/reflection/v1/reflection.pb.h"
|
|
|
|
|
// IWYU pragma: no_include "src/proto/grpc/reflection/v1alpha/reflection.pb.h"
|
|
|
|
|
|
|
|
|
|
namespace grpc { |
|
|
|
|
|
|
|
|
|
ProtoServerReflection::ProtoServerReflection() |
|
|
|
|
: descriptor_pool_(protobuf::DescriptorPool::generated_pool()) {} |
|
|
|
|
|
|
|
|
|
void ProtoServerReflection::SetServiceList( |
|
|
|
|
const std::vector<std::string>* services) { |
|
|
|
|
services_ = services; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflection::ServerReflectionInfo( |
|
|
|
|
ServerContext* context, |
|
|
|
|
ServerReaderWriter<ServerReflectionResponse, ServerReflectionRequest>* |
|
|
|
|
stream) { |
|
|
|
|
ServerReflectionRequest request; |
|
|
|
|
ServerReflectionResponse response; |
|
|
|
|
template <typename Request, typename Response> |
|
|
|
|
Status ProtoServerReflectionBackend::ServerReflectionInfo( |
|
|
|
|
ServerReaderWriter<Response, Request>* stream) const { |
|
|
|
|
Request request; |
|
|
|
|
Response response; |
|
|
|
|
Status status; |
|
|
|
|
while (stream->Read(&request)) { |
|
|
|
|
switch (request.message_request_case()) { |
|
|
|
|
case ServerReflectionRequest::MessageRequestCase::kFileByFilename: |
|
|
|
|
status = GetFileByName(context, request.file_by_filename(), &response); |
|
|
|
|
case Request::MessageRequestCase::kFileByFilename: |
|
|
|
|
status = GetFileByName(request.file_by_filename(), &response); |
|
|
|
|
break; |
|
|
|
|
case ServerReflectionRequest::MessageRequestCase::kFileContainingSymbol: |
|
|
|
|
status = GetFileContainingSymbol( |
|
|
|
|
context, request.file_containing_symbol(), &response); |
|
|
|
|
case Request::MessageRequestCase::kFileContainingSymbol: |
|
|
|
|
status = GetFileContainingSymbol(request.file_containing_symbol(), |
|
|
|
|
&response); |
|
|
|
|
break; |
|
|
|
|
case ServerReflectionRequest::MessageRequestCase:: |
|
|
|
|
kFileContainingExtension: |
|
|
|
|
case Request::MessageRequestCase::kFileContainingExtension: |
|
|
|
|
status = GetFileContainingExtension( |
|
|
|
|
context, &request.file_containing_extension(), &response); |
|
|
|
|
&request.file_containing_extension(), &response); |
|
|
|
|
break; |
|
|
|
|
case ServerReflectionRequest::MessageRequestCase:: |
|
|
|
|
kAllExtensionNumbersOfType: |
|
|
|
|
case Request::MessageRequestCase::kAllExtensionNumbersOfType: |
|
|
|
|
status = GetAllExtensionNumbers( |
|
|
|
|
context, request.all_extension_numbers_of_type(), |
|
|
|
|
request.all_extension_numbers_of_type(), |
|
|
|
|
response.mutable_all_extension_numbers_response()); |
|
|
|
|
break; |
|
|
|
|
case ServerReflectionRequest::MessageRequestCase::kListServices: |
|
|
|
|
status = |
|
|
|
|
ListService(context, response.mutable_list_services_response()); |
|
|
|
|
case Request::MessageRequestCase::kListServices: |
|
|
|
|
status = ListService(response.mutable_list_services_response()); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
status = Status(StatusCode::UNIMPLEMENTED, ""); |
|
|
|
@ -84,35 +67,34 @@ Status ProtoServerReflection::ServerReflectionInfo( |
|
|
|
|
FillErrorResponse(status, response.mutable_error_response()); |
|
|
|
|
} |
|
|
|
|
response.set_valid_host(request.host()); |
|
|
|
|
response.set_allocated_original_request( |
|
|
|
|
new ServerReflectionRequest(request)); |
|
|
|
|
response.set_allocated_original_request(new Request(request)); |
|
|
|
|
stream->Write(response); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ProtoServerReflection::FillErrorResponse(const Status& status, |
|
|
|
|
ErrorResponse* error_response) { |
|
|
|
|
template <typename Response> |
|
|
|
|
void ProtoServerReflectionBackend::FillErrorResponse( |
|
|
|
|
const Status& status, Response* error_response) const { |
|
|
|
|
error_response->set_error_code(status.error_code()); |
|
|
|
|
error_response->set_error_message(status.error_message()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflection::ListService(ServerContext* /*context*/, |
|
|
|
|
ListServiceResponse* response) { |
|
|
|
|
template <typename Response> |
|
|
|
|
Status ProtoServerReflectionBackend::ListService(Response* response) const { |
|
|
|
|
if (services_ == nullptr) { |
|
|
|
|
return Status(StatusCode::NOT_FOUND, "Services not found."); |
|
|
|
|
} |
|
|
|
|
for (const auto& value : *services_) { |
|
|
|
|
ServiceResponse* service_response = response->add_service(); |
|
|
|
|
auto* service_response = response->add_service(); |
|
|
|
|
service_response->set_name(value); |
|
|
|
|
} |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflection::GetFileByName( |
|
|
|
|
ServerContext* /*context*/, const std::string& file_name, |
|
|
|
|
ServerReflectionResponse* response) { |
|
|
|
|
template <typename Response> |
|
|
|
|
Status ProtoServerReflectionBackend::GetFileByName(const std::string& file_name, |
|
|
|
|
Response* response) const { |
|
|
|
|
if (descriptor_pool_ == nullptr) { |
|
|
|
|
return Status::CANCELLED; |
|
|
|
|
} |
|
|
|
@ -127,9 +109,9 @@ Status ProtoServerReflection::GetFileByName( |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflection::GetFileContainingSymbol( |
|
|
|
|
ServerContext* /*context*/, const std::string& symbol, |
|
|
|
|
ServerReflectionResponse* response) { |
|
|
|
|
template <typename Response> |
|
|
|
|
Status ProtoServerReflectionBackend::GetFileContainingSymbol( |
|
|
|
|
const std::string& symbol, Response* response) const { |
|
|
|
|
if (descriptor_pool_ == nullptr) { |
|
|
|
|
return Status::CANCELLED; |
|
|
|
|
} |
|
|
|
@ -144,9 +126,9 @@ Status ProtoServerReflection::GetFileContainingSymbol( |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflection::GetFileContainingExtension( |
|
|
|
|
ServerContext* /*context*/, const ExtensionRequest* request, |
|
|
|
|
ServerReflectionResponse* response) { |
|
|
|
|
template <typename Request, typename Response> |
|
|
|
|
Status ProtoServerReflectionBackend::GetFileContainingExtension( |
|
|
|
|
const Request* request, Response* response) const { |
|
|
|
|
if (descriptor_pool_ == nullptr) { |
|
|
|
|
return Status::CANCELLED; |
|
|
|
|
} |
|
|
|
@ -168,9 +150,9 @@ Status ProtoServerReflection::GetFileContainingExtension( |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflection::GetAllExtensionNumbers( |
|
|
|
|
ServerContext* /*context*/, const std::string& type, |
|
|
|
|
ExtensionNumberResponse* response) { |
|
|
|
|
template <typename Response> |
|
|
|
|
Status ProtoServerReflectionBackend::GetAllExtensionNumbers( |
|
|
|
|
const std::string& type, Response* response) const { |
|
|
|
|
if (descriptor_pool_ == nullptr) { |
|
|
|
|
return Status::CANCELLED; |
|
|
|
|
} |
|
|
|
@ -190,10 +172,10 @@ Status ProtoServerReflection::GetAllExtensionNumbers( |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ProtoServerReflection::FillFileDescriptorResponse( |
|
|
|
|
const protobuf::FileDescriptor* file_desc, |
|
|
|
|
ServerReflectionResponse* response, |
|
|
|
|
std::unordered_set<std::string>* seen_files) { |
|
|
|
|
template <typename Response> |
|
|
|
|
void ProtoServerReflectionBackend::FillFileDescriptorResponse( |
|
|
|
|
const protobuf::FileDescriptor* file_desc, Response* response, |
|
|
|
|
std::unordered_set<std::string>* seen_files) const { |
|
|
|
|
if (seen_files->find(file_desc->name()) != seen_files->end()) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
@ -210,4 +192,18 @@ void ProtoServerReflection::FillFileDescriptorResponse( |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflection::ServerReflectionInfo( |
|
|
|
|
ServerContext* /* context */, |
|
|
|
|
ServerReaderWriter<reflection::v1alpha::ServerReflectionResponse, |
|
|
|
|
reflection::v1alpha::ServerReflectionRequest>* stream) { |
|
|
|
|
return backend_->ServerReflectionInfo(stream); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status ProtoServerReflectionV1::ServerReflectionInfo( |
|
|
|
|
ServerContext* /* context */, |
|
|
|
|
ServerReaderWriter<reflection::v1::ServerReflectionResponse, |
|
|
|
|
reflection::v1::ServerReflectionRequest>* stream) { |
|
|
|
|
return backend_->ServerReflectionInfo(stream); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace grpc
|
|
|
|
|