blob: b1d3eb56a562bdc753e8f867a448d28bdd9f5808 [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. Roth5b4768f2016-08-04 13:41:46 -070046#include "src/core/lib/iomgr/timer.h"
Craig Tiller87a7e1f2016-11-09 09:42:19 -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. Roth4623e1c2016-07-18 14:09:18 -070057 // State saved while performing the handshake.
58 grpc_endpoint* endpoint;
59 grpc_channel_args* args;
60 grpc_handshaker_done_cb cb;
61 void* user_data;
62
63 // Objects for processing the HTTP CONNECT request and response.
Craig Tillerd41a4a72016-10-26 16:16:06 -070064 grpc_slice_buffer write_buffer;
65 grpc_slice_buffer* read_buffer; // Ownership passes through this object.
Mark D. Roth4623e1c2016-07-18 14:09:18 -070066 grpc_closure request_done_closure;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070067 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070068 grpc_http_parser http_parser;
69 grpc_http_response http_response;
Mark D. Roth5b4768f2016-08-04 13:41:46 -070070 grpc_timer timeout_timer;
71
72 gpr_refcount refcount;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070073} http_connect_handshaker;
74
Mark D. Roth5b4768f2016-08-04 13:41:46 -070075// Unref and clean up handshaker.
Craig Tiller87a7e1f2016-11-09 09:42:19 -080076static void http_connect_handshaker_unref(grpc_exec_ctx* exec_ctx,
77 http_connect_handshaker* handshaker) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -070078 if (gpr_unref(&handshaker->refcount)) {
79 gpr_free(handshaker->proxy_server);
80 gpr_free(handshaker->server_name);
Craig Tillera59c16c2016-10-31 07:25:01 -070081 grpc_slice_buffer_destroy_internal(exec_ctx, &handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -070082 grpc_http_parser_destroy(&handshaker->http_parser);
83 grpc_http_response_destroy(&handshaker->http_response);
84 gpr_free(handshaker);
85 }
86}
87
88// Callback invoked when deadline is exceeded.
89static void on_timeout(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
90 http_connect_handshaker* handshaker = arg;
Mark D. Rotha47a2462016-08-11 09:02:30 -070091 if (error == GRPC_ERROR_NONE) { // Timer fired, rather than being cancelled.
Mark D. Roth5b4768f2016-08-04 13:41:46 -070092 grpc_endpoint_shutdown(exec_ctx, handshaker->endpoint);
Mark D. Rotha47a2462016-08-11 09:02:30 -070093 }
Craig Tiller87a7e1f2016-11-09 09:42:19 -080094 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Roth5b4768f2016-08-04 13:41:46 -070095}
96
Mark D. Roth4623e1c2016-07-18 14:09:18 -070097// Callback invoked when finished writing HTTP CONNECT request.
98static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
99 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700100 http_connect_handshaker* handshaker = arg;
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700101 if (error != GRPC_ERROR_NONE) {
102 // If the write failed, invoke the callback immediately with the error.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700103 handshaker->cb(exec_ctx, handshaker->endpoint, handshaker->args,
104 handshaker->read_buffer, handshaker->user_data,
105 GRPC_ERROR_REF(error));
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700106 } else {
107 // Otherwise, read the response.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700108 grpc_endpoint_read(exec_ctx, handshaker->endpoint, handshaker->read_buffer,
109 &handshaker->response_read_closure);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700110 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700111}
112
Mark D. Rothf7250192016-07-22 08:06:09 -0700113// Callback invoked for reading HTTP CONNECT response.
114static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
115 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700116 http_connect_handshaker* handshaker = arg;
Mark D. Rothb954df12016-08-01 09:53:52 -0700117 if (error != GRPC_ERROR_NONE) {
118 GRPC_ERROR_REF(error); // Take ref to pass to the handshake-done callback.
119 goto done;
120 }
121 // Add buffer to parser.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700122 for (size_t i = 0; i < handshaker->read_buffer->count; ++i) {
Craig Tiller618e67d2016-10-26 21:08:10 -0700123 if (GRPC_SLICE_LENGTH(handshaker->read_buffer->slices[i]) > 0) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700124 size_t body_start_offset = 0;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700125 error = grpc_http_parser_parse(&handshaker->http_parser,
126 handshaker->read_buffer->slices[i],
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700127 &body_start_offset);
128 if (error != GRPC_ERROR_NONE) goto done;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700129 if (handshaker->http_parser.state == GRPC_HTTP_BODY) {
130 // We've gotten back a successul response, so stop the timeout timer.
131 grpc_timer_cancel(exec_ctx, &handshaker->timeout_timer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700132 // Remove the data we've already read from the read buffer,
133 // leaving only the leftover bytes (if any).
Craig Tillerd41a4a72016-10-26 16:16:06 -0700134 grpc_slice_buffer tmp_buffer;
135 grpc_slice_buffer_init(&tmp_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700136 if (body_start_offset <
Craig Tiller618e67d2016-10-26 21:08:10 -0700137 GRPC_SLICE_LENGTH(handshaker->read_buffer->slices[i])) {
Craig Tillerd41a4a72016-10-26 16:16:06 -0700138 grpc_slice_buffer_add(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700139 &tmp_buffer,
Craig Tillerd41a4a72016-10-26 16:16:06 -0700140 grpc_slice_split_tail(&handshaker->read_buffer->slices[i],
Craig Tiller28b72422016-10-26 21:15:29 -0700141 body_start_offset));
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700142 }
Craig Tillerd41a4a72016-10-26 16:16:06 -0700143 grpc_slice_buffer_addn(&tmp_buffer,
Craig Tiller28b72422016-10-26 21:15:29 -0700144 &handshaker->read_buffer->slices[i + 1],
145 handshaker->read_buffer->count - i - 1);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700146 grpc_slice_buffer_swap(handshaker->read_buffer, &tmp_buffer);
Craig Tillera59c16c2016-10-31 07:25:01 -0700147 grpc_slice_buffer_destroy_internal(exec_ctx, &tmp_buffer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700148 break;
149 }
Mark D. Rothf7250192016-07-22 08:06:09 -0700150 }
Mark D. Rothb954df12016-08-01 09:53:52 -0700151 }
152 // If we're not done reading the response, read more data.
153 // TODO(roth): In practice, I suspect that the response to a CONNECT
154 // request will never include a body, in which case this check is
155 // sufficient. However, the language of RFC-2817 doesn't explicitly
156 // forbid the response from including a body. If there is a body,
157 // it's possible that we might have parsed part but not all of the
158 // body, in which case this check will cause us to fail to parse the
159 // remainder of the body. If that ever becomes an issue, we may
160 // need to fix the HTTP parser to understand when the body is
161 // complete (e.g., handling chunked transfer encoding or looking
162 // at the Content-Length: header).
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700163 if (handshaker->http_parser.state != GRPC_HTTP_BODY) {
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800164 grpc_slice_buffer_reset_and_unref_internal(exec_ctx,
165 handshaker->read_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700166 grpc_endpoint_read(exec_ctx, handshaker->endpoint, handshaker->read_buffer,
167 &handshaker->response_read_closure);
Mark D. Rothb954df12016-08-01 09:53:52 -0700168 return;
169 }
170 // Make sure we got a 2xx response.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700171 if (handshaker->http_response.status < 200 ||
172 handshaker->http_response.status >= 300) {
Mark D. Rothb954df12016-08-01 09:53:52 -0700173 char* msg;
174 gpr_asprintf(&msg, "HTTP proxy returned response code %d",
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700175 handshaker->http_response.status);
Mark D. Rothb954df12016-08-01 09:53:52 -0700176 error = GRPC_ERROR_CREATE(msg);
177 gpr_free(msg);
Mark D. Rothf7250192016-07-22 08:06:09 -0700178 }
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700179done:
Mark D. Rothf7250192016-07-22 08:06:09 -0700180 // Invoke handshake-done callback.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700181 handshaker->cb(exec_ctx, handshaker->endpoint, handshaker->args,
182 handshaker->read_buffer, handshaker->user_data, error);
Mark D. Rothf7250192016-07-22 08:06:09 -0700183}
184
Mark D. Roth9136bb12016-07-21 09:52:12 -0700185//
186// Public handshaker methods
187//
188
189static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700190 grpc_handshaker* handshaker_in) {
191 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800192 http_connect_handshaker_unref(exec_ctx, handshaker);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700193}
194
195static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700196 grpc_handshaker* handshaker) {}
Mark D. Roth9136bb12016-07-21 09:52:12 -0700197
198static void http_connect_handshaker_do_handshake(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700199 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker_in,
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700200 grpc_endpoint* endpoint, grpc_channel_args* args,
Craig Tillerd41a4a72016-10-26 16:16:06 -0700201 grpc_slice_buffer* read_buffer, gpr_timespec deadline,
Mark D. Roth9136bb12016-07-21 09:52:12 -0700202 grpc_tcp_server_acceptor* acceptor, grpc_handshaker_done_cb cb,
203 void* user_data) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700204 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700205 // Save state in the handshaker object.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700206 handshaker->endpoint = endpoint;
207 handshaker->args = args;
208 handshaker->cb = cb;
209 handshaker->user_data = user_data;
210 handshaker->read_buffer = read_buffer;
Mark D. Rothf7250192016-07-22 08:06:09 -0700211 // Send HTTP CONNECT request.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700212 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s",
213 handshaker->server_name, handshaker->proxy_server);
Mark D. Rothf7250192016-07-22 08:06:09 -0700214 grpc_httpcli_request request;
215 memset(&request, 0, sizeof(request));
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700216 request.host = handshaker->proxy_server;
Mark D. Rothb954df12016-08-01 09:53:52 -0700217 request.http.method = "CONNECT";
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700218 request.http.path = handshaker->server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -0700219 request.handshaker = &grpc_httpcli_plaintext;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700220 grpc_slice request_slice = grpc_httpcli_format_connect_request(&request);
221 grpc_slice_buffer_add(&handshaker->write_buffer, request_slice);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700222 grpc_endpoint_write(exec_ctx, endpoint, &handshaker->write_buffer,
223 &handshaker->request_done_closure);
224 // Set timeout timer. The timer gets a reference to the handshaker.
225 gpr_ref(&handshaker->refcount);
226 grpc_timer_init(exec_ctx, &handshaker->timeout_timer,
227 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
228 on_timeout, handshaker, gpr_now(GPR_CLOCK_MONOTONIC));
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700229}
230
231static const struct grpc_handshaker_vtable http_connect_handshaker_vtable = {
232 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
233 http_connect_handshaker_do_handshake};
234
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700235grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server,
236 const char* server_name) {
237 GPR_ASSERT(proxy_server != NULL);
238 GPR_ASSERT(server_name != NULL);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700239 http_connect_handshaker* handshaker =
240 gpr_malloc(sizeof(http_connect_handshaker));
241 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700242 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
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);
252 gpr_ref_init(&handshaker->refcount, 1);
Mark D. Rotha47a2462016-08-11 09:02:30 -0700253 return &handshaker->base;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700254}
Mark D. Roth39b58712016-09-06 12:50:42 -0700255
256char* grpc_get_http_proxy_server() {
257 char* uri_str = gpr_getenv("http_proxy");
258 if (uri_str == NULL) return NULL;
259 grpc_uri* uri = grpc_uri_parse(uri_str, false /* suppress_errors */);
260 char* proxy_name = NULL;
261 if (uri == NULL || uri->authority == NULL) {
262 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
263 goto done;
264 }
265 if (strcmp(uri->scheme, "http") != 0) {
266 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
267 goto done;
268 }
269 if (strchr(uri->authority, '@') != NULL) {
270 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
271 goto done;
272 }
273 proxy_name = gpr_strdup(uri->authority);
274done:
Mark D. Rothd66a6022016-09-08 16:57:34 +0000275 gpr_free(uri_str);
Mark D. Roth39b58712016-09-06 12:50:42 -0700276 grpc_uri_destroy(uri);
277 return proxy_name;
278}