Convert grpc_proxy_mapper to C++.

pull/21207/head
Mark D. Roth 5 years ago
parent b073f5416d
commit 4e393dc035
  1. 1
      BUILD
  2. 2
      BUILD.gn
  3. 7
      CMakeLists.txt
  4. 7
      Makefile
  5. 1
      build.yaml
  6. 1
      config.m4
  7. 1
      config.w32
  8. 1
      gRPC-Core.podspec
  9. 1
      grpc.gemspec
  10. 6
      grpc.gyp
  11. 1
      package.xml
  12. 4
      src/core/ext/filters/client_channel/client_channel.cc
  13. 6
      src/core/ext/filters/client_channel/client_channel_plugin.cc
  14. 223
      src/core/ext/filters/client_channel/http_proxy.cc
  15. 6
      src/core/ext/filters/client_channel/http_proxy.h
  16. 48
      src/core/ext/filters/client_channel/proxy_mapper.cc
  17. 48
      src/core/ext/filters/client_channel/proxy_mapper.h
  18. 125
      src/core/ext/filters/client_channel/proxy_mapper_registry.cc
  19. 40
      src/core/ext/filters/client_channel/proxy_mapper_registry.h
  20. 2
      src/core/ext/filters/client_channel/subchannel.cc
  21. 1
      src/python/grpcio/grpc_core_dependencies.py
  22. 1
      tools/doxygen/Doxyfile.c++.internal
  23. 1
      tools/doxygen/Doxyfile.core.internal

@ -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",

@ -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",

@ -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

@ -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 \

@ -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

@ -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 \

@ -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 " +

@ -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',

@ -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 )

@ -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',

@ -130,7 +130,6 @@
<file baseinstalldir="/" name="src/core/ext/filters/client_channel/local_subchannel_pool.h" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/client_channel/parse_address.cc" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/client_channel/parse_address.h" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/client_channel/proxy_mapper.cc" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/client_channel/proxy_mapper.h" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/client_channel/proxy_mapper_registry.cc" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/client_channel/proxy_mapper_registry.h" role="src" />

@ -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

@ -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();

@ -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<char> server_host;
grpc_core::UniquePtr<char> 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<char> server_host;
grpc_core::UniquePtr<char> 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<ProxyMapperInterface>(new HttpProxyMapper()));
}
} // namespace grpc_core

@ -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 */

@ -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 <grpc/support/port_platform.h>
#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);
}

@ -21,54 +21,34 @@
#include <grpc/support/port_platform.h>
#include <stdbool.h>
#include <grpc/impl/codegen/grpc_types.h>
#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 */

@ -20,103 +20,70 @@
#include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"
#include <string.h>
#include <memory>
#include <vector>
#include <grpc/support/alloc.h>
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<std::unique_ptr<ProxyMapperInterface>>;
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<grpc_proxy_mapper**>(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<ProxyMapperInterface> 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

@ -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<ProxyMapperInterface> 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 */

@ -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;

@ -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',

@ -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 \

@ -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 \

Loading…
Cancel
Save