blob: 201adf4f35c7c3b468115d9bfd2dbe53888bf611 [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/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <grpc/support/log.h>
39
ctillerd74729d2015-01-12 13:31:36 -080040typedef struct call_data {
Craig Tiller6902ad22015-04-16 08:01:49 -070041 gpr_uint8 got_initial_metadata;
42 gpr_uint8 seen_path;
43 gpr_uint8 seen_post;
Craig Tillerd2d0a112015-01-19 16:07:52 -080044 gpr_uint8 sent_status;
45 gpr_uint8 seen_scheme;
Craig Tillerd2d0a112015-01-19 16:07:52 -080046 gpr_uint8 seen_te_trailers;
Craig Tillerc4b56b62015-07-23 17:44:11 -070047 gpr_uint8 seen_authority;
Craig Tiller6902ad22015-04-16 08:01:49 -070048 grpc_linked_mdelem status;
Craig Tillercb014172015-09-08 16:40:26 -070049 grpc_linked_mdelem content_type;
Craig Tiller83f88d92015-04-21 16:02:05 -070050
51 grpc_stream_op_buffer *recv_ops;
Craig Tiller98bf7e62015-06-24 08:47:07 -070052 /** Closure to call when finished with the hs_on_recv hook */
Craig Tiller33825112015-09-18 07:44:19 -070053 grpc_closure *on_done_recv;
Craig Tiller98bf7e62015-06-24 08:47:07 -070054 /** Receive closures are chained: we inject this closure as the on_done_recv
55 up-call on transport_op, and remember to call our on_done_recv member
56 after handling it. */
Craig Tiller33825112015-09-18 07:44:19 -070057 grpc_closure hs_on_recv;
ctillerd74729d2015-01-12 13:31:36 -080058} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
klempner1e273282014-12-10 13:55:41 -080060typedef struct channel_data {
61 grpc_mdelem *te_trailers;
Craig Tillerc923cd72015-01-19 16:14:31 -080062 grpc_mdelem *method_post;
ctillerd74729d2015-01-12 13:31:36 -080063 grpc_mdelem *http_scheme;
64 grpc_mdelem *https_scheme;
65 /* TODO(klempner): Remove this once we stop using it */
66 grpc_mdelem *grpc_scheme;
67 grpc_mdelem *content_type;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080068 grpc_mdelem *status_ok;
69 grpc_mdelem *status_not_found;
Craig Tillerd2d0a112015-01-19 16:07:52 -080070 grpc_mdstr *path_key;
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +090071 grpc_mdstr *authority_key;
72 grpc_mdstr *host_key;
73
74 grpc_mdctx *mdctx;
klempner1e273282014-12-10 13:55:41 -080075} channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076
Craig Tillerd1bec032015-09-18 17:29:00 -070077typedef struct {
78 grpc_call_element *elem;
79 grpc_call_list *call_list;
80} server_filter_args;
81
Craig Tiller6902ad22015-04-16 08:01:49 -070082static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tillerd1bec032015-09-18 17:29:00 -070083 server_filter_args *a = user_data;
84 grpc_call_element *elem = a->elem;
Craig Tillerce6e3502015-01-19 18:04:42 -080085 channel_data *channeld = elem->channel_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080086 call_data *calld = elem->call_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080087
Craig Tiller6902ad22015-04-16 08:01:49 -070088 /* Check if it is one of the headers we care about. */
Craig Tiller87d5b192015-04-16 14:37:57 -070089 if (md == channeld->te_trailers || md == channeld->method_post ||
90 md == channeld->http_scheme || md == channeld->https_scheme ||
91 md == channeld->grpc_scheme || md == channeld->content_type) {
Craig Tiller6902ad22015-04-16 08:01:49 -070092 /* swallow it */
93 if (md == channeld->method_post) {
94 calld->seen_post = 1;
95 } else if (md->key == channeld->http_scheme->key) {
96 calld->seen_scheme = 1;
97 } else if (md == channeld->te_trailers) {
98 calld->seen_te_trailers = 1;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080099 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700100 /* TODO(klempner): Track that we've seen all the headers we should
101 require */
102 return NULL;
103 } else if (md->key == channeld->content_type->key) {
Craig Tiller87d5b192015-04-16 14:37:57 -0700104 if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) ==
105 0) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700106 /* Although the C implementation doesn't (currently) generate them,
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700107 any custom +-suffix is explicitly valid. */
Craig Tiller6902ad22015-04-16 08:01:49 -0700108 /* TODO(klempner): We should consider preallocating common values such
109 as +proto or +json, or at least stashing them if we see them. */
110 /* TODO(klempner): Should we be surfacing this to application code? */
111 } else {
112 /* TODO(klempner): We're currently allowing this, but we shouldn't
113 see it without a proxy so log for now. */
114 gpr_log(GPR_INFO, "Unexpected content-type %s",
115 channeld->content_type->key);
116 }
117 return NULL;
118 } else if (md->key == channeld->te_trailers->key ||
119 md->key == channeld->method_post->key ||
120 md->key == channeld->http_scheme->key ||
121 md->key == channeld->content_type->key) {
122 gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
Craig Tiller87d5b192015-04-16 14:37:57 -0700123 grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value));
Craig Tiller6902ad22015-04-16 08:01:49 -0700124 /* swallow it and error everything out. */
125 /* TODO(klempner): We ought to generate more descriptive error messages
126 on the wire here. */
Craig Tillerd1bec032015-09-18 17:29:00 -0700127 grpc_call_element_send_cancel(elem, a->call_list);
Craig Tiller6902ad22015-04-16 08:01:49 -0700128 return NULL;
129 } else if (md->key == channeld->path_key) {
130 if (calld->seen_path) {
131 gpr_log(GPR_ERROR, "Received :path twice");
132 return NULL;
133 }
134 calld->seen_path = 1;
135 return md;
Craig Tillerc4b56b62015-07-23 17:44:11 -0700136 } else if (md->key == channeld->authority_key) {
137 calld->seen_authority = 1;
138 return md;
Craig Tiller6902ad22015-04-16 08:01:49 -0700139 } else if (md->key == channeld->host_key) {
140 /* translate host to :authority since :authority may be
141 omitted */
142 grpc_mdelem *authority = grpc_mdelem_from_metadata_strings(
Craig Tiller1a65a232015-07-06 10:22:32 -0700143 channeld->mdctx, GRPC_MDSTR_REF(channeld->authority_key),
144 GRPC_MDSTR_REF(md->value));
145 GRPC_MDELEM_UNREF(md);
Craig Tillerc4b56b62015-07-23 17:44:11 -0700146 calld->seen_authority = 1;
Craig Tiller6902ad22015-04-16 08:01:49 -0700147 return authority;
148 } else {
149 return md;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800150 }
Craig Tillerce6e3502015-01-19 18:04:42 -0800151}
152
Craig Tillerd1bec032015-09-18 17:29:00 -0700153static void hs_on_recv(void *user_data, int success,
154 grpc_call_list *call_list) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700155 grpc_call_element *elem = user_data;
156 call_data *calld = elem->call_data;
157 if (success) {
158 size_t i;
159 size_t nops = calld->recv_ops->nops;
160 grpc_stream_op *ops = calld->recv_ops->ops;
161 for (i = 0; i < nops; i++) {
162 grpc_stream_op *op = &ops[i];
Craig Tillerd1bec032015-09-18 17:29:00 -0700163 server_filter_args a;
Craig Tiller83f88d92015-04-21 16:02:05 -0700164 if (op->type != GRPC_OP_METADATA) continue;
165 calld->got_initial_metadata = 1;
Craig Tillerd1bec032015-09-18 17:29:00 -0700166 a.elem = elem;
167 a.call_list = call_list;
168 grpc_metadata_batch_filter(&op->data.metadata, server_filter, &a);
Craig Tiller83f88d92015-04-21 16:02:05 -0700169 /* Have we seen the required http2 transport headers?
170 (:method, :scheme, content-type, with :path and :authority covered
171 at the channel level right now) */
172 if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
Craig Tillerc4b56b62015-07-23 17:44:11 -0700173 calld->seen_path && calld->seen_authority) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700174 /* do nothing */
175 } else {
Craig Tiller77979b02015-04-22 07:56:09 -0700176 if (!calld->seen_path) {
177 gpr_log(GPR_ERROR, "Missing :path header");
178 }
Craig Tillerc4b56b62015-07-23 17:44:11 -0700179 if (!calld->seen_authority) {
180 gpr_log(GPR_ERROR, "Missing :authority header");
181 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700182 if (!calld->seen_post) {
183 gpr_log(GPR_ERROR, "Missing :method header");
184 }
185 if (!calld->seen_scheme) {
186 gpr_log(GPR_ERROR, "Missing :scheme header");
187 }
188 if (!calld->seen_te_trailers) {
189 gpr_log(GPR_ERROR, "Missing te trailers header");
190 }
191 /* Error this call out */
192 success = 0;
Craig Tillerd1bec032015-09-18 17:29:00 -0700193 grpc_call_element_send_cancel(elem, call_list);
Craig Tiller83f88d92015-04-21 16:02:05 -0700194 }
195 }
196 }
Craig Tillerd1bec032015-09-18 17:29:00 -0700197 calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success, call_list);
Craig Tiller83f88d92015-04-21 16:02:05 -0700198}
199
Craig Tillerb7959a02015-06-25 08:50:54 -0700200static void hs_mutate_op(grpc_call_element *elem,
201 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800202 /* grab pointers to our data from the call element */
203 call_data *calld = elem->call_data;
204 channel_data *channeld = elem->channel_data;
Craig Tiller83f88d92015-04-21 16:02:05 -0700205 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206
Craig Tiller83f88d92015-04-21 16:02:05 -0700207 if (op->send_ops && !calld->sent_status) {
208 size_t nops = op->send_ops->nops;
209 grpc_stream_op *ops = op->send_ops->ops;
210 for (i = 0; i < nops; i++) {
211 grpc_stream_op *op = &ops[i];
212 if (op->type != GRPC_OP_METADATA) continue;
213 calld->sent_status = 1;
214 grpc_metadata_batch_add_head(&op->data.metadata, &calld->status,
Craig Tiller1a65a232015-07-06 10:22:32 -0700215 GRPC_MDELEM_REF(channeld->status_ok));
Craig Tillercb014172015-09-08 16:40:26 -0700216 grpc_metadata_batch_add_tail(&op->data.metadata, &calld->content_type,
217 GRPC_MDELEM_REF(channeld->content_type));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218 break;
Craig Tiller83f88d92015-04-21 16:02:05 -0700219 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700221
222 if (op->recv_ops && !calld->got_initial_metadata) {
223 /* substitute our callback for the higher callback */
224 calld->recv_ops = op->recv_ops;
225 calld->on_done_recv = op->on_done_recv;
Craig Tiller1e6facb2015-06-11 22:47:11 -0700226 op->on_done_recv = &calld->hs_on_recv;
Craig Tiller83f88d92015-04-21 16:02:05 -0700227 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700228}
Craig Tiller83f88d92015-04-21 16:02:05 -0700229
Craig Tiller06aeea72015-04-23 10:54:45 -0700230static void hs_start_transport_op(grpc_call_element *elem,
Craig Tillerd1bec032015-09-18 17:29:00 -0700231 grpc_transport_stream_op *op,
232 grpc_call_list *call_list) {
Craig Tiller50d9db52015-04-23 10:52:14 -0700233 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
234 hs_mutate_op(elem, op);
Craig Tillerd1bec032015-09-18 17:29:00 -0700235 grpc_call_next_op(elem, op, call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800236}
237
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800238/* Constructor for call_data */
239static void init_call_elem(grpc_call_element *elem,
Craig Tiller06aeea72015-04-23 10:54:45 -0700240 const void *server_transport_data,
Craig Tillerb7959a02015-06-25 08:50:54 -0700241 grpc_transport_stream_op *initial_op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242 /* grab pointers to our data from the call element */
243 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800244 /* initialize members */
Craig Tiller6902ad22015-04-16 08:01:49 -0700245 memset(calld, 0, sizeof(*calld));
Craig Tiller33825112015-09-18 07:44:19 -0700246 grpc_closure_init(&calld->hs_on_recv, hs_on_recv, elem);
Craig Tiller50d9db52015-04-23 10:52:14 -0700247 if (initial_op) hs_mutate_op(elem, initial_op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800248}
249
250/* Destructor for call_data */
Craig Tillerd1bec032015-09-18 17:29:00 -0700251static void destroy_call_elem(grpc_call_element *elem,
252 grpc_call_list *call_list) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800253
254/* Constructor for channel_data */
Craig Tiller079a11b2015-06-30 10:07:15 -0700255static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256 const grpc_channel_args *args, grpc_mdctx *mdctx,
Craig Tillerd1bec032015-09-18 17:29:00 -0700257 int is_first, int is_last,
258 grpc_call_list *call_list) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800259 /* grab pointers to our data from the channel element */
260 channel_data *channeld = elem->channel_data;
261
262 /* The first and the last filters tend to be implemented differently to
263 handle the case that there's no 'next' filter to call on the up or down
264 path */
265 GPR_ASSERT(!is_first);
266 GPR_ASSERT(!is_last);
267
268 /* initialize members */
269 channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers");
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800270 channeld->status_ok = grpc_mdelem_from_strings(mdctx, ":status", "200");
271 channeld->status_not_found =
272 grpc_mdelem_from_strings(mdctx, ":status", "404");
Craig Tillerc923cd72015-01-19 16:14:31 -0800273 channeld->method_post = grpc_mdelem_from_strings(mdctx, ":method", "POST");
ctillerd74729d2015-01-12 13:31:36 -0800274 channeld->http_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "http");
275 channeld->https_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "https");
276 channeld->grpc_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "grpc");
Craig Tiller6999c092015-07-22 08:14:12 -0700277 channeld->path_key = grpc_mdstr_from_string(mdctx, ":path", 0);
278 channeld->authority_key = grpc_mdstr_from_string(mdctx, ":authority", 0);
279 channeld->host_key = grpc_mdstr_from_string(mdctx, "host", 0);
ctillerd74729d2015-01-12 13:31:36 -0800280 channeld->content_type =
281 grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc");
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800282
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900283 channeld->mdctx = mdctx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284}
285
286/* Destructor for channel data */
Craig Tillerd1bec032015-09-18 17:29:00 -0700287static void destroy_channel_elem(grpc_channel_element *elem,
288 grpc_call_list *call_list) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800289 /* grab pointers to our data from the channel element */
290 channel_data *channeld = elem->channel_data;
291
Craig Tiller1a65a232015-07-06 10:22:32 -0700292 GRPC_MDELEM_UNREF(channeld->te_trailers);
293 GRPC_MDELEM_UNREF(channeld->status_ok);
294 GRPC_MDELEM_UNREF(channeld->status_not_found);
295 GRPC_MDELEM_UNREF(channeld->method_post);
296 GRPC_MDELEM_UNREF(channeld->http_scheme);
297 GRPC_MDELEM_UNREF(channeld->https_scheme);
298 GRPC_MDELEM_UNREF(channeld->grpc_scheme);
299 GRPC_MDELEM_UNREF(channeld->content_type);
300 GRPC_MDSTR_UNREF(channeld->path_key);
301 GRPC_MDSTR_UNREF(channeld->authority_key);
302 GRPC_MDSTR_UNREF(channeld->host_key);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303}
304
305const grpc_channel_filter grpc_http_server_filter = {
Craig Tillere039f032015-06-25 12:54:23 -0700306 hs_start_transport_op, grpc_channel_next_op, sizeof(call_data),
307 init_call_elem, destroy_call_elem, sizeof(channel_data),
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700308 init_channel_elem, destroy_channel_elem, grpc_call_next_get_peer,
309 "http-server"};