blob: faa4b3c319cc70176f563dadc880156d192ff788 [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"
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070033#include "src/core/lib/support/string.h"
34#include "src/core/lib/slice/b64.h"
Mark D. Rothd58a9852017-01-18 08:28:57 -080035
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070036static void grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx,
37 char **name_to_resolve,
38 char **user_cred) {
39 *name_to_resolve = NULL;
Mark D. Rothd58a9852017-01-18 08:28:57 -080040 char* uri_str = gpr_getenv("http_proxy");
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070041 if (uri_str == NULL) return;
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080042 grpc_uri* uri =
43 grpc_uri_parse(exec_ctx, uri_str, false /* suppress_errors */);
Mark D. Rothd58a9852017-01-18 08:28:57 -080044 if (uri == NULL || uri->authority == NULL) {
45 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
46 goto done;
47 }
48 if (strcmp(uri->scheme, "http") != 0) {
49 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
50 goto done;
51 }
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070052 char *user_cred_end = strchr(uri->authority, '@');
53 if (user_cred_end != NULL) {
54 *name_to_resolve = gpr_strdup(user_cred_end + 1);
55 *user_cred_end = '\0';
56 *user_cred = gpr_strdup(uri->authority);
57 gpr_log(GPR_INFO, "userinfo found in proxy URI");
58 } else {
59 *name_to_resolve = gpr_strdup(uri->authority);
Mark D. Rothd58a9852017-01-18 08:28:57 -080060 }
Mark D. Rothd58a9852017-01-18 08:28:57 -080061done:
62 gpr_free(uri_str);
63 grpc_uri_destroy(uri);
Mark D. Rothd58a9852017-01-18 08:28:57 -080064}
Mark D. Rothdc9bee72017-02-07 12:29:14 -080065
66static bool proxy_mapper_map_name(grpc_exec_ctx* exec_ctx,
67 grpc_proxy_mapper* mapper,
68 const char* server_uri,
69 const grpc_channel_args* args,
70 char** name_to_resolve,
71 grpc_channel_args** new_args) {
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070072 char *user_cred = NULL;
73 grpc_get_http_proxy_server(exec_ctx, name_to_resolve, &user_cred);
Mark D. Rothdc9bee72017-02-07 12:29:14 -080074 if (*name_to_resolve == NULL) return false;
David Garcia Quintasdcb71e02017-03-06 10:16:26 -080075 grpc_uri* uri =
76 grpc_uri_parse(exec_ctx, server_uri, false /* suppress_errors */);
Mark D. Rothdc9bee72017-02-07 12:29:14 -080077 if (uri == NULL || uri->path[0] == '\0') {
78 gpr_log(GPR_ERROR,
79 "'http_proxy' environment variable set, but cannot "
80 "parse server URI '%s' -- not using proxy",
81 server_uri);
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070082 if (uri != NULL) {
83 gpr_free(user_cred);
84 grpc_uri_destroy(uri);
85 }
Mark D. Rothdc9bee72017-02-07 12:29:14 -080086 return false;
87 }
Mark D. Roth38788592017-02-09 09:28:53 -080088 if (strcmp(uri->scheme, "unix") == 0) {
89 gpr_log(GPR_INFO, "not using proxy for Unix domain socket '%s'",
90 server_uri);
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070091 gpr_free(user_cred);
Mark D. Roth38788592017-02-09 09:28:53 -080092 grpc_uri_destroy(uri);
93 return false;
94 }
Yash Tibrewalf7350ea2017-07-19 10:26:41 -070095
96 grpc_arg args_to_add[2];
97 args_to_add[0] = grpc_channel_arg_string_create(
Mark D. Roth8d5e60b2017-06-09 09:08:23 -070098 GRPC_ARG_HTTP_CONNECT_SERVER,
99 uri->path[0] == '/' ? uri->path + 1 : uri->path);
Yash Tibrewalf7350ea2017-07-19 10:26:41 -0700100
101 if(user_cred != NULL) {
102 /* Use base64 encoding for user credentials */
103 char *encoded_user_cred =
104 grpc_base64_encode(user_auth, strlen(user_cred), 0, 0);
105 char *header;
106 gpr_asprintf(&header, "Proxy-Authorization:Basic %s", encoded_user_cred);
107 gpr_free(encoded_user_cred);
108 args_to_add[1] = grpc_channel_arg_string_create(
109 GRPC_ARG_HTTP_CONNECT_HEADERS, header);
110 *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 2);
111 gpr_free(header);
112 } else {
113 *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 1);
114 }
115 gpr_free(user_cred);
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800116 grpc_uri_destroy(uri);
117 return true;
118}
119
120static bool proxy_mapper_map_address(grpc_exec_ctx* exec_ctx,
121 grpc_proxy_mapper* mapper,
122 const grpc_resolved_address* address,
123 const grpc_channel_args* args,
124 grpc_resolved_address** new_address,
125 grpc_channel_args** new_args) {
126 return false;
127}
128
129static void proxy_mapper_destroy(grpc_proxy_mapper* mapper) {}
130
131static const grpc_proxy_mapper_vtable proxy_mapper_vtable = {
132 proxy_mapper_map_name, proxy_mapper_map_address, proxy_mapper_destroy};
133
134static grpc_proxy_mapper proxy_mapper = {&proxy_mapper_vtable};
135
136void grpc_register_http_proxy_mapper() {
137 grpc_proxy_mapper_register(true /* at_start */, &proxy_mapper);
138}