Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 19 | #include "src/core/ext/filters/client_channel/http_proxy.h" |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 20 | |
| 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 Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 28 | #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. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 31 | #include "src/core/lib/channel/channel_args.h" |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 32 | #include "src/core/lib/support/env.h" |
| 33 | |
David Garcia Quintas | dcb71e0 | 2017-03-06 10:16:26 -0800 | [diff] [blame] | 34 | static char* grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx) { |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 35 | char* uri_str = gpr_getenv("http_proxy"); |
| 36 | if (uri_str == NULL) return NULL; |
David Garcia Quintas | dcb71e0 | 2017-03-06 10:16:26 -0800 | [diff] [blame] | 37 | grpc_uri* uri = |
| 38 | grpc_uri_parse(exec_ctx, uri_str, false /* suppress_errors */); |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 39 | 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); |
| 53 | done: |
| 54 | gpr_free(uri_str); |
| 55 | grpc_uri_destroy(uri); |
| 56 | return proxy_name; |
| 57 | } |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 58 | |
| 59 | static 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 Quintas | dcb71e0 | 2017-03-06 10:16:26 -0800 | [diff] [blame] | 65 | *name_to_resolve = grpc_get_http_proxy_server(exec_ctx); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 66 | if (*name_to_resolve == NULL) return false; |
David Garcia Quintas | dcb71e0 | 2017-03-06 10:16:26 -0800 | [diff] [blame] | 67 | grpc_uri* uri = |
| 68 | grpc_uri_parse(exec_ctx, server_uri, false /* suppress_errors */); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 69 | 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. Roth | 3878859 | 2017-02-09 09:28:53 -0800 | [diff] [blame] | 77 | 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. Roth | 8d5e60b | 2017-06-09 09:08:23 -0700 | [diff] [blame^] | 83 | 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. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 86 | *new_args = grpc_channel_args_copy_and_add(args, &new_arg, 1); |
| 87 | grpc_uri_destroy(uri); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | static 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 | |
| 100 | static void proxy_mapper_destroy(grpc_proxy_mapper* mapper) {} |
| 101 | |
| 102 | static const grpc_proxy_mapper_vtable proxy_mapper_vtable = { |
| 103 | proxy_mapper_map_name, proxy_mapper_map_address, proxy_mapper_destroy}; |
| 104 | |
| 105 | static grpc_proxy_mapper proxy_mapper = {&proxy_mapper_vtable}; |
| 106 | |
| 107 | void grpc_register_http_proxy_mapper() { |
| 108 | grpc_proxy_mapper_register(true /* at_start */, &proxy_mapper); |
| 109 | } |