blob: cfb5ec6f00eed71ca66cf103b1ff237480adc438 [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>
25#include <grpc/support/log.h>
26#include <grpc/support/string_util.h>
27
Craig Tiller9eb0fde2017-03-31 16:59:30 -070028#include "src/core/ext/filters/client_channel/http_connect_handshaker.h"
29#include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"
30#include "src/core/ext/filters/client_channel/uri_parser.h"
Mark D. Rothdc9bee72017-02-07 12:29:14 -080031#include "src/core/lib/channel/channel_args.h"
Mark D. Rothd58a9852017-01-18 08:28:57 -080032#include "src/core/lib/support/env.h"
33
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080034static char* grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx) {
Mark D. Rothd58a9852017-01-18 08:28:57 -080035 char* uri_str = gpr_getenv("http_proxy");
36 if (uri_str == NULL) return NULL;
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080037 grpc_uri* uri =
38 grpc_uri_parse(exec_ctx, uri_str, false /* suppress_errors */);
Mark D. Rothd58a9852017-01-18 08:28:57 -080039 char* proxy_name = NULL;
40 if (uri == NULL || uri->authority == NULL) {
41 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
42 goto done;
43 }
44 if (strcmp(uri->scheme, "http") != 0) {
45 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
46 goto done;
47 }
48 if (strchr(uri->authority, '@') != NULL) {
49 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
50 goto done;
51 }
52 proxy_name = gpr_strdup(uri->authority);
53done:
54 gpr_free(uri_str);
55 grpc_uri_destroy(uri);
56 return proxy_name;
57}
Mark D. Rothdc9bee72017-02-07 12:29:14 -080058
59static bool proxy_mapper_map_name(grpc_exec_ctx* exec_ctx,
60 grpc_proxy_mapper* mapper,
61 const char* server_uri,
62 const grpc_channel_args* args,
63 char** name_to_resolve,
64 grpc_channel_args** new_args) {
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080065 *name_to_resolve = grpc_get_http_proxy_server(exec_ctx);
Mark D. Rothdc9bee72017-02-07 12:29:14 -080066 if (*name_to_resolve == NULL) return false;
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080067 grpc_uri* uri =
68 grpc_uri_parse(exec_ctx, server_uri, false /* suppress_errors */);
Mark D. Rothdc9bee72017-02-07 12:29:14 -080069 if (uri == NULL || uri->path[0] == '\0') {
70 gpr_log(GPR_ERROR,
71 "'http_proxy' environment variable set, but cannot "
72 "parse server URI '%s' -- not using proxy",
73 server_uri);
74 if (uri != NULL) grpc_uri_destroy(uri);
75 return false;
76 }
Mark D. Roth38788592017-02-09 09:28:53 -080077 if (strcmp(uri->scheme, "unix") == 0) {
78 gpr_log(GPR_INFO, "not using proxy for Unix domain socket '%s'",
79 server_uri);
80 grpc_uri_destroy(uri);
81 return false;
82 }
Mark D. Roth8d5e60b2017-06-09 09:08:23 -070083 grpc_arg new_arg = grpc_channel_arg_string_create(
84 GRPC_ARG_HTTP_CONNECT_SERVER,
85 uri->path[0] == '/' ? uri->path + 1 : uri->path);
Mark D. Rothdc9bee72017-02-07 12:29:14 -080086 *new_args = grpc_channel_args_copy_and_add(args, &new_arg, 1);
87 grpc_uri_destroy(uri);
88 return true;
89}
90
91static bool proxy_mapper_map_address(grpc_exec_ctx* exec_ctx,
92 grpc_proxy_mapper* mapper,
93 const grpc_resolved_address* address,
94 const grpc_channel_args* args,
95 grpc_resolved_address** new_address,
96 grpc_channel_args** new_args) {
97 return false;
98}
99
100static void proxy_mapper_destroy(grpc_proxy_mapper* mapper) {}
101
102static const grpc_proxy_mapper_vtable proxy_mapper_vtable = {
103 proxy_mapper_map_name, proxy_mapper_map_address, proxy_mapper_destroy};
104
105static grpc_proxy_mapper proxy_mapper = {&proxy_mapper_vtable};
106
107void grpc_register_http_proxy_mapper() {
108 grpc_proxy_mapper_register(true /* at_start */, &proxy_mapper);
109}