blob: 407b8ce0232c23c98d07083a3ce0615ce6c25e79 [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. Rothf7250192016-07-22 08:06:09 -070044#include "src/core/lib/http/format_request.h"
45#include "src/core/lib/http/parser.h"
Mark D. Roth39b58712016-09-06 12:50:42 -070046#include "src/core/lib/support/env.h"
Mark D. Roth4623e1c2016-07-18 14:09:18 -070047
48typedef struct http_connect_handshaker {
49 // Base class. Must be first.
50 grpc_handshaker base;
51
Mark D. Roth77613b22016-07-22 09:41:10 -070052 char* proxy_server;
Mark D. Rothe273b032016-07-22 13:24:21 -070053 char* server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -070054
Mark D. Rothb16c1e32016-11-14 12:08:13 -080055 gpr_refcount refcount;
56 gpr_mu mu;
57
Mark D. Roth4623e1c2016-07-18 14:09:18 -070058 // State saved while performing the handshake.
Mark D. Roth27588bb2016-11-11 15:03:39 -080059 grpc_handshaker_args* args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080060 grpc_closure* on_handshake_done;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070061
62 // Objects for processing the HTTP CONNECT request and response.
Craig Tillerd41a4a72016-10-26 16:16:06 -070063 grpc_slice_buffer write_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070064 grpc_closure request_done_closure;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070065 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070066 grpc_http_parser http_parser;
67 grpc_http_response http_response;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070068} http_connect_handshaker;
69
Mark D. Roth5b4768f2016-08-04 13:41:46 -070070// Unref and clean up handshaker.
71static void http_connect_handshaker_unref(http_connect_handshaker* handshaker) {
72 if (gpr_unref(&handshaker->refcount)) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -080073 gpr_mu_destroy(&handshaker->mu);
Mark D. Roth5b4768f2016-08-04 13:41:46 -070074 gpr_free(handshaker->proxy_server);
75 gpr_free(handshaker->server_name);
Craig Tillerd41a4a72016-10-26 16:16:06 -070076 grpc_slice_buffer_destroy(&handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -070077 grpc_http_parser_destroy(&handshaker->http_parser);
78 grpc_http_response_destroy(&handshaker->http_response);
79 gpr_free(handshaker);
80 }
81}
82
Mark D. Roth4623e1c2016-07-18 14:09:18 -070083// Callback invoked when finished writing HTTP CONNECT request.
84static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
85 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -070086 http_connect_handshaker* handshaker = arg;
Mark D. Roth28ea7e22016-07-25 11:06:22 -070087 if (error != GRPC_ERROR_NONE) {
88 // If the write failed, invoke the callback immediately with the error.
Mark D. Rothb16c1e32016-11-14 12:08:13 -080089 gpr_mu_lock(&handshaker->mu);
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080090 grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done,
91 GRPC_ERROR_REF(error), NULL);
Mark D. Rothb16c1e32016-11-14 12:08:13 -080092 handshaker->args = NULL;
93 gpr_mu_unlock(&handshaker->mu);
94 http_connect_handshaker_unref(handshaker);
Mark D. Roth28ea7e22016-07-25 11:06:22 -070095 } else {
96 // Otherwise, read the response.
Mark D. Rothb16c1e32016-11-14 12:08:13 -080097 // The read callback inherits our ref to the handshaker.
98 gpr_mu_lock(&handshaker->mu);
99 GPR_ASSERT(handshaker->args != NULL);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800100 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
101 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700102 &handshaker->response_read_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800103 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700104 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700105}
106
Mark D. Rothf7250192016-07-22 08:06:09 -0700107// Callback invoked for reading HTTP CONNECT response.
108static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
109 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700110 http_connect_handshaker* handshaker = arg;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800111 gpr_mu_lock(&handshaker->mu);
112 GPR_ASSERT(handshaker->args != NULL);
Mark D. Rothb954df12016-08-01 09:53:52 -0700113 if (error != GRPC_ERROR_NONE) {
114 GRPC_ERROR_REF(error); // Take ref to pass to the handshake-done callback.
115 goto done;
116 }
117 // Add buffer to parser.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800118 for (size_t i = 0; i < handshaker->args->read_buffer->count; ++i) {
119 if (GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i]) > 0) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700120 size_t body_start_offset = 0;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700121 error = grpc_http_parser_parse(&handshaker->http_parser,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800122 handshaker->args->read_buffer->slices[i],
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700123 &body_start_offset);
124 if (error != GRPC_ERROR_NONE) goto done;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700125 if (handshaker->http_parser.state == GRPC_HTTP_BODY) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700126 // Remove the data we've already read from the read buffer,
127 // leaving only the leftover bytes (if any).
Craig Tillerd41a4a72016-10-26 16:16:06 -0700128 grpc_slice_buffer tmp_buffer;
129 grpc_slice_buffer_init(&tmp_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700130 if (body_start_offset <
Mark D. Roth27588bb2016-11-11 15:03:39 -0800131 GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i])) {
Craig Tillerd41a4a72016-10-26 16:16:06 -0700132 grpc_slice_buffer_add(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700133 &tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800134 grpc_slice_split_tail(&handshaker->args->read_buffer->slices[i],
Craig Tiller28b72422016-10-26 21:15:29 -0700135 body_start_offset));
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700136 }
Craig Tillerd41a4a72016-10-26 16:16:06 -0700137 grpc_slice_buffer_addn(&tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800138 &handshaker->args->read_buffer->slices[i + 1],
139 handshaker->args->read_buffer->count - i - 1);
140 grpc_slice_buffer_swap(handshaker->args->read_buffer, &tmp_buffer);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700141 grpc_slice_buffer_destroy(&tmp_buffer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700142 break;
143 }
Mark D. Rothf7250192016-07-22 08:06:09 -0700144 }
Mark D. Rothb954df12016-08-01 09:53:52 -0700145 }
146 // If we're not done reading the response, read more data.
147 // TODO(roth): In practice, I suspect that the response to a CONNECT
148 // request will never include a body, in which case this check is
149 // sufficient. However, the language of RFC-2817 doesn't explicitly
150 // forbid the response from including a body. If there is a body,
151 // it's possible that we might have parsed part but not all of the
152 // body, in which case this check will cause us to fail to parse the
153 // remainder of the body. If that ever becomes an issue, we may
154 // need to fix the HTTP parser to understand when the body is
155 // complete (e.g., handling chunked transfer encoding or looking
156 // at the Content-Length: header).
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700157 if (handshaker->http_parser.state != GRPC_HTTP_BODY) {
Mark D. Roth27588bb2016-11-11 15:03:39 -0800158 grpc_slice_buffer_reset_and_unref(handshaker->args->read_buffer);
159 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
160 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700161 &handshaker->response_read_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800162 gpr_mu_unlock(&handshaker->mu);
Mark D. Rothb954df12016-08-01 09:53:52 -0700163 return;
164 }
165 // Make sure we got a 2xx response.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700166 if (handshaker->http_response.status < 200 ||
167 handshaker->http_response.status >= 300) {
Mark D. Rothb954df12016-08-01 09:53:52 -0700168 char* msg;
169 gpr_asprintf(&msg, "HTTP proxy returned response code %d",
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700170 handshaker->http_response.status);
Mark D. Rothb954df12016-08-01 09:53:52 -0700171 error = GRPC_ERROR_CREATE(msg);
172 gpr_free(msg);
Mark D. Rothf7250192016-07-22 08:06:09 -0700173 }
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700174done:
Mark D. Rothf7250192016-07-22 08:06:09 -0700175 // Invoke handshake-done callback.
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800176 handshaker->args = NULL;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -0800177 grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done, error, NULL);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800178 gpr_mu_unlock(&handshaker->mu);
179 http_connect_handshaker_unref(handshaker);
Mark D. Rothf7250192016-07-22 08:06:09 -0700180}
181
Mark D. Roth9136bb12016-07-21 09:52:12 -0700182//
183// Public handshaker methods
184//
185
186static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700187 grpc_handshaker* handshaker_in) {
188 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
189 http_connect_handshaker_unref(handshaker);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700190}
191
192static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800193 grpc_handshaker* handshaker_in) {
194 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
195 gpr_mu_lock(&handshaker->mu);
196 if (handshaker->args != NULL) {
197 grpc_endpoint_shutdown(exec_ctx, handshaker->args->endpoint);
198 handshaker->args = NULL;
199 }
200 gpr_mu_unlock(&handshaker->mu);
201}
Mark D. Roth9136bb12016-07-21 09:52:12 -0700202
203static void http_connect_handshaker_do_handshake(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700204 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker_in,
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800205 grpc_tcp_server_acceptor* acceptor, grpc_closure* on_handshake_done,
206 grpc_handshaker_args* args) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700207 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800208 gpr_mu_lock(&handshaker->mu);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700209 // Save state in the handshaker object.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700210 handshaker->args = args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -0800211 handshaker->on_handshake_done = on_handshake_done;
Mark D. Rothf7250192016-07-22 08:06:09 -0700212 // Send HTTP CONNECT request.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700213 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s",
214 handshaker->server_name, handshaker->proxy_server);
Mark D. Rothf7250192016-07-22 08:06:09 -0700215 grpc_httpcli_request request;
216 memset(&request, 0, sizeof(request));
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700217 request.host = handshaker->proxy_server;
Mark D. Rothb954df12016-08-01 09:53:52 -0700218 request.http.method = "CONNECT";
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700219 request.http.path = handshaker->server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -0700220 request.handshaker = &grpc_httpcli_plaintext;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700221 grpc_slice request_slice = grpc_httpcli_format_connect_request(&request);
222 grpc_slice_buffer_add(&handshaker->write_buffer, request_slice);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800223 // Take a new ref to be held by the write callback.
224 gpr_ref(&handshaker->refcount);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800225 grpc_endpoint_write(exec_ctx, args->endpoint, &handshaker->write_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700226 &handshaker->request_done_closure);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800227 gpr_mu_unlock(&handshaker->mu);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700228}
229
Mark D. Roth27588bb2016-11-11 15:03:39 -0800230static const grpc_handshaker_vtable http_connect_handshaker_vtable = {
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700231 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
232 http_connect_handshaker_do_handshake};
233
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700234grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server,
235 const char* server_name) {
236 GPR_ASSERT(proxy_server != NULL);
237 GPR_ASSERT(server_name != NULL);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800238 http_connect_handshaker* handshaker = gpr_malloc(sizeof(*handshaker));
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700239 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700240 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800241 gpr_mu_init(&handshaker->mu);
242 gpr_ref_init(&handshaker->refcount, 1);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700243 handshaker->proxy_server = gpr_strdup(proxy_server);
244 handshaker->server_name = gpr_strdup(server_name);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700245 grpc_slice_buffer_init(&handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700246 grpc_closure_init(&handshaker->request_done_closure, on_write_done,
247 handshaker);
248 grpc_closure_init(&handshaker->response_read_closure, on_read_done,
249 handshaker);
250 grpc_http_parser_init(&handshaker->http_parser, GRPC_HTTP_RESPONSE,
251 &handshaker->http_response);
Mark D. Rotha47a2462016-08-11 09:02:30 -0700252 return &handshaker->base;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700253}
Mark D. Roth39b58712016-09-06 12:50:42 -0700254
255char* grpc_get_http_proxy_server() {
256 char* uri_str = gpr_getenv("http_proxy");
257 if (uri_str == NULL) return NULL;
258 grpc_uri* uri = grpc_uri_parse(uri_str, false /* suppress_errors */);
259 char* proxy_name = NULL;
260 if (uri == NULL || uri->authority == NULL) {
261 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
262 goto done;
263 }
264 if (strcmp(uri->scheme, "http") != 0) {
265 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
266 goto done;
267 }
268 if (strchr(uri->authority, '@') != NULL) {
269 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
270 goto done;
271 }
272 proxy_name = gpr_strdup(uri->authority);
273done:
Mark D. Rothd66a6022016-09-08 16:57:34 +0000274 gpr_free(uri_str);
Mark D. Roth39b58712016-09-06 12:50:42 -0700275 grpc_uri_destroy(uri);
276 return proxy_name;
277}