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