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 | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 59 | gpr_slice_buffer request_buffer; |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 60 | grpc_closure request_done_closure; |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 61 | gpr_slice_buffer response_buffer; |
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. |
| 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. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 81 | // Callback invoked for reading HTTP CONNECT response. |
| 82 | static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg, |
| 83 | grpc_error* error) { |
| 84 | http_connect_handshaker* h = arg; |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 85 | 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. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 96 | } |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 97 | } |
| 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. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 122 | } |
| 123 | done: |
| 124 | // Invoke handshake-done callback. |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 125 | h->cb(exec_ctx, h->endpoint, h->args, h->user_data, error); |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Mark D. Roth | 9136bb1 | 2016-07-21 09:52:12 -0700 | [diff] [blame] | 128 | // |
| 129 | // Public handshaker methods |
| 130 | // |
| 131 | |
| 132 | static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx, |
| 133 | grpc_handshaker* handshaker) { |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 134 | http_connect_handshaker* h = (http_connect_handshaker*)handshaker; |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 135 | gpr_free(h->proxy_server); |
| 136 | gpr_free(h->server_name); |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 137 | 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. Roth | 9136bb1 | 2016-07-21 09:52:12 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx, |
| 145 | grpc_handshaker* handshaker) { |
| 146 | } |
| 147 | |
Mark D. Roth | 64d88bd | 2016-07-27 12:44:10 -0700 | [diff] [blame] | 148 | // FIXME BEFORE MERGING: apply deadline |
Mark D. Roth | 9136bb1 | 2016-07-21 09:52:12 -0700 | [diff] [blame] | 149 | static 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. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 154 | 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. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 160 | // Initialize fields. |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 161 | gpr_slice_buffer_init(&h->request_buffer); |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 162 | grpc_closure_init(&h->request_done_closure, on_write_done, h); |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 163 | 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. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 168 | gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s", |
| 169 | h->server_name, h->proxy_server); |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 170 | grpc_httpcli_request request; |
| 171 | memset(&request, 0, sizeof(request)); |
Mark D. Roth | b954df1 | 2016-08-01 09:53:52 -0700 | [diff] [blame] | 172 | request.host = h->proxy_server; |
| 173 | request.http.method = "CONNECT"; |
| 174 | request.http.path = h->server_name; |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 175 | request.handshaker = &grpc_httpcli_plaintext; |
| 176 | gpr_slice request_slice = grpc_httpcli_format_connect_request(&request); |
Mark D. Roth | f725019 | 2016-07-22 08:06:09 -0700 | [diff] [blame] | 177 | gpr_slice_buffer_add(&h->request_buffer, request_slice); |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 178 | grpc_endpoint_write(exec_ctx, endpoint, &h->request_buffer, |
| 179 | &h->request_done_closure); |
| 180 | } |
| 181 | |
| 182 | static 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. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 186 | grpc_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. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 190 | http_connect_handshaker* handshaker = |
| 191 | gpr_malloc(sizeof(http_connect_handshaker)); |
| 192 | memset(handshaker, 0, sizeof(*handshaker)); |
Mark D. Roth | 77613b2 | 2016-07-22 09:41:10 -0700 | [diff] [blame] | 193 | grpc_handshaker_init(&http_connect_handshaker_vtable, &handshaker->base); |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 194 | handshaker->proxy_server = gpr_strdup(proxy_server); |
| 195 | handshaker->server_name = gpr_strdup(server_name); |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 196 | return (grpc_handshaker*)handshaker; |
| 197 | } |