Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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" |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
Craig Tiller | c50e398 | 2015-01-20 16:30:54 -0800 | [diff] [blame] | 37 | #include <grpc/grpc_http.h> |
| 38 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
| 40 | |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 41 | typedef struct call_data { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 42 | gpr_uint8 got_initial_metadata; |
| 43 | gpr_uint8 seen_path; |
| 44 | gpr_uint8 seen_post; |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 45 | gpr_uint8 sent_status; |
| 46 | gpr_uint8 seen_scheme; |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 47 | gpr_uint8 seen_te_trailers; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 48 | grpc_linked_mdelem status; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 49 | |
| 50 | grpc_stream_op_buffer *recv_ops; |
| 51 | void (*on_done_recv)(void *user_data, int success); |
| 52 | void *recv_user_data; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 53 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | |
klempner | 1e27328 | 2014-12-10 13:55:41 -0800 | [diff] [blame] | 55 | typedef struct channel_data { |
| 56 | grpc_mdelem *te_trailers; |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 57 | grpc_mdelem *method_post; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 58 | grpc_mdelem *http_scheme; |
| 59 | grpc_mdelem *https_scheme; |
| 60 | /* TODO(klempner): Remove this once we stop using it */ |
| 61 | grpc_mdelem *grpc_scheme; |
| 62 | grpc_mdelem *content_type; |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 63 | grpc_mdelem *status_ok; |
| 64 | grpc_mdelem *status_not_found; |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 65 | grpc_mdstr *path_key; |
Tatsuhiro Tsujikawa | 229000f | 2015-03-13 23:52:56 +0900 | [diff] [blame] | 66 | grpc_mdstr *authority_key; |
| 67 | grpc_mdstr *host_key; |
| 68 | |
| 69 | grpc_mdctx *mdctx; |
klempner | 1e27328 | 2014-12-10 13:55:41 -0800 | [diff] [blame] | 70 | } channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | |
| 72 | /* used to silence 'variable not used' warnings */ |
| 73 | static void ignore_unused(void *ignored) {} |
| 74 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 75 | static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) { |
| 76 | grpc_call_element *elem = user_data; |
Craig Tiller | ce6e350 | 2015-01-19 18:04:42 -0800 | [diff] [blame] | 77 | channel_data *channeld = elem->channel_data; |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 78 | call_data *calld = elem->call_data; |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 79 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 80 | /* Check if it is one of the headers we care about. */ |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 81 | if (md == channeld->te_trailers || md == channeld->method_post || |
| 82 | md == channeld->http_scheme || md == channeld->https_scheme || |
| 83 | md == channeld->grpc_scheme || md == channeld->content_type) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 84 | /* swallow it */ |
| 85 | if (md == channeld->method_post) { |
| 86 | calld->seen_post = 1; |
| 87 | } else if (md->key == channeld->http_scheme->key) { |
| 88 | calld->seen_scheme = 1; |
| 89 | } else if (md == channeld->te_trailers) { |
| 90 | calld->seen_te_trailers = 1; |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 91 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 92 | /* TODO(klempner): Track that we've seen all the headers we should |
| 93 | require */ |
| 94 | return NULL; |
| 95 | } else if (md->key == channeld->content_type->key) { |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 96 | if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) == |
| 97 | 0) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 98 | /* Although the C implementation doesn't (currently) generate them, |
Craig Tiller | 9c9d4e0 | 2015-04-20 09:03:29 -0700 | [diff] [blame] | 99 | any custom +-suffix is explicitly valid. */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 100 | /* TODO(klempner): We should consider preallocating common values such |
| 101 | as +proto or +json, or at least stashing them if we see them. */ |
| 102 | /* TODO(klempner): Should we be surfacing this to application code? */ |
| 103 | } else { |
| 104 | /* TODO(klempner): We're currently allowing this, but we shouldn't |
| 105 | see it without a proxy so log for now. */ |
| 106 | gpr_log(GPR_INFO, "Unexpected content-type %s", |
| 107 | channeld->content_type->key); |
| 108 | } |
| 109 | return NULL; |
| 110 | } else if (md->key == channeld->te_trailers->key || |
| 111 | md->key == channeld->method_post->key || |
| 112 | md->key == channeld->http_scheme->key || |
| 113 | md->key == channeld->content_type->key) { |
| 114 | gpr_log(GPR_ERROR, "Invalid %s: header: '%s'", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 115 | grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value)); |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 116 | /* swallow it and error everything out. */ |
| 117 | /* TODO(klempner): We ought to generate more descriptive error messages |
| 118 | on the wire here. */ |
| 119 | grpc_call_element_send_cancel(elem); |
| 120 | return NULL; |
| 121 | } else if (md->key == channeld->path_key) { |
| 122 | if (calld->seen_path) { |
| 123 | gpr_log(GPR_ERROR, "Received :path twice"); |
| 124 | return NULL; |
| 125 | } |
| 126 | calld->seen_path = 1; |
| 127 | return md; |
| 128 | } else if (md->key == channeld->host_key) { |
| 129 | /* translate host to :authority since :authority may be |
| 130 | omitted */ |
| 131 | grpc_mdelem *authority = grpc_mdelem_from_metadata_strings( |
| 132 | channeld->mdctx, grpc_mdstr_ref(channeld->authority_key), |
| 133 | grpc_mdstr_ref(md->value)); |
| 134 | grpc_mdelem_unref(md); |
| 135 | return authority; |
| 136 | } else { |
| 137 | return md; |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 138 | } |
Craig Tiller | ce6e350 | 2015-01-19 18:04:42 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 141 | static void hs_on_recv(void *user_data, int success) { |
| 142 | grpc_call_element *elem = user_data; |
| 143 | call_data *calld = elem->call_data; |
| 144 | if (success) { |
| 145 | size_t i; |
| 146 | size_t nops = calld->recv_ops->nops; |
| 147 | grpc_stream_op *ops = calld->recv_ops->ops; |
| 148 | for (i = 0; i < nops; i++) { |
| 149 | grpc_stream_op *op = &ops[i]; |
| 150 | if (op->type != GRPC_OP_METADATA) continue; |
| 151 | calld->got_initial_metadata = 1; |
| 152 | grpc_metadata_batch_filter(&op->data.metadata, server_filter, elem); |
| 153 | /* Have we seen the required http2 transport headers? |
| 154 | (:method, :scheme, content-type, with :path and :authority covered |
| 155 | at the channel level right now) */ |
| 156 | if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers && |
| 157 | calld->seen_path) { |
| 158 | /* do nothing */ |
| 159 | } else { |
Craig Tiller | 77979b0 | 2015-04-22 07:56:09 -0700 | [diff] [blame] | 160 | if (!calld->seen_path) { |
| 161 | gpr_log(GPR_ERROR, "Missing :path header"); |
| 162 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 163 | if (!calld->seen_post) { |
| 164 | gpr_log(GPR_ERROR, "Missing :method header"); |
| 165 | } |
| 166 | if (!calld->seen_scheme) { |
| 167 | gpr_log(GPR_ERROR, "Missing :scheme header"); |
| 168 | } |
| 169 | if (!calld->seen_te_trailers) { |
| 170 | gpr_log(GPR_ERROR, "Missing te trailers header"); |
| 171 | } |
| 172 | /* Error this call out */ |
| 173 | success = 0; |
| 174 | grpc_call_element_send_cancel(elem); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | calld->on_done_recv(calld->recv_user_data, success); |
| 179 | } |
| 180 | |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 181 | static void hs_mutate_op(grpc_call_element *elem, grpc_transport_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | /* grab pointers to our data from the call element */ |
| 183 | call_data *calld = elem->call_data; |
| 184 | channel_data *channeld = elem->channel_data; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 185 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 187 | if (op->send_ops && !calld->sent_status) { |
| 188 | size_t nops = op->send_ops->nops; |
| 189 | grpc_stream_op *ops = op->send_ops->ops; |
| 190 | for (i = 0; i < nops; i++) { |
| 191 | grpc_stream_op *op = &ops[i]; |
| 192 | if (op->type != GRPC_OP_METADATA) continue; |
| 193 | calld->sent_status = 1; |
| 194 | grpc_metadata_batch_add_head(&op->data.metadata, &calld->status, |
| 195 | grpc_mdelem_ref(channeld->status_ok)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | break; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 197 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 199 | |
| 200 | if (op->recv_ops && !calld->got_initial_metadata) { |
| 201 | /* substitute our callback for the higher callback */ |
| 202 | calld->recv_ops = op->recv_ops; |
| 203 | calld->on_done_recv = op->on_done_recv; |
| 204 | calld->recv_user_data = op->recv_user_data; |
| 205 | op->on_done_recv = hs_on_recv; |
| 206 | op->recv_user_data = elem; |
| 207 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 208 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 209 | |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 210 | static void hs_start_transport_op(grpc_call_element *elem, |
| 211 | grpc_transport_op *op) { |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 212 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 213 | hs_mutate_op(elem, op); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 214 | grpc_call_next_op(elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* Called on special channel events, such as disconnection or new incoming |
| 218 | calls on the server */ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 219 | static void channel_op(grpc_channel_element *elem, |
| 220 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 221 | /* grab pointers to our data from the channel element */ |
| 222 | channel_data *channeld = elem->channel_data; |
| 223 | |
| 224 | ignore_unused(channeld); |
| 225 | |
| 226 | switch (op->type) { |
| 227 | default: |
| 228 | /* pass control up or down the stack depending on op->dir */ |
| 229 | grpc_channel_next_op(elem, op); |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* Constructor for call_data */ |
| 235 | static void init_call_elem(grpc_call_element *elem, |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 236 | const void *server_transport_data, |
| 237 | grpc_transport_op *initial_op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 238 | /* grab pointers to our data from the call element */ |
| 239 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 240 | /* initialize members */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 241 | memset(calld, 0, sizeof(*calld)); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 242 | if (initial_op) hs_mutate_op(elem, initial_op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* Destructor for call_data */ |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 246 | static void destroy_call_elem(grpc_call_element *elem) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 247 | |
| 248 | /* Constructor for channel_data */ |
| 249 | static void init_channel_elem(grpc_channel_element *elem, |
| 250 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 251 | int is_first, int is_last) { |
| 252 | /* grab pointers to our data from the channel element */ |
| 253 | channel_data *channeld = elem->channel_data; |
| 254 | |
| 255 | /* The first and the last filters tend to be implemented differently to |
| 256 | handle the case that there's no 'next' filter to call on the up or down |
| 257 | path */ |
| 258 | GPR_ASSERT(!is_first); |
| 259 | GPR_ASSERT(!is_last); |
| 260 | |
| 261 | /* initialize members */ |
| 262 | channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers"); |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 263 | channeld->status_ok = grpc_mdelem_from_strings(mdctx, ":status", "200"); |
| 264 | channeld->status_not_found = |
| 265 | grpc_mdelem_from_strings(mdctx, ":status", "404"); |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 266 | channeld->method_post = grpc_mdelem_from_strings(mdctx, ":method", "POST"); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 267 | channeld->http_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "http"); |
| 268 | channeld->https_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "https"); |
| 269 | channeld->grpc_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "grpc"); |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 270 | channeld->path_key = grpc_mdstr_from_string(mdctx, ":path"); |
Tatsuhiro Tsujikawa | 229000f | 2015-03-13 23:52:56 +0900 | [diff] [blame] | 271 | channeld->authority_key = grpc_mdstr_from_string(mdctx, ":authority"); |
| 272 | channeld->host_key = grpc_mdstr_from_string(mdctx, "host"); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 273 | channeld->content_type = |
| 274 | grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc"); |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 275 | |
Tatsuhiro Tsujikawa | 229000f | 2015-03-13 23:52:56 +0900 | [diff] [blame] | 276 | channeld->mdctx = mdctx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Destructor for channel data */ |
| 280 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 281 | /* grab pointers to our data from the channel element */ |
| 282 | channel_data *channeld = elem->channel_data; |
| 283 | |
| 284 | grpc_mdelem_unref(channeld->te_trailers); |
Craig Tiller | fd7d3ec | 2015-01-21 13:37:09 -0800 | [diff] [blame] | 285 | grpc_mdelem_unref(channeld->status_ok); |
| 286 | grpc_mdelem_unref(channeld->status_not_found); |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 287 | grpc_mdelem_unref(channeld->method_post); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 288 | grpc_mdelem_unref(channeld->http_scheme); |
| 289 | grpc_mdelem_unref(channeld->https_scheme); |
| 290 | grpc_mdelem_unref(channeld->grpc_scheme); |
| 291 | grpc_mdelem_unref(channeld->content_type); |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 292 | grpc_mdstr_unref(channeld->path_key); |
Tatsuhiro Tsujikawa | 229000f | 2015-03-13 23:52:56 +0900 | [diff] [blame] | 293 | grpc_mdstr_unref(channeld->authority_key); |
| 294 | grpc_mdstr_unref(channeld->host_key); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | const grpc_channel_filter grpc_http_server_filter = { |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 298 | hs_start_transport_op, channel_op, sizeof(call_data), init_call_elem, |
| 299 | destroy_call_elem, sizeof(channel_data), init_channel_elem, |
| 300 | destroy_channel_elem, "http-server"}; |