blob: c649796f42713121e81d2d6131c7455e6bca9dc0 [file] [log] [blame]
Mark D. Roth4623e1c2016-07-18 14:09:18 -07001/*
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>
38
Mark D. Rothf7250192016-07-22 08:06:09 -070039#include "src/core/lib/http/format_request.h"
40#include "src/core/lib/http/parser.h"
Mark D. Roth4623e1c2016-07-18 14:09:18 -070041#include "src/core/ext/client_config/http_connect_handshaker.h"
42
43typedef struct http_connect_handshaker {
44 // Base class. Must be first.
45 grpc_handshaker base;
46
47 // State saved while performing the handshake.
48 grpc_endpoint* endpoint;
49 grpc_channel_args* args;
50 grpc_handshaker_done_cb cb;
51 void* user_data;
52
53 // Objects for processing the HTTP CONNECT request and response.
54 grpc_slice_buffer request_buffer;
55 grpc_closure request_done_closure;
56 grpc_slice_buffer response_buffer;
57 grpc_closure response_read_closure;
Mark D. Rothf7250192016-07-22 08:06:09 -070058 grpc_http_parser http_parser;
59 grpc_http_response http_response;
Mark D. Roth4623e1c2016-07-18 14:09:18 -070060} http_connect_handshaker;
61
Mark D. Roth4623e1c2016-07-18 14:09:18 -070062// Callback invoked when finished writing HTTP CONNECT request.
63static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
64 grpc_error* error) {
65 http_connect_handshaker* h = arg;
66 // Read HTTP CONNECT response.
Mark D. Roth4623e1c2016-07-18 14:09:18 -070067 grpc_endpoint_read(exec_ctx, h->endpoint, &h->response_buffer,
68 &h->response_read_closure);
69}
70
Mark D. Rothf7250192016-07-22 08:06:09 -070071// Callback invoked for reading HTTP CONNECT response.
72static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
73 grpc_error* error) {
74 http_connect_handshaker* h = arg;
75 if (error == GRPC_ERROR_NONE) {
76 for (size_t i = 0; i < h->response_buffer.count; ++i) {
77 if (GPR_SLICE_LENGTH(h->response_buffer.slices[i]) > 0) {
78 error = grpc_http_parser_parse(
79 &h->http_parser, h->response_buffer.slices[i]);
80 if (error != GRPC_ERROR_NONE)
81 goto done;
82 }
83 }
84 // If we're not done reading the response, read more data.
85 // TODO(roth): In practice, I suspect that the response to a CONNECT
86 // request will never include a body, in which case this check is
87 // sufficient. However, the language of RFC-2817 doesn't explicitly
88 // forbid the response from including a body. If there is a body,
89 // it's possible that we might have parsed part but not all of the
90 // body, in which case this check will cause us to fail to parse the
91 // remainder of the body. If that ever becomes an issue, we may
92 // need to fix the HTTP parser to understand when the body is
93 // complete (e.g., handling chunked transfer encoding or looking
94 // at the Content-Length: header).
95 if (h->http_parser->state != GRPC_HTTP_BODY) {
96 grpc_endpoint_read(exec_ctx, h->endpoint, &h->response_buffer,
97 &h->response_read_closure);
98 return;
99 }
100 }
101 done:
102 // Invoke handshake-done callback.
103// FIXME: pass error to callback
104 h->cb(exec_ctx, h->endpoint, h->args, h->user_data);
105}
106
Mark D. Roth9136bb12016-07-21 09:52:12 -0700107//
108// Public handshaker methods
109//
110
111static void http_connect_handshaker_destroy(grpc_exec_ctx* exec_ctx,
112 grpc_handshaker* handshaker) {
Mark D. Rothf7250192016-07-22 08:06:09 -0700113 grpc_slice_buffer_destroy(&handshaker->request_buffer);
114 grpc_slice_buffer_destroy(&handshaker->response_buffer);
115 grpc_http_parser_destroy(&handshaker->http_parser);
116 grpc_http_response_destroy(&handshaker->http_response);
Mark D. Roth9136bb12016-07-21 09:52:12 -0700117 gpr_free(handshaker);
118}
119
120static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
121 grpc_handshaker* handshaker) {
122}
123
124static void http_connect_handshaker_do_handshake(
125 grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker,
126 grpc_endpoint* endpoint, grpc_channel_args* args, gpr_timespec deadline,
127 grpc_tcp_server_acceptor* acceptor, grpc_handshaker_done_cb cb,
128 void* user_data) {
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700129 http_connect_handshaker* h = (http_connect_handshaker*)handshaker;
130 // Save state in the handshaker object.
131 h->endpoint = endpoint;
132 h->args = args;
133 h->cb = cb;
134 h->user_data = user_data;
Mark D. Rothf7250192016-07-22 08:06:09 -0700135 // Initialize fields.
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700136 gpr_slice_buffer_init(&h->request_buffer);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700137 grpc_closure_init(&h->request_done_closure, on_write_done, h);
Mark D. Rothf7250192016-07-22 08:06:09 -0700138 gpr_slice_buffer_init(&h->response_buffer);
139 grpc_closure_init(&h->response_read_closure, on_read_done, h);
140 grpc_http_parser_init(&h->http_parser, GRPC_HTTP_RESPONSE,
141 &h->http_response);
142 // Send HTTP CONNECT request.
143 grpc_httpcli_request request;
144 memset(&request, 0, sizeof(request));
145 // FIXME: get proxy name from somewhere...
146 request.host = gpr_strdup("");
147 request.http.method = gpr_strdup("CONNECT");
148 // FIXME: get server name from somewhere...
149 request.http.path = gpr_strdup("");
150 request.handshaker = grpc_httpcli_plaintext;
151 gpr_slice request_slice = grpc_httpcli_format_connect_request(request);
152 gpr_slice_buffer_add(&h->request_buffer, request_slice);
Mark D. Roth4623e1c2016-07-18 14:09:18 -0700153 grpc_endpoint_write(exec_ctx, endpoint, &h->request_buffer,
154 &h->request_done_closure);
155}
156
157static const struct grpc_handshaker_vtable http_connect_handshaker_vtable = {
158 http_connect_handshaker_destroy, http_connect_handshaker_shutdown,
159 http_connect_handshaker_do_handshake};
160
161grpc_handshaker* grpc_http_connect_handshaker_create() {
162 http_connect_handshaker* handshaker =
163 gpr_malloc(sizeof(http_connect_handshaker));
164 memset(handshaker, 0, sizeof(*handshaker));
165 grpc_handshaker_init(http_connect_handshaker_vtable, &handshaker->base);
166 return (grpc_handshaker*)handshaker;
167}