blob: a07efcc6d5ba368b0eb80c5ffbd5d8fe9016f1bc [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"
Mark D. Roth39b58712016-09-06 12:50:42 -070047#include "src/core/lib/support/env.h"
Mark D. Roth4623e1c2016-07-18 14:09:18 -070048
49typedef struct http_connect_handshaker {
50 // Base class. Must be first.
51 grpc_handshaker base;
52
Mark D. Roth77613b22016-07-22 09:41:10 -070053 char* proxy_server;
Mark D. Rothe273b032016-07-22 13:24:21 -070054 char* server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -070055
Mark D. Roth4623e1c2016-07-18 14:09:18 -070056 // State saved while performing the handshake.
Mark D. Roth27588bb2016-11-11 15:03:39 -080057 grpc_handshaker_args* args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080058 grpc_closure* on_handshake_done;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070059
60 // Objects for processing the HTTP CONNECT request and response.
Craig Tillerd41a4a72016-10-26 16:16:06 -070061 grpc_slice_buffer write_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070062 grpc_closure request_done_closure;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070063 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070064 grpc_http_parser http_parser;
65 grpc_http_response http_response;
Mark D. Roth5b4768f2016-08-04 13:41:46 -070066 grpc_timer timeout_timer;
67
68 gpr_refcount refcount;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070069} http_connect_handshaker;
70
Mark D. Roth5b4768f2016-08-04 13:41:46 -070071// Unref and clean up handshaker.
72static void http_connect_handshaker_unref(http_connect_handshaker* handshaker) {
73 if (gpr_unref(&handshaker->refcount)) {
74 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
83// Callback invoked when deadline is exceeded.
84static void on_timeout(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
85 http_connect_handshaker* handshaker = arg;
Mark D. Rotha47a2462016-08-11 09:02:30 -070086 if (error == GRPC_ERROR_NONE) { // Timer fired, rather than being cancelled.
Mark D. Roth27588bb2016-11-11 15:03:39 -080087 grpc_endpoint_shutdown(exec_ctx, handshaker->args->endpoint);
Mark D. Rotha47a2462016-08-11 09:02:30 -070088 }
Mark D. Roth5b4768f2016-08-04 13:41:46 -070089 http_connect_handshaker_unref(handshaker);
90}
91
Mark D. Roth4623e1c2016-07-18 14:09:18 -070092// Callback invoked when finished writing HTTP CONNECT request.
93static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
94 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -070095 http_connect_handshaker* handshaker = arg;
Mark D. Roth28ea7e22016-07-25 11:06:22 -070096 if (error != GRPC_ERROR_NONE) {
97 // If the write failed, invoke the callback immediately with the error.
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080098 grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done,
99 GRPC_ERROR_REF(error), NULL);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700100 } else {
101 // Otherwise, read the response.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800102 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
103 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700104 &handshaker->response_read_closure);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700105 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700106}
107
Mark D. Rothf7250192016-07-22 08:06:09 -0700108// Callback invoked for reading HTTP CONNECT response.
109static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
110 grpc_error* error) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700111 http_connect_handshaker* handshaker = arg;
Mark D. Rothb954df12016-08-01 09:53:52 -0700112 if (error != GRPC_ERROR_NONE) {
113 GRPC_ERROR_REF(error); // Take ref to pass to the handshake-done callback.
114 goto done;
115 }
116 // Add buffer to parser.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800117 for (size_t i = 0; i < handshaker->args->read_buffer->count; ++i) {
118 if (GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i]) > 0) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700119 size_t body_start_offset = 0;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700120 error = grpc_http_parser_parse(&handshaker->http_parser,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800121 handshaker->args->read_buffer->slices[i],
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700122 &body_start_offset);
123 if (error != GRPC_ERROR_NONE) goto done;
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700124 if (handshaker->http_parser.state == GRPC_HTTP_BODY) {
125 // We've gotten back a successul response, so stop the timeout timer.
126 grpc_timer_cancel(exec_ctx, &handshaker->timeout_timer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700127 // Remove the data we've already read from the read buffer,
128 // leaving only the leftover bytes (if any).
Craig Tillerd41a4a72016-10-26 16:16:06 -0700129 grpc_slice_buffer tmp_buffer;
130 grpc_slice_buffer_init(&tmp_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700131 if (body_start_offset <
Mark D. Roth27588bb2016-11-11 15:03:39 -0800132 GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i])) {
Craig Tillerd41a4a72016-10-26 16:16:06 -0700133 grpc_slice_buffer_add(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700134 &tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800135 grpc_slice_split_tail(&handshaker->args->read_buffer->slices[i],
Craig Tiller28b72422016-10-26 21:15:29 -0700136 body_start_offset));
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700137 }
Craig Tillerd41a4a72016-10-26 16:16:06 -0700138 grpc_slice_buffer_addn(&tmp_buffer,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800139 &handshaker->args->read_buffer->slices[i + 1],
140 handshaker->args->read_buffer->count - i - 1);
141 grpc_slice_buffer_swap(handshaker->args->read_buffer, &tmp_buffer);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700142 grpc_slice_buffer_destroy(&tmp_buffer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700143 break;
144 }
Mark D. Rothf7250192016-07-22 08:06:09 -0700145 }
Mark D. Rothb954df12016-08-01 09:53:52 -0700146 }
147 // If we're not done reading the response, read more data.
148 // TODO(roth): In practice, I suspect that the response to a CONNECT
149 // request will never include a body, in which case this check is
150 // sufficient. However, the language of RFC-2817 doesn't explicitly
151 // forbid the response from including a body. If there is a body,
152 // it's possible that we might have parsed part but not all of the
153 // body, in which case this check will cause us to fail to parse the
154 // remainder of the body. If that ever becomes an issue, we may
155 // need to fix the HTTP parser to understand when the body is
156 // complete (e.g., handling chunked transfer encoding or looking
157 // at the Content-Length: header).
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700158 if (handshaker->http_parser.state != GRPC_HTTP_BODY) {
Mark D. Roth27588bb2016-11-11 15:03:39 -0800159 grpc_slice_buffer_reset_and_unref(handshaker->args->read_buffer);
160 grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
161 handshaker->args->read_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700162 &handshaker->response_read_closure);
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. Roth4b5cdb72016-11-14 11:37:36 -0800176 grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done, error, NULL);
Mark D. Rothf7250192016-07-22 08:06:09 -0700177}
178
Mark D. Roth9136bb12016-07-21 09:52:12 -0700179//
180// Public handshaker methods
181//
182
183static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700184 grpc_handshaker* handshaker_in) {
185 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
186 http_connect_handshaker_unref(handshaker);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700187}
188
189static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700190 grpc_handshaker* handshaker) {}
Mark D. Roth9136bb12016-07-21 09:52:12 -0700191
192static void http_connect_handshaker_do_handshake(
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700193 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker_in,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800194 gpr_timespec deadline, grpc_tcp_server_acceptor* acceptor,
Mark D. Roth4b5cdb72016-11-14 11:37:36 -0800195 grpc_closure* on_handshake_done, grpc_handshaker_args* args) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700196 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700197 // Save state in the handshaker object.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700198 handshaker->args = args;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -0800199 handshaker->on_handshake_done = on_handshake_done;
Mark D. Rothf7250192016-07-22 08:06:09 -0700200 // Send HTTP CONNECT request.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700201 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s",
202 handshaker->server_name, handshaker->proxy_server);
Mark D. Rothf7250192016-07-22 08:06:09 -0700203 grpc_httpcli_request request;
204 memset(&request, 0, sizeof(request));
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700205 request.host = handshaker->proxy_server;
Mark D. Rothb954df12016-08-01 09:53:52 -0700206 request.http.method = "CONNECT";
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700207 request.http.path = handshaker->server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -0700208 request.handshaker = &grpc_httpcli_plaintext;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700209 grpc_slice request_slice = grpc_httpcli_format_connect_request(&request);
210 grpc_slice_buffer_add(&handshaker->write_buffer, request_slice);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800211 grpc_endpoint_write(exec_ctx, args->endpoint, &handshaker->write_buffer,
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700212 &handshaker->request_done_closure);
213 // Set timeout timer. The timer gets a reference to the handshaker.
214 gpr_ref(&handshaker->refcount);
215 grpc_timer_init(exec_ctx, &handshaker->timeout_timer,
216 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
217 on_timeout, handshaker, gpr_now(GPR_CLOCK_MONOTONIC));
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700218}
219
Mark D. Roth27588bb2016-11-11 15:03:39 -0800220static const grpc_handshaker_vtable http_connect_handshaker_vtable = {
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700221 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
222 http_connect_handshaker_do_handshake};
223
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700224grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server,
225 const char* server_name) {
226 GPR_ASSERT(proxy_server != NULL);
227 GPR_ASSERT(server_name != NULL);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800228 http_connect_handshaker* handshaker = gpr_malloc(sizeof(*handshaker));
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700229 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700230 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700231 handshaker->proxy_server = gpr_strdup(proxy_server);
232 handshaker->server_name = gpr_strdup(server_name);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700233 grpc_slice_buffer_init(&handshaker->write_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700234 grpc_closure_init(&handshaker->request_done_closure, on_write_done,
235 handshaker);
236 grpc_closure_init(&handshaker->response_read_closure, on_read_done,
237 handshaker);
238 grpc_http_parser_init(&handshaker->http_parser, GRPC_HTTP_RESPONSE,
239 &handshaker->http_response);
240 gpr_ref_init(&handshaker->refcount, 1);
Mark D. Rotha47a2462016-08-11 09:02:30 -0700241 return &handshaker->base;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700242}
Mark D. Roth39b58712016-09-06 12:50:42 -0700243
244char* grpc_get_http_proxy_server() {
245 char* uri_str = gpr_getenv("http_proxy");
246 if (uri_str == NULL) return NULL;
247 grpc_uri* uri = grpc_uri_parse(uri_str, false /* suppress_errors */);
248 char* proxy_name = NULL;
249 if (uri == NULL || uri->authority == NULL) {
250 gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var");
251 goto done;
252 }
253 if (strcmp(uri->scheme, "http") != 0) {
254 gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
255 goto done;
256 }
257 if (strchr(uri->authority, '@') != NULL) {
258 gpr_log(GPR_ERROR, "userinfo not supported in proxy URI");
259 goto done;
260 }
261 proxy_name = gpr_strdup(uri->authority);
262done:
Mark D. Rothd66a6022016-09-08 16:57:34 +0000263 gpr_free(uri_str);
Mark D. Roth39b58712016-09-06 12:50:42 -0700264 grpc_uri_destroy(uri);
265 return proxy_name;
266}