1 /* 2 * 3 * Copyright 2016 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #include <grpc/support/port_platform.h> 20 21 #include "src/core/ext/filters/client_channel/http_connect_handshaker.h" 22 23 #include <string.h> 24 25 #include <grpc/slice_buffer.h> 26 #include <grpc/support/alloc.h> 27 #include <grpc/support/log.h> 28 #include <grpc/support/string_util.h> 29 30 #include "src/core/ext/filters/client_channel/client_channel.h" 31 #include "src/core/ext/filters/client_channel/resolver_registry.h" 32 #include "src/core/ext/filters/client_channel/uri_parser.h" 33 #include "src/core/lib/channel/channel_args.h" 34 #include "src/core/lib/channel/handshaker_registry.h" 35 #include "src/core/lib/gpr/env.h" 36 #include "src/core/lib/gpr/string.h" 37 #include "src/core/lib/http/format_request.h" 38 #include "src/core/lib/http/parser.h" 39 #include "src/core/lib/slice/slice_internal.h" 40 41 typedef struct http_connect_handshaker { 42 // Base class. Must be first. 43 grpc_handshaker base; 44 45 gpr_refcount refcount; 46 gpr_mu mu; 47 48 bool shutdown; 49 // Endpoint and read buffer to destroy after a shutdown. 50 grpc_endpoint* endpoint_to_destroy; 51 grpc_slice_buffer* read_buffer_to_destroy; 52 53 // State saved while performing the handshake. 54 grpc_handshaker_args* args; 55 grpc_closure* on_handshake_done; 56 57 // Objects for processing the HTTP CONNECT request and response. 58 grpc_slice_buffer write_buffer; 59 grpc_closure request_done_closure; 60 grpc_closure response_read_closure; 61 grpc_http_parser http_parser; 62 grpc_http_response http_response; 63 } http_connect_handshaker; 64 65 // Unref and clean up handshaker. 66 static void http_connect_handshaker_unref(http_connect_handshaker* handshaker) { 67 if (gpr_unref(&handshaker->refcount)) { 68 gpr_mu_destroy(&handshaker->mu); 69 if (handshaker->endpoint_to_destroy != nullptr) { 70 grpc_endpoint_destroy(handshaker->endpoint_to_destroy); 71 } 72 if (handshaker->read_buffer_to_destroy != nullptr) { 73 grpc_slice_buffer_destroy_internal(handshaker->read_buffer_to_destroy); 74 gpr_free(handshaker->read_buffer_to_destroy); 75 } 76 grpc_slice_buffer_destroy_internal(&handshaker->write_buffer); 77 grpc_http_parser_destroy(&handshaker->http_parser); 78 grpc_http_response_destroy(&handshaker->http_response); 79 gpr_free(handshaker); 80 } 81 } 82 83 // Set args fields to nullptr, saving the endpoint and read buffer for 84 // later destruction. 85 static void cleanup_args_for_failure_locked( 86 http_connect_handshaker* handshaker) { 87 handshaker->endpoint_to_destroy = handshaker->args->endpoint; 88 handshaker->args->endpoint = nullptr; 89 handshaker->read_buffer_to_destroy = handshaker->args->read_buffer; 90 handshaker->args->read_buffer = nullptr; 91 grpc_channel_args_destroy(handshaker->args->args); 92 handshaker->args->args = nullptr; 93 } 94 95 // If the handshake failed or we're shutting down, clean up and invoke the 96 // callback with the error. 97 static void handshake_failed_locked(http_connect_handshaker* handshaker, 98 grpc_error* error) { 99 if (error == GRPC_ERROR_NONE) { 100 // If we were shut down after an endpoint operation succeeded but 101 // before the endpoint callback was invoked, we need to generate our 102 // own error. 103 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Handshaker shutdown"); 104 } 105 if (!handshaker->shutdown) { 106 // TODO(ctiller): It is currently necessary to shutdown endpoints 107 // before destroying them, even if we know that there are no 108 // pending read/write callbacks. This should be fixed, at which 109 // point this can be removed. 110 grpc_endpoint_shutdown(handshaker->args->endpoint, GRPC_ERROR_REF(error)); 111 // Not shutting down, so the handshake failed. Clean up before 112 // invoking the callback. 113 cleanup_args_for_failure_locked(handshaker); 114 // Set shutdown to true so that subsequent calls to 115 // http_connect_handshaker_shutdown() do nothing. 116 handshaker->shutdown = true; 117 } 118 // Invoke callback. 119 GRPC_CLOSURE_SCHED(handshaker->on_handshake_done, error); 120 } 121 122 // Callback invoked when finished writing HTTP CONNECT request. 123 static void on_write_done(void* arg, grpc_error* error) { 124 http_connect_handshaker* handshaker = 125 static_cast<http_connect_handshaker*>(arg); 126 gpr_mu_lock(&handshaker->mu); 127 if (error != GRPC_ERROR_NONE || handshaker->shutdown) { 128 // If the write failed or we're shutting down, clean up and invoke the 129 // callback with the error. 130 handshake_failed_locked(handshaker, GRPC_ERROR_REF(error)); 131 gpr_mu_unlock(&handshaker->mu); 132 http_connect_handshaker_unref(handshaker); 133 } else { 134 // Otherwise, read the response. 135 // The read callback inherits our ref to the handshaker. 136 grpc_endpoint_read(handshaker->args->endpoint, 137 handshaker->args->read_buffer, 138 &handshaker->response_read_closure); 139 gpr_mu_unlock(&handshaker->mu); 140 } 141 } 142 143 // Callback invoked for reading HTTP CONNECT response. 144 static void on_read_done(void* arg, grpc_error* error) { 145 http_connect_handshaker* handshaker = 146 static_cast<http_connect_handshaker*>(arg); 147 gpr_mu_lock(&handshaker->mu); 148 if (error != GRPC_ERROR_NONE || handshaker->shutdown) { 149 // If the read failed or we're shutting down, clean up and invoke the 150 // callback with the error. 151 handshake_failed_locked(handshaker, GRPC_ERROR_REF(error)); 152 goto done; 153 } 154 // Add buffer to parser. 155 for (size_t i = 0; i < handshaker->args->read_buffer->count; ++i) { 156 if (GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i]) > 0) { 157 size_t body_start_offset = 0; 158 error = grpc_http_parser_parse(&handshaker->http_parser, 159 handshaker->args->read_buffer->slices[i], 160 &body_start_offset); 161 if (error != GRPC_ERROR_NONE) { 162 handshake_failed_locked(handshaker, error); 163 goto done; 164 } 165 if (handshaker->http_parser.state == GRPC_HTTP_BODY) { 166 // Remove the data we've already read from the read buffer, 167 // leaving only the leftover bytes (if any). 168 grpc_slice_buffer tmp_buffer; 169 grpc_slice_buffer_init(&tmp_buffer); 170 if (body_start_offset < 171 GRPC_SLICE_LENGTH(handshaker->args->read_buffer->slices[i])) { 172 grpc_slice_buffer_add( 173 &tmp_buffer, 174 grpc_slice_split_tail(&handshaker->args->read_buffer->slices[i], 175 body_start_offset)); 176 } 177 grpc_slice_buffer_addn(&tmp_buffer, 178 &handshaker->args->read_buffer->slices[i + 1], 179 handshaker->args->read_buffer->count - i - 1); 180 grpc_slice_buffer_swap(handshaker->args->read_buffer, &tmp_buffer); 181 grpc_slice_buffer_destroy_internal(&tmp_buffer); 182 break; 183 } 184 } 185 } 186 // If we're not done reading the response, read more data. 187 // TODO(roth): In practice, I suspect that the response to a CONNECT 188 // request will never include a body, in which case this check is 189 // sufficient. However, the language of RFC-2817 doesn't explicitly 190 // forbid the response from including a body. If there is a body, 191 // it's possible that we might have parsed part but not all of the 192 // body, in which case this check will cause us to fail to parse the 193 // remainder of the body. If that ever becomes an issue, we may 194 // need to fix the HTTP parser to understand when the body is 195 // complete (e.g., handling chunked transfer encoding or looking 196 // at the Content-Length: header). 197 if (handshaker->http_parser.state != GRPC_HTTP_BODY) { 198 grpc_slice_buffer_reset_and_unref_internal(handshaker->args->read_buffer); 199 grpc_endpoint_read(handshaker->args->endpoint, 200 handshaker->args->read_buffer, 201 &handshaker->response_read_closure); 202 gpr_mu_unlock(&handshaker->mu); 203 return; 204 } 205 // Make sure we got a 2xx response. 206 if (handshaker->http_response.status < 200 || 207 handshaker->http_response.status >= 300) { 208 char* msg; 209 gpr_asprintf(&msg, "HTTP proxy returned response code %d", 210 handshaker->http_response.status); 211 error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg); 212 gpr_free(msg); 213 handshake_failed_locked(handshaker, error); 214 goto done; 215 } 216 // Success. Invoke handshake-done callback. 217 GRPC_CLOSURE_SCHED(handshaker->on_handshake_done, error); 218 done: 219 // Set shutdown to true so that subsequent calls to 220 // http_connect_handshaker_shutdown() do nothing. 221 handshaker->shutdown = true; 222 gpr_mu_unlock(&handshaker->mu); 223 http_connect_handshaker_unref(handshaker); 224 } 225 226 // 227 // Public handshaker methods 228 // 229 230 static void http_connect_handshaker_destroy(grpc_handshaker* handshaker_in) { 231 http_connect_handshaker* handshaker = 232 reinterpret_cast<http_connect_handshaker*>(handshaker_in); 233 http_connect_handshaker_unref(handshaker); 234 } 235 236 static void http_connect_handshaker_shutdown(grpc_handshaker* handshaker_in, 237 grpc_error* why) { 238 http_connect_handshaker* handshaker = 239 reinterpret_cast<http_connect_handshaker*>(handshaker_in); 240 gpr_mu_lock(&handshaker->mu); 241 if (!handshaker->shutdown) { 242 handshaker->shutdown = true; 243 grpc_endpoint_shutdown(handshaker->args->endpoint, GRPC_ERROR_REF(why)); 244 cleanup_args_for_failure_locked(handshaker); 245 } 246 gpr_mu_unlock(&handshaker->mu); 247 GRPC_ERROR_UNREF(why); 248 } 249 250 static void http_connect_handshaker_do_handshake( 251 grpc_handshaker* handshaker_in, grpc_tcp_server_acceptor* acceptor, 252 grpc_closure* on_handshake_done, grpc_handshaker_args* args) { 253 http_connect_handshaker* handshaker = 254 reinterpret_cast<http_connect_handshaker*>(handshaker_in); 255 // Check for HTTP CONNECT channel arg. 256 // If not found, invoke on_handshake_done without doing anything. 257 const grpc_arg* arg = 258 grpc_channel_args_find(args->args, GRPC_ARG_HTTP_CONNECT_SERVER); 259 char* server_name = grpc_channel_arg_get_string(arg); 260 if (server_name == nullptr) { 261 // Set shutdown to true so that subsequent calls to 262 // http_connect_handshaker_shutdown() do nothing. 263 gpr_mu_lock(&handshaker->mu); 264 handshaker->shutdown = true; 265 gpr_mu_unlock(&handshaker->mu); 266 GRPC_CLOSURE_SCHED(on_handshake_done, GRPC_ERROR_NONE); 267 return; 268 } 269 // Get headers from channel args. 270 arg = grpc_channel_args_find(args->args, GRPC_ARG_HTTP_CONNECT_HEADERS); 271 char* arg_header_string = grpc_channel_arg_get_string(arg); 272 grpc_http_header* headers = nullptr; 273 size_t num_headers = 0; 274 char** header_strings = nullptr; 275 size_t num_header_strings = 0; 276 if (arg_header_string != nullptr) { 277 gpr_string_split(arg_header_string, "\n", &header_strings, 278 &num_header_strings); 279 headers = static_cast<grpc_http_header*>( 280 gpr_malloc(sizeof(grpc_http_header) * num_header_strings)); 281 for (size_t i = 0; i < num_header_strings; ++i) { 282 char* sep = strchr(header_strings[i], ':'); 283 if (sep == nullptr) { 284 gpr_log(GPR_ERROR, "skipping unparseable HTTP CONNECT header: %s", 285 header_strings[i]); 286 continue; 287 } 288 *sep = '\0'; 289 headers[num_headers].key = header_strings[i]; 290 headers[num_headers].value = sep + 1; 291 ++num_headers; 292 } 293 } 294 // Save state in the handshaker object. 295 gpr_mu_lock(&handshaker->mu); 296 handshaker->args = args; 297 handshaker->on_handshake_done = on_handshake_done; 298 // Log connection via proxy. 299 char* proxy_name = grpc_endpoint_get_peer(args->endpoint); 300 gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s", server_name, 301 proxy_name); 302 gpr_free(proxy_name); 303 // Construct HTTP CONNECT request. 304 grpc_httpcli_request request; 305 memset(&request, 0, sizeof(request)); 306 request.host = server_name; 307 request.http.method = (char*)"CONNECT"; 308 request.http.path = server_name; 309 request.http.hdrs = headers; 310 request.http.hdr_count = num_headers; 311 request.handshaker = &grpc_httpcli_plaintext; 312 grpc_slice request_slice = grpc_httpcli_format_connect_request(&request); 313 grpc_slice_buffer_add(&handshaker->write_buffer, request_slice); 314 // Clean up. 315 gpr_free(headers); 316 for (size_t i = 0; i < num_header_strings; ++i) { 317 gpr_free(header_strings[i]); 318 } 319 gpr_free(header_strings); 320 // Take a new ref to be held by the write callback. 321 gpr_ref(&handshaker->refcount); 322 grpc_endpoint_write(args->endpoint, &handshaker->write_buffer, 323 &handshaker->request_done_closure, nullptr); 324 gpr_mu_unlock(&handshaker->mu); 325 } 326 327 static const grpc_handshaker_vtable http_connect_handshaker_vtable = { 328 http_connect_handshaker_destroy, http_connect_handshaker_shutdown, 329 http_connect_handshaker_do_handshake, "http_connect"}; 330 331 static grpc_handshaker* grpc_http_connect_handshaker_create() { 332 http_connect_handshaker* handshaker = 333 static_cast<http_connect_handshaker*>(gpr_malloc(sizeof(*handshaker))); 334 memset(handshaker, 0, sizeof(*handshaker)); 335 grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base); 336 gpr_mu_init(&handshaker->mu); 337 gpr_ref_init(&handshaker->refcount, 1); 338 grpc_slice_buffer_init(&handshaker->write_buffer); 339 GRPC_CLOSURE_INIT(&handshaker->request_done_closure, on_write_done, 340 handshaker, grpc_schedule_on_exec_ctx); 341 GRPC_CLOSURE_INIT(&handshaker->response_read_closure, on_read_done, 342 handshaker, grpc_schedule_on_exec_ctx); 343 grpc_http_parser_init(&handshaker->http_parser, GRPC_HTTP_RESPONSE, 344 &handshaker->http_response); 345 return &handshaker->base; 346 } 347 348 // 349 // handshaker factory 350 // 351 352 static void handshaker_factory_add_handshakers( 353 grpc_handshaker_factory* factory, const grpc_channel_args* args, 354 grpc_handshake_manager* handshake_mgr) { 355 grpc_handshake_manager_add(handshake_mgr, 356 grpc_http_connect_handshaker_create()); 357 } 358 359 static void handshaker_factory_destroy(grpc_handshaker_factory* factory) {} 360 361 static const grpc_handshaker_factory_vtable handshaker_factory_vtable = { 362 handshaker_factory_add_handshakers, handshaker_factory_destroy}; 363 364 static grpc_handshaker_factory handshaker_factory = { 365 &handshaker_factory_vtable}; 366 367 void grpc_http_connect_register_handshaker_factory() { 368 grpc_handshaker_factory_register(true /* at_start */, HANDSHAKER_CLIENT, 369 &handshaker_factory); 370 } 371