From 561281f268e84f28ccf51da9386baca091352085 Mon Sep 17 00:00:00 2001 From: Kevin Freeman Date: Tue, 29 Oct 2019 19:18:41 -0700 Subject: [PATCH] Add GRPC_ARG_HTTP_PROXY channel argument. Introduce the GRPC_ARG_HTTP_PROXY channel argument so that HTTP proxy settings can be controlled per channel. --- include/grpc/impl/codegen/grpc_types.h | 3 +++ src/core/ext/filters/client_channel/http_proxy.cc | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/grpc/impl/codegen/grpc_types.h b/include/grpc/impl/codegen/grpc_types.h index cb28de0f9b1..4aa1e785ca8 100644 --- a/include/grpc/impl/codegen/grpc_types.h +++ b/include/grpc/impl/codegen/grpc_types.h @@ -373,6 +373,9 @@ typedef struct { "grpc.disable_client_authority_filter" /** If set to zero, disables use of http proxies. Enabled by default. */ #define GRPC_ARG_ENABLE_HTTP_PROXY "grpc.enable_http_proxy" +/** Channel arg to set http proxy per channel. If set, the channel arg + * value will be prefered over the http_proxy envrionment settings. */ +#define GRPC_ARG_HTTP_PROXY "grpc.http_proxy" /** If set to non zero, surfaces the user agent string to the server. User agent is surfaced by default. */ #define GRPC_ARG_SURFACE_USER_AGENT "grpc.surface_user_agent" diff --git a/src/core/ext/filters/client_channel/http_proxy.cc b/src/core/ext/filters/client_channel/http_proxy.cc index 1c4a227576a..5034a840b0f 100644 --- a/src/core/ext/filters/client_channel/http_proxy.cc +++ b/src/core/ext/filters/client_channel/http_proxy.cc @@ -42,16 +42,19 @@ * 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) { +static char* get_http_proxy_server(const grpc_channel_args* args, char** user_cred) { GPR_ASSERT(user_cred != nullptr); char* proxy_name = nullptr; char** authority_strs = nullptr; size_t authority_nstrs; - /* Prefer using 'grpc_proxy'. Fallback on 'http_proxy' if it is not set. + /* Check channel arguments in case a per channel proxy is set. Otherwise, + * prefer using 'grpc_proxy'. Fallback on 'http_proxy' if it is not set. * Also prefer using 'https_proxy' with fallback on 'http_proxy'. The * fallback behavior can be removed if there's a demand for it. */ - char* uri_str = gpr_getenv("grpc_proxy"); + const grpc_arg* arg = grpc_channel_args_find(args, GRPC_ARG_HTTP_PROXY); + char* uri_str = gpr_strdup(grpc_channel_arg_get_string(arg)); + if (uri_str == nullptr) uri_str = gpr_getenv("grpc_proxy"); if (uri_str == nullptr) uri_str = gpr_getenv("https_proxy"); if (uri_str == nullptr) uri_str = gpr_getenv("http_proxy"); if (uri_str == nullptr) return nullptr; @@ -108,7 +111,7 @@ static bool proxy_mapper_map_name(grpc_proxy_mapper* /*mapper*/, return false; } char* user_cred = nullptr; - *name_to_resolve = get_http_proxy_server(&user_cred); + *name_to_resolve = get_http_proxy_server(args, &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 */);