blob: aa3f61c99171c57b848e3ef5400c6004c203ed5d [file] [log] [blame]
Mark D. Rothd58a9852017-01-18 08:28:57 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Mark D. Rothd58a9852017-01-18 08:28:57 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Mark D. Rothd58a9852017-01-18 08:28:57 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Mark D. Rothd58a9852017-01-18 08:28:57 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Mark D. Rothd58a9852017-01-18 08:28:57 -080016 *
17 */
18
Craig Tiller9eb0fde2017-03-31 16:59:30 -070019#include "src/core/ext/filters/client_channel/http_proxy.h"
Mark D. Rothd58a9852017-01-18 08:28:57 -080020
21#include <stdbool.h>
22#include <string.h>
23
24#include <grpc/support/alloc.h>
Ben Sykes02d426e2017-07-11 13:58:42 -070025#include <grpc/support/host_port.h>
Mark D. Rothd58a9852017-01-18 08:28:57 -080026#include <grpc/support/log.h>
27#include <grpc/support/string_util.h>
28
Craig Tiller9eb0fde2017-03-31 16:59:30 -070029#include "src/core/ext/filters/client_channel/http_connect_handshaker.h"
30#include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"
31#include "src/core/ext/filters/client_channel/uri_parser.h"
Mark D. Rothdc9bee72017-02-07 12:29:14 -080032#include "src/core/lib/channel/channel_args.h"
Mark D. Rothd58a9852017-01-18 08:28:57 -080033#include "src/core/lib/support/env.h"
Ben Sykes02d426e2017-07-11 13:58:42 -070034#include "src/core/lib/support/string.h"
Mark D. Rothd58a9852017-01-18 08:28:57 -080035
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080036static char* grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx) {
Mark D. Rothd58a9852017-01-18 08:28:57 -080037 char* uri_str = gpr_getenv("http_proxy");
38 if (uri_str == NULL) return NULL;
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080039 grpc_uri* uri =
40 grpc_uri_parse(exec_ctx, uri_str, false /* suppress_errors */);
Mark D. Rothd58a9852017-01-18 08:28:57 -080041 char* proxy_name = NULL;
42 if (uri == NULL || uri->authority == NULL) {
43 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
44 goto done;
45 }
46 if (strcmp(uri->scheme, "http") != 0) {
47 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
48 goto done;
49 }
50 if (strchr(uri->authority, '@') != NULL) {
51 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
52 goto done;
53 }
54 proxy_name = gpr_strdup(uri->authority);
55done:
56 gpr_free(uri_str);
57 grpc_uri_destroy(uri);
58 return proxy_name;
59}
Mark D. Rothdc9bee72017-02-07 12:29:14 -080060
61static bool proxy_mapper_map_name(grpc_exec_ctx* exec_ctx,
62 grpc_proxy_mapper* mapper,
63 const char* server_uri,
64 const grpc_channel_args* args,
65 char** name_to_resolve,
66 grpc_channel_args** new_args) {
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080067 *name_to_resolve = grpc_get_http_proxy_server(exec_ctx);
Mark D. Rothdc9bee72017-02-07 12:29:14 -080068 if (*name_to_resolve == NULL) return false;
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080069 grpc_uri* uri =
70 grpc_uri_parse(exec_ctx, server_uri, false /* suppress_errors */);
Mark D. Rothdc9bee72017-02-07 12:29:14 -080071 if (uri == NULL || uri->path[0] == '\0') {
72 gpr_log(GPR_ERROR,
73 "'http_proxy' environment variable set, but cannot "
74 "parse server URI '%s' -- not using proxy",
75 server_uri);
76 if (uri != NULL) grpc_uri_destroy(uri);
77 return false;
78 }
Mark D. Roth38788592017-02-09 09:28:53 -080079 if (strcmp(uri->scheme, "unix") == 0) {
80 gpr_log(GPR_INFO, "not using proxy for Unix domain socket '%s'",
81 server_uri);
82 grpc_uri_destroy(uri);
83 return false;
84 }
Ben Sykes02d426e2017-07-11 13:58:42 -070085 char* no_proxy_str = gpr_getenv("no_proxy");
86 if (no_proxy_str != NULL) {
87 static const char* NO_PROXY_SEPARATOR = ",";
88 bool use_proxy = true;
89 char* server_host;
90 char* server_port;
91 if (!gpr_split_host_port(uri->path[0] == '/' ? uri->path + 1 : uri->path,
92 &server_host, &server_port)) {
93 gpr_log(GPR_INFO,
94 "unable to split host and port, not checking no_proxy list for "
95 "host '%s'",
96 server_uri);
97 } else {
98 size_t uri_len = strlen(server_host);
99 char** no_proxy_hosts;
100 size_t num_no_proxy_hosts;
101 gpr_string_split(no_proxy_str, NO_PROXY_SEPARATOR, &no_proxy_hosts,
102 &num_no_proxy_hosts);
103 for (size_t i = 0; i < num_no_proxy_hosts; i++) {
104 char* no_proxy_entry = no_proxy_hosts[i];
105 size_t no_proxy_len = strlen(no_proxy_entry);
106 if (no_proxy_len <= uri_len &&
107 gpr_stricmp(no_proxy_entry, &server_host[uri_len - no_proxy_len]) ==
108 0) {
109 gpr_log(GPR_INFO, "not using proxy for host in no_proxy list '%s'",
110 server_uri);
111 use_proxy = false;
112 break;
113 }
114 }
115 for (size_t i = 0; i < num_no_proxy_hosts; i++) {
116 gpr_free(no_proxy_hosts[i]);
117 }
118 gpr_free(no_proxy_hosts);
119 gpr_free(server_host);
120 gpr_free(server_port);
121 if (!use_proxy) {
122 grpc_uri_destroy(uri);
123 gpr_free(*name_to_resolve);
124 *name_to_resolve = NULL;
125 return false;
126 }
127 }
128 }
Mark D. Roth8d5e60b2017-06-09 09:08:23 -0700129 grpc_arg new_arg = grpc_channel_arg_string_create(
130 GRPC_ARG_HTTP_CONNECT_SERVER,
131 uri->path[0] == '/' ? uri->path + 1 : uri->path);
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800132 *new_args = grpc_channel_args_copy_and_add(args, &new_arg, 1);
133 grpc_uri_destroy(uri);
134 return true;
135}
136
137static bool proxy_mapper_map_address(grpc_exec_ctx* exec_ctx,
138 grpc_proxy_mapper* mapper,
139 const grpc_resolved_address* address,
140 const grpc_channel_args* args,
141 grpc_resolved_address** new_address,
142 grpc_channel_args** new_args) {
143 return false;
144}
145
146static void proxy_mapper_destroy(grpc_proxy_mapper* mapper) {}
147
148static const grpc_proxy_mapper_vtable proxy_mapper_vtable = {
149 proxy_mapper_map_name, proxy_mapper_map_address, proxy_mapper_destroy};
150
151static grpc_proxy_mapper proxy_mapper = {&proxy_mapper_vtable};
152
153void grpc_register_http_proxy_mapper() {
154 grpc_proxy_mapper_register(true /* at_start */, &proxy_mapper);
155}