blob: d18c38d93c8e32548c96b97f6cfaa050468b0f8f [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;
Craig Tiller83f88d92015-04-21 16:02:05 -070055
56 grpc_stream_op_buffer *recv_ops;
57 void (*on_done_recv)(void *user_data, int success);
58 void *recv_user_data;
ctillerd74729d2015-01-12 13:31:36 -080059} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060
klempner1e273282014-12-10 13:55:41 -080061typedef struct channel_data {
62 grpc_mdelem *te_trailers;
Craig Tillerc923cd72015-01-19 16:14:31 -080063 grpc_mdelem *method_post;
ctillerd74729d2015-01-12 13:31:36 -080064 grpc_mdelem *http_scheme;
65 grpc_mdelem *https_scheme;
66 /* TODO(klempner): Remove this once we stop using it */
67 grpc_mdelem *grpc_scheme;
68 grpc_mdelem *content_type;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080069 grpc_mdelem *status_ok;
70 grpc_mdelem *status_not_found;
Craig Tillerd2d0a112015-01-19 16:07:52 -080071 grpc_mdstr *path_key;
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +090072 grpc_mdstr *authority_key;
73 grpc_mdstr *host_key;
74
75 grpc_mdctx *mdctx;
Craig Tillerc50e3982015-01-20 16:30:54 -080076
77 size_t gettable_count;
78 gettable *gettables;
klempner1e273282014-12-10 13:55:41 -080079} channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080
81/* used to silence 'variable not used' warnings */
82static void ignore_unused(void *ignored) {}
83
Craig Tiller6902ad22015-04-16 08:01:49 -070084static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
85 grpc_call_element *elem = user_data;
Craig Tillerce6e3502015-01-19 18:04:42 -080086 channel_data *channeld = elem->channel_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080087 call_data *calld = elem->call_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080088
Craig Tiller6902ad22015-04-16 08:01:49 -070089 /* Check if it is one of the headers we care about. */
Craig Tiller87d5b192015-04-16 14:37:57 -070090 if (md == channeld->te_trailers || md == channeld->method_post ||
91 md == channeld->http_scheme || md == channeld->https_scheme ||
92 md == channeld->grpc_scheme || md == channeld->content_type) {
Craig Tiller6902ad22015-04-16 08:01:49 -070093 /* swallow it */
94 if (md == channeld->method_post) {
95 calld->seen_post = 1;
96 } else if (md->key == channeld->http_scheme->key) {
97 calld->seen_scheme = 1;
98 } else if (md == channeld->te_trailers) {
99 calld->seen_te_trailers = 1;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800100 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700101 /* TODO(klempner): Track that we've seen all the headers we should
102 require */
103 return NULL;
104 } else if (md->key == channeld->content_type->key) {
Craig Tiller87d5b192015-04-16 14:37:57 -0700105 if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) ==
106 0) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700107 /* Although the C implementation doesn't (currently) generate them,
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700108 any custom +-suffix is explicitly valid. */
Craig Tiller6902ad22015-04-16 08:01:49 -0700109 /* TODO(klempner): We should consider preallocating common values such
110 as +proto or +json, or at least stashing them if we see them. */
111 /* TODO(klempner): Should we be surfacing this to application code? */
112 } else {
113 /* TODO(klempner): We're currently allowing this, but we shouldn't
114 see it without a proxy so log for now. */
115 gpr_log(GPR_INFO, "Unexpected content-type %s",
116 channeld->content_type->key);
117 }
118 return NULL;
119 } else if (md->key == channeld->te_trailers->key ||
120 md->key == channeld->method_post->key ||
121 md->key == channeld->http_scheme->key ||
122 md->key == channeld->content_type->key) {
123 gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
Craig Tiller87d5b192015-04-16 14:37:57 -0700124 grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value));
Craig Tiller6902ad22015-04-16 08:01:49 -0700125 /* swallow it and error everything out. */
126 /* TODO(klempner): We ought to generate more descriptive error messages
127 on the wire here. */
128 grpc_call_element_send_cancel(elem);
129 return NULL;
130 } else if (md->key == channeld->path_key) {
131 if (calld->seen_path) {
132 gpr_log(GPR_ERROR, "Received :path twice");
133 return NULL;
134 }
135 calld->seen_path = 1;
136 return md;
137 } else if (md->key == channeld->host_key) {
138 /* translate host to :authority since :authority may be
139 omitted */
140 grpc_mdelem *authority = grpc_mdelem_from_metadata_strings(
141 channeld->mdctx, grpc_mdstr_ref(channeld->authority_key),
142 grpc_mdstr_ref(md->value));
143 grpc_mdelem_unref(md);
144 return authority;
145 } else {
146 return md;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800147 }
Craig Tillerce6e3502015-01-19 18:04:42 -0800148}
149
Craig Tiller83f88d92015-04-21 16:02:05 -0700150static void hs_on_recv(void *user_data, int success) {
151 grpc_call_element *elem = user_data;
152 call_data *calld = elem->call_data;
153 if (success) {
154 size_t i;
155 size_t nops = calld->recv_ops->nops;
156 grpc_stream_op *ops = calld->recv_ops->ops;
157 for (i = 0; i < nops; i++) {
158 grpc_stream_op *op = &ops[i];
159 if (op->type != GRPC_OP_METADATA) continue;
160 calld->got_initial_metadata = 1;
161 grpc_metadata_batch_filter(&op->data.metadata, server_filter, elem);
162 /* Have we seen the required http2 transport headers?
163 (:method, :scheme, content-type, with :path and :authority covered
164 at the channel level right now) */
165 if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
166 calld->seen_path) {
167 /* do nothing */
168 } else {
169 if (!calld->seen_post) {
170 gpr_log(GPR_ERROR, "Missing :method header");
171 }
172 if (!calld->seen_scheme) {
173 gpr_log(GPR_ERROR, "Missing :scheme header");
174 }
175 if (!calld->seen_te_trailers) {
176 gpr_log(GPR_ERROR, "Missing te trailers header");
177 }
178 /* Error this call out */
179 success = 0;
180 grpc_call_element_send_cancel(elem);
181 }
182 }
183 }
184 calld->on_done_recv(calld->recv_user_data, success);
185}
186
187static void hs_start_transport_op(grpc_call_element *elem, grpc_transport_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188 /* grab pointers to our data from the call element */
189 call_data *calld = elem->call_data;
190 channel_data *channeld = elem->channel_data;
Craig Tiller83f88d92015-04-21 16:02:05 -0700191 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
193
Craig Tiller83f88d92015-04-21 16:02:05 -0700194 if (op->send_ops && !calld->sent_status) {
195 size_t nops = op->send_ops->nops;
196 grpc_stream_op *ops = op->send_ops->ops;
197 for (i = 0; i < nops; i++) {
198 grpc_stream_op *op = &ops[i];
199 if (op->type != GRPC_OP_METADATA) continue;
200 calld->sent_status = 1;
201 grpc_metadata_batch_add_head(&op->data.metadata, &calld->status,
202 grpc_mdelem_ref(channeld->status_ok));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800203 break;
Craig Tiller83f88d92015-04-21 16:02:05 -0700204 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700206
207 if (op->recv_ops && !calld->got_initial_metadata) {
208 /* substitute our callback for the higher callback */
209 calld->recv_ops = op->recv_ops;
210 calld->on_done_recv = op->on_done_recv;
211 calld->recv_user_data = op->recv_user_data;
212 op->on_done_recv = hs_on_recv;
213 op->recv_user_data = elem;
214 }
215
216 grpc_call_next_op(elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800217}
218
219/* Called on special channel events, such as disconnection or new incoming
220 calls on the server */
ctillerf962f522014-12-10 15:28:27 -0800221static void channel_op(grpc_channel_element *elem,
222 grpc_channel_element *from_elem, grpc_channel_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223 /* grab pointers to our data from the channel element */
224 channel_data *channeld = elem->channel_data;
225
226 ignore_unused(channeld);
227
228 switch (op->type) {
229 default:
230 /* pass control up or down the stack depending on op->dir */
231 grpc_channel_next_op(elem, op);
232 break;
233 }
234}
235
236/* Constructor for call_data */
237static void init_call_elem(grpc_call_element *elem,
238 const void *server_transport_data) {
239 /* grab pointers to our data from the call element */
240 call_data *calld = elem->call_data;
241 channel_data *channeld = elem->channel_data;
242
243 ignore_unused(channeld);
244
245 /* initialize members */
Craig Tiller6902ad22015-04-16 08:01:49 -0700246 memset(calld, 0, sizeof(*calld));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800247}
248
249/* Destructor for call_data */
Craig Tiller87d5b192015-04-16 14:37:57 -0700250static void destroy_call_elem(grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251
252/* Constructor for channel_data */
253static void init_channel_elem(grpc_channel_element *elem,
254 const grpc_channel_args *args, grpc_mdctx *mdctx,
255 int is_first, int is_last) {
Craig Tillerc50e3982015-01-20 16:30:54 -0800256 size_t i;
257 size_t gettable_capacity = 0;
258
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 Tillerd2d0a112015-01-19 16:07:52 -0800277 channeld->path_key = grpc_mdstr_from_string(mdctx, ":path");
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900278 channeld->authority_key = grpc_mdstr_from_string(mdctx, ":authority");
279 channeld->host_key = grpc_mdstr_from_string(mdctx, "host");
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;
284
Craig Tiller53d5f9d2015-01-21 13:03:18 -0800285 /* initialize http download support */
286 channeld->gettable_count = 0;
287 channeld->gettables = NULL;
Craig Tillerc50e3982015-01-20 16:30:54 -0800288 for (i = 0; i < args->num_args; i++) {
289 if (0 == strcmp(args->args[i].key, GRPC_ARG_SERVE_OVER_HTTP)) {
290 gettable *g;
291 gpr_slice slice;
292 grpc_http_server_page *p = args->args[i].value.pointer.p;
293 if (channeld->gettable_count == gettable_capacity) {
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800294 gettable_capacity =
295 GPR_MAX(gettable_capacity * 3 / 2, gettable_capacity + 1);
Craig Tiller5a8a8072015-01-30 09:41:25 -0800296 channeld->gettables = gpr_realloc(channeld->gettables,
297 gettable_capacity * sizeof(gettable));
Craig Tillerc50e3982015-01-20 16:30:54 -0800298 }
299 g = &channeld->gettables[channeld->gettable_count++];
300 g->path = grpc_mdelem_from_strings(mdctx, ":path", p->path);
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800301 g->content_type =
302 grpc_mdelem_from_strings(mdctx, "content-type", p->content_type);
Craig Tillerc50e3982015-01-20 16:30:54 -0800303 slice = gpr_slice_from_copied_string(p->content);
304 g->content = grpc_byte_buffer_create(&slice, 1);
Craig Tillerd3756632015-01-30 09:45:50 -0800305 gpr_slice_unref(slice);
Craig Tillerc50e3982015-01-20 16:30:54 -0800306 }
307 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800308}
309
310/* Destructor for channel data */
311static void destroy_channel_elem(grpc_channel_element *elem) {
Craig Tiller5a8a8072015-01-30 09:41:25 -0800312 size_t i;
313
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800314 /* grab pointers to our data from the channel element */
315 channel_data *channeld = elem->channel_data;
316
Craig Tiller5a8a8072015-01-30 09:41:25 -0800317 for (i = 0; i < channeld->gettable_count; i++) {
318 grpc_mdelem_unref(channeld->gettables[i].path);
319 grpc_mdelem_unref(channeld->gettables[i].content_type);
Craig Tillerd3756632015-01-30 09:45:50 -0800320 grpc_byte_buffer_destroy(channeld->gettables[i].content);
Craig Tiller5a8a8072015-01-30 09:41:25 -0800321 }
322 gpr_free(channeld->gettables);
323
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800324 grpc_mdelem_unref(channeld->te_trailers);
Craig Tillerfd7d3ec2015-01-21 13:37:09 -0800325 grpc_mdelem_unref(channeld->status_ok);
326 grpc_mdelem_unref(channeld->status_not_found);
Craig Tillerc923cd72015-01-19 16:14:31 -0800327 grpc_mdelem_unref(channeld->method_post);
ctillerd74729d2015-01-12 13:31:36 -0800328 grpc_mdelem_unref(channeld->http_scheme);
329 grpc_mdelem_unref(channeld->https_scheme);
330 grpc_mdelem_unref(channeld->grpc_scheme);
331 grpc_mdelem_unref(channeld->content_type);
Craig Tillerd2d0a112015-01-19 16:07:52 -0800332 grpc_mdstr_unref(channeld->path_key);
Tatsuhiro Tsujikawa229000f2015-03-13 23:52:56 +0900333 grpc_mdstr_unref(channeld->authority_key);
334 grpc_mdstr_unref(channeld->host_key);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335}
336
337const grpc_channel_filter grpc_http_server_filter = {
Craig Tiller83f88d92015-04-21 16:02:05 -0700338 hs_start_transport_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem,
Craig Tiller5a8a8072015-01-30 09:41:25 -0800339 sizeof(channel_data), init_channel_elem, destroy_channel_elem,
Craig Tiller190d3602015-02-18 09:23:38 -0800340 "http-server"};