| 1 |
// |
| 2 |
// Copyright 2015 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/core/lib/resolver/resolver_registry.h" |
| 20 |
|
| 21 |
#include "absl/status/status.h" |
| 22 |
#include "absl/status/statusor.h" |
| 23 |
#include "absl/strings/str_cat.h" |
| 24 |
#include "absl/strings/str_format.h" |
| 25 |
|
| 26 |
#include <grpc/support/log.h> |
| 27 |
|
| 28 |
namespace grpc_core { |
| 29 |
|
| 30 |
// |
| 31 |
// ResolverRegistry::Builder |
| 32 |
// |
| 33 |
|
| 34 |
ResolverRegistry::Builder::Builder() { Reset(); } |
| 35 |
|
| 36 |
void ResolverRegistry::Builder::SetDefaultPrefix(std::string default_prefix) { |
| 37 |
state_.default_prefix = std::move(default_prefix); |
| 38 |
} |
| 39 |
|
| 40 |
void ResolverRegistry::Builder::RegisterResolverFactory( |
| 41 |
std::unique_ptr<ResolverFactory> factory) { |
| 42 |
auto p = state_.factories.emplace(factory->scheme(), std::move(factory)); |
| 43 |
GPR_ASSERT(p.second); |
| 44 |
} |
| 45 |
|
| 46 |
bool ResolverRegistry::Builder::HasResolverFactory( |
| 47 |
absl::string_view scheme) const { |
| 48 |
return state_.factories.find(scheme) != state_.factories.end(); |
| 49 |
} |
| 50 |
|
| 51 |
void ResolverRegistry::Builder::Reset() { |
| 52 |
state_.factories.clear(); |
| 53 |
state_.default_prefix = "dns:///"; |
| 54 |
} |
| 55 |
|
| 56 |
ResolverRegistry ResolverRegistry::Builder::Build() { |
| 57 |
return ResolverRegistry(std::move(state_)); |
| 58 |
} |
| 59 |
|
| 60 |
// |
| 61 |
// ResolverRegistry |
| 62 |
// |
| 63 |
|
| 64 |
bool ResolverRegistry::IsValidTarget(absl::string_view target) const { |
| 65 |
std::string canonical_target; |
| 66 |
URI uri; |
| 67 |
ResolverFactory* factory = |
| 68 |
FindResolverFactory(target, &uri, &canonical_target); |
| 69 |
if (factory == nullptr) return false; |
| 70 |
return factory->IsValidUri(uri); |
| 71 |
} |
| 72 |
|
| 73 |
OrphanablePtr<Resolver> ResolverRegistry::CreateResolver( |
| 74 |
absl::string_view target, const ChannelArgs& args, |
| 75 |
grpc_pollset_set* pollset_set, |
| 76 |
std::shared_ptr<WorkSerializer> work_serializer, |
| 77 |
std::unique_ptr<Resolver::ResultHandler> result_handler) const { |
| 78 |
std::string canonical_target; |
| 79 |
ResolverArgs resolver_args; |
| 80 |
ResolverFactory* factory = |
| 81 |
FindResolverFactory(target, &resolver_args.uri, &canonical_target); |
| 82 |
if (factory == nullptr) return nullptr; |
| 83 |
resolver_args.args = args; |
| 84 |
resolver_args.pollset_set = pollset_set; |
| 85 |
resolver_args.work_serializer = std::move(work_serializer); |
| 86 |
resolver_args.result_handler = std::move(result_handler); |
| 87 |
return factory->CreateResolver(std::move(resolver_args)); |
| 88 |
} |
| 89 |
|
| 90 |
std::string ResolverRegistry::GetDefaultAuthority( |
| 91 |
absl::string_view target) const { |
| 92 |
std::string canonical_target; |
| 93 |
URI uri; |
| 94 |
ResolverFactory* factory = |
| 95 |
FindResolverFactory(target, &uri, &canonical_target); |
| 96 |
if (factory == nullptr) return ""; |
| 97 |
return factory->GetDefaultAuthority(uri); |
| 98 |
} |
| 99 |
|
| 100 |
std::string ResolverRegistry::AddDefaultPrefixIfNeeded( |
| 101 |
absl::string_view target) const { |
| 102 |
std::string canonical_target; |
| 103 |
URI uri; |
| 104 |
FindResolverFactory(target, &uri, &canonical_target); |
| 105 |
return canonical_target.empty() ? std::string(target) : canonical_target; |
| 106 |
} |
| 107 |
|
| 108 |
ResolverFactory* ResolverRegistry::LookupResolverFactory( |
| 109 |
absl::string_view scheme) const { |
| 110 |
auto it = state_.factories.find(scheme); |
| 111 |
if (it == state_.factories.end()) return nullptr; |
| 112 |
return it->second.get(); |
| 113 |
} |
| 114 |
|
| 115 |
// Returns the factory for the scheme of \a target. If \a target does |
| 116 |
// not parse as a URI, prepends \a default_prefix_ and tries again. |
| 117 |
// If URI parsing is successful (in either attempt), sets \a uri to |
| 118 |
// point to the parsed URI. |
| 119 |
ResolverFactory* ResolverRegistry::FindResolverFactory( |
| 120 |
absl::string_view target, URI* uri, std::string* canonical_target) const { |
| 121 |
GPR_ASSERT(uri != nullptr); |
| 122 |
absl::StatusOr<URI> tmp_uri = URI::Parse(target); |
| 123 |
ResolverFactory* factory = |
| 124 |
tmp_uri.ok() ? LookupResolverFactory(tmp_uri->scheme()) : nullptr; |
| 125 |
if (factory != nullptr) { |
| 126 |
*uri = std::move(*tmp_uri); |
| 127 |
return factory; |
| 128 |
} |
| 129 |
*canonical_target = absl::StrCat(state_.default_prefix, target); |
| 130 |
absl::StatusOr<URI> tmp_uri2 = URI::Parse(*canonical_target); |
| 131 |
factory = tmp_uri2.ok() ? LookupResolverFactory(tmp_uri2->scheme()) : nullptr; |
| 132 |
if (factory != nullptr) { |
| 133 |
*uri = std::move(*tmp_uri2); |
| 134 |
return factory; |
| 135 |
} |
| 136 |
if (!tmp_uri.ok() || !tmp_uri2.ok()) { |
| 137 |
gpr_log(GPR_ERROR, "%s", |
| 138 |
absl::StrFormat("Error parsing URI(s). '%s':%s; '%s':%s", target, |
| 139 |
tmp_uri.status().ToString(), *canonical_target, |
| 140 |
tmp_uri2.status().ToString()) |
| 141 |
.c_str()); |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
gpr_log(GPR_ERROR, "Don't know how to resolve '%s' or '%s'.", |
| 145 |
std::string(target).c_str(), canonical_target->c_str()); |
| 146 |
return nullptr; |
| 147 |
} |
| 148 |
|
| 149 |
} // namespace grpc_core |