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 | |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/impl/codegen/alloc.h> |
| 37 | #include <grpc/impl/codegen/log.h> |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 38 | #include <grpc/impl/codegen/slice_buffer.h> |
| 39 | #include <grpc/support/string_util.h> |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 40 | |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 41 | #include "src/core/lib/http/format_request.h" |
| 42 | #include "src/core/lib/http/parser.h" |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 43 | #include "src/core/ext/client_config/http_connect_handshaker.h" |
| 44 | |
| 45 | typedef struct http_connect_handshaker { |
| 46 | // Base class. Must be first. |
| 47 | grpc_handshaker base; |
| 48 | |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 49 | char* proxy_server; |
Mark D. Roth | e273b03 | 2016-07-22 13:24:21 -0700 | [diff] [blame] | 50 | char* server_name; |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 51 | |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 52 | // 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. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 59 | gpr_slice_buffer write_buffer; |
| 60 | gpr_slice_buffer* read_buffer; |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 61 | grpc_closure request_done_closure; |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 62 | grpc_closure response_read_closure; |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 63 | grpc_http_parser http_parser; |
| 64 | grpc_http_response http_response; |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 65 | } http_connect_handshaker; |
| 66 | |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 67 | // Callback invoked when finished writing HTTP CONNECT request. |
| 68 | static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg, |
| 69 | grpc_error* error) { |
| 70 | http_connect_handshaker* h = arg; |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 71 | if (error != GRPC_ERROR_NONE) { |
| 72 | // If the write failed, invoke the callback immediately with the error. |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 73 | h->cb(exec_ctx, h->endpoint, h->args, h->read_buffer, h->user_data, |
| 74 | GRPC_ERROR_REF(error)); |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 75 | } else { |
| 76 | // Otherwise, read the response. |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 77 | grpc_endpoint_read(exec_ctx, h->endpoint, h->read_buffer, |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 78 | &h->response_read_closure); |
| 79 | } |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 82 | // Callback invoked for reading HTTP CONNECT response. |
| 83 | static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg, |
| 84 | grpc_error* error) { |
| 85 | http_connect_handshaker* h = arg; |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 86 | if (error != GRPC_ERROR_NONE) { |
| 87 | GRPC_ERROR_REF(error); // Take ref to pass to the handshake-done callback. |
| 88 | goto done; |
| 89 | } |
| 90 | // Add buffer to parser. |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 91 | for (size_t i = 0; i < h->read_buffer->count; ++i) { |
| 92 | if (GPR_SLICE_LENGTH(h->read_buffer->slices[i]) > 0) { |
| 93 | size_t body_start_offset = 0; |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 94 | error = grpc_http_parser_parse( |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 95 | &h->http_parser, h->read_buffer->slices[i], &body_start_offset); |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 96 | if (error != GRPC_ERROR_NONE) |
| 97 | goto done; |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 98 | 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. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 114 | } |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 115 | } |
| 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. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 128 | gpr_slice_buffer_reset_and_unref(h->read_buffer); |
| 129 | grpc_endpoint_read(exec_ctx, h->endpoint, h->read_buffer, |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 130 | &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. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 140 | } |
| 141 | done: |
| 142 | // Invoke handshake-done callback. |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 143 | h->cb(exec_ctx, h->endpoint, h->args, h->read_buffer, h->user_data, error); |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Mark D. Roth | 9136bb1 | 2016-07-21 09:52:12 -0700 | [diff] [blame] | 146 | // |
| 147 | // Public handshaker methods |
| 148 | // |
| 149 | |
| 150 | static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx, |
| 151 | grpc_handshaker* handshaker) { |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 152 | http_connect_handshaker* h = (http_connect_handshaker*)handshaker; |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 153 | gpr_free(h->proxy_server); |
| 154 | gpr_free(h->server_name); |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 155 | gpr_slice_buffer_destroy(&h->write_buffer); |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 156 | grpc_http_parser_destroy(&h->http_parser); |
| 157 | grpc_http_response_destroy(&h->http_response); |
| 158 | gpr_free(h); |
Mark D. Roth | 9136bb1 | 2016-07-21 09:52:12 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx, |
| 162 | grpc_handshaker* handshaker) { |
| 163 | } |
| 164 | |
Mark D. Roth | 64d88bd | 2016-07-27 12:44:10 -0700 | [diff] [blame] | 165 | // FIXME BEFORE MERGING: apply deadline |
Mark D. Roth | 9136bb1 | 2016-07-21 09:52:12 -0700 | [diff] [blame] | 166 | static void http_connect_handshaker_do_handshake( |
| 167 | grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker, |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 168 | grpc_endpoint* endpoint, grpc_channel_args* args, |
| 169 | gpr_slice_buffer* read_buffer, gpr_timespec deadline, |
Mark D. Roth | 9136bb1 | 2016-07-21 09:52:12 -0700 | [diff] [blame] | 170 | grpc_tcp_server_acceptor* acceptor, grpc_handshaker_done_cb cb, |
| 171 | void* user_data) { |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 172 | http_connect_handshaker* h = (http_connect_handshaker*)handshaker; |
| 173 | // Save state in the handshaker object. |
| 174 | h->endpoint = endpoint; |
| 175 | h->args = args; |
| 176 | h->cb = cb; |
| 177 | h->user_data = user_data; |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 178 | // Initialize fields. |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 179 | gpr_slice_buffer_init(&h->write_buffer); |
| 180 | h->read_buffer = read_buffer; |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 181 | grpc_closure_init(&h->request_done_closure, on_write_done, h); |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 182 | grpc_closure_init(&h->response_read_closure, on_read_done, h); |
| 183 | grpc_http_parser_init(&h->http_parser, GRPC_HTTP_RESPONSE, |
| 184 | &h->http_response); |
| 185 | // Send HTTP CONNECT request. |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 186 | gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s", |
| 187 | h->server_name, h->proxy_server); |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 188 | grpc_httpcli_request request; |
| 189 | memset(&request, 0, sizeof(request)); |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 190 | request.host = h->proxy_server; |
| 191 | request.http.method = "CONNECT"; |
| 192 | request.http.path = h->server_name; |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 193 | request.handshaker = &grpc_httpcli_plaintext; |
| 194 | gpr_slice request_slice = grpc_httpcli_format_connect_request(&request); |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame^] | 195 | gpr_slice_buffer_add(&h->write_buffer, request_slice); |
| 196 | grpc_endpoint_write(exec_ctx, endpoint, &h->write_buffer, |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 197 | &h->request_done_closure); |
| 198 | } |
| 199 | |
| 200 | static const struct grpc_handshaker_vtable http_connect_handshaker_vtable = { |
| 201 | http_connect_handshaker_destroy, http_connect_handshaker_shutdown, |
| 202 | http_connect_handshaker_do_handshake}; |
| 203 | |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 204 | grpc_handshaker* grpc_http_connect_handshaker_create(const char* proxy_server, |
| 205 | const char* server_name) { |
| 206 | GPR_ASSERT(proxy_server != NULL); |
| 207 | GPR_ASSERT(server_name != NULL); |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 208 | http_connect_handshaker* handshaker = |
| 209 | gpr_malloc(sizeof(http_connect_handshaker)); |
| 210 | memset(handshaker, 0, sizeof(*handshaker)); |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 211 | grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base); |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 212 | handshaker->proxy_server = gpr_strdup(proxy_server); |
| 213 | handshaker->server_name = gpr_strdup(server_name); |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 214 | return (grpc_handshaker*)handshaker; |
| 215 | } |