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