diff --git a/BUILD b/BUILD index d003c5c77aa..abbce780073 100644 --- a/BUILD +++ b/BUILD @@ -1029,7 +1029,6 @@ grpc_cc_library( "src/core/ext/filters/client_channel/lb_policy_registry.cc", "src/core/ext/filters/client_channel/local_subchannel_pool.cc", "src/core/ext/filters/client_channel/parse_address.cc", - "src/core/ext/filters/client_channel/proxy_mapper.cc", "src/core/ext/filters/client_channel/proxy_mapper_registry.cc", "src/core/ext/filters/client_channel/resolver.cc", "src/core/ext/filters/client_channel/resolver_registry.cc", diff --git a/BUILD.gn b/BUILD.gn index 598171c46e4..162627cc322 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -252,7 +252,6 @@ config("grpc_config") { "src/core/ext/filters/client_channel/local_subchannel_pool.h", "src/core/ext/filters/client_channel/parse_address.cc", "src/core/ext/filters/client_channel/parse_address.h", - "src/core/ext/filters/client_channel/proxy_mapper.cc", "src/core/ext/filters/client_channel/proxy_mapper.h", "src/core/ext/filters/client_channel/proxy_mapper_registry.cc", "src/core/ext/filters/client_channel/proxy_mapper_registry.h", @@ -1220,7 +1219,6 @@ config("grpc_config") { "src/core/ext/filters/client_channel/local_subchannel_pool.h", "src/core/ext/filters/client_channel/parse_address.cc", "src/core/ext/filters/client_channel/parse_address.h", - "src/core/ext/filters/client_channel/proxy_mapper.cc", "src/core/ext/filters/client_channel/proxy_mapper.h", "src/core/ext/filters/client_channel/proxy_mapper_registry.cc", "src/core/ext/filters/client_channel/proxy_mapper_registry.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index 44fc1a9da48..eec75f0a81a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1376,7 +1376,6 @@ add_library(grpc src/core/ext/filters/client_channel/lb_policy_registry.cc src/core/ext/filters/client_channel/local_subchannel_pool.cc src/core/ext/filters/client_channel/parse_address.cc - src/core/ext/filters/client_channel/proxy_mapper.cc src/core/ext/filters/client_channel/proxy_mapper_registry.cc src/core/ext/filters/client_channel/resolver.cc src/core/ext/filters/client_channel/resolver_registry.cc @@ -1781,7 +1780,6 @@ add_library(grpc_cronet src/core/ext/filters/client_channel/lb_policy_registry.cc src/core/ext/filters/client_channel/local_subchannel_pool.cc src/core/ext/filters/client_channel/parse_address.cc - src/core/ext/filters/client_channel/proxy_mapper.cc src/core/ext/filters/client_channel/proxy_mapper_registry.cc src/core/ext/filters/client_channel/resolver.cc src/core/ext/filters/client_channel/resolver_registry.cc @@ -2178,7 +2176,6 @@ add_library(grpc_test_util src/core/ext/filters/client_channel/lb_policy_registry.cc src/core/ext/filters/client_channel/local_subchannel_pool.cc src/core/ext/filters/client_channel/parse_address.cc - src/core/ext/filters/client_channel/proxy_mapper.cc src/core/ext/filters/client_channel/proxy_mapper_registry.cc src/core/ext/filters/client_channel/resolver.cc src/core/ext/filters/client_channel/resolver_registry.cc @@ -2521,7 +2518,6 @@ add_library(grpc_test_util_unsecure src/core/ext/filters/client_channel/lb_policy_registry.cc src/core/ext/filters/client_channel/local_subchannel_pool.cc src/core/ext/filters/client_channel/parse_address.cc - src/core/ext/filters/client_channel/proxy_mapper.cc src/core/ext/filters/client_channel/proxy_mapper_registry.cc src/core/ext/filters/client_channel/resolver.cc src/core/ext/filters/client_channel/resolver_registry.cc @@ -2875,7 +2871,6 @@ add_library(grpc_unsecure src/core/ext/filters/client_channel/lb_policy_registry.cc src/core/ext/filters/client_channel/local_subchannel_pool.cc src/core/ext/filters/client_channel/parse_address.cc - src/core/ext/filters/client_channel/proxy_mapper.cc src/core/ext/filters/client_channel/proxy_mapper_registry.cc src/core/ext/filters/client_channel/resolver.cc src/core/ext/filters/client_channel/resolver_registry.cc @@ -3313,7 +3308,6 @@ add_library(grpc++ src/core/ext/filters/client_channel/lb_policy_registry.cc src/core/ext/filters/client_channel/local_subchannel_pool.cc src/core/ext/filters/client_channel/parse_address.cc - src/core/ext/filters/client_channel/proxy_mapper.cc src/core/ext/filters/client_channel/proxy_mapper_registry.cc src/core/ext/filters/client_channel/resolver.cc src/core/ext/filters/client_channel/resolver_registry.cc @@ -4578,7 +4572,6 @@ add_library(grpc++_unsecure src/core/ext/filters/client_channel/lb_policy_registry.cc src/core/ext/filters/client_channel/local_subchannel_pool.cc src/core/ext/filters/client_channel/parse_address.cc - src/core/ext/filters/client_channel/proxy_mapper.cc src/core/ext/filters/client_channel/proxy_mapper_registry.cc src/core/ext/filters/client_channel/resolver.cc src/core/ext/filters/client_channel/resolver_registry.cc diff --git a/Makefile b/Makefile index 8fd7044dd96..7c57534efec 100644 --- a/Makefile +++ b/Makefile @@ -3861,7 +3861,6 @@ LIBGRPC_SRC = \ src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver_registry.cc \ @@ -4258,7 +4257,6 @@ LIBGRPC_CRONET_SRC = \ src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver_registry.cc \ @@ -4646,7 +4644,6 @@ LIBGRPC_TEST_UTIL_SRC = \ src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver_registry.cc \ @@ -4975,7 +4972,6 @@ LIBGRPC_TEST_UTIL_UNSECURE_SRC = \ src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver_registry.cc \ @@ -5302,7 +5298,6 @@ LIBGRPC_UNSECURE_SRC = \ src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver_registry.cc \ @@ -5705,7 +5700,6 @@ LIBGRPC++_SRC = \ src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver_registry.cc \ @@ -6936,7 +6930,6 @@ LIBGRPC++_UNSECURE_SRC = \ src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver_registry.cc \ diff --git a/build.yaml b/build.yaml index 9ddd364b221..393ab409310 100644 --- a/build.yaml +++ b/build.yaml @@ -1007,7 +1007,6 @@ filegroups: - src/core/ext/filters/client_channel/lb_policy_registry.cc - src/core/ext/filters/client_channel/local_subchannel_pool.cc - src/core/ext/filters/client_channel/parse_address.cc - - src/core/ext/filters/client_channel/proxy_mapper.cc - src/core/ext/filters/client_channel/proxy_mapper_registry.cc - src/core/ext/filters/client_channel/resolver.cc - src/core/ext/filters/client_channel/resolver_registry.cc diff --git a/config.m4 b/config.m4 index 933e940b948..e826b2eb5eb 100644 --- a/config.m4 +++ b/config.m4 @@ -62,7 +62,6 @@ if test "$PHP_GRPC" != "no"; then src/core/ext/filters/client_channel/lb_policy_registry.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/parse_address.cc \ - src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/resolver.cc \ src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc \ diff --git a/config.w32 b/config.w32 index 934f7c63240..512be64abfd 100644 --- a/config.w32 +++ b/config.w32 @@ -333,7 +333,6 @@ if (PHP_GRPC != "no") { "src\\core\\ext\\filters\\client_channel\\lb_policy_registry.cc " + "src\\core\\ext\\filters\\client_channel\\local_subchannel_pool.cc " + "src\\core\\ext\\filters\\client_channel\\parse_address.cc " + - "src\\core\\ext\\filters\\client_channel\\proxy_mapper.cc " + "src\\core\\ext\\filters\\client_channel\\proxy_mapper_registry.cc " + "src\\core\\ext\\filters\\client_channel\\resolver.cc " + "src\\core\\ext\\filters\\client_channel\\resolver_registry.cc " + diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 8989626e319..9cb5a0bad27 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -235,7 +235,6 @@ Pod::Spec.new do |s| 'src/core/ext/filters/client_channel/local_subchannel_pool.h', 'src/core/ext/filters/client_channel/parse_address.cc', 'src/core/ext/filters/client_channel/parse_address.h', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper.h', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.h', diff --git a/grpc.gemspec b/grpc.gemspec index ed711536378..c05c3459266 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -147,7 +147,6 @@ Gem::Specification.new do |s| s.files += %w( src/core/ext/filters/client_channel/local_subchannel_pool.h ) s.files += %w( src/core/ext/filters/client_channel/parse_address.cc ) s.files += %w( src/core/ext/filters/client_channel/parse_address.h ) - s.files += %w( src/core/ext/filters/client_channel/proxy_mapper.cc ) s.files += %w( src/core/ext/filters/client_channel/proxy_mapper.h ) s.files += %w( src/core/ext/filters/client_channel/proxy_mapper_registry.cc ) s.files += %w( src/core/ext/filters/client_channel/proxy_mapper_registry.h ) diff --git a/grpc.gyp b/grpc.gyp index 87672f62429..0707f11200b 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -502,7 +502,6 @@ 'src/core/ext/filters/client_channel/lb_policy_registry.cc', 'src/core/ext/filters/client_channel/local_subchannel_pool.cc', 'src/core/ext/filters/client_channel/parse_address.cc', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/resolver.cc', 'src/core/ext/filters/client_channel/resolver_registry.cc', @@ -814,7 +813,6 @@ 'src/core/ext/filters/client_channel/lb_policy_registry.cc', 'src/core/ext/filters/client_channel/local_subchannel_pool.cc', 'src/core/ext/filters/client_channel/parse_address.cc', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/resolver.cc', 'src/core/ext/filters/client_channel/resolver_registry.cc', @@ -1077,7 +1075,6 @@ 'src/core/ext/filters/client_channel/lb_policy_registry.cc', 'src/core/ext/filters/client_channel/local_subchannel_pool.cc', 'src/core/ext/filters/client_channel/parse_address.cc', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/resolver.cc', 'src/core/ext/filters/client_channel/resolver_registry.cc', @@ -1351,7 +1348,6 @@ 'src/core/ext/filters/client_channel/lb_policy_registry.cc', 'src/core/ext/filters/client_channel/local_subchannel_pool.cc', 'src/core/ext/filters/client_channel/parse_address.cc', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/resolver.cc', 'src/core/ext/filters/client_channel/resolver_registry.cc', @@ -1562,7 +1558,6 @@ 'src/core/ext/filters/client_channel/lb_policy_registry.cc', 'src/core/ext/filters/client_channel/local_subchannel_pool.cc', 'src/core/ext/filters/client_channel/parse_address.cc', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/resolver.cc', 'src/core/ext/filters/client_channel/resolver_registry.cc', @@ -1916,7 +1911,6 @@ 'src/core/ext/filters/client_channel/lb_policy_registry.cc', 'src/core/ext/filters/client_channel/local_subchannel_pool.cc', 'src/core/ext/filters/client_channel/parse_address.cc', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/resolver.cc', 'src/core/ext/filters/client_channel/resolver_registry.cc', diff --git a/package.xml b/package.xml index b5c5f312825..3e2a47fd63b 100644 --- a/package.xml +++ b/package.xml @@ -130,7 +130,6 @@ - diff --git a/src/core/ext/filters/client_channel/client_channel.cc b/src/core/ext/filters/client_channel/client_channel.cc index ce3c3ea56ef..7e3fe1b3be9 100644 --- a/src/core/ext/filters/client_channel/client_channel.cc +++ b/src/core/ext/filters/client_channel/client_channel.cc @@ -1465,8 +1465,8 @@ ChannelData::ChannelData(grpc_channel_element_args* args, grpc_error** error) grpc_uri_destroy(uri); char* proxy_name = nullptr; grpc_channel_args* new_args = nullptr; - grpc_proxy_mappers_map_name(server_uri, args->channel_args, &proxy_name, - &new_args); + ProxyMapperRegistry::MapName(server_uri, args->channel_args, &proxy_name, + &new_args); target_uri_.reset(proxy_name != nullptr ? proxy_name : gpr_strdup(server_uri)); channel_args_ = new_args != nullptr diff --git a/src/core/ext/filters/client_channel/client_channel_plugin.cc b/src/core/ext/filters/client_channel/client_channel_plugin.cc index c5662e22a20..2788d4bb9e7 100644 --- a/src/core/ext/filters/client_channel/client_channel_plugin.cc +++ b/src/core/ext/filters/client_channel/client_channel_plugin.cc @@ -48,8 +48,8 @@ void grpc_client_channel_init(void) { grpc_core::LoadBalancingPolicyRegistry::Builder::InitRegistry(); grpc_core::ResolverRegistry::Builder::InitRegistry(); grpc_core::internal::ServerRetryThrottleMap::Init(); - grpc_proxy_mapper_registry_init(); - grpc_register_http_proxy_mapper(); + grpc_core::ProxyMapperRegistry::Init(); + grpc_core::RegisterHttpProxyMapper(); grpc_core::GlobalSubchannelPool::Init(); grpc_channel_init_register_stage( GRPC_CLIENT_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, append_filter, @@ -61,7 +61,7 @@ void grpc_client_channel_init(void) { void grpc_client_channel_shutdown(void) { grpc_core::GlobalSubchannelPool::Shutdown(); grpc_channel_init_shutdown(); - grpc_proxy_mapper_registry_shutdown(); + grpc_core::ProxyMapperRegistry::Shutdown(); grpc_core::internal::ServerRetryThrottleMap::Shutdown(); grpc_core::ResolverRegistry::Builder::ShutdownRegistry(); grpc_core::LoadBalancingPolicyRegistry::Builder::ShutdownRegistry(); diff --git a/src/core/ext/filters/client_channel/http_proxy.cc b/src/core/ext/filters/client_channel/http_proxy.cc index 1c4a227576a..6a2b122f68c 100644 --- a/src/core/ext/filters/client_channel/http_proxy.cc +++ b/src/core/ext/filters/client_channel/http_proxy.cc @@ -36,13 +36,16 @@ #include "src/core/lib/slice/b64.h" #include "src/core/lib/uri/uri_parser.h" +namespace grpc_core { +namespace { + /** * Parses the 'https_proxy' env var (fallback on 'http_proxy') and returns the * proxy hostname to resolve or nullptr on error. Also sets 'user_cred' to user * credentials if present in the 'http_proxy' env var, otherwise leaves it * unchanged. It is caller's responsibility to gpr_free user_cred. */ -static char* get_http_proxy_server(char** user_cred) { +char* GetHttpProxyServer(char** user_cred) { GPR_ASSERT(user_cred != nullptr); char* proxy_name = nullptr; char** authority_strs = nullptr; @@ -89,127 +92,115 @@ done: return proxy_name; } -/** - * Checks the value of GRPC_ARG_ENABLE_HTTP_PROXY to determine if http_proxy - * should be used. - */ -bool http_proxy_enabled(const grpc_channel_args* args) { - const grpc_arg* arg = - grpc_channel_args_find(args, GRPC_ARG_ENABLE_HTTP_PROXY); - return grpc_channel_arg_get_bool(arg, true); -} - -static bool proxy_mapper_map_name(grpc_proxy_mapper* /*mapper*/, - const char* server_uri, - const grpc_channel_args* args, - char** name_to_resolve, - grpc_channel_args** new_args) { - if (!http_proxy_enabled(args)) { - return false; - } - char* user_cred = nullptr; - *name_to_resolve = get_http_proxy_server(&user_cred); - if (*name_to_resolve == nullptr) return false; - char* no_proxy_str = nullptr; - grpc_uri* uri = grpc_uri_parse(server_uri, false /* suppress_errors */); - if (uri == nullptr || uri->path[0] == '\0') { - gpr_log(GPR_ERROR, - "'http_proxy' environment variable set, but cannot " - "parse server URI '%s' -- not using proxy", - server_uri); - goto no_use_proxy; - } - if (strcmp(uri->scheme, "unix") == 0) { - gpr_log(GPR_INFO, "not using proxy for Unix domain socket '%s'", - server_uri); - goto no_use_proxy; - } - /* Prefer using 'no_grpc_proxy'. Fallback on 'no_proxy' if it is not set. */ - no_proxy_str = gpr_getenv("no_grpc_proxy"); - if (no_proxy_str == nullptr) no_proxy_str = gpr_getenv("no_proxy"); - if (no_proxy_str != nullptr) { - static const char* NO_PROXY_SEPARATOR = ","; - bool use_proxy = true; - grpc_core::UniquePtr server_host; - grpc_core::UniquePtr server_port; - if (!grpc_core::SplitHostPort( - uri->path[0] == '/' ? uri->path + 1 : uri->path, &server_host, - &server_port)) { - gpr_log(GPR_INFO, - "unable to split host and port, not checking no_proxy list for " - "host '%s'", +class HttpProxyMapper : public ProxyMapperInterface { + public: + bool MapName(const char* server_uri, const grpc_channel_args* args, + char** name_to_resolve, grpc_channel_args** new_args) override { + if (!grpc_channel_args_find_bool(args, GRPC_ARG_ENABLE_HTTP_PROXY, true)) { + return false; + } + char* user_cred = nullptr; + *name_to_resolve = GetHttpProxyServer(&user_cred); + if (*name_to_resolve == nullptr) return false; + char* no_proxy_str = nullptr; + grpc_uri* uri = grpc_uri_parse(server_uri, false /* suppress_errors */); + if (uri == nullptr || uri->path[0] == '\0') { + gpr_log(GPR_ERROR, + "'http_proxy' environment variable set, but cannot " + "parse server URI '%s' -- not using proxy", server_uri); - gpr_free(no_proxy_str); - } else { - size_t uri_len = strlen(server_host.get()); - char** no_proxy_hosts; - size_t num_no_proxy_hosts; - gpr_string_split(no_proxy_str, NO_PROXY_SEPARATOR, &no_proxy_hosts, - &num_no_proxy_hosts); - for (size_t i = 0; i < num_no_proxy_hosts; i++) { - char* no_proxy_entry = no_proxy_hosts[i]; - size_t no_proxy_len = strlen(no_proxy_entry); - if (no_proxy_len <= uri_len && - gpr_stricmp(no_proxy_entry, - &(server_host.get()[uri_len - no_proxy_len])) == 0) { - gpr_log(GPR_INFO, "not using proxy for host in no_proxy list '%s'", - server_uri); - use_proxy = false; - break; + goto no_use_proxy; + } + if (strcmp(uri->scheme, "unix") == 0) { + gpr_log(GPR_INFO, "not using proxy for Unix domain socket '%s'", + server_uri); + goto no_use_proxy; + } + /* Prefer using 'no_grpc_proxy'. Fallback on 'no_proxy' if it is not set. */ + no_proxy_str = gpr_getenv("no_grpc_proxy"); + if (no_proxy_str == nullptr) no_proxy_str = gpr_getenv("no_proxy"); + if (no_proxy_str != nullptr) { + static const char* NO_PROXY_SEPARATOR = ","; + bool use_proxy = true; + grpc_core::UniquePtr server_host; + grpc_core::UniquePtr server_port; + if (!grpc_core::SplitHostPort( + uri->path[0] == '/' ? uri->path + 1 : uri->path, &server_host, + &server_port)) { + gpr_log(GPR_INFO, + "unable to split host and port, not checking no_proxy list for " + "host '%s'", + server_uri); + gpr_free(no_proxy_str); + } else { + size_t uri_len = strlen(server_host.get()); + char** no_proxy_hosts; + size_t num_no_proxy_hosts; + gpr_string_split(no_proxy_str, NO_PROXY_SEPARATOR, &no_proxy_hosts, + &num_no_proxy_hosts); + for (size_t i = 0; i < num_no_proxy_hosts; i++) { + char* no_proxy_entry = no_proxy_hosts[i]; + size_t no_proxy_len = strlen(no_proxy_entry); + if (no_proxy_len <= uri_len && + gpr_stricmp(no_proxy_entry, + &(server_host.get()[uri_len - no_proxy_len])) == 0) { + gpr_log(GPR_INFO, "not using proxy for host in no_proxy list '%s'", + server_uri); + use_proxy = false; + break; + } } + for (size_t i = 0; i < num_no_proxy_hosts; i++) { + gpr_free(no_proxy_hosts[i]); + } + gpr_free(no_proxy_hosts); + gpr_free(no_proxy_str); + if (!use_proxy) goto no_use_proxy; } - for (size_t i = 0; i < num_no_proxy_hosts; i++) { - gpr_free(no_proxy_hosts[i]); - } - gpr_free(no_proxy_hosts); - gpr_free(no_proxy_str); - if (!use_proxy) goto no_use_proxy; } + grpc_arg args_to_add[2]; + args_to_add[0] = grpc_channel_arg_string_create( + (char*)GRPC_ARG_HTTP_CONNECT_SERVER, + uri->path[0] == '/' ? uri->path + 1 : uri->path); + if (user_cred != nullptr) { + /* Use base64 encoding for user credentials as stated in RFC 7617 */ + char* encoded_user_cred = + grpc_base64_encode(user_cred, strlen(user_cred), 0, 0); + char* header; + gpr_asprintf(&header, "Proxy-Authorization:Basic %s", encoded_user_cred); + gpr_free(encoded_user_cred); + args_to_add[1] = grpc_channel_arg_string_create( + (char*)GRPC_ARG_HTTP_CONNECT_HEADERS, header); + *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 2); + gpr_free(header); + } else { + *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 1); + } + grpc_uri_destroy(uri); + gpr_free(user_cred); + return true; + no_use_proxy: + if (uri != nullptr) grpc_uri_destroy(uri); + gpr_free(*name_to_resolve); + *name_to_resolve = nullptr; + gpr_free(user_cred); + return false; } - grpc_arg args_to_add[2]; - args_to_add[0] = grpc_channel_arg_string_create( - (char*)GRPC_ARG_HTTP_CONNECT_SERVER, - uri->path[0] == '/' ? uri->path + 1 : uri->path); - if (user_cred != nullptr) { - /* Use base64 encoding for user credentials as stated in RFC 7617 */ - char* encoded_user_cred = - grpc_base64_encode(user_cred, strlen(user_cred), 0, 0); - char* header; - gpr_asprintf(&header, "Proxy-Authorization:Basic %s", encoded_user_cred); - gpr_free(encoded_user_cred); - args_to_add[1] = grpc_channel_arg_string_create( - (char*)GRPC_ARG_HTTP_CONNECT_HEADERS, header); - *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 2); - gpr_free(header); - } else { - *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 1); - } - grpc_uri_destroy(uri); - gpr_free(user_cred); - return true; -no_use_proxy: - if (uri != nullptr) grpc_uri_destroy(uri); - gpr_free(*name_to_resolve); - *name_to_resolve = nullptr; - gpr_free(user_cred); - return false; -} - -static bool proxy_mapper_map_address(grpc_proxy_mapper* /*mapper*/, - const grpc_resolved_address* /*address*/, - const grpc_channel_args* /*args*/, - grpc_resolved_address** /*new_address*/, - grpc_channel_args** /*new_args*/) { - return false; -} - -static void proxy_mapper_destroy(grpc_proxy_mapper* /*mapper*/) {} -static const grpc_proxy_mapper_vtable proxy_mapper_vtable = { - proxy_mapper_map_name, proxy_mapper_map_address, proxy_mapper_destroy}; + bool MapAddress(const grpc_resolved_address& address, + const grpc_channel_args* args, + grpc_resolved_address** new_address, + grpc_channel_args** new_args) override { + return false; + } +}; -static grpc_proxy_mapper proxy_mapper = {&proxy_mapper_vtable}; +} // namespace -void grpc_register_http_proxy_mapper() { - grpc_proxy_mapper_register(true /* at_start */, &proxy_mapper); +void RegisterHttpProxyMapper() { + ProxyMapperRegistry::Register( + true /* at_start */, + std::unique_ptr(new HttpProxyMapper())); } + +} // namespace grpc_core diff --git a/src/core/ext/filters/client_channel/http_proxy.h b/src/core/ext/filters/client_channel/http_proxy.h index 34694931d04..cecdaf8478a 100644 --- a/src/core/ext/filters/client_channel/http_proxy.h +++ b/src/core/ext/filters/client_channel/http_proxy.h @@ -19,6 +19,10 @@ #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HTTP_PROXY_H #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HTTP_PROXY_H -void grpc_register_http_proxy_mapper(); +namespace grpc_core { + +void RegisterHttpProxyMapper(); + +} // namespace grpc_core #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HTTP_PROXY_H */ diff --git a/src/core/ext/filters/client_channel/proxy_mapper.cc b/src/core/ext/filters/client_channel/proxy_mapper.cc deleted file mode 100644 index c4da06778d2..00000000000 --- a/src/core/ext/filters/client_channel/proxy_mapper.cc +++ /dev/null @@ -1,48 +0,0 @@ -/* - * - * Copyright 2017 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include - -#include "src/core/ext/filters/client_channel/proxy_mapper.h" - -void grpc_proxy_mapper_init(const grpc_proxy_mapper_vtable* vtable, - grpc_proxy_mapper* mapper) { - mapper->vtable = vtable; -} - -bool grpc_proxy_mapper_map_name(grpc_proxy_mapper* mapper, - const char* server_uri, - const grpc_channel_args* args, - char** name_to_resolve, - grpc_channel_args** new_args) { - return mapper->vtable->map_name(mapper, server_uri, args, name_to_resolve, - new_args); -} - -bool grpc_proxy_mapper_map_address(grpc_proxy_mapper* mapper, - const grpc_resolved_address* address, - const grpc_channel_args* args, - grpc_resolved_address** new_address, - grpc_channel_args** new_args) { - return mapper->vtable->map_address(mapper, address, args, new_address, - new_args); -} - -void grpc_proxy_mapper_destroy(grpc_proxy_mapper* mapper) { - mapper->vtable->destroy(mapper); -} diff --git a/src/core/ext/filters/client_channel/proxy_mapper.h b/src/core/ext/filters/client_channel/proxy_mapper.h index 634b0ed7bf5..3e297d9a6f0 100644 --- a/src/core/ext/filters/client_channel/proxy_mapper.h +++ b/src/core/ext/filters/client_channel/proxy_mapper.h @@ -21,54 +21,34 @@ #include -#include - #include #include "src/core/lib/iomgr/resolve_address.h" -typedef struct grpc_proxy_mapper grpc_proxy_mapper; +namespace grpc_core { + +class ProxyMapperInterface { + public: + virtual ~ProxyMapperInterface() = default; -typedef struct { /// Determines the proxy name to resolve for \a server_uri. /// If no proxy is needed, returns false. /// Otherwise, sets \a name_to_resolve, optionally sets \a new_args, /// and returns true. - bool (*map_name)(grpc_proxy_mapper* mapper, const char* server_uri, - const grpc_channel_args* args, char** name_to_resolve, - grpc_channel_args** new_args); + virtual bool MapName(const char* server_uri, const grpc_channel_args* args, + char** name_to_resolve, + grpc_channel_args** new_args) = 0; + /// Determines the proxy address to use to contact \a address. /// If no proxy is needed, returns false. /// Otherwise, sets \a new_address, optionally sets \a new_args, and /// returns true. - bool (*map_address)(grpc_proxy_mapper* mapper, - const grpc_resolved_address* address, - const grpc_channel_args* args, - grpc_resolved_address** new_address, - grpc_channel_args** new_args); - /// Destroys \a mapper. - void (*destroy)(grpc_proxy_mapper* mapper); -} grpc_proxy_mapper_vtable; - -struct grpc_proxy_mapper { - const grpc_proxy_mapper_vtable* vtable; + virtual bool MapAddress(const grpc_resolved_address& address, + const grpc_channel_args* args, + grpc_resolved_address** new_address, + grpc_channel_args** new_args) = 0; }; -void grpc_proxy_mapper_init(const grpc_proxy_mapper_vtable* vtable, - grpc_proxy_mapper* mapper); - -bool grpc_proxy_mapper_map_name(grpc_proxy_mapper* mapper, - const char* server_uri, - const grpc_channel_args* args, - char** name_to_resolve, - grpc_channel_args** new_args); - -bool grpc_proxy_mapper_map_address(grpc_proxy_mapper* mapper, - const grpc_resolved_address* address, - const grpc_channel_args* args, - grpc_resolved_address** new_address, - grpc_channel_args** new_args); - -void grpc_proxy_mapper_destroy(grpc_proxy_mapper* mapper); +} // namespace grpc_core #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_PROXY_MAPPER_H */ diff --git a/src/core/ext/filters/client_channel/proxy_mapper_registry.cc b/src/core/ext/filters/client_channel/proxy_mapper_registry.cc index a02a5f5e2cd..2fc25725258 100644 --- a/src/core/ext/filters/client_channel/proxy_mapper_registry.cc +++ b/src/core/ext/filters/client_channel/proxy_mapper_registry.cc @@ -20,103 +20,70 @@ #include "src/core/ext/filters/client_channel/proxy_mapper_registry.h" -#include +#include +#include -#include +namespace grpc_core { -// -// grpc_proxy_mapper_list -// +namespace { -typedef struct { - grpc_proxy_mapper** list; - size_t num_mappers; -} grpc_proxy_mapper_list; +using ProxyMapperList = std::vector>; +ProxyMapperList* g_proxy_mapper_list; -static void grpc_proxy_mapper_list_register(grpc_proxy_mapper_list* list, - bool at_start, - grpc_proxy_mapper* mapper) { - list->list = static_cast(gpr_realloc( - list->list, (list->num_mappers + 1) * sizeof(grpc_proxy_mapper*))); +} // namespace + +void ProxyMapperRegistry::Init() { + if (g_proxy_mapper_list == nullptr) { + g_proxy_mapper_list = new ProxyMapperList(); + } +} + +void ProxyMapperRegistry::Shutdown() { + delete g_proxy_mapper_list; + // Clean up in case we re-initialze later. + // TODO(roth): This should ideally live in Init(). However, if we did this + // there, then we would do it AFTER we start registering proxy mappers from + // third-party plugins, so they'd never show up (and would leak memory). + // We probably need some sort of dependency system for plugins to fix + // this. + g_proxy_mapper_list = nullptr; +} + +void ProxyMapperRegistry::Register( + bool at_start, std::unique_ptr mapper) { + Init(); if (at_start) { - memmove(list->list + 1, list->list, - sizeof(grpc_proxy_mapper*) * list->num_mappers); - list->list[0] = mapper; + g_proxy_mapper_list->insert(g_proxy_mapper_list->begin(), + std::move(mapper)); } else { - list->list[list->num_mappers] = mapper; + g_proxy_mapper_list->emplace_back(std::move(mapper)); } - ++list->num_mappers; } -static bool grpc_proxy_mapper_list_map_name(grpc_proxy_mapper_list* list, - const char* server_uri, - const grpc_channel_args* args, - char** name_to_resolve, - grpc_channel_args** new_args) { - for (size_t i = 0; i < list->num_mappers; ++i) { - if (grpc_proxy_mapper_map_name(list->list[i], server_uri, args, - name_to_resolve, new_args)) { +bool ProxyMapperRegistry::MapName(const char* server_uri, + const grpc_channel_args* args, + char** name_to_resolve, + grpc_channel_args** new_args) { + Init(); + for (const auto& mapper : *g_proxy_mapper_list) { + if (mapper->MapName(server_uri, args, name_to_resolve, new_args)) { return true; } } return false; } -static bool grpc_proxy_mapper_list_map_address( - grpc_proxy_mapper_list* list, const grpc_resolved_address* address, - const grpc_channel_args* args, grpc_resolved_address** new_address, - grpc_channel_args** new_args) { - for (size_t i = 0; i < list->num_mappers; ++i) { - if (grpc_proxy_mapper_map_address(list->list[i], address, args, new_address, - new_args)) { +bool ProxyMapperRegistry::MapAddress(const grpc_resolved_address& address, + const grpc_channel_args* args, + grpc_resolved_address** new_address, + grpc_channel_args** new_args) { + Init(); + for (const auto& mapper : *g_proxy_mapper_list) { + if (mapper->MapAddress(address, args, new_address, new_args)) { return true; } } return false; } -static void grpc_proxy_mapper_list_destroy(grpc_proxy_mapper_list* list) { - for (size_t i = 0; i < list->num_mappers; ++i) { - grpc_proxy_mapper_destroy(list->list[i]); - } - gpr_free(list->list); - // Clean up in case we re-initialze later. - // TODO(ctiller): This should ideally live in - // grpc_proxy_mapper_registry_init(). However, if we did this there, - // then we would do it AFTER we start registering proxy mappers from - // third-party plugins, so they'd never show up (and would leak memory). - // We probably need some sort of dependency system for plugins to fix - // this. - memset(list, 0, sizeof(*list)); -} - -// -// plugin -// - -static grpc_proxy_mapper_list g_proxy_mapper_list; - -void grpc_proxy_mapper_registry_init() {} - -void grpc_proxy_mapper_registry_shutdown() { - grpc_proxy_mapper_list_destroy(&g_proxy_mapper_list); -} - -void grpc_proxy_mapper_register(bool at_start, grpc_proxy_mapper* mapper) { - grpc_proxy_mapper_list_register(&g_proxy_mapper_list, at_start, mapper); -} - -bool grpc_proxy_mappers_map_name(const char* server_uri, - const grpc_channel_args* args, - char** name_to_resolve, - grpc_channel_args** new_args) { - return grpc_proxy_mapper_list_map_name(&g_proxy_mapper_list, server_uri, args, - name_to_resolve, new_args); -} -bool grpc_proxy_mappers_map_address(const grpc_resolved_address* address, - const grpc_channel_args* args, - grpc_resolved_address** new_address, - grpc_channel_args** new_args) { - return grpc_proxy_mapper_list_map_address(&g_proxy_mapper_list, address, args, - new_address, new_args); -} +} // namespace grpc_core diff --git a/src/core/ext/filters/client_channel/proxy_mapper_registry.h b/src/core/ext/filters/client_channel/proxy_mapper_registry.h index 326b582b993..b6273e1adf5 100644 --- a/src/core/ext/filters/client_channel/proxy_mapper_registry.h +++ b/src/core/ext/filters/client_channel/proxy_mapper_registry.h @@ -23,22 +23,28 @@ #include "src/core/ext/filters/client_channel/proxy_mapper.h" -void grpc_proxy_mapper_registry_init(); -void grpc_proxy_mapper_registry_shutdown(); - -/// Registers a new proxy mapper. Takes ownership. -/// If \a at_start is true, the new mapper will be at the beginning of -/// the list. Otherwise, it will be added to the end. -void grpc_proxy_mapper_register(bool at_start, grpc_proxy_mapper* mapper); - -bool grpc_proxy_mappers_map_name(const char* server_uri, - const grpc_channel_args* args, - char** name_to_resolve, - grpc_channel_args** new_args); - -bool grpc_proxy_mappers_map_address(const grpc_resolved_address* address, - const grpc_channel_args* args, - grpc_resolved_address** new_address, - grpc_channel_args** new_args); +namespace grpc_core { + +class ProxyMapperRegistry { + public: + static void Init(); + static void Shutdown(); + + /// Registers a new proxy mapper. + /// If \a at_start is true, the new mapper will be at the beginning of + /// the list. Otherwise, it will be added to the end. + static void Register(bool at_start, + std::unique_ptr mapper); + + static bool MapName(const char* server_uri, const grpc_channel_args* args, + char** name_to_resolve, grpc_channel_args** new_args); + + static bool MapAddress(const grpc_resolved_address& address, + const grpc_channel_args* args, + grpc_resolved_address** new_address, + grpc_channel_args** new_args); +}; + +} // namespace grpc_core #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_PROXY_MAPPER_REGISTRY_H */ diff --git a/src/core/ext/filters/client_channel/subchannel.cc b/src/core/ext/filters/client_channel/subchannel.cc index 26769e861e7..8ef241b25e5 100644 --- a/src/core/ext/filters/client_channel/subchannel.cc +++ b/src/core/ext/filters/client_channel/subchannel.cc @@ -627,7 +627,7 @@ Subchannel::Subchannel(SubchannelKey* key, GetAddressFromSubchannelAddressArg(args, addr); grpc_resolved_address* new_address = nullptr; grpc_channel_args* new_args = nullptr; - if (grpc_proxy_mappers_map_address(addr, args, &new_address, &new_args)) { + if (ProxyMapperRegistry::MapAddress(*addr, args, &new_address, &new_args)) { GPR_ASSERT(new_address != nullptr); gpr_free(addr); addr = new_address; diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index 103bcf83cb6..56b87a286a5 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -41,7 +41,6 @@ CORE_SOURCE_FILES = [ 'src/core/ext/filters/client_channel/lb_policy_registry.cc', 'src/core/ext/filters/client_channel/local_subchannel_pool.cc', 'src/core/ext/filters/client_channel/parse_address.cc', - 'src/core/ext/filters/client_channel/proxy_mapper.cc', 'src/core/ext/filters/client_channel/proxy_mapper_registry.cc', 'src/core/ext/filters/client_channel/resolver.cc', 'src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc', diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 35968c22f12..2794f29a0c5 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1093,7 +1093,6 @@ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.h \ src/core/ext/filters/client_channel/parse_address.cc \ src/core/ext/filters/client_channel/parse_address.h \ -src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper.h \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.h \ diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index bec0ab35f8e..828c9811999 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -922,7 +922,6 @@ src/core/ext/filters/client_channel/local_subchannel_pool.cc \ src/core/ext/filters/client_channel/local_subchannel_pool.h \ src/core/ext/filters/client_channel/parse_address.cc \ src/core/ext/filters/client_channel/parse_address.h \ -src/core/ext/filters/client_channel/proxy_mapper.cc \ src/core/ext/filters/client_channel/proxy_mapper.h \ src/core/ext/filters/client_channel/proxy_mapper_registry.cc \ src/core/ext/filters/client_channel/proxy_mapper_registry.h \