blob: 85224f28e5b4c2d00c4f12bc533661686786698c [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
Craig Tillerc50e3982015-01-20 16:30:54 -080041typedef struct {
42 grpc_mdelem *path;
43 grpc_mdelem *content_type;
44 grpc_byte_buffer *content;
45} gettable;
46
ctillerd74729d2015-01-12 13:31:36 -080047typedef struct call_data {
Craig Tiller6902ad22015-04-16 08:01:49 -070048 gpr_uint8 got_initial_metadata;
49 gpr_uint8 seen_path;
50 gpr_uint8 seen_post;
Craig Tillerd2d0a112015-01-19 16:07:52 -080051 gpr_uint8 sent_status;
52 gpr_uint8 seen_scheme;
Craig Tillerd2d0a112015-01-19 16:07:52 -080053 gpr_uint8 seen_te_trailers;
Craig Tiller6902ad22015-04-16 08:01:49 -070054 grpc_linked_mdelem status;
ctillerd74729d2015-01-12 13:31:36 -080055} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056
klempner1e273282014-12-10 13:55:41 -080057typedef struct channel_data {
58 grpc_mdelem *te_trailers;
Craig Tillerc923cd72015-01-19 16:14:31 -080059 grpc_mdelem *method_post;
ctillerd74729d2015-01-12 13:31:36 -080060 grpc_mdelem *http_scheme;
61 grpc_mdelem *https_scheme;
62 /* TODO(klempner): Remove this once we stop using it */
63 grpc_mdelem *grpc_scheme;
64 grpc_mdelem *content_type;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080065 grpc_mdelem *status_ok;
66 grpc_mdelem *status_not_found;
Craig Tillerd2d0a112015-01-19 16:07:52 -080067 grpc_mdstr *path_key;
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +090068 grpc_mdstr *authority_key;
69 grpc_mdstr *host_key;
70
71 grpc_mdctx *mdctx;
Craig Tillerc50e3982015-01-20 16:30:54 -080072
73 size_t gettable_count;
74 gettable *gettables;
klempner1e273282014-12-10 13:55:41 -080075} channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076
77/* used to silence 'variable not used' warnings */
78static void ignore_unused(void *ignored) {}
79
Craig Tiller6902ad22015-04-16 08:01:49 -070080static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
81 grpc_call_element *elem = user_data;
Craig Tillerce6e3502015-01-19 18:04:42 -080082 channel_data *channeld = elem->channel_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080083 call_data *calld = elem->call_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080084
Craig Tiller6902ad22015-04-16 08:01:49 -070085 /* Check if it is one of the headers we care about. */
Craig Tiller87d5b192015-04-16 14:37:57 -070086 if (md == channeld->te_trailers || md == channeld->method_post ||
87 md == channeld->http_scheme || md == channeld->https_scheme ||
88 md == channeld->grpc_scheme || md == channeld->content_type) {
Craig Tiller6902ad22015-04-16 08:01:49 -070089 /* swallow it */
90 if (md == channeld->method_post) {
91 calld->seen_post = 1;
92 } else if (md->key == channeld->http_scheme->key) {
93 calld->seen_scheme = 1;
94 } else if (md == channeld->te_trailers) {
95 calld->seen_te_trailers = 1;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080096 }
Craig Tiller6902ad22015-04-16 08:01:49 -070097 /* TODO(klempner): Track that we've seen all the headers we should
98 require */
99 return NULL;
100 } else if (md->key == channeld->content_type->key) {
Craig Tiller87d5b192015-04-16 14:37:57 -0700101 if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) ==
102 0) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700103 /* Although the C implementation doesn't (currently) generate them,
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700104 any custom +-suffix is explicitly valid. */
Craig Tiller6902ad22015-04-16 08:01:49 -0700105 /* TODO(klempner): We should consider preallocating common values such
106 as +proto or +json, or at least stashing them if we see them. */
107 /* TODO(klempner): Should we be surfacing this to application code? */
108 } else {
109 /* TODO(klempner): We're currently allowing this, but we shouldn't
110 see it without a proxy so log for now. */
111 gpr_log(GPR_INFO, "Unexpected content-type %s",
112 channeld->content_type->key);
113 }
114 return NULL;
115 } else if (md->key == channeld->te_trailers->key ||
116 md->key == channeld->method_post->key ||
117 md->key == channeld->http_scheme->key ||
118 md->key == channeld->content_type->key) {
119 gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
Craig Tiller87d5b192015-04-16 14:37:57 -0700120 grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value));
Craig Tiller6902ad22015-04-16 08:01:49 -0700121 /* swallow it and error everything out. */
122 /* TODO(klempner): We ought to generate more descriptive error messages
123 on the wire here. */
124 grpc_call_element_send_cancel(elem);
125 return NULL;
126 } else if (md->key == channeld->path_key) {
127 if (calld->seen_path) {
128 gpr_log(GPR_ERROR, "Received :path twice");
129 return NULL;
130 }
131 calld->seen_path = 1;
132 return md;
133 } else if (md->key == channeld->host_key) {
134 /* translate host to :authority since :authority may be
135 omitted */
136 grpc_mdelem *authority = grpc_mdelem_from_metadata_strings(
137 channeld->mdctx, grpc_mdstr_ref(channeld->authority_key),
138 grpc_mdstr_ref(md->value));
139 grpc_mdelem_unref(md);
140 return authority;
141 } else {
142 return md;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800143 }
Craig Tillerce6e3502015-01-19 18:04:42 -0800144}
145
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146/* Called either:
147 - in response to an API call (or similar) from above, to send something
148 - a network event (or similar) from below, to receive something
149 op contains type and call direction information, in addition to the data
150 that is being sent or received. */
ctillerf962f522014-12-10 15:28:27 -0800151static void call_op(grpc_call_element *elem, grpc_call_element *from_elem,
152 grpc_call_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153 /* grab pointers to our data from the call element */
154 call_data *calld = elem->call_data;
155 channel_data *channeld = elem->channel_data;
156 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
157
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158 switch (op->type) {
159 case GRPC_RECV_METADATA:
Craig Tiller205aee12015-04-16 14:46:41 -0700160 grpc_metadata_batch_filter(&op->data.metadata, server_filter, elem);
Craig Tiller6902ad22015-04-16 08:01:49 -0700161 if (!calld->got_initial_metadata) {
162 calld->got_initial_metadata = 1;
163 /* Have we seen the required http2 transport headers?
164 (:method, :scheme, content-type, with :path and :authority covered
165 at the channel level right now) */
Craig Tiller87d5b192015-04-16 14:37:57 -0700166 if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
167 calld->seen_path) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700168 grpc_call_next_op(elem, op);
ctillerd74729d2015-01-12 13:31:36 -0800169 } else {
Craig Tiller6902ad22015-04-16 08:01:49 -0700170 if (!calld->seen_post) {
171 gpr_log(GPR_ERROR, "Missing :method header");
172 }
173 if (!calld->seen_scheme) {
174 gpr_log(GPR_ERROR, "Missing :scheme header");
175 }
176 if (!calld->seen_te_trailers) {
177 gpr_log(GPR_ERROR, "Missing te trailers header");
178 }
179 /* Error this call out */
Craig Tillereb40a532015-04-17 16:46:20 -0700180 grpc_metadata_batch_destroy(&op->data.metadata);
Craig Tiller6902ad22015-04-16 08:01:49 -0700181 op->done_cb(op->user_data, GRPC_OP_OK);
182 grpc_call_element_send_cancel(elem);
ctillerd74729d2015-01-12 13:31:36 -0800183 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184 } else {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185 grpc_call_next_op(elem, op);
186 }
187 break;
klempner1e273282014-12-10 13:55:41 -0800188 case GRPC_SEND_METADATA:
189 /* If we haven't sent status 200 yet, we need to so so because it needs to
190 come before any non : prefixed metadata. */
Craig Tiller8b282cb2015-04-17 14:57:44 -0700191 if (!calld->sent_status) {
192 calld->sent_status = 1;
193 grpc_metadata_batch_add_head(&op->data.metadata, &calld->status,
Craig Tiller76f5d462015-04-17 14:58:12 -0700194 grpc_mdelem_ref(channeld->status_ok));
Craig Tiller8b282cb2015-04-17 14:57:44 -0700195 }
klempner1e273282014-12-10 13:55:41 -0800196 grpc_call_next_op(elem, op);
197 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 default:
199 /* pass control up or down the stack depending on op->dir */
200 grpc_call_next_op(elem, op);
201 break;
202 }
203}
204
205/* Called on special channel events, such as disconnection or new incoming
206 calls on the server */
ctillerf962f522014-12-10 15:28:27 -0800207static void channel_op(grpc_channel_element *elem,
208 grpc_channel_element *from_elem, grpc_channel_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 /* grab pointers to our data from the channel element */
210 channel_data *channeld = elem->channel_data;
211
212 ignore_unused(channeld);
213
214 switch (op->type) {
215 default:
216 /* pass control up or down the stack depending on op->dir */
217 grpc_channel_next_op(elem, op);
218 break;
219 }
220}
221
222/* Constructor for call_data */
223static void init_call_elem(grpc_call_element *elem,
224 const void *server_transport_data) {
225 /* grab pointers to our data from the call element */
226 call_data *calld = elem->call_data;
227 channel_data *channeld = elem->channel_data;
228
229 ignore_unused(channeld);
230
231 /* initialize members */
Craig Tiller6902ad22015-04-16 08:01:49 -0700232 memset(calld, 0, sizeof(*calld));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800233}
234
235/* Destructor for call_data */
Craig Tiller87d5b192015-04-16 14:37:57 -0700236static void destroy_call_elem(grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800237
238/* Constructor for channel_data */
239static void init_channel_elem(grpc_channel_element *elem,
240 const grpc_channel_args *args, grpc_mdctx *mdctx,
241 int is_first, int is_last) {
Craig Tillerc50e3982015-01-20 16:30:54 -0800242 size_t i;
243 size_t gettable_capacity = 0;
244
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800245 /* grab pointers to our data from the channel element */
246 channel_data *channeld = elem->channel_data;
247
248 /* The first and the last filters tend to be implemented differently to
249 handle the case that there's no 'next' filter to call on the up or down
250 path */
251 GPR_ASSERT(!is_first);
252 GPR_ASSERT(!is_last);
253
254 /* initialize members */
255 channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers");
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800256 channeld->status_ok = grpc_mdelem_from_strings(mdctx, ":status", "200");
257 channeld->status_not_found =
258 grpc_mdelem_from_strings(mdctx, ":status", "404");
Craig Tillerc923cd72015-01-19 16:14:31 -0800259 channeld->method_post = grpc_mdelem_from_strings(mdctx, ":method", "POST");
ctillerd74729d2015-01-12 13:31:36 -0800260 channeld->http_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "http");
261 channeld->https_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "https");
262 channeld->grpc_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "grpc");
Craig Tillerd2d0a112015-01-19 16:07:52 -0800263 channeld->path_key = grpc_mdstr_from_string(mdctx, ":path");
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900264 channeld->authority_key = grpc_mdstr_from_string(mdctx, ":authority");
265 channeld->host_key = grpc_mdstr_from_string(mdctx, "host");
ctillerd74729d2015-01-12 13:31:36 -0800266 channeld->content_type =
267 grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc");
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800268
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900269 channeld->mdctx = mdctx;
270
Craig Tiller53d5f9d2015-01-21 13:03:18 -0800271 /* initialize http download support */
272 channeld->gettable_count = 0;
273 channeld->gettables = NULL;
Craig Tillerc50e3982015-01-20 16:30:54 -0800274 for (i = 0; i < args->num_args; i++) {
275 if (0 == strcmp(args->args[i].key, GRPC_ARG_SERVE_OVER_HTTP)) {
276 gettable *g;
277 gpr_slice slice;
278 grpc_http_server_page *p = args->args[i].value.pointer.p;
279 if (channeld->gettable_count == gettable_capacity) {
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800280 gettable_capacity =
281 GPR_MAX(gettable_capacity * 3 / 2, gettable_capacity + 1);
Craig Tiller5a8a8072015-01-30 09:41:25 -0800282 channeld->gettables = gpr_realloc(channeld->gettables,
283 gettable_capacity * sizeof(gettable));
Craig Tillerc50e3982015-01-20 16:30:54 -0800284 }
285 g = &channeld->gettables[channeld->gettable_count++];
286 g->path = grpc_mdelem_from_strings(mdctx, ":path", p->path);
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800287 g->content_type =
288 grpc_mdelem_from_strings(mdctx, "content-type", p->content_type);
Craig Tillerc50e3982015-01-20 16:30:54 -0800289 slice = gpr_slice_from_copied_string(p->content);
290 g->content = grpc_byte_buffer_create(&slice, 1);
Craig Tillerd3756632015-01-30 09:45:50 -0800291 gpr_slice_unref(slice);
Craig Tillerc50e3982015-01-20 16:30:54 -0800292 }
293 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800294}
295
296/* Destructor for channel data */
297static void destroy_channel_elem(grpc_channel_element *elem) {
Craig Tiller5a8a8072015-01-30 09:41:25 -0800298 size_t i;
299
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800300 /* grab pointers to our data from the channel element */
301 channel_data *channeld = elem->channel_data;
302
Craig Tiller5a8a8072015-01-30 09:41:25 -0800303 for (i = 0; i < channeld->gettable_count; i++) {
304 grpc_mdelem_unref(channeld->gettables[i].path);
305 grpc_mdelem_unref(channeld->gettables[i].content_type);
Craig Tillerd3756632015-01-30 09:45:50 -0800306 grpc_byte_buffer_destroy(channeld->gettables[i].content);
Craig Tiller5a8a8072015-01-30 09:41:25 -0800307 }
308 gpr_free(channeld->gettables);
309
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800310 grpc_mdelem_unref(channeld->te_trailers);
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800311 grpc_mdelem_unref(channeld->status_ok);
312 grpc_mdelem_unref(channeld->status_not_found);
Craig Tillerc923cd72015-01-19 16:14:31 -0800313 grpc_mdelem_unref(channeld->method_post);
ctillerd74729d2015-01-12 13:31:36 -0800314 grpc_mdelem_unref(channeld->http_scheme);
315 grpc_mdelem_unref(channeld->https_scheme);
316 grpc_mdelem_unref(channeld->grpc_scheme);
317 grpc_mdelem_unref(channeld->content_type);
Craig Tillerd2d0a112015-01-19 16:07:52 -0800318 grpc_mdstr_unref(channeld->path_key);
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900319 grpc_mdstr_unref(channeld->authority_key);
320 grpc_mdstr_unref(channeld->host_key);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321}
322
323const grpc_channel_filter grpc_http_server_filter = {
Craig Tiller5a8a8072015-01-30 09:41:25 -0800324 call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem,
325 sizeof(channel_data), init_channel_elem, destroy_channel_elem,
Craig Tiller190d3602015-02-18 09:23:38 -0800326 "http-server"};