blob: bc3a56cbf027b17fad7ea5dda8e5a9c2b368f7a7 [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
Craig Tillerc50e3982015-01-20 16:30:54 -080036#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <grpc/support/log.h>
yang-gd88e1d82015-12-02 13:23:33 -080038#include <string.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;
ctillerd74729d2015-01-12 13:31:36 -080059} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060
Craig Tillerebdef9d2015-11-19 17:09:49 -080061typedef struct channel_data { gpr_uint8 unused; } channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
Craig Tillera82950e2015-09-22 12:33:20 -070063typedef struct {
Craig Tillerd1bec032015-09-18 17:29:00 -070064 grpc_call_element *elem;
Craig Tiller8af4c332015-09-22 12:32:31 -070065 grpc_exec_ctx *exec_ctx;
Craig Tillerd1bec032015-09-18 17:29:00 -070066} server_filter_args;
67
Craig Tillera82950e2015-09-22 12:33:20 -070068static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tillerd1bec032015-09-18 17:29:00 -070069 server_filter_args *a = user_data;
70 grpc_call_element *elem = a->elem;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080071 call_data *calld = elem->call_data;
Craig Tillerfd7d3ec2015-01-21 13:37:09 -080072
Craig Tiller6902ad22015-04-16 08:01:49 -070073 /* Check if it is one of the headers we care about. */
Craig Tillerebdef9d2015-11-19 17:09:49 -080074 if (md == GRPC_MDELEM_TE_TRAILERS || md == GRPC_MDELEM_METHOD_POST ||
75 md == GRPC_MDELEM_SCHEME_HTTP || md == GRPC_MDELEM_SCHEME_HTTPS ||
76 md == GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC) {
Craig Tillera82950e2015-09-22 12:33:20 -070077 /* swallow it */
Craig Tillerebdef9d2015-11-19 17:09:49 -080078 if (md == GRPC_MDELEM_METHOD_POST) {
Craig Tillera82950e2015-09-22 12:33:20 -070079 calld->seen_post = 1;
Craig Tillerebdef9d2015-11-19 17:09:49 -080080 } else if (md->key == GRPC_MDSTR_SCHEME) {
Craig Tillera82950e2015-09-22 12:33:20 -070081 calld->seen_scheme = 1;
Craig Tillerebdef9d2015-11-19 17:09:49 -080082 } else if (md == GRPC_MDELEM_TE_TRAILERS) {
Craig Tillera82950e2015-09-22 12:33:20 -070083 calld->seen_te_trailers = 1;
84 }
85 /* TODO(klempner): Track that we've seen all the headers we should
86 require */
87 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -080088 } else if (md->key == GRPC_MDSTR_CONTENT_TYPE) {
Craig Tillera82950e2015-09-22 12:33:20 -070089 if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) ==
90 0) {
91 /* Although the C implementation doesn't (currently) generate them,
92 any custom +-suffix is explicitly valid. */
93 /* TODO(klempner): We should consider preallocating common values such
94 as +proto or +json, or at least stashing them if we see them. */
95 /* TODO(klempner): Should we be surfacing this to application code? */
96 } else {
97 /* TODO(klempner): We're currently allowing this, but we shouldn't
98 see it without a proxy so log for now. */
99 gpr_log(GPR_INFO, "Unexpected content-type %s",
Craig Tillerebdef9d2015-11-19 17:09:49 -0800100 grpc_mdstr_as_c_string(md->value));
Craig Tillera82950e2015-09-22 12:33:20 -0700101 }
102 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800103 } else if (md->key == GRPC_MDSTR_TE || md->key == GRPC_MDSTR_METHOD ||
104 md->key == GRPC_MDSTR_SCHEME) {
Craig Tillera82950e2015-09-22 12:33:20 -0700105 gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
106 grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value));
107 /* swallow it and error everything out. */
108 /* TODO(klempner): We ought to generate more descriptive error messages
109 on the wire here. */
110 grpc_call_element_send_cancel(a->exec_ctx, elem);
111 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800112 } else if (md->key == GRPC_MDSTR_PATH) {
Craig Tillera82950e2015-09-22 12:33:20 -0700113 if (calld->seen_path) {
114 gpr_log(GPR_ERROR, "Received :path twice");
Craig Tiller6902ad22015-04-16 08:01:49 -0700115 return NULL;
116 }
Craig Tillera82950e2015-09-22 12:33:20 -0700117 calld->seen_path = 1;
118 return md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800119 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tillera82950e2015-09-22 12:33:20 -0700120 calld->seen_authority = 1;
121 return md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800122 } else if (md->key == GRPC_MDSTR_HOST) {
Craig Tillera82950e2015-09-22 12:33:20 -0700123 /* translate host to :authority since :authority may be
124 omitted */
125 grpc_mdelem *authority = grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -0800126 GRPC_MDSTR_AUTHORITY, GRPC_MDSTR_REF(md->value));
Craig Tillera82950e2015-09-22 12:33:20 -0700127 calld->seen_authority = 1;
128 return authority;
129 } else {
130 return md;
131 }
Craig Tillerce6e3502015-01-19 18:04:42 -0800132}
133
Craig Tillera82950e2015-09-22 12:33:20 -0700134static void hs_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700135 grpc_call_element *elem = user_data;
136 call_data *calld = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700137 if (success) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800138 server_filter_args a;
139 a.elem = elem;
140 a.exec_ctx = exec_ctx;
141 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, &a);
142 /* Have we seen the required http2 transport headers?
143 (:method, :scheme, content-type, with :path and :authority covered
144 at the channel level right now) */
145 if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
146 calld->seen_path && calld->seen_authority) {
147 /* do nothing */
148 } else {
149 if (!calld->seen_path) {
150 gpr_log(GPR_ERROR, "Missing :path header");
Craig Tillera82950e2015-09-22 12:33:20 -0700151 }
Craig Tiller577c9b22015-11-02 14:11:15 -0800152 if (!calld->seen_authority) {
153 gpr_log(GPR_ERROR, "Missing :authority header");
154 }
155 if (!calld->seen_post) {
156 gpr_log(GPR_ERROR, "Missing :method header");
157 }
158 if (!calld->seen_scheme) {
159 gpr_log(GPR_ERROR, "Missing :scheme header");
160 }
161 if (!calld->seen_te_trailers) {
162 gpr_log(GPR_ERROR, "Missing te trailers header");
163 }
164 /* Error this call out */
165 success = 0;
166 grpc_call_element_send_cancel(exec_ctx, elem);
Craig Tiller83f88d92015-04-21 16:02:05 -0700167 }
Craig Tillera82950e2015-09-22 12:33:20 -0700168 }
169 calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
Craig Tiller83f88d92015-04-21 16:02:05 -0700170}
171
Craig Tillera82950e2015-09-22 12:33:20 -0700172static void hs_mutate_op(grpc_call_element *elem,
173 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174 /* grab pointers to our data from the call element */
175 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176
Craig Tiller577c9b22015-11-02 14:11:15 -0800177 if (op->send_initial_metadata != NULL && !calld->sent_status) {
178 calld->sent_status = 1;
179 grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->status,
Craig Tillerebdef9d2015-11-19 17:09:49 -0800180 GRPC_MDELEM_STATUS_200);
181 grpc_metadata_batch_add_tail(
182 op->send_initial_metadata, &calld->content_type,
183 GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC);
Craig Tillera82950e2015-09-22 12:33:20 -0700184 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700185
Craig Tiller577c9b22015-11-02 14:11:15 -0800186 if (op->recv_initial_metadata) {
Craig Tillera82950e2015-09-22 12:33:20 -0700187 /* substitute our callback for the higher callback */
Craig Tiller577c9b22015-11-02 14:11:15 -0800188 calld->recv_initial_metadata = op->recv_initial_metadata;
189 calld->on_done_recv = op->on_complete;
190 op->on_complete = &calld->hs_on_recv;
Craig Tillera82950e2015-09-22 12:33:20 -0700191 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700192}
Craig Tiller83f88d92015-04-21 16:02:05 -0700193
Craig Tillera82950e2015-09-22 12:33:20 -0700194static void hs_start_transport_op(grpc_exec_ctx *exec_ctx,
195 grpc_call_element *elem,
196 grpc_transport_stream_op *op) {
197 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700198 GPR_TIMER_BEGIN("hs_start_transport_op", 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700199 hs_mutate_op(elem, op);
200 grpc_call_next_op(exec_ctx, elem, op);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700201 GPR_TIMER_END("hs_start_transport_op", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800202}
203
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204/* Constructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700205static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800206 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800207 /* grab pointers to our data from the call element */
208 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 /* initialize members */
Craig Tillera82950e2015-09-22 12:33:20 -0700210 memset(calld, 0, sizeof(*calld));
211 grpc_closure_init(&calld->hs_on_recv, hs_on_recv, elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212}
213
214/* Destructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700215static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
216 grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800217
218/* Constructor for channel_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700219static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800220 grpc_channel_element *elem,
221 grpc_channel_element_args *args) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800222 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223}
224
225/* Destructor for channel data */
Craig Tillera82950e2015-09-22 12:33:20 -0700226static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
227 grpc_channel_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800228}
229
230const grpc_channel_filter grpc_http_server_filter = {
Craig Tillera82950e2015-09-22 12:33:20 -0700231 hs_start_transport_op, grpc_channel_next_op, sizeof(call_data),
Craig Tiller577c9b22015-11-02 14:11:15 -0800232 init_call_elem, grpc_call_stack_ignore_set_pollset, destroy_call_elem,
233 sizeof(channel_data), init_channel_elem, destroy_channel_elem,
234 grpc_call_next_get_peer, "http-server"};