blob: 54f592ef614b0db2f98da72b467ee9467ee12d39 [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. Roth4623e1c2016-07-18 14:09:18 -070045
46typedef struct http_connect_handshaker {
47 // Base class. Must be first.
48 grpc_handshaker base;
49
Mark D. Roth77613b22016-07-22 09:41:10 -070050 char* proxy_server;
Mark D. Rothe273b032016-07-22 13:24:21 -070051 char* server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -070052
Mark D. Roth4623e1c2016-07-18 14:09:18 -070053 // State saved while performing the handshake.
54 grpc_endpoint* endpoint;
55 grpc_channel_args* args;
56 grpc_handshaker_done_cb cb;
57 void* user_data;
58
59 // Objects for processing the HTTP CONNECT request and response.
Mark D. Roth714c7ec2016-08-04 12:58:16 -070060 gpr_slice_buffer write_buffer;
61 gpr_slice_buffer* read_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. Roth4623e1c2016-07-18 14:09:18 -070066} http_connect_handshaker;
67
Mark D. Roth4623e1c2016-07-18 14:09:18 -070068// Callback invoked when finished writing HTTP CONNECT request.
69static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
70 grpc_error* error) {
71 http_connect_handshaker* h = arg;
Mark D. Roth28ea7e22016-07-25 11:06:22 -070072 if (error != GRPC_ERROR_NONE) {
73 // If the write failed, invoke the callback immediately with the error.
Mark D. Roth714c7ec2016-08-04 12:58:16 -070074 h->cb(exec_ctx, h->endpoint, h->args, h->read_buffer, h->user_data,
75 GRPC_ERROR_REF(error));
Mark D. Roth28ea7e22016-07-25 11:06:22 -070076 } else {
77 // Otherwise, read the response.
Mark D. Roth714c7ec2016-08-04 12:58:16 -070078 grpc_endpoint_read(exec_ctx, h->endpoint, h->read_buffer,
Mark D. Roth28ea7e22016-07-25 11:06:22 -070079 &h->response_read_closure);
80 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -070081}
82
Mark D. Rothf7250192016-07-22 08:06:09 -070083// Callback invoked for reading HTTP CONNECT response.
84static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
85 grpc_error* error) {
86 http_connect_handshaker* h = arg;
Mark D. Rothb954df12016-08-01 09:53:52 -070087 if (error != GRPC_ERROR_NONE) {
88 GRPC_ERROR_REF(error); // Take ref to pass to the handshake-done callback.
89 goto done;
90 }
91 // Add buffer to parser.
Mark D. Roth714c7ec2016-08-04 12:58:16 -070092 for (size_t i = 0; i < h->read_buffer->count; ++i) {
93 if (GPR_SLICE_LENGTH(h->read_buffer->slices[i]) > 0) {
94 size_t body_start_offset = 0;
Mark D. Roth0a05ab62016-08-04 13:10:13 -070095 error = grpc_http_parser_parse(&h->http_parser, h->read_buffer->slices[i],
96 &body_start_offset);
97 if (error != GRPC_ERROR_NONE) goto done;
Mark D. Roth714c7ec2016-08-04 12:58:16 -070098 if (h->http_parser.state == GRPC_HTTP_BODY) {
99 // Remove the data we've already read from the read buffer,
100 // leaving only the leftover bytes (if any).
101 gpr_slice_buffer tmp_buffer;
102 gpr_slice_buffer_init(&tmp_buffer);
103 if (body_start_offset < GPR_SLICE_LENGTH(h->read_buffer->slices[i])) {
104 gpr_slice_buffer_add(&tmp_buffer,
105 gpr_slice_split_tail(&h->read_buffer->slices[i],
106 body_start_offset));
107 }
108 gpr_slice_buffer_addn(&tmp_buffer, &h->read_buffer->slices[i + 1],
109 h->read_buffer->count - i - 1);
110 gpr_slice_buffer_swap(h->read_buffer, &tmp_buffer);
111 gpr_slice_buffer_destroy(&tmp_buffer);
112 break;
113 }
Mark D. Rothf7250192016-07-22 08:06:09 -0700114 }
Mark D. Rothb954df12016-08-01 09:53:52 -0700115 }
116 // If we're not done reading the response, read more data.
117 // TODO(roth): In practice, I suspect that the response to a CONNECT
118 // request will never include a body, in which case this check is
119 // sufficient. However, the language of RFC-2817 doesn't explicitly
120 // forbid the response from including a body. If there is a body,
121 // it's possible that we might have parsed part but not all of the
122 // body, in which case this check will cause us to fail to parse the
123 // remainder of the body. If that ever becomes an issue, we may
124 // need to fix the HTTP parser to understand when the body is
125 // complete (e.g., handling chunked transfer encoding or looking
126 // at the Content-Length: header).
127 if (h->http_parser.state != GRPC_HTTP_BODY) {
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700128 gpr_slice_buffer_reset_and_unref(h->read_buffer);
129 grpc_endpoint_read(exec_ctx, h->endpoint, h->read_buffer,
Mark D. Rothb954df12016-08-01 09:53:52 -0700130 &h->response_read_closure);
131 return;
132 }
133 // Make sure we got a 2xx response.
134 if (h->http_response.status < 200 || h->http_response.status >= 300) {
135 char* msg;
136 gpr_asprintf(&msg, "HTTP proxy returned response code %d",
137 h->http_response.status);
138 error = GRPC_ERROR_CREATE(msg);
139 gpr_free(msg);
Mark D. Rothf7250192016-07-22 08:06:09 -0700140 }
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700141done:
Mark D. Rothf7250192016-07-22 08:06:09 -0700142 // Invoke handshake-done callback.
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700143 h->cb(exec_ctx, h->endpoint, h->args, h->read_buffer, h->user_data, error);
Mark D. Rothf7250192016-07-22 08:06:09 -0700144}
145
Mark D. Roth9136bb12016-07-21 09:52:12 -0700146//
147// Public handshaker methods
148//
149
150static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
151 grpc_handshaker* handshaker) {
Mark D. Roth77613b22016-07-22 09:41:10 -0700152 http_connect_handshaker* h = (http_connect_handshaker*)handshaker;
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700153 gpr_free(h->proxy_server);
154 gpr_free(h->server_name);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700155 gpr_slice_buffer_destroy(&h->write_buffer);
Mark D. Roth77613b22016-07-22 09:41:10 -0700156 grpc_http_parser_destroy(&h->http_parser);
157 grpc_http_response_destroy(&h->http_response);
158 gpr_free(h);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700159}
160
161static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700162 grpc_handshaker* handshaker) {}
Mark D. Roth9136bb12016-07-21 09:52:12 -0700163
Mark D. Roth64d88bd2016-07-27 12:44:10 -0700164// FIXME BEFORE MERGING: apply deadline
Mark D. Roth9136bb12016-07-21 09:52:12 -0700165static void http_connect_handshaker_do_handshake(
166 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker,
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700167 grpc_endpoint* endpoint, grpc_channel_args* args,
168 gpr_slice_buffer* read_buffer, gpr_timespec deadline,
Mark D. Roth9136bb12016-07-21 09:52:12 -0700169 grpc_tcp_server_acceptor* acceptor, grpc_handshaker_done_cb cb,
170 void* user_data) {
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700171 http_connect_handshaker* h = (http_connect_handshaker*)handshaker;
172 // Save state in the handshaker object.
173 h->endpoint = endpoint;
174 h->args = args;
175 h->cb = cb;
176 h->user_data = user_data;
Mark D. Rothf7250192016-07-22 08:06:09 -0700177 // Initialize fields.
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700178 gpr_slice_buffer_init(&h->write_buffer);
179 h->read_buffer = read_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700180 grpc_closure_init(&h->request_done_closure, on_write_done, h);
Mark D. Rothf7250192016-07-22 08:06:09 -0700181 grpc_closure_init(&h->response_read_closure, on_read_done, h);
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700182 grpc_http_parser_init(&h->http_parser, GRPC_HTTP_RESPONSE, &h->http_response);
Mark D. Rothf7250192016-07-22 08:06:09 -0700183 // Send HTTP CONNECT request.
Mark D. Roth0a05ab62016-08-04 13:10:13 -0700184 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s", h->server_name,
185 h->proxy_server);
Mark D. Rothf7250192016-07-22 08:06:09 -0700186 grpc_httpcli_request request;
187 memset(&request, 0, sizeof(request));
Mark D. Rothb954df12016-08-01 09:53:52 -0700188 request.host = h->proxy_server;
189 request.http.method = "CONNECT";
190 request.http.path = h->server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -0700191 request.handshaker = &grpc_httpcli_plaintext;
192 gpr_slice request_slice = grpc_httpcli_format_connect_request(&request);
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700193 gpr_slice_buffer_add(&h->write_buffer, request_slice);
194 grpc_endpoint_write(exec_ctx, endpoint, &h->write_buffer,
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700195 &h->request_done_closure);
196}
197
198static const struct grpc_handshaker_vtable http_connect_handshaker_vtable = {
199 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
200 http_connect_handshaker_do_handshake};
201
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700202grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server,
203 const char* server_name) {
204 GPR_ASSERT(proxy_server != NULL);
205 GPR_ASSERT(server_name != NULL);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700206 http_connect_handshaker* handshaker =
207 gpr_malloc(sizeof(http_connect_handshaker));
208 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700209 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700210 handshaker->proxy_server = gpr_strdup(proxy_server);
211 handshaker->server_name = gpr_strdup(server_name);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700212 return (grpc_handshaker*)handshaker;
213}