blob: 25851c2efb63dca24997e2ba433ab35212727b77 [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
34#include <string.h>
35
36#include <grpc/impl/codegen/alloc.h>
37#include <grpc/impl/codegen/log.h>
Mark D. Roth77613b22016-07-22 09:41:10 -070038#include <grpc/impl/codegen/slice_buffer.h>
39#include <grpc/support/string_util.h>
Mark D. Roth4623e1c2016-07-18 14:09:18 -070040
Mark D. Rothf7250192016-07-22 08:06:09 -070041#include "src/core/lib/http/format_request.h"
42#include "src/core/lib/http/parser.h"
Mark D. Roth4623e1c2016-07-18 14:09:18 -070043#include "src/core/ext/client_config/http_connect_handshaker.h"
44
45typedef struct http_connect_handshaker {
46 // Base class. Must be first.
47 grpc_handshaker base;
48
Mark D. Roth77613b22016-07-22 09:41:10 -070049 char* proxy_server;
Mark D. Rothe273b032016-07-22 13:24:21 -070050 char* server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -070051
Mark D. Roth4623e1c2016-07-18 14:09:18 -070052 // State saved while performing the handshake.
53 grpc_endpoint* endpoint;
54 grpc_channel_args* args;
55 grpc_handshaker_done_cb cb;
56 void* user_data;
57
58 // Objects for processing the HTTP CONNECT request and response.
Mark D. Roth77613b22016-07-22 09:41:10 -070059 gpr_slice_buffer request_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070060 grpc_closure request_done_closure;
Mark D. Roth77613b22016-07-22 09:41:10 -070061 gpr_slice_buffer response_buffer;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070062 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070063 grpc_http_parser http_parser;
64 grpc_http_response http_response;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070065} http_connect_handshaker;
66
Mark D. Roth4623e1c2016-07-18 14:09:18 -070067// Callback invoked when finished writing HTTP CONNECT request.
68static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
69 grpc_error* error) {
70 http_connect_handshaker* h = arg;
Mark D. Roth28ea7e22016-07-25 11:06:22 -070071 if (error != GRPC_ERROR_NONE) {
72 // If the write failed, invoke the callback immediately with the error.
73 h->cb(exec_ctx, h->endpoint, h->args, h->user_data, GRPC_ERROR_REF(error));
74 } else {
75 // Otherwise, read the response.
76 grpc_endpoint_read(exec_ctx, h->endpoint, &h->response_buffer,
77 &h->response_read_closure);
78 }
Mark D. Roth4623e1c2016-07-18 14:09:18 -070079}
80
Mark D. Rothf7250192016-07-22 08:06:09 -070081// Callback invoked for reading HTTP CONNECT response.
82static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
83 grpc_error* error) {
84 http_connect_handshaker* h = arg;
Mark D. Rothb954df12016-08-01 09:53:52 -070085 if (error != GRPC_ERROR_NONE) {
86 GRPC_ERROR_REF(error); // Take ref to pass to the handshake-done callback.
87 goto done;
88 }
89 // Add buffer to parser.
90 for (size_t i = 0; i < h->response_buffer.count; ++i) {
91 if (GPR_SLICE_LENGTH(h->response_buffer.slices[i]) > 0) {
92 error = grpc_http_parser_parse(
93 &h->http_parser, h->response_buffer.slices[i]);
94 if (error != GRPC_ERROR_NONE)
95 goto done;
Mark D. Rothf7250192016-07-22 08:06:09 -070096 }
Mark D. Rothb954df12016-08-01 09:53:52 -070097 }
98 // If we're not done reading the response, read more data.
99 // TODO(roth): In practice, I suspect that the response to a CONNECT
100 // request will never include a body, in which case this check is
101 // sufficient. However, the language of RFC-2817 doesn't explicitly
102 // forbid the response from including a body. If there is a body,
103 // it's possible that we might have parsed part but not all of the
104 // body, in which case this check will cause us to fail to parse the
105 // remainder of the body. If that ever becomes an issue, we may
106 // need to fix the HTTP parser to understand when the body is
107 // complete (e.g., handling chunked transfer encoding or looking
108 // at the Content-Length: header).
109 if (h->http_parser.state != GRPC_HTTP_BODY) {
110 gpr_slice_buffer_reset_and_unref(&h->response_buffer);
111 grpc_endpoint_read(exec_ctx, h->endpoint, &h->response_buffer,
112 &h->response_read_closure);
113 return;
114 }
115 // Make sure we got a 2xx response.
116 if (h->http_response.status < 200 || h->http_response.status >= 300) {
117 char* msg;
118 gpr_asprintf(&msg, "HTTP proxy returned response code %d",
119 h->http_response.status);
120 error = GRPC_ERROR_CREATE(msg);
121 gpr_free(msg);
Mark D. Rothf7250192016-07-22 08:06:09 -0700122 }
123 done:
124 // Invoke handshake-done callback.
Mark D. Rothb954df12016-08-01 09:53:52 -0700125 h->cb(exec_ctx, h->endpoint, h->args, h->user_data, error);
Mark D. Rothf7250192016-07-22 08:06:09 -0700126}
127
Mark D. Roth9136bb12016-07-21 09:52:12 -0700128//
129// Public handshaker methods
130//
131
132static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
133 grpc_handshaker* handshaker) {
Mark D. Roth77613b22016-07-22 09:41:10 -0700134 http_connect_handshaker* h = (http_connect_handshaker*)handshaker;
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700135 gpr_free(h->proxy_server);
136 gpr_free(h->server_name);
Mark D. Roth77613b22016-07-22 09:41:10 -0700137 gpr_slice_buffer_destroy(&h->request_buffer);
138 gpr_slice_buffer_destroy(&h->response_buffer);
139 grpc_http_parser_destroy(&h->http_parser);
140 grpc_http_response_destroy(&h->http_response);
141 gpr_free(h);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700142}
143
144static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
145 grpc_handshaker* handshaker) {
146}
147
Mark D. Roth64d88bd2016-07-27 12:44:10 -0700148// FIXME BEFORE MERGING: apply deadline
Mark D. Roth9136bb12016-07-21 09:52:12 -0700149static void http_connect_handshaker_do_handshake(
150 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker,
151 grpc_endpoint* endpoint, grpc_channel_args* args, gpr_timespec deadline,
152 grpc_tcp_server_acceptor* acceptor, grpc_handshaker_done_cb cb,
153 void* user_data) {
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700154 http_connect_handshaker* h = (http_connect_handshaker*)handshaker;
155 // Save state in the handshaker object.
156 h->endpoint = endpoint;
157 h->args = args;
158 h->cb = cb;
159 h->user_data = user_data;
Mark D. Rothf7250192016-07-22 08:06:09 -0700160 // Initialize fields.
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700161 gpr_slice_buffer_init(&h->request_buffer);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700162 grpc_closure_init(&h->request_done_closure, on_write_done, h);
Mark D. Rothf7250192016-07-22 08:06:09 -0700163 gpr_slice_buffer_init(&h->response_buffer);
164 grpc_closure_init(&h->response_read_closure, on_read_done, h);
165 grpc_http_parser_init(&h->http_parser, GRPC_HTTP_RESPONSE,
166 &h->http_response);
167 // Send HTTP CONNECT request.
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700168 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s",
169 h->server_name, h->proxy_server);
Mark D. Rothf7250192016-07-22 08:06:09 -0700170 grpc_httpcli_request request;
171 memset(&request, 0, sizeof(request));
Mark D. Rothb954df12016-08-01 09:53:52 -0700172 request.host = h->proxy_server;
173 request.http.method = "CONNECT";
174 request.http.path = h->server_name;
Mark D. Roth77613b22016-07-22 09:41:10 -0700175 request.handshaker = &grpc_httpcli_plaintext;
176 gpr_slice request_slice = grpc_httpcli_format_connect_request(&request);
Mark D. Rothf7250192016-07-22 08:06:09 -0700177 gpr_slice_buffer_add(&h->request_buffer, request_slice);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700178 grpc_endpoint_write(exec_ctx, endpoint, &h->request_buffer,
179 &h->request_done_closure);
180}
181
182static const struct grpc_handshaker_vtable http_connect_handshaker_vtable = {
183 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
184 http_connect_handshaker_do_handshake};
185
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700186grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server,
187 const char* server_name) {
188 GPR_ASSERT(proxy_server != NULL);
189 GPR_ASSERT(server_name != NULL);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700190 http_connect_handshaker* handshaker =
191 gpr_malloc(sizeof(http_connect_handshaker));
192 memset(handshaker, 0, sizeof(*handshaker));
Mark D. Roth77613b22016-07-22 09:41:10 -0700193 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base);
Mark D. Roth28ea7e22016-07-25 11:06:22 -0700194 handshaker->proxy_server = gpr_strdup(proxy_server);
195 handshaker->server_name = gpr_strdup(server_name);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700196 return (grpc_handshaker*)handshaker;
197}