blob: 1f64df68e36200c02a1b43d8f5a3743df4baa761 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 "src/core/channel/http_server_filter.h"
ctillerd74729d2015-01-12 13:31:36 -080035
36#include <string.h>
Craig Tillerc50e3982015-01-20 16:30:54 -080037#include <grpc/grpc_http.h>
38#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/log.h>
40
ctillerd74729d2015-01-12 13:31:36 -080041typedef struct call_data {
Craig Tiller6902ad22015-04-16 08:01:49 -070042 gpr_uint8 got_initial_metadata;
43 gpr_uint8 seen_path;
44 gpr_uint8 seen_post;
Craig Tillerd2d0a112015-01-19 16:07:52 -080045 gpr_uint8 sent_status;
46 gpr_uint8 seen_scheme;
Craig Tillerd2d0a112015-01-19 16:07:52 -080047 gpr_uint8 seen_te_trailers;
Craig Tiller6902ad22015-04-16 08:01:49 -070048 grpc_linked_mdelem status;
Craig Tiller83f88d92015-04-21 16:02:05 -070049
50 grpc_stream_op_buffer *recv_ops;
51 void (*on_done_recv)(void *user_data, int success);
52 void *recv_user_data;
ctillerd74729d2015-01-12 13:31:36 -080053} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
klempner1e273282014-12-10 13:55:41 -080055typedef struct channel_data {
56 grpc_mdelem *te_trailers;
Craig Tillerc923cd72015-01-19 16:14:31 -080057 grpc_mdelem *method_post;
ctillerd74729d2015-01-12 13:31:36 -080058 grpc_mdelem *http_scheme;
59 grpc_mdelem *https_scheme;
60 /* TODO(klempner): Remove this once we stop using it */
61 grpc_mdelem *grpc_scheme;
62 grpc_mdelem *content_type;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080063 grpc_mdelem *status_ok;
64 grpc_mdelem *status_not_found;
Craig Tillerd2d0a112015-01-19 16:07:52 -080065 grpc_mdstr *path_key;
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +090066 grpc_mdstr *authority_key;
67 grpc_mdstr *host_key;
68
69 grpc_mdctx *mdctx;
klempner1e273282014-12-10 13:55:41 -080070} channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071
72/* used to silence 'variable not used' warnings */
73static void ignore_unused(void *ignored) {}
74
Craig Tiller6902ad22015-04-16 08:01:49 -070075static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
76 grpc_call_element *elem = user_data;
Craig Tillerce6e3502015-01-19 18:04:42 -080077 channel_data *channeld = elem->channel_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080078 call_data *calld = elem->call_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080079
Craig Tiller6902ad22015-04-16 08:01:49 -070080 /* Check if it is one of the headers we care about. */
Craig Tiller87d5b192015-04-16 14:37:57 -070081 if (md == channeld->te_trailers || md == channeld->method_post ||
82 md == channeld->http_scheme || md == channeld->https_scheme ||
83 md == channeld->grpc_scheme || md == channeld->content_type) {
Craig Tiller6902ad22015-04-16 08:01:49 -070084 /* swallow it */
85 if (md == channeld->method_post) {
86 calld->seen_post = 1;
87 } else if (md->key == channeld->http_scheme->key) {
88 calld->seen_scheme = 1;
89 } else if (md == channeld->te_trailers) {
90 calld->seen_te_trailers = 1;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080091 }
Craig Tiller6902ad22015-04-16 08:01:49 -070092 /* TODO(klempner): Track that we've seen all the headers we should
93 require */
94 return NULL;
95 } else if (md->key == channeld->content_type->key) {
Craig Tiller87d5b192015-04-16 14:37:57 -070096 if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) ==
97 0) {
Craig Tiller6902ad22015-04-16 08:01:49 -070098 /* Although the C implementation doesn't (currently) generate them,
Craig Tiller9c9d4e02015-04-20 09:03:29 -070099 any custom +-suffix is explicitly valid. */
Craig Tiller6902ad22015-04-16 08:01:49 -0700100 /* TODO(klempner): We should consider preallocating common values such
101 as +proto or +json, or at least stashing them if we see them. */
102 /* TODO(klempner): Should we be surfacing this to application code? */
103 } else {
104 /* TODO(klempner): We're currently allowing this, but we shouldn't
105 see it without a proxy so log for now. */
106 gpr_log(GPR_INFO, "Unexpected content-type %s",
107 channeld->content_type->key);
108 }
109 return NULL;
110 } else if (md->key == channeld->te_trailers->key ||
111 md->key == channeld->method_post->key ||
112 md->key == channeld->http_scheme->key ||
113 md->key == channeld->content_type->key) {
114 gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
Craig Tiller87d5b192015-04-16 14:37:57 -0700115 grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value));
Craig Tiller6902ad22015-04-16 08:01:49 -0700116 /* swallow it and error everything out. */
117 /* TODO(klempner): We ought to generate more descriptive error messages
118 on the wire here. */
119 grpc_call_element_send_cancel(elem);
120 return NULL;
121 } else if (md->key == channeld->path_key) {
122 if (calld->seen_path) {
123 gpr_log(GPR_ERROR, "Received :path twice");
124 return NULL;
125 }
126 calld->seen_path = 1;
127 return md;
128 } else if (md->key == channeld->host_key) {
129 /* translate host to :authority since :authority may be
130 omitted */
131 grpc_mdelem *authority = grpc_mdelem_from_metadata_strings(
132 channeld->mdctx, grpc_mdstr_ref(channeld->authority_key),
133 grpc_mdstr_ref(md->value));
134 grpc_mdelem_unref(md);
135 return authority;
136 } else {
137 return md;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800138 }
Craig Tillerce6e3502015-01-19 18:04:42 -0800139}
140
Craig Tiller83f88d92015-04-21 16:02:05 -0700141static void hs_on_recv(void *user_data, int success) {
142 grpc_call_element *elem = user_data;
143 call_data *calld = elem->call_data;
144 if (success) {
145 size_t i;
146 size_t nops = calld->recv_ops->nops;
147 grpc_stream_op *ops = calld->recv_ops->ops;
148 for (i = 0; i < nops; i++) {
149 grpc_stream_op *op = &ops[i];
150 if (op->type != GRPC_OP_METADATA) continue;
151 calld->got_initial_metadata = 1;
152 grpc_metadata_batch_filter(&op->data.metadata, server_filter, elem);
153 /* Have we seen the required http2 transport headers?
154 (:method, :scheme, content-type, with :path and :authority covered
155 at the channel level right now) */
156 if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
157 calld->seen_path) {
158 /* do nothing */
159 } else {
Craig Tiller77979b02015-04-22 07:56:09 -0700160 if (!calld->seen_path) {
161 gpr_log(GPR_ERROR, "Missing :path header");
162 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700163 if (!calld->seen_post) {
164 gpr_log(GPR_ERROR, "Missing :method header");
165 }
166 if (!calld->seen_scheme) {
167 gpr_log(GPR_ERROR, "Missing :scheme header");
168 }
169 if (!calld->seen_te_trailers) {
170 gpr_log(GPR_ERROR, "Missing te trailers header");
171 }
172 /* Error this call out */
173 success = 0;
174 grpc_call_element_send_cancel(elem);
175 }
176 }
177 }
178 calld->on_done_recv(calld->recv_user_data, success);
179}
180
Craig Tiller50d9db52015-04-23 10:52:14 -0700181static void hs_mutate_op(grpc_call_element *elem, grpc_transport_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182 /* grab pointers to our data from the call element */
183 call_data *calld = elem->call_data;
184 channel_data *channeld = elem->channel_data;
Craig Tiller83f88d92015-04-21 16:02:05 -0700185 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186
Craig Tiller83f88d92015-04-21 16:02:05 -0700187 if (op->send_ops && !calld->sent_status) {
188 size_t nops = op->send_ops->nops;
189 grpc_stream_op *ops = op->send_ops->ops;
190 for (i = 0; i < nops; i++) {
191 grpc_stream_op *op = &ops[i];
192 if (op->type != GRPC_OP_METADATA) continue;
193 calld->sent_status = 1;
194 grpc_metadata_batch_add_head(&op->data.metadata, &calld->status,
195 grpc_mdelem_ref(channeld->status_ok));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196 break;
Craig Tiller83f88d92015-04-21 16:02:05 -0700197 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700199
200 if (op->recv_ops && !calld->got_initial_metadata) {
201 /* substitute our callback for the higher callback */
202 calld->recv_ops = op->recv_ops;
203 calld->on_done_recv = op->on_done_recv;
204 calld->recv_user_data = op->recv_user_data;
205 op->on_done_recv = hs_on_recv;
206 op->recv_user_data = elem;
207 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700208}
Craig Tiller83f88d92015-04-21 16:02:05 -0700209
Craig Tiller06aeea72015-04-23 10:54:45 -0700210static void hs_start_transport_op(grpc_call_element *elem,
211 grpc_transport_op *op) {
Craig Tiller50d9db52015-04-23 10:52:14 -0700212 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
213 hs_mutate_op(elem, op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700214 grpc_call_next_op(elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800215}
216
217/* Called on special channel events, such as disconnection or new incoming
218 calls on the server */
ctillerf962f522014-12-10 15:28:27 -0800219static void channel_op(grpc_channel_element *elem,
220 grpc_channel_element *from_elem, grpc_channel_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221 /* grab pointers to our data from the channel element */
222 channel_data *channeld = elem->channel_data;
223
224 ignore_unused(channeld);
225
226 switch (op->type) {
227 default:
228 /* pass control up or down the stack depending on op->dir */
229 grpc_channel_next_op(elem, op);
230 break;
231 }
232}
233
234/* Constructor for call_data */
235static void init_call_elem(grpc_call_element *elem,
Craig Tiller06aeea72015-04-23 10:54:45 -0700236 const void *server_transport_data,
237 grpc_transport_op *initial_op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800238 /* grab pointers to our data from the call element */
239 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800240 /* initialize members */
Craig Tiller6902ad22015-04-16 08:01:49 -0700241 memset(calld, 0, sizeof(*calld));
Craig Tiller50d9db52015-04-23 10:52:14 -0700242 if (initial_op) hs_mutate_op(elem, initial_op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800243}
244
245/* Destructor for call_data */
Craig Tiller87d5b192015-04-16 14:37:57 -0700246static void destroy_call_elem(grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800247
248/* Constructor for channel_data */
249static void init_channel_elem(grpc_channel_element *elem,
250 const grpc_channel_args *args, grpc_mdctx *mdctx,
251 int is_first, int is_last) {
252 /* grab pointers to our data from the channel element */
253 channel_data *channeld = elem->channel_data;
254
255 /* The first and the last filters tend to be implemented differently to
256 handle the case that there's no 'next' filter to call on the up or down
257 path */
258 GPR_ASSERT(!is_first);
259 GPR_ASSERT(!is_last);
260
261 /* initialize members */
262 channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers");
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800263 channeld->status_ok = grpc_mdelem_from_strings(mdctx, ":status", "200");
264 channeld->status_not_found =
265 grpc_mdelem_from_strings(mdctx, ":status", "404");
Craig Tillerc923cd72015-01-19 16:14:31 -0800266 channeld->method_post = grpc_mdelem_from_strings(mdctx, ":method", "POST");
ctillerd74729d2015-01-12 13:31:36 -0800267 channeld->http_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "http");
268 channeld->https_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "https");
269 channeld->grpc_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "grpc");
Craig Tillerd2d0a112015-01-19 16:07:52 -0800270 channeld->path_key = grpc_mdstr_from_string(mdctx, ":path");
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900271 channeld->authority_key = grpc_mdstr_from_string(mdctx, ":authority");
272 channeld->host_key = grpc_mdstr_from_string(mdctx, "host");
ctillerd74729d2015-01-12 13:31:36 -0800273 channeld->content_type =
274 grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc");
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800275
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900276 channeld->mdctx = mdctx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800277}
278
279/* Destructor for channel data */
280static void destroy_channel_elem(grpc_channel_element *elem) {
281 /* grab pointers to our data from the channel element */
282 channel_data *channeld = elem->channel_data;
283
284 grpc_mdelem_unref(channeld->te_trailers);
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800285 grpc_mdelem_unref(channeld->status_ok);
286 grpc_mdelem_unref(channeld->status_not_found);
Craig Tillerc923cd72015-01-19 16:14:31 -0800287 grpc_mdelem_unref(channeld->method_post);
ctillerd74729d2015-01-12 13:31:36 -0800288 grpc_mdelem_unref(channeld->http_scheme);
289 grpc_mdelem_unref(channeld->https_scheme);
290 grpc_mdelem_unref(channeld->grpc_scheme);
291 grpc_mdelem_unref(channeld->content_type);
Craig Tillerd2d0a112015-01-19 16:07:52 -0800292 grpc_mdstr_unref(channeld->path_key);
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900293 grpc_mdstr_unref(channeld->authority_key);
294 grpc_mdstr_unref(channeld->host_key);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800295}
296
297const grpc_channel_filter grpc_http_server_filter = {
Craig Tiller06aeea72015-04-23 10:54:45 -0700298 hs_start_transport_op, channel_op, sizeof(call_data), init_call_elem,
299 destroy_call_elem, sizeof(channel_data), init_channel_elem,
300 destroy_channel_elem, "http-server"};