blob: a4ff379784408e625b166e89fc4a7a75673af2bd [file] [log] [blame]
Mark D. Roth4623e1c2016-07-18 14:09:18 -07001/*
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
Mark D. Rotheb35a1d2016-10-19 14:53:42 -070034#include "src/core/ext/client_channel/http_connect_handshaker.h"
Mark D. Roth0a05ab62016-08-04 13:10:13 -070035
Mark D. Roth4623e1c2016-07-18 14:09:18 -070036#include <string.h>
37
Craig Tiller28b72422016-10-26 21:15:29 -070038#include <grpc/slice_buffer.h>
Mark D. Roth5c280962016-09-16 13:23:58 -070039#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
Mark D. Roth77613b22016-07-22 09:41:10 -070041#include <grpc/support/string_util.h>
Mark D. Roth4623e1c2016-07-18 14:09:18 -070042
Mark D. Rothbe5e3ca2016-12-12 09:58:20 -080043#include "src/core/ext/client_channel/client_channel.h"
Mark D. Roth201db7d2016-12-12 09:36:02 -080044#include "src/core/ext/client_channel/resolver_registry.h"
Mark D. Rotheb35a1d2016-10-19 14:53:42 -070045#include "src/core/ext/client_channel/uri_parser.h"
Mark D. Roth30f698f2016-11-29 14:02:32 -080046#include "src/core/lib/channel/channel_args.h"
Mark D. Roth1f0f23c2017-01-06 13:07:19 -080047#include "src/core/lib/channel/handshaker_registry.h"
Mark D. Rothf7250192016-07-22 08:06:09 -070048#include "src/core/lib/http/format_request.h"
49#include "src/core/lib/http/parser.h"
Craig Tiller6822a7a2016-12-06 19:28:52 -080050#include "src/core/lib/slice/slice_internal.h"
Mark D. Roth39b58712016-09-06 12:50:42 -070051#include "src/core/lib/support/env.h"
Mark D. Rothb3242872017-01-18 07:46:30 -080052#include "src/core/lib/support/string.h"
Mark D. Roth4623e1c2016-07-18 14:09:18 -070053
54typedef struct http_connect_handshaker {
55 // Base class. Must be first.
56 grpc_handshaker base;
57
Mark D. Rothb16c1e32016-11-14 12:08:13 -080058 gpr_refcount refcount;
59 gpr_mu mu;
60
Mark D. Roth30f698f2016-11-29 14:02:32 -080061 bool shutdown;
62 // Endpoint and read buffer to destroy after a shutdown.
63 grpc_endpoint* endpoint_to_destroy;
64 grpc_slice_buffer* read_buffer_to_destroy;
65
Mark D. Roth4623e1c2016-07-18 14:09:18 -070066 // State saved while performing the handshake.
Mark D. Roth27588bb2016-11-11 15:03:39 -080067 grpc_handshaker_args* args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080068 grpc_closure* on_handshake_done;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070069
70 // Objects for processing the HTTP CONNECT request and response.
Craig Tillerd41a4a72016-10-26 16:16:06 -070071 grpc_slice_buffer write_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070072 grpc_closure request_done_closure;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070073 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070074 grpc_http_parser http_parser;
75 grpc_http_response http_response;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070076} http_connect_handshaker;
77
Mark D. Roth5b4768f2016-08-04 13:41:46 -070078// Unref and clean up handshaker.
Craig Tiller87a7e1f2016-11-09 09:42:19 -080079static void http_connect_handshaker_unref(grpc_exec_ctx* exec_ctx,
80 http_connect_handshaker* handshaker) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -070081 if (gpr_unref(&handshaker->refcount)) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -080082 gpr_mu_destroy(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -080083 if (handshaker->endpoint_to_destroy != NULL) {
84 grpc_endpoint_destroy(exec_ctx, handshaker->endpoint_to_destroy);
85 }
86 if (handshaker->read_buffer_to_destroy != NULL) {
Craig Tiller6822a7a2016-12-06 19:28:52 -080087 grpc_slice_buffer_destroy_internal(exec_ctx,
88 handshaker->read_buffer_to_destroy);
Mark D. Roth30f698f2016-11-29 14:02:32 -080089 gpr_free(handshaker->read_buffer_to_destroy);
90 }
Craig Tiller6822a7a2016-12-06 19:28:52 -080091 grpc_slice_buffer_destroy_internal(exec_ctx, &handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -070092 grpc_http_parser_destroy(&handshaker->http_parser);
93 grpc_http_response_destroy(&handshaker->http_response);
94 gpr_free(handshaker);
95 }
96}
97
Mark D. Roth30f698f2016-11-29 14:02:32 -080098// Set args fields to NULL, saving the endpoint and read buffer for
99// later destruction.
100static void cleanup_args_for_failure_locked(
Craig Tillerb6821f62016-12-06 15:10:42 -0800101 grpc_exec_ctx* exec_ctx, http_connect_handshaker* handshaker) {
Mark D. Roth30f698f2016-11-29 14:02:32 -0800102 handshaker->endpoint_to_destroy = handshaker->args->endpoint;
103 handshaker->args->endpoint = NULL;
104 handshaker->read_buffer_to_destroy = handshaker->args->read_buffer;
105 handshaker->args->read_buffer = NULL;
Craig Tiller397bff32016-12-06 15:05:59 -0800106 grpc_channel_args_destroy(exec_ctx, handshaker->args->args);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800107 handshaker->args->args = NULL;
108}
109
Mark D. Rothc584d992016-11-30 09:45:32 -0800110// If the handshake failed or we're shutting down, clean up and invoke the
111// callback with the error.
112static void handshake_failed_locked(grpc_exec_ctx* exec_ctx,
113 http_connect_handshaker* handshaker,
114 grpc_error* error) {
115 if (error == GRPC_ERROR_NONE) {
116 // If we were shut down after an endpoint operation succeeded but
117 // before the endpoint callback was invoked, we need to generate our
118 // own error.
119 error = GRPC_ERROR_CREATE("Handshaker shutdown");
Mark D. Rotha47a2462016-08-11 09:02:30 -0700120 }
Mark D. Rothc584d992016-11-30 09:45:32 -0800121 if (!handshaker->shutdown) {
122 // TODO(ctiller): It is currently necessary to shutdown endpoints
123 // before destroying them, even if we know that there are no
124 // pending read/write callbacks. This should be fixed, at which
125 // point this can be removed.
126 grpc_endpoint_shutdown(exec_ctx, handshaker->args->endpoint);
127 // Not shutting down, so the handshake failed. Clean up before
128 // invoking the callback.
Craig Tiller397bff32016-12-06 15:05:59 -0800129 cleanup_args_for_failure_locked(exec_ctx, handshaker);
Mark D. Roth8aa2f702016-12-02 11:00:27 -0800130 // Set shutdown to true so that subsequent calls to
131 // http_connect_handshaker_shutdown() do nothing.
132 handshaker->shutdown = true;
Mark D. Rothc584d992016-11-30 09:45:32 -0800133 }
134 // Invoke callback.
Craig Tiller91031da2016-12-28 15:44:25 -0800135 grpc_closure_sched(exec_ctx, handshaker->on_handshake_done, error);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700136}
137
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700138// Callback invoked when finished writing HTTP CONNECT request.
139static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
140 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700141 http_connect_handshaker* handshaker = arg;
Mark D. Roth4cdcd122016-11-29 12:39:54 -0800142 gpr_mu_lock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800143 if (error != GRPC_ERROR_NONE || handshaker->shutdown) {
144 // If the write failed or we're shutting down, clean up and invoke the
145 // callback with the error.
Mark D. Rothc584d992016-11-30 09:45:32 -0800146 handshake_failed_locked(exec_ctx, handshaker, GRPC_ERROR_REF(error));
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800147 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800148 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700149 } else {
150 // Otherwise, read the response.
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800151 // The read callback inherits our ref to the handshaker.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800152 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
153 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700154 &handshaker->response_read_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800155 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700156 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700157}
158
Mark D. Rothf7250192016-07-22 08:06:09 -0700159// Callback invoked for reading HTTP CONNECT response.
160static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
161 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700162 http_connect_handshaker* handshaker = arg;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800163 gpr_mu_lock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800164 if (error != GRPC_ERROR_NONE || handshaker->shutdown) {
Mark D. Rothc584d992016-11-30 09:45:32 -0800165 // If the read failed or we're shutting down, clean up and invoke the
Mark D. Roth30f698f2016-11-29 14:02:32 -0800166 // callback with the error.
Mark D. Rothc584d992016-11-30 09:45:32 -0800167 handshake_failed_locked(exec_ctx, handshaker, GRPC_ERROR_REF(error));
Mark D. Rothb954df12016-08-01 09:53:52 -0700168 goto done;
169 }
170 // Add buffer to parser.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800171 for (size_t i = 0; i < handshaker->args->read_buffer->count; ++i) {
172 if (GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i]) > 0) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700173 size_t body_start_offset = 0;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700174 error = grpc_http_parser_parse(&handshaker->http_parser,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800175 handshaker->args->read_buffer->slices[i],
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700176 &body_start_offset);
Mark D. Rothc584d992016-11-30 09:45:32 -0800177 if (error != GRPC_ERROR_NONE) {
178 handshake_failed_locked(exec_ctx, handshaker, error);
179 goto done;
180 }
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700181 if (handshaker->http_parser.state == GRPC_HTTP_BODY) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700182 // Remove the data we've already read from the read buffer,
183 // leaving only the leftover bytes (if any).
Craig Tillerd41a4a72016-10-26 16:16:06 -0700184 grpc_slice_buffer tmp_buffer;
185 grpc_slice_buffer_init(&tmp_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700186 if (body_start_offset <
Mark D. Roth27588bb2016-11-11 15:03:39 -0800187 GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i])) {
Craig Tillerd41a4a72016-10-26 16:16:06 -0700188 grpc_slice_buffer_add(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700189 &tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800190 grpc_slice_split_tail(&handshaker->args->read_buffer->slices[i],
Craig Tiller28b72422016-10-26 21:15:29 -0700191 body_start_offset));
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700192 }
Craig Tillerd41a4a72016-10-26 16:16:06 -0700193 grpc_slice_buffer_addn(&tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800194 &handshaker->args->read_buffer->slices[i + 1],
195 handshaker->args->read_buffer->count - i - 1);
196 grpc_slice_buffer_swap(handshaker->args->read_buffer, &tmp_buffer);
Craig Tiller6822a7a2016-12-06 19:28:52 -0800197 grpc_slice_buffer_destroy_internal(exec_ctx, &tmp_buffer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700198 break;
199 }
Mark D. Rothf7250192016-07-22 08:06:09 -0700200 }
Mark D. Rothb954df12016-08-01 09:53:52 -0700201 }
202 // If we're not done reading the response, read more data.
203 // TODO(roth): In practice, I suspect that the response to a CONNECT
204 // request will never include a body, in which case this check is
205 // sufficient. However, the language of RFC-2817 doesn't explicitly
206 // forbid the response from including a body. If there is a body,
207 // it's possible that we might have parsed part but not all of the
208 // body, in which case this check will cause us to fail to parse the
209 // remainder of the body. If that ever becomes an issue, we may
210 // need to fix the HTTP parser to understand when the body is
211 // complete (e.g., handling chunked transfer encoding or looking
212 // at the Content-Length: header).
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700213 if (handshaker->http_parser.state != GRPC_HTTP_BODY) {
Craig Tiller6822a7a2016-12-06 19:28:52 -0800214 grpc_slice_buffer_reset_and_unref_internal(exec_ctx,
215 handshaker->args->read_buffer);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800216 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
217 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700218 &handshaker->response_read_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800219 gpr_mu_unlock(&handshaker->mu);
Mark D. Rothb954df12016-08-01 09:53:52 -0700220 return;
221 }
222 // Make sure we got a 2xx response.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700223 if (handshaker->http_response.status < 200 ||
224 handshaker->http_response.status >= 300) {
Mark D. Rothb954df12016-08-01 09:53:52 -0700225 char* msg;
226 gpr_asprintf(&msg, "HTTP proxy returned response code %d",
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700227 handshaker->http_response.status);
Mark D. Rothb954df12016-08-01 09:53:52 -0700228 error = GRPC_ERROR_CREATE(msg);
229 gpr_free(msg);
Mark D. Rothc584d992016-11-30 09:45:32 -0800230 handshake_failed_locked(exec_ctx, handshaker, error);
231 goto done;
Mark D. Rothf7250192016-07-22 08:06:09 -0700232 }
Mark D. Rothc584d992016-11-30 09:45:32 -0800233 // Success. Invoke handshake-done callback.
Craig Tiller91031da2016-12-28 15:44:25 -0800234 grpc_closure_sched(exec_ctx, handshaker->on_handshake_done, error);
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700235done:
Mark D. Roth53bd6932016-12-01 08:00:38 -0800236 // Set shutdown to true so that subsequent calls to
237 // http_connect_handshaker_shutdown() do nothing.
238 handshaker->shutdown = true;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800239 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800240 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Rothf7250192016-07-22 08:06:09 -0700241}
242
Mark D. Roth9136bb12016-07-21 09:52:12 -0700243//
244// Public handshaker methods
245//
246
247static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700248 grpc_handshaker* handshaker_in) {
249 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800250 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700251}
252
253static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800254 grpc_handshaker* handshaker_in) {
255 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
256 gpr_mu_lock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800257 if (!handshaker->shutdown) {
258 handshaker->shutdown = true;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800259 grpc_endpoint_shutdown(exec_ctx, handshaker->args->endpoint);
Craig Tiller397bff32016-12-06 15:05:59 -0800260 cleanup_args_for_failure_locked(exec_ctx, handshaker);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800261 }
262 gpr_mu_unlock(&handshaker->mu);
263}
Mark D. Roth9136bb12016-07-21 09:52:12 -0700264
265static void http_connect_handshaker_do_handshake(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700266 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker_in,
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800267 grpc_tcp_server_acceptor* acceptor, grpc_closure* on_handshake_done,
268 grpc_handshaker_args* args) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700269 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Mark D. Roth37225c42017-01-18 07:23:36 -0800270 // Check for HTTP CONNECT channel arg.
271 // If not found, invoke on_handshake_done without doing anything.
272 const grpc_arg* arg =
273 grpc_channel_args_find(args->args, GRPC_ARG_HTTP_CONNECT_SERVER);
274 if (arg == NULL) {
275 grpc_closure_sched(exec_ctx, on_handshake_done, GRPC_ERROR_NONE);
276 return;
277 }
Mark D. Rothb3242872017-01-18 07:46:30 -0800278 GPR_ASSERT(arg->type == GRPC_ARG_STRING);
Mark D. Roth37225c42017-01-18 07:23:36 -0800279 char* server_name = arg->value.string;
Mark D. Rothb3242872017-01-18 07:46:30 -0800280 // Get headers from channel args.
281 arg = grpc_channel_args_find(args->args, GRPC_ARG_HTTP_CONNECT_HEADERS);
282 grpc_http_header* headers = NULL;
283 size_t num_headers = 0;
284 char** header_strings = NULL;
285 size_t num_header_strings = 0;
286 if (arg != NULL) {
287 GPR_ASSERT(arg->type == GRPC_ARG_STRING);
288 gpr_string_split(arg->value.string, "\n", &header_strings,
289 &num_header_strings);
290 headers = gpr_malloc(sizeof(grpc_http_header) * num_header_strings);
291 for (size_t i = 0; i < num_header_strings; ++i) {
292 char* sep = strchr(header_strings[i], ':');
293 if (sep == NULL) {
294 gpr_log(GPR_ERROR, "skipping unparseable HTTP CONNECT header: %s",
295 header_strings[i]);
296 continue;
297 }
298 *sep = '\0';
299 headers[num_headers].key = header_strings[i];
300 headers[num_headers].value = sep + 1;
301 ++num_headers;
302 }
303 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700304 // Save state in the handshaker object.
Mark D. Roth201db7d2016-12-12 09:36:02 -0800305 gpr_mu_lock(&handshaker->mu);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700306 handshaker->args = args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -0800307 handshaker->on_handshake_done = on_handshake_done;
Mark D. Rothb3242872017-01-18 07:46:30 -0800308 // Log connection via proxy.
Mark D. Roth37225c42017-01-18 07:23:36 -0800309 char* proxy_name = grpc_endpoint_get_peer(args->endpoint);
Mark D. Roth201db7d2016-12-12 09:36:02 -0800310 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s", server_name,
Mark D. Roth37225c42017-01-18 07:23:36 -0800311 proxy_name);
312 gpr_free(proxy_name);
Mark D. Rothb3242872017-01-18 07:46:30 -0800313 // Construct HTTP CONNECT request.
Mark D. Rothf7250192016-07-22 08:06:09 -0700314 grpc_httpcli_request request;
315 memset(&request, 0, sizeof(request));
Mark D. Roth201db7d2016-12-12 09:36:02 -0800316 request.host = server_name;
Mark D. Rothb954df12016-08-01 09:53:52 -0700317 request.http.method = "CONNECT";
Mark D. Roth201db7d2016-12-12 09:36:02 -0800318 request.http.path = server_name;
Mark D. Rothb3242872017-01-18 07:46:30 -0800319 request.http.hdrs = headers;
320 request.http.hdr_count = num_headers;
Mark D. Roth77613b22016-07-22 09:41:10 -0700321 request.handshaker = &grpc_httpcli_plaintext;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700322 grpc_slice request_slice = grpc_httpcli_format_connect_request(&request);
323 grpc_slice_buffer_add(&handshaker->write_buffer, request_slice);
Mark D. Rothb3242872017-01-18 07:46:30 -0800324 // Clean up.
325 gpr_free(headers);
326 for (size_t i = 0; i < num_header_strings; ++i) {
327 gpr_free(header_strings[i]);
328 }
329 gpr_free(header_strings);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800330 // Take a new ref to be held by the write callback.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700331 gpr_ref(&handshaker->refcount);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800332 grpc_endpoint_write(exec_ctx, args->endpoint, &handshaker->write_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700333 &handshaker->request_done_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800334 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700335}
336
Mark D. Roth27588bb2016-11-11 15:03:39 -0800337static const grpc_handshaker_vtable http_connect_handshaker_vtable = {
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700338 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
339 http_connect_handshaker_do_handshake};
340
Mark D. Rothb3242872017-01-18 07:46:30 -0800341static grpc_handshaker* grpc_http_connect_handshaker_create() {
Mark D. Roth27588bb2016-11-11 15:03:39 -0800342 http_connect_handshaker* handshaker = gpr_malloc(sizeof(*handshaker));
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700343 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700344 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800345 gpr_mu_init(&handshaker->mu);
346 gpr_ref_init(&handshaker->refcount, 1);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700347 grpc_slice_buffer_init(&handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700348 grpc_closure_init(&handshaker->request_done_closure, on_write_done,
Craig Tiller91031da2016-12-28 15:44:25 -0800349 handshaker, grpc_schedule_on_exec_ctx);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700350 grpc_closure_init(&handshaker->response_read_closure, on_read_done,
Craig Tiller91031da2016-12-28 15:44:25 -0800351 handshaker, grpc_schedule_on_exec_ctx);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700352 grpc_http_parser_init(&handshaker->http_parser, GRPC_HTTP_RESPONSE,
353 &handshaker->http_response);
Mark D. Rotha47a2462016-08-11 09:02:30 -0700354 return &handshaker->base;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700355}
Mark D. Roth39b58712016-09-06 12:50:42 -0700356
357char* grpc_get_http_proxy_server() {
358 char* uri_str = gpr_getenv("http_proxy");
359 if (uri_str == NULL) return NULL;
360 grpc_uri* uri = grpc_uri_parse(uri_str, false /* suppress_errors */);
361 char* proxy_name = NULL;
362 if (uri == NULL || uri->authority == NULL) {
363 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
364 goto done;
365 }
366 if (strcmp(uri->scheme, "http") != 0) {
367 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
368 goto done;
369 }
370 if (strchr(uri->authority, '@') != NULL) {
371 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
372 goto done;
373 }
374 proxy_name = gpr_strdup(uri->authority);
375done:
Mark D. Rothd66a6022016-09-08 16:57:34 +0000376 gpr_free(uri_str);
Mark D. Roth39b58712016-09-06 12:50:42 -0700377 grpc_uri_destroy(uri);
378 return proxy_name;
379}
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800380
381//
382// handshaker factory
383//
384
385static void handshaker_factory_add_handshakers(
386 grpc_exec_ctx* exec_ctx, grpc_handshaker_factory* factory,
387 const grpc_channel_args* args, grpc_handshake_manager* handshake_mgr) {
Mark D. Roth37225c42017-01-18 07:23:36 -0800388 grpc_handshake_manager_add(handshake_mgr,
Mark D. Rothb3242872017-01-18 07:46:30 -0800389 grpc_http_connect_handshaker_create());
Mark D. Roth1f0f23c2017-01-06 13:07:19 -0800390}
391
392static void handshaker_factory_destroy(grpc_exec_ctx* exec_ctx,
393 grpc_handshaker_factory* factory) {}
394
395static const grpc_handshaker_factory_vtable handshaker_factory_vtable = {
396 handshaker_factory_add_handshakers, handshaker_factory_destroy};
397
398static grpc_handshaker_factory handshaker_factory = {
399 &handshaker_factory_vtable};
400
401void grpc_http_connect_register_handshaker_factory() {
402 grpc_handshaker_factory_register(true /* at_start */, HANDSHAKER_CLIENT,
403 &handshaker_factory);
404}