blob: 82d84da580b1ed03bf091be7f4e2bbf1fb1520ce [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. Roth0a05ab62016-08-04 13:10:13 -070034#include "src/core/ext/client_config/http_connect_handshaker.h"
35
Mark D. Roth4623e1c2016-07-18 14:09:18 -070036#include <string.h>
37
38#include <grpc/impl/codegen/alloc.h>
39#include <grpc/impl/codegen/log.h>
Mark D. Roth77613b22016-07-22 09:41:10 -070040#include <grpc/impl/codegen/slice_buffer.h>
41#include <grpc/support/string_util.h>
Mark D. Roth4623e1c2016-07-18 14:09:18 -070042
Mark D. Rothf7250192016-07-22 08:06:09 -070043#include "src/core/lib/http/format_request.h"
44#include "src/core/lib/http/parser.h"
Mark D. Roth5b4768f2016-08-04 13:41:46 -070045#include "src/core/lib/iomgr/timer.h"
Mark D. Roth4623e1c2016-07-18 14:09:18 -070046
47typedef struct http_connect_handshaker {
48 // Base class. Must be first.
49 grpc_handshaker base;
50
Mark D. Roth77613b22016-07-22 09:41:10 -070051 char* proxy_server;
Mark D. Rothe273b032016-07-22 13:24:21 -070052 char* server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -070053
Mark D. Roth4623e1c2016-07-18 14:09:18 -070054 // State saved while performing the handshake.
55 grpc_endpoint* endpoint;
56 grpc_channel_args* args;
57 grpc_handshaker_done_cb cb;
58 void* user_data;
59
60 // Objects for processing the HTTP CONNECT request and response.
Mark D. Roth714c7ec2016-08-04 12:58:16 -070061 gpr_slice_buffer write_buffer;
62 gpr_slice_buffer* read_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070063 grpc_closure request_done_closure;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070064 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070065 grpc_http_parser http_parser;
66 grpc_http_response http_response;
Mark D. Roth5b4768f2016-08-04 13:41:46 -070067 grpc_timer timeout_timer;
68
69 gpr_refcount refcount;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070070} http_connect_handshaker;
71
Mark D. Roth5b4768f2016-08-04 13:41:46 -070072// Unref and clean up handshaker.
73static void http_connect_handshaker_unref(http_connect_handshaker* handshaker) {
74 if (gpr_unref(&handshaker->refcount)) {
75 gpr_free(handshaker->proxy_server);
76 gpr_free(handshaker->server_name);
77 gpr_slice_buffer_destroy(&handshaker->write_buffer);
78 grpc_http_parser_destroy(&handshaker->http_parser);
79 grpc_http_response_destroy(&handshaker->http_response);
80 gpr_free(handshaker);
81 }
82}
83
84// Callback invoked when deadline is exceeded.
85static void on_timeout(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
86 http_connect_handshaker* handshaker = arg;
87 if (error == GRPC_ERROR_NONE) // Timer fired, rather than being cancelled.
88 grpc_endpoint_shutdown(exec_ctx, handshaker->endpoint);
89 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. Roth5b4768f2016-08-04 13:41:46 -070098 handshaker->cb(exec_ctx, handshaker->endpoint, handshaker->args,
99 handshaker->read_buffer, handshaker->user_data,
100 GRPC_ERROR_REF(error));
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700101 } else {
102 // Otherwise, read the response.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700103 grpc_endpoint_read(exec_ctx, handshaker->endpoint, handshaker->read_buffer,
104 &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. Roth5b4768f2016-08-04 13:41:46 -0700117 for (size_t i = 0; i < handshaker->read_buffer->count; ++i) {
118 if (GPR_SLICE_LENGTH(handshaker->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,
121 handshaker->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).
129 gpr_slice_buffer tmp_buffer;
130 gpr_slice_buffer_init(&tmp_buffer);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700131 if (body_start_offset <
132 GPR_SLICE_LENGTH(handshaker->read_buffer->slices[i])) {
133 gpr_slice_buffer_add(
134 &tmp_buffer,
135 gpr_slice_split_tail(&handshaker->read_buffer->slices[i],
136 body_start_offset));
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700137 }
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700138 gpr_slice_buffer_addn(&tmp_buffer,
139 &handshaker->read_buffer->slices[i + 1],
140 handshaker->read_buffer->count - i - 1);
141 gpr_slice_buffer_swap(handshaker->read_buffer, &tmp_buffer);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700142 gpr_slice_buffer_destroy(&tmp_buffer);
143 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) {
159 gpr_slice_buffer_reset_and_unref(handshaker->read_buffer);
160 grpc_endpoint_read(exec_ctx, handshaker->endpoint, handshaker->read_buffer,
161 &handshaker->response_read_closure);
Mark D. Rothb954df12016-08-01 09:53:52 -0700162 return;
163 }
164 // Make sure we got a 2xx response.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700165 if (handshaker->http_response.status < 200 ||
166 handshaker->http_response.status >= 300) {
Mark D. Rothb954df12016-08-01 09:53:52 -0700167 char* msg;
168 gpr_asprintf(&msg, "HTTP proxy returned response code %d",
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700169 handshaker->http_response.status);
Mark D. Rothb954df12016-08-01 09:53:52 -0700170 error = GRPC_ERROR_CREATE(msg);
171 gpr_free(msg);
Mark D. Rothf7250192016-07-22 08:06:09 -0700172 }
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700173done:
Mark D. Rothf7250192016-07-22 08:06:09 -0700174 // Invoke handshake-done callback.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700175 handshaker->cb(exec_ctx, handshaker->endpoint, handshaker->args,
176 handshaker->read_buffer, handshaker->user_data, error);
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. Roth714c7ec2016-08-04 12:58:16 -0700194 grpc_endpoint* endpoint, grpc_channel_args* args,
195 gpr_slice_buffer* read_buffer, gpr_timespec deadline,
Mark D. Roth9136bb12016-07-21 09:52:12 -0700196 grpc_tcp_server_acceptor* acceptor, grpc_handshaker_done_cb cb,
197 void* user_data) {
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700198 http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700199 // Save state in the handshaker object.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700200 handshaker->endpoint = endpoint;
201 handshaker->args = args;
202 handshaker->cb = cb;
203 handshaker->user_data = user_data;
204 handshaker->read_buffer = read_buffer;
Mark D. Rothf7250192016-07-22 08:06:09 -0700205 // Send HTTP CONNECT request.
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700206 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s",
207 handshaker->server_name, handshaker->proxy_server);
Mark D. Rothf7250192016-07-22 08:06:09 -0700208 grpc_httpcli_request request;
209 memset(&request, 0, sizeof(request));
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700210 request.host = handshaker->proxy_server;
Mark D. Rothb954df12016-08-01 09:53:52 -0700211 request.http.method = "CONNECT";
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700212 request.http.path = handshaker->server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -0700213 request.handshaker = &grpc_httpcli_plaintext;
214 gpr_slice request_slice = grpc_httpcli_format_connect_request(&request);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700215 gpr_slice_buffer_add(&handshaker->write_buffer, request_slice);
216 grpc_endpoint_write(exec_ctx, endpoint, &handshaker->write_buffer,
217 &handshaker->request_done_closure);
218 // Set timeout timer. The timer gets a reference to the handshaker.
219 gpr_ref(&handshaker->refcount);
220 grpc_timer_init(exec_ctx, &handshaker->timeout_timer,
221 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
222 on_timeout, handshaker, gpr_now(GPR_CLOCK_MONOTONIC));
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700223}
224
225static const struct grpc_handshaker_vtable http_connect_handshaker_vtable = {
226 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
227 http_connect_handshaker_do_handshake};
228
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700229grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server,
230 const char* server_name) {
231 GPR_ASSERT(proxy_server != NULL);
232 GPR_ASSERT(server_name != NULL);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700233 http_connect_handshaker* handshaker =
234 gpr_malloc(sizeof(http_connect_handshaker));
235 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700236 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700237 handshaker->proxy_server = gpr_strdup(proxy_server);
238 handshaker->server_name = gpr_strdup(server_name);
Mark D. Roth5b4768f2016-08-04 13:41:46 -0700239 gpr_slice_buffer_init(&handshaker->write_buffer);
240 grpc_closure_init(&handshaker->request_done_closure, on_write_done,
241 handshaker);
242 grpc_closure_init(&handshaker->response_read_closure, on_read_done,
243 handshaker);
244 grpc_http_parser_init(&handshaker->http_parser, GRPC_HTTP_RESPONSE,
245 &handshaker->http_response);
246 gpr_ref_init(&handshaker->refcount, 1);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700247 return (grpc_handshaker*)handshaker;
248}