blob: bbe4ff550c6539dc4d9f9c4ebc9cc4a6abb32d5f [file] [log] [blame]
Mark D. Rothd58a9852017-01-18 08:28:57 -08001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/ext/client_channel/http_proxy.h"
35
36#include <stdbool.h>
37#include <string.h>
38
39#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
41#include <grpc/support/string_util.h>
42
Mark D. Rothdc9bee72017-02-07 12:29:14 -080043#include "src/core/ext/client_channel/http_connect_handshaker.h"
44#include "src/core/ext/client_channel/proxy_mapper_registry.h"
Mark D. Rothd58a9852017-01-18 08:28:57 -080045#include "src/core/ext/client_channel/uri_parser.h"
Mark D. Rothdc9bee72017-02-07 12:29:14 -080046#include "src/core/lib/channel/channel_args.h"
Mark D. Rothd58a9852017-01-18 08:28:57 -080047#include "src/core/lib/support/env.h"
48
Mark D. Rothdc9bee72017-02-07 12:29:14 -080049static char* grpc_get_http_proxy_server() {
Mark D. Rothd58a9852017-01-18 08:28:57 -080050 char* uri_str = gpr_getenv("http_proxy");
51 if (uri_str == NULL) return NULL;
52 grpc_uri* uri = grpc_uri_parse(uri_str, false /* suppress_errors */);
53 char* proxy_name = NULL;
54 if (uri == NULL || uri->authority == NULL) {
55 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
56 goto done;
57 }
58 if (strcmp(uri->scheme, "http") != 0) {
59 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
60 goto done;
61 }
62 if (strchr(uri->authority, '@') != NULL) {
63 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
64 goto done;
65 }
66 proxy_name = gpr_strdup(uri->authority);
67done:
68 gpr_free(uri_str);
69 grpc_uri_destroy(uri);
70 return proxy_name;
71}
Mark D. Rothdc9bee72017-02-07 12:29:14 -080072
73static bool proxy_mapper_map_name(grpc_exec_ctx* exec_ctx,
74 grpc_proxy_mapper* mapper,
75 const char* server_uri,
76 const grpc_channel_args* args,
77 char** name_to_resolve,
78 grpc_channel_args** new_args) {
79 *name_to_resolve = grpc_get_http_proxy_server();
80 if (*name_to_resolve == NULL) return false;
81 grpc_uri* uri = grpc_uri_parse(server_uri, false /* suppress_errors */);
82 if (uri == NULL || uri->path[0] == '\0') {
83 gpr_log(GPR_ERROR,
84 "'http_proxy' environment variable set, but cannot "
85 "parse server URI '%s' -- not using proxy",
86 server_uri);
87 if (uri != NULL) grpc_uri_destroy(uri);
88 return false;
89 }
Mark D. Roth38788592017-02-09 09:28:53 -080090 if (strcmp(uri->scheme, "unix") == 0) {
91 gpr_log(GPR_INFO, "not using proxy for Unix domain socket '%s'",
92 server_uri);
93 grpc_uri_destroy(uri);
94 return false;
95 }
Mark D. Rothdc9bee72017-02-07 12:29:14 -080096 grpc_arg new_arg;
97 new_arg.key = GRPC_ARG_HTTP_CONNECT_SERVER;
98 new_arg.type = GRPC_ARG_STRING;
99 new_arg.value.string = uri->path[0] == '/' ? uri->path + 1 : uri->path;
100 *new_args = grpc_channel_args_copy_and_add(args, &new_arg, 1);
101 grpc_uri_destroy(uri);
102 return true;
103}
104
105static bool proxy_mapper_map_address(grpc_exec_ctx* exec_ctx,
106 grpc_proxy_mapper* mapper,
107 const grpc_resolved_address* address,
108 const grpc_channel_args* args,
109 grpc_resolved_address** new_address,
110 grpc_channel_args** new_args) {
111 return false;
112}
113
114static void proxy_mapper_destroy(grpc_proxy_mapper* mapper) {}
115
116static const grpc_proxy_mapper_vtable proxy_mapper_vtable = {
117 proxy_mapper_map_name, proxy_mapper_map_address, proxy_mapper_destroy};
118
119static grpc_proxy_mapper proxy_mapper = {&proxy_mapper_vtable};
120
121void grpc_register_http_proxy_mapper() {
122 grpc_proxy_mapper_register(true /* at_start */, &proxy_mapper);
123}