blob: c9db9470e064d60be03abe6059f9e820a8dd69f3 [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>
Craig Tiller1f41b6b2015-10-09 15:07:02 -070039#include "src/core/profiling/timers.h"
Craig Tillerebdef9d2015-11-19 17:09:49 -080040#include "src/core/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
Craig Tillera82950e2015-09-22 12:33:20 -070042typedef struct call_data {
Craig Tiller6902ad22015-04-16 08:01:49 -070043 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 Tillerc4b56b62015-07-23 17:44:11 -070048 gpr_uint8 seen_authority;
Craig Tiller6902ad22015-04-16 08:01:49 -070049 grpc_linked_mdelem status;
Craig Tillercb014172015-09-08 16:40:26 -070050 grpc_linked_mdelem content_type;
Craig Tiller83f88d92015-04-21 16:02:05 -070051
Craig Tiller577c9b22015-11-02 14:11:15 -080052 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller98bf7e62015-06-24 08:47:07 -070053 /** Closure to call when finished with the hs_on_recv hook */
Craig Tiller33825112015-09-18 07:44:19 -070054 grpc_closure *on_done_recv;
Craig Tiller98bf7e62015-06-24 08:47:07 -070055 /** Receive closures are chained: we inject this closure as the on_done_recv
56 up-call on transport_op, and remember to call our on_done_recv member
57 after handling it. */
Craig Tiller33825112015-09-18 07:44:19 -070058 grpc_closure hs_on_recv;
Craig Tillerebdef9d2015-11-19 17:09:49 -080059 grpc_mdctx *mdctx;
ctillerd74729d2015-01-12 13:31:36 -080060} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
Craig Tillerebdef9d2015-11-19 17:09:49 -080062typedef struct channel_data { gpr_uint8 unused; } channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
Craig Tillera82950e2015-09-22 12:33:20 -070064typedef struct {
Craig Tillerd1bec032015-09-18 17:29:00 -070065 grpc_call_element *elem;
Craig Tiller8af4c332015-09-22 12:32:31 -070066 grpc_exec_ctx *exec_ctx;
Craig Tillerd1bec032015-09-18 17:29:00 -070067} server_filter_args;
68
Craig Tillera82950e2015-09-22 12:33:20 -070069static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tillerd1bec032015-09-18 17:29:00 -070070 server_filter_args *a = user_data;
71 grpc_call_element *elem = a->elem;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080072 call_data *calld = elem->call_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080073
Craig Tiller6902ad22015-04-16 08:01:49 -070074 /* Check if it is one of the headers we care about. */
Craig Tillerebdef9d2015-11-19 17:09:49 -080075 if (md == GRPC_MDELEM_TE_TRAILERS || md == GRPC_MDELEM_METHOD_POST ||
76 md == GRPC_MDELEM_SCHEME_HTTP || md == GRPC_MDELEM_SCHEME_HTTPS ||
77 md == GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC) {
Craig Tillera82950e2015-09-22 12:33:20 -070078 /* swallow it */
Craig Tillerebdef9d2015-11-19 17:09:49 -080079 if (md == GRPC_MDELEM_METHOD_POST) {
Craig Tillera82950e2015-09-22 12:33:20 -070080 calld->seen_post = 1;
Craig Tillerebdef9d2015-11-19 17:09:49 -080081 } else if (md->key == GRPC_MDSTR_SCHEME) {
Craig Tillera82950e2015-09-22 12:33:20 -070082 calld->seen_scheme = 1;
Craig Tillerebdef9d2015-11-19 17:09:49 -080083 } else if (md == GRPC_MDELEM_TE_TRAILERS) {
Craig Tillera82950e2015-09-22 12:33:20 -070084 calld->seen_te_trailers = 1;
85 }
86 /* TODO(klempner): Track that we've seen all the headers we should
87 require */
88 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -080089 } else if (md->key == GRPC_MDSTR_CONTENT_TYPE) {
Craig Tillera82950e2015-09-22 12:33:20 -070090 if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) ==
91 0) {
92 /* Although the C implementation doesn't (currently) generate them,
93 any custom +-suffix is explicitly valid. */
94 /* TODO(klempner): We should consider preallocating common values such
95 as +proto or +json, or at least stashing them if we see them. */
96 /* TODO(klempner): Should we be surfacing this to application code? */
97 } else {
98 /* TODO(klempner): We're currently allowing this, but we shouldn't
99 see it without a proxy so log for now. */
100 gpr_log(GPR_INFO, "Unexpected content-type %s",
Craig Tillerebdef9d2015-11-19 17:09:49 -0800101 grpc_mdstr_as_c_string(md->value));
Craig Tillera82950e2015-09-22 12:33:20 -0700102 }
103 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800104 } else if (md->key == GRPC_MDSTR_TE || md->key == GRPC_MDSTR_METHOD ||
105 md->key == GRPC_MDSTR_SCHEME) {
Craig Tillera82950e2015-09-22 12:33:20 -0700106 gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
107 grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value));
108 /* swallow it and error everything out. */
109 /* TODO(klempner): We ought to generate more descriptive error messages
110 on the wire here. */
111 grpc_call_element_send_cancel(a->exec_ctx, elem);
112 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800113 } else if (md->key == GRPC_MDSTR_PATH) {
Craig Tillera82950e2015-09-22 12:33:20 -0700114 if (calld->seen_path) {
115 gpr_log(GPR_ERROR, "Received :path twice");
Craig Tiller6902ad22015-04-16 08:01:49 -0700116 return NULL;
117 }
Craig Tillera82950e2015-09-22 12:33:20 -0700118 calld->seen_path = 1;
119 return md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800120 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tillera82950e2015-09-22 12:33:20 -0700121 calld->seen_authority = 1;
122 return md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800123 } else if (md->key == GRPC_MDSTR_HOST) {
Craig Tillera82950e2015-09-22 12:33:20 -0700124 /* translate host to :authority since :authority may be
125 omitted */
126 grpc_mdelem *authority = grpc_mdelem_from_metadata_strings(
Craig Tillerebdef9d2015-11-19 17:09:49 -0800127 calld->mdctx, GRPC_MDSTR_AUTHORITY, GRPC_MDSTR_REF(md->value));
Craig Tillera82950e2015-09-22 12:33:20 -0700128 GRPC_MDELEM_UNREF(md);
129 calld->seen_authority = 1;
130 return authority;
131 } else {
132 return md;
133 }
Craig Tillerce6e3502015-01-19 18:04:42 -0800134}
135
Craig Tillera82950e2015-09-22 12:33:20 -0700136static void hs_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700137 grpc_call_element *elem = user_data;
138 call_data *calld = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700139 if (success) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800140 server_filter_args a;
141 a.elem = elem;
142 a.exec_ctx = exec_ctx;
143 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, &a);
144 /* Have we seen the required http2 transport headers?
145 (:method, :scheme, content-type, with :path and :authority covered
146 at the channel level right now) */
147 if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
148 calld->seen_path && calld->seen_authority) {
149 /* do nothing */
150 } else {
151 if (!calld->seen_path) {
152 gpr_log(GPR_ERROR, "Missing :path header");
Craig Tillera82950e2015-09-22 12:33:20 -0700153 }
Craig Tiller577c9b22015-11-02 14:11:15 -0800154 if (!calld->seen_authority) {
155 gpr_log(GPR_ERROR, "Missing :authority header");
156 }
157 if (!calld->seen_post) {
158 gpr_log(GPR_ERROR, "Missing :method header");
159 }
160 if (!calld->seen_scheme) {
161 gpr_log(GPR_ERROR, "Missing :scheme header");
162 }
163 if (!calld->seen_te_trailers) {
164 gpr_log(GPR_ERROR, "Missing te trailers header");
165 }
166 /* Error this call out */
167 success = 0;
168 grpc_call_element_send_cancel(exec_ctx, elem);
Craig Tiller83f88d92015-04-21 16:02:05 -0700169 }
Craig Tillera82950e2015-09-22 12:33:20 -0700170 }
171 calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
Craig Tiller83f88d92015-04-21 16:02:05 -0700172}
173
Craig Tillera82950e2015-09-22 12:33:20 -0700174static void hs_mutate_op(grpc_call_element *elem,
175 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176 /* grab pointers to our data from the call element */
177 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178
Craig Tiller577c9b22015-11-02 14:11:15 -0800179 if (op->send_initial_metadata != NULL && !calld->sent_status) {
180 calld->sent_status = 1;
181 grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->status,
Craig Tillerebdef9d2015-11-19 17:09:49 -0800182 GRPC_MDELEM_STATUS_200);
183 grpc_metadata_batch_add_tail(
184 op->send_initial_metadata, &calld->content_type,
185 GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC);
Craig Tillera82950e2015-09-22 12:33:20 -0700186 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700187
Craig Tiller577c9b22015-11-02 14:11:15 -0800188 if (op->recv_initial_metadata) {
Craig Tillera82950e2015-09-22 12:33:20 -0700189 /* substitute our callback for the higher callback */
Craig Tiller577c9b22015-11-02 14:11:15 -0800190 calld->recv_initial_metadata = op->recv_initial_metadata;
191 calld->on_done_recv = op->on_complete;
192 op->on_complete = &calld->hs_on_recv;
Craig Tillera82950e2015-09-22 12:33:20 -0700193 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700194}
Craig Tiller83f88d92015-04-21 16:02:05 -0700195
Craig Tillera82950e2015-09-22 12:33:20 -0700196static void hs_start_transport_op(grpc_exec_ctx *exec_ctx,
197 grpc_call_element *elem,
198 grpc_transport_stream_op *op) {
199 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700200 GPR_TIMER_BEGIN("hs_start_transport_op", 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700201 hs_mutate_op(elem, op);
202 grpc_call_next_op(exec_ctx, elem, op);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700203 GPR_TIMER_END("hs_start_transport_op", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204}
205
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206/* Constructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700207static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800208 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 /* grab pointers to our data from the call element */
210 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800211 /* initialize members */
Craig Tillera82950e2015-09-22 12:33:20 -0700212 memset(calld, 0, sizeof(*calld));
213 grpc_closure_init(&calld->hs_on_recv, hs_on_recv, elem);
Craig Tillerebdef9d2015-11-19 17:09:49 -0800214 calld->mdctx = args->metadata_context;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800215}
216
217/* Destructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700218static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
219 grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220
221/* Constructor for channel_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700222static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800223 grpc_channel_element *elem,
224 grpc_channel_element_args *args) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800225 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800226}
227
228/* Destructor for channel data */
Craig Tillera82950e2015-09-22 12:33:20 -0700229static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
230 grpc_channel_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231}
232
233const grpc_channel_filter grpc_http_server_filter = {
Craig Tillera82950e2015-09-22 12:33:20 -0700234 hs_start_transport_op, grpc_channel_next_op, sizeof(call_data),
Craig Tiller577c9b22015-11-02 14:11:15 -0800235 init_call_elem, grpc_call_stack_ignore_set_pollset, destroy_call_elem,
236 sizeof(channel_data), init_channel_elem, destroy_channel_elem,
237 grpc_call_next_get_peer, "http-server"};