blob: c1645c2ba0777343348e1500230ec0209f9d9dc9 [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;
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 GRPC_MDELEM_UNREF(md);
128 calld->seen_authority = 1;
129 return authority;
130 } else {
131 return md;
132 }
Craig Tillerce6e3502015-01-19 18:04:42 -0800133}
134
Craig Tillera82950e2015-09-22 12:33:20 -0700135static void hs_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700136 grpc_call_element *elem = user_data;
137 call_data *calld = elem->call_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700138 if (success) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800139 server_filter_args a;
140 a.elem = elem;
141 a.exec_ctx = exec_ctx;
142 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, &a);
143 /* Have we seen the required http2 transport headers?
144 (:method, :scheme, content-type, with :path and :authority covered
145 at the channel level right now) */
146 if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
147 calld->seen_path && calld->seen_authority) {
148 /* do nothing */
149 } else {
150 if (!calld->seen_path) {
151 gpr_log(GPR_ERROR, "Missing :path header");
Craig Tillera82950e2015-09-22 12:33:20 -0700152 }
Craig Tiller577c9b22015-11-02 14:11:15 -0800153 if (!calld->seen_authority) {
154 gpr_log(GPR_ERROR, "Missing :authority header");
155 }
156 if (!calld->seen_post) {
157 gpr_log(GPR_ERROR, "Missing :method header");
158 }
159 if (!calld->seen_scheme) {
160 gpr_log(GPR_ERROR, "Missing :scheme header");
161 }
162 if (!calld->seen_te_trailers) {
163 gpr_log(GPR_ERROR, "Missing te trailers header");
164 }
165 /* Error this call out */
166 success = 0;
167 grpc_call_element_send_cancel(exec_ctx, elem);
Craig Tiller83f88d92015-04-21 16:02:05 -0700168 }
Craig Tillera82950e2015-09-22 12:33:20 -0700169 }
170 calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
Craig Tiller83f88d92015-04-21 16:02:05 -0700171}
172
Craig Tillera82950e2015-09-22 12:33:20 -0700173static void hs_mutate_op(grpc_call_element *elem,
174 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175 /* grab pointers to our data from the call element */
176 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177
Craig Tiller577c9b22015-11-02 14:11:15 -0800178 if (op->send_initial_metadata != NULL && !calld->sent_status) {
179 calld->sent_status = 1;
180 grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->status,
Craig Tillerebdef9d2015-11-19 17:09:49 -0800181 GRPC_MDELEM_STATUS_200);
182 grpc_metadata_batch_add_tail(
183 op->send_initial_metadata, &calld->content_type,
184 GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC);
Craig Tillera82950e2015-09-22 12:33:20 -0700185 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700186
Craig Tiller577c9b22015-11-02 14:11:15 -0800187 if (op->recv_initial_metadata) {
Craig Tillera82950e2015-09-22 12:33:20 -0700188 /* substitute our callback for the higher callback */
Craig Tiller577c9b22015-11-02 14:11:15 -0800189 calld->recv_initial_metadata = op->recv_initial_metadata;
190 calld->on_done_recv = op->on_complete;
191 op->on_complete = &calld->hs_on_recv;
Craig Tillera82950e2015-09-22 12:33:20 -0700192 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700193}
Craig Tiller83f88d92015-04-21 16:02:05 -0700194
Craig Tillera82950e2015-09-22 12:33:20 -0700195static void hs_start_transport_op(grpc_exec_ctx *exec_ctx,
196 grpc_call_element *elem,
197 grpc_transport_stream_op *op) {
198 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700199 GPR_TIMER_BEGIN("hs_start_transport_op", 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700200 hs_mutate_op(elem, op);
201 grpc_call_next_op(exec_ctx, elem, op);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700202 GPR_TIMER_END("hs_start_transport_op", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800203}
204
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205/* Constructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700206static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800207 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208 /* grab pointers to our data from the call element */
209 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210 /* initialize members */
Craig Tillera82950e2015-09-22 12:33:20 -0700211 memset(calld, 0, sizeof(*calld));
212 grpc_closure_init(&calld->hs_on_recv, hs_on_recv, elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213}
214
215/* Destructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700216static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
217 grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218
219/* Constructor for channel_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700220static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800221 grpc_channel_element *elem,
222 grpc_channel_element_args *args) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800223 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224}
225
226/* Destructor for channel data */
Craig Tillera82950e2015-09-22 12:33:20 -0700227static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
228 grpc_channel_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800229}
230
231const grpc_channel_filter grpc_http_server_filter = {
Craig Tillera82950e2015-09-22 12:33:20 -0700232 hs_start_transport_op, grpc_channel_next_op, sizeof(call_data),
Craig Tiller577c9b22015-11-02 14:11:15 -0800233 init_call_elem, grpc_call_stack_ignore_set_pollset, destroy_call_elem,
234 sizeof(channel_data), init_channel_elem, destroy_channel_elem,
235 grpc_call_next_get_peer, "http-server"};