blob: 1e198cba977c94524e5e7a63af70bab25f466307 [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. Rotheb35a1d2016-10-19 14:53:42 -070043#include "src/core/ext/client_channel/uri_parser.h"
Mark D. Roth30f698f2016-11-29 14:02:32 -080044#include "src/core/lib/channel/channel_args.h"
Mark D. Rothf7250192016-07-22 08:06:09 -070045#include "src/core/lib/http/format_request.h"
46#include "src/core/lib/http/parser.h"
Craig Tiller6822a7a2016-12-06 19:28:52 -080047#include "src/core/lib/slice/slice_internal.h"
Mark D. Roth39b58712016-09-06 12:50:42 -070048#include "src/core/lib/support/env.h"
Mark D. Roth4623e1c2016-07-18 14:09:18 -070049
50typedef struct http_connect_handshaker {
51 // Base class. Must be first.
52 grpc_handshaker base;
53
Mark D. Roth77613b22016-07-22 09:41:10 -070054 char* proxy_server;
Mark D. Rothe273b032016-07-22 13:24:21 -070055 char* server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -070056
Mark D. Rothb16c1e32016-11-14 12:08:13 -080057 gpr_refcount refcount;
58 gpr_mu mu;
59
Mark D. Roth30f698f2016-11-29 14:02:32 -080060 bool shutdown;
61 // Endpoint and read buffer to destroy after a shutdown.
62 grpc_endpoint* endpoint_to_destroy;
63 grpc_slice_buffer* read_buffer_to_destroy;
64
Mark D. Roth4623e1c2016-07-18 14:09:18 -070065 // State saved while performing the handshake.
Mark D. Roth27588bb2016-11-11 15:03:39 -080066 grpc_handshaker_args* args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080067 grpc_closure* on_handshake_done;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070068
69 // Objects for processing the HTTP CONNECT request and response.
Craig Tillerd41a4a72016-10-26 16:16:06 -070070 grpc_slice_buffer write_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070071 grpc_closure request_done_closure;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070072 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070073 grpc_http_parser http_parser;
74 grpc_http_response http_response;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070075} http_connect_handshaker;
76
Mark D. Roth5b4768f2016-08-04 13:41:46 -070077// Unref and clean up handshaker.
Craig Tiller87a7e1f2016-11-09 09:42:19 -080078static void http_connect_handshaker_unref(grpc_exec_ctx* exec_ctx,
79 http_connect_handshaker* handshaker) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -070080 if (gpr_unref(&handshaker->refcount)) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -080081 gpr_mu_destroy(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -080082 if (handshaker->endpoint_to_destroy != NULL) {
83 grpc_endpoint_destroy(exec_ctx, handshaker->endpoint_to_destroy);
84 }
85 if (handshaker->read_buffer_to_destroy != NULL) {
Craig Tiller6822a7a2016-12-06 19:28:52 -080086 grpc_slice_buffer_destroy_internal(exec_ctx,
87 handshaker->read_buffer_to_destroy);
Mark D. Roth30f698f2016-11-29 14:02:32 -080088 gpr_free(handshaker->read_buffer_to_destroy);
89 }
Mark D. Roth5b4768f2016-08-04 13:41:46 -070090 gpr_free(handshaker->proxy_server);
91 gpr_free(handshaker->server_name);
Craig Tiller6822a7a2016-12-06 19:28:52 -080092 grpc_slice_buffer_destroy_internal(exec_ctx, &handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -070093 grpc_http_parser_destroy(&handshaker->http_parser);
94 grpc_http_response_destroy(&handshaker->http_response);
95 gpr_free(handshaker);
96 }
97}
98
Mark D. Roth30f698f2016-11-29 14:02:32 -080099// Set args fields to NULL, saving the endpoint and read buffer for
100// later destruction.
101static void cleanup_args_for_failure_locked(
Craig Tillerb6821f62016-12-06 15:10:42 -0800102 grpc_exec_ctx* exec_ctx, http_connect_handshaker* handshaker) {
Mark D. Roth30f698f2016-11-29 14:02:32 -0800103 handshaker->endpoint_to_destroy = handshaker->args->endpoint;
104 handshaker->args->endpoint = NULL;
105 handshaker->read_buffer_to_destroy = handshaker->args->read_buffer;
106 handshaker->args->read_buffer = NULL;
Craig Tiller397bff32016-12-06 15:05:59 -0800107 grpc_channel_args_destroy(exec_ctx, handshaker->args->args);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800108 handshaker->args->args = NULL;
109}
110
Mark D. Rothc584d992016-11-30 09:45:32 -0800111// If the handshake failed or we're shutting down, clean up and invoke the
112// callback with the error.
113static void handshake_failed_locked(grpc_exec_ctx* exec_ctx,
114 http_connect_handshaker* handshaker,
115 grpc_error* error) {
116 if (error == GRPC_ERROR_NONE) {
117 // If we were shut down after an endpoint operation succeeded but
118 // before the endpoint callback was invoked, we need to generate our
119 // own error.
120 error = GRPC_ERROR_CREATE("Handshaker shutdown");
Mark D. Rotha47a2462016-08-11 09:02:30 -0700121 }
Mark D. Rothc584d992016-11-30 09:45:32 -0800122 if (!handshaker->shutdown) {
123 // TODO(ctiller): It is currently necessary to shutdown endpoints
124 // before destroying them, even if we know that there are no
125 // pending read/write callbacks. This should be fixed, at which
126 // point this can be removed.
127 grpc_endpoint_shutdown(exec_ctx, handshaker->args->endpoint);
128 // Not shutting down, so the handshake failed. Clean up before
129 // invoking the callback.
Craig Tiller397bff32016-12-06 15:05:59 -0800130 cleanup_args_for_failure_locked(exec_ctx, handshaker);
Mark D. Roth8aa2f702016-12-02 11:00:27 -0800131 // Set shutdown to true so that subsequent calls to
132 // http_connect_handshaker_shutdown() do nothing.
133 handshaker->shutdown = true;
Mark D. Rothc584d992016-11-30 09:45:32 -0800134 }
135 // Invoke callback.
136 grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done, error, NULL);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700137}
138
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700139// Callback invoked when finished writing HTTP CONNECT request.
140static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
141 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700142 http_connect_handshaker* handshaker = arg;
Mark D. Roth4cdcd122016-11-29 12:39:54 -0800143 gpr_mu_lock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800144 if (error != GRPC_ERROR_NONE || handshaker->shutdown) {
145 // If the write failed or we're shutting down, clean up and invoke the
146 // callback with the error.
Mark D. Rothc584d992016-11-30 09:45:32 -0800147 handshake_failed_locked(exec_ctx, handshaker, GRPC_ERROR_REF(error));
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800148 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800149 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700150 } else {
151 // Otherwise, read the response.
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800152 // The read callback inherits our ref to the handshaker.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800153 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
154 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700155 &handshaker->response_read_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800156 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700157 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700158}
159
Mark D. Rothf7250192016-07-22 08:06:09 -0700160// Callback invoked for reading HTTP CONNECT response.
161static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
162 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700163 http_connect_handshaker* handshaker = arg;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800164 gpr_mu_lock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800165 if (error != GRPC_ERROR_NONE || handshaker->shutdown) {
Mark D. Rothc584d992016-11-30 09:45:32 -0800166 // If the read failed or we're shutting down, clean up and invoke the
Mark D. Roth30f698f2016-11-29 14:02:32 -0800167 // callback with the error.
Mark D. Rothc584d992016-11-30 09:45:32 -0800168 handshake_failed_locked(exec_ctx, handshaker, GRPC_ERROR_REF(error));
Mark D. Rothb954df12016-08-01 09:53:52 -0700169 goto done;
170 }
171 // Add buffer to parser.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800172 for (size_t i = 0; i < handshaker->args->read_buffer->count; ++i) {
173 if (GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i]) > 0) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700174 size_t body_start_offset = 0;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700175 error = grpc_http_parser_parse(&handshaker->http_parser,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800176 handshaker->args->read_buffer->slices[i],
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700177 &body_start_offset);
Mark D. Rothc584d992016-11-30 09:45:32 -0800178 if (error != GRPC_ERROR_NONE) {
179 handshake_failed_locked(exec_ctx, handshaker, error);
180 goto done;
181 }
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700182 if (handshaker->http_parser.state == GRPC_HTTP_BODY) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700183 // Remove the data we've already read from the read buffer,
184 // leaving only the leftover bytes (if any).
Craig Tillerd41a4a72016-10-26 16:16:06 -0700185 grpc_slice_buffer tmp_buffer;
186 grpc_slice_buffer_init(&tmp_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700187 if (body_start_offset <
Mark D. Roth27588bb2016-11-11 15:03:39 -0800188 GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i])) {
Craig Tillerd41a4a72016-10-26 16:16:06 -0700189 grpc_slice_buffer_add(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700190 &tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800191 grpc_slice_split_tail(&handshaker->args->read_buffer->slices[i],
Craig Tiller28b72422016-10-26 21:15:29 -0700192 body_start_offset));
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700193 }
Craig Tillerd41a4a72016-10-26 16:16:06 -0700194 grpc_slice_buffer_addn(&tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800195 &handshaker->args->read_buffer->slices[i + 1],
196 handshaker->args->read_buffer->count - i - 1);
197 grpc_slice_buffer_swap(handshaker->args->read_buffer, &tmp_buffer);
Craig Tiller6822a7a2016-12-06 19:28:52 -0800198 grpc_slice_buffer_destroy_internal(exec_ctx, &tmp_buffer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700199 break;
200 }
Mark D. Rothf7250192016-07-22 08:06:09 -0700201 }
Mark D. Rothb954df12016-08-01 09:53:52 -0700202 }
203 // If we're not done reading the response, read more data.
204 // TODO(roth): In practice, I suspect that the response to a CONNECT
205 // request will never include a body, in which case this check is
206 // sufficient. However, the language of RFC-2817 doesn't explicitly
207 // forbid the response from including a body. If there is a body,
208 // it's possible that we might have parsed part but not all of the
209 // body, in which case this check will cause us to fail to parse the
210 // remainder of the body. If that ever becomes an issue, we may
211 // need to fix the HTTP parser to understand when the body is
212 // complete (e.g., handling chunked transfer encoding or looking
213 // at the Content-Length: header).
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700214 if (handshaker->http_parser.state != GRPC_HTTP_BODY) {
Craig Tiller6822a7a2016-12-06 19:28:52 -0800215 grpc_slice_buffer_reset_and_unref_internal(exec_ctx,
216 handshaker->args->read_buffer);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800217 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
218 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700219 &handshaker->response_read_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800220 gpr_mu_unlock(&handshaker->mu);
Mark D. Rothb954df12016-08-01 09:53:52 -0700221 return;
222 }
223 // Make sure we got a 2xx response.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700224 if (handshaker->http_response.status < 200 ||
225 handshaker->http_response.status >= 300) {
Mark D. Rothb954df12016-08-01 09:53:52 -0700226 char* msg;
227 gpr_asprintf(&msg, "HTTP proxy returned response code %d",
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700228 handshaker->http_response.status);
Mark D. Rothb954df12016-08-01 09:53:52 -0700229 error = GRPC_ERROR_CREATE(msg);
230 gpr_free(msg);
Mark D. Rothc584d992016-11-30 09:45:32 -0800231 handshake_failed_locked(exec_ctx, handshaker, error);
232 goto done;
Mark D. Rothf7250192016-07-22 08:06:09 -0700233 }
Mark D. Rothc584d992016-11-30 09:45:32 -0800234 // Success. Invoke handshake-done callback.
Mark D. Roth4b5cdb72016-11-14 11:37:36 -0800235 grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done, error, NULL);
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700236done:
Mark D. Roth53bd6932016-12-01 08:00:38 -0800237 // Set shutdown to true so that subsequent calls to
238 // http_connect_handshaker_shutdown() do nothing.
239 handshaker->shutdown = true;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800240 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800241 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Rothf7250192016-07-22 08:06:09 -0700242}
243
Mark D. Roth9136bb12016-07-21 09:52:12 -0700244//
245// Public handshaker methods
246//
247
248static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700249 grpc_handshaker* handshaker_in) {
250 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800251 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700252}
253
254static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800255 grpc_handshaker* handshaker_in) {
256 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
257 gpr_mu_lock(&handshaker->mu);
Mark D. Roth30f698f2016-11-29 14:02:32 -0800258 if (!handshaker->shutdown) {
259 handshaker->shutdown = true;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800260 grpc_endpoint_shutdown(exec_ctx, handshaker->args->endpoint);
Craig Tiller397bff32016-12-06 15:05:59 -0800261 cleanup_args_for_failure_locked(exec_ctx, handshaker);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800262 }
263 gpr_mu_unlock(&handshaker->mu);
264}
Mark D. Roth9136bb12016-07-21 09:52:12 -0700265
266static void http_connect_handshaker_do_handshake(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700267 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker_in,
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800268 grpc_tcp_server_acceptor* acceptor, grpc_closure* on_handshake_done,
269 grpc_handshaker_args* args) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700270 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800271 gpr_mu_lock(&handshaker->mu);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700272 // Save state in the handshaker object.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700273 handshaker->args = args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -0800274 handshaker->on_handshake_done = on_handshake_done;
Mark D. Rothf7250192016-07-22 08:06:09 -0700275 // Send HTTP CONNECT request.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700276 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s",
277 handshaker->server_name, handshaker->proxy_server);
Mark D. Rothf7250192016-07-22 08:06:09 -0700278 grpc_httpcli_request request;
279 memset(&request, 0, sizeof(request));
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700280 request.host = handshaker->proxy_server;
Mark D. Rothb954df12016-08-01 09:53:52 -0700281 request.http.method = "CONNECT";
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700282 request.http.path = handshaker->server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -0700283 request.handshaker = &grpc_httpcli_plaintext;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700284 grpc_slice request_slice = grpc_httpcli_format_connect_request(&request);
285 grpc_slice_buffer_add(&handshaker->write_buffer, request_slice);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800286 // Take a new ref to be held by the write callback.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700287 gpr_ref(&handshaker->refcount);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800288 grpc_endpoint_write(exec_ctx, args->endpoint, &handshaker->write_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700289 &handshaker->request_done_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800290 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700291}
292
Mark D. Roth27588bb2016-11-11 15:03:39 -0800293static const grpc_handshaker_vtable http_connect_handshaker_vtable = {
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700294 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
295 http_connect_handshaker_do_handshake};
296
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700297grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server,
298 const char* server_name) {
299 GPR_ASSERT(proxy_server != NULL);
300 GPR_ASSERT(server_name != NULL);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800301 http_connect_handshaker* handshaker = gpr_malloc(sizeof(*handshaker));
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700302 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700303 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800304 gpr_mu_init(&handshaker->mu);
305 gpr_ref_init(&handshaker->refcount, 1);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700306 handshaker->proxy_server = gpr_strdup(proxy_server);
307 handshaker->server_name = gpr_strdup(server_name);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700308 grpc_slice_buffer_init(&handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700309 grpc_closure_init(&handshaker->request_done_closure, on_write_done,
310 handshaker);
311 grpc_closure_init(&handshaker->response_read_closure, on_read_done,
312 handshaker);
313 grpc_http_parser_init(&handshaker->http_parser, GRPC_HTTP_RESPONSE,
314 &handshaker->http_response);
Mark D. Rotha47a2462016-08-11 09:02:30 -0700315 return &handshaker->base;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700316}
Mark D. Roth39b58712016-09-06 12:50:42 -0700317
318char* grpc_get_http_proxy_server() {
319 char* uri_str = gpr_getenv("http_proxy");
320 if (uri_str == NULL) return NULL;
321 grpc_uri* uri = grpc_uri_parse(uri_str, false /* suppress_errors */);
322 char* proxy_name = NULL;
323 if (uri == NULL || uri->authority == NULL) {
324 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
325 goto done;
326 }
327 if (strcmp(uri->scheme, "http") != 0) {
328 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
329 goto done;
330 }
331 if (strchr(uri->authority, '@') != NULL) {
332 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
333 goto done;
334 }
335 proxy_name = gpr_strdup(uri->authority);
336done:
Mark D. Rothd66a6022016-09-08 16:57:34 +0000337 gpr_free(uri_str);
Mark D. Roth39b58712016-09-06 12:50:42 -0700338 grpc_uri_destroy(uri);
339 return proxy_name;
340}