Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <grpc/support/log.h> |
| 38 | |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 39 | typedef enum { |
| 40 | NOT_RECEIVED, |
| 41 | POST, |
| 42 | GET |
| 43 | } known_method_type; |
| 44 | |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 45 | typedef struct call_data { |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 46 | known_method_type seen_method; |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 47 | gpr_uint8 sent_status; |
| 48 | gpr_uint8 seen_scheme; |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 49 | gpr_uint8 seen_te_trailers; |
| 50 | grpc_mdelem *path; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 51 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
klempner | 1e27328 | 2014-12-10 13:55:41 -0800 | [diff] [blame] | 53 | typedef struct channel_data { |
| 54 | grpc_mdelem *te_trailers; |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 55 | grpc_mdelem *method_get; |
| 56 | grpc_mdelem *method_post; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 57 | grpc_mdelem *http_scheme; |
| 58 | grpc_mdelem *https_scheme; |
| 59 | /* TODO(klempner): Remove this once we stop using it */ |
| 60 | grpc_mdelem *grpc_scheme; |
| 61 | grpc_mdelem *content_type; |
| 62 | grpc_mdelem *status; |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 63 | grpc_mdstr *path_key; |
klempner | 1e27328 | 2014-12-10 13:55:41 -0800 | [diff] [blame] | 64 | } channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
| 66 | /* used to silence 'variable not used' warnings */ |
| 67 | static void ignore_unused(void *ignored) {} |
| 68 | |
Craig Tiller | ce6e350 | 2015-01-19 18:04:42 -0800 | [diff] [blame^] | 69 | /* Handle 'GET': not technically grpc, so probably a web browser hitting |
| 70 | us */ |
| 71 | static void handle_get(grpc_call_element *elem) { |
| 72 | channel_data *channeld = elem->channel_data; |
| 73 | grpc_call_element_send_metadata(elem, channeld->status); |
| 74 | grpc_call_element_send_finish(elem); |
| 75 | } |
| 76 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | /* Called either: |
| 78 | - in response to an API call (or similar) from above, to send something |
| 79 | - a network event (or similar) from below, to receive something |
| 80 | op contains type and call direction information, in addition to the data |
| 81 | that is being sent or received. */ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 82 | static void call_op(grpc_call_element *elem, grpc_call_element *from_elem, |
| 83 | grpc_call_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | /* grab pointers to our data from the call element */ |
| 85 | call_data *calld = elem->call_data; |
| 86 | channel_data *channeld = elem->channel_data; |
| 87 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 88 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | switch (op->type) { |
| 90 | case GRPC_RECV_METADATA: |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 91 | /* Check if it is one of the headers we care about. */ |
| 92 | if (op->data.metadata == channeld->te_trailers || |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 93 | op->data.metadata == channeld->method_get || |
| 94 | op->data.metadata == channeld->method_post || |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 95 | op->data.metadata == channeld->http_scheme || |
| 96 | op->data.metadata == channeld->https_scheme || |
| 97 | op->data.metadata == channeld->grpc_scheme || |
| 98 | op->data.metadata == channeld->content_type) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | /* swallow it */ |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 100 | if (op->data.metadata == channeld->method_get) { |
| 101 | calld->seen_method = GET; |
| 102 | } else if (op->data.metadata == channeld->method_post) { |
| 103 | calld->seen_method = POST; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 104 | } else if (op->data.metadata->key == channeld->http_scheme->key) { |
| 105 | calld->seen_scheme = 1; |
| 106 | } else if (op->data.metadata == channeld->te_trailers) { |
| 107 | calld->seen_te_trailers = 1; |
| 108 | } |
| 109 | /* TODO(klempner): Track that we've seen all the headers we should |
| 110 | require */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | grpc_mdelem_unref(op->data.metadata); |
| 112 | op->done_cb(op->user_data, GRPC_OP_OK); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 113 | } else if (op->data.metadata->key == channeld->content_type->key) { |
| 114 | if (strncmp(grpc_mdstr_as_c_string(op->data.metadata->value), |
| 115 | "application/grpc+", 17) == 0) { |
| 116 | /* Although the C implementation doesn't (currently) generate them, |
| 117 | any |
| 118 | custom +-suffix is explicitly valid. */ |
| 119 | /* TODO(klempner): We should consider preallocating common values such |
| 120 | as +proto or +json, or at least stashing them if we see them. */ |
| 121 | /* TODO(klempner): Should we be surfacing this to application code? */ |
| 122 | } else { |
| 123 | /* TODO(klempner): We're currently allowing this, but we shouldn't |
| 124 | see it without a proxy so log for now. */ |
| 125 | gpr_log(GPR_INFO, "Unexpected content-type %s", |
| 126 | channeld->content_type->key); |
| 127 | } |
| 128 | grpc_mdelem_unref(op->data.metadata); |
| 129 | op->done_cb(op->user_data, GRPC_OP_OK); |
| 130 | } else if (op->data.metadata->key == channeld->te_trailers->key || |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 131 | op->data.metadata->key == channeld->method_post->key || |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 132 | op->data.metadata->key == channeld->http_scheme->key || |
| 133 | op->data.metadata->key == channeld->content_type->key) { |
| 134 | gpr_log(GPR_ERROR, "Invalid %s: header: '%s'", |
| 135 | grpc_mdstr_as_c_string(op->data.metadata->key), |
ctiller | 03dc30e | 2014-12-01 17:13:04 -0800 | [diff] [blame] | 136 | grpc_mdstr_as_c_string(op->data.metadata->value)); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 137 | /* swallow it and error everything out. */ |
| 138 | /* TODO(klempner): We ought to generate more descriptive error messages |
| 139 | on the wire here. */ |
ctiller | 03dc30e | 2014-12-01 17:13:04 -0800 | [diff] [blame] | 140 | grpc_mdelem_unref(op->data.metadata); |
| 141 | op->done_cb(op->user_data, GRPC_OP_OK); |
| 142 | grpc_call_element_send_cancel(elem); |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 143 | } else if (op->data.metadata->key == channeld->path_key) { |
| 144 | if (calld->path != NULL) { |
| 145 | gpr_log(GPR_ERROR, "Received :path twice"); |
| 146 | grpc_mdelem_unref(calld->path); |
| 147 | } |
Craig Tiller | 12b533d | 2015-01-19 20:31:44 -0800 | [diff] [blame] | 148 | calld->path = op->data.metadata; |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 149 | op->done_cb(op->user_data, GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | } else { |
| 151 | /* pass the event up */ |
| 152 | grpc_call_next_op(elem, op); |
| 153 | } |
| 154 | break; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 155 | case GRPC_RECV_END_OF_INITIAL_METADATA: |
| 156 | /* Have we seen the required http2 transport headers? |
| 157 | (:method, :scheme, content-type, with :path and :authority covered |
| 158 | at the channel level right now) */ |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 159 | if (calld->seen_method == POST && calld->seen_scheme && calld->seen_te_trailers && |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 160 | calld->path) { |
| 161 | grpc_call_element_recv_metadata(elem, calld->path); |
| 162 | calld->path = NULL; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 163 | grpc_call_next_op(elem, op); |
Craig Tiller | ce6e350 | 2015-01-19 18:04:42 -0800 | [diff] [blame^] | 164 | } else if (calld->seen_method == GET) { |
| 165 | handle_get(elem); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 166 | } else { |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 167 | if (calld->seen_method == NOT_RECEIVED) { |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 168 | gpr_log(GPR_ERROR, "Missing :method header"); |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 169 | } |
| 170 | if (!calld->seen_scheme) { |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 171 | gpr_log(GPR_ERROR, "Missing :scheme header"); |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 172 | } |
| 173 | if (!calld->seen_te_trailers) { |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 174 | gpr_log(GPR_ERROR, "Missing te trailers header"); |
| 175 | } |
| 176 | /* Error this call out */ |
| 177 | op->done_cb(op->user_data, GRPC_OP_OK); |
| 178 | grpc_call_element_send_cancel(elem); |
| 179 | } |
| 180 | break; |
klempner | 1e27328 | 2014-12-10 13:55:41 -0800 | [diff] [blame] | 181 | case GRPC_SEND_START: |
| 182 | case GRPC_SEND_METADATA: |
| 183 | /* If we haven't sent status 200 yet, we need to so so because it needs to |
| 184 | come before any non : prefixed metadata. */ |
| 185 | if (!calld->sent_status) { |
| 186 | calld->sent_status = 1; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 187 | /* status is reffed by grpc_call_element_send_metadata */ |
Craig Tiller | 12b533d | 2015-01-19 20:31:44 -0800 | [diff] [blame] | 188 | grpc_call_element_send_metadata(elem, grpc_mdelem_ref(channeld->status)); |
klempner | 1e27328 | 2014-12-10 13:55:41 -0800 | [diff] [blame] | 189 | } |
| 190 | grpc_call_next_op(elem, op); |
| 191 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | default: |
| 193 | /* pass control up or down the stack depending on op->dir */ |
| 194 | grpc_call_next_op(elem, op); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* Called on special channel events, such as disconnection or new incoming |
| 200 | calls on the server */ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 201 | static void channel_op(grpc_channel_element *elem, |
| 202 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 203 | /* grab pointers to our data from the channel element */ |
| 204 | channel_data *channeld = elem->channel_data; |
| 205 | |
| 206 | ignore_unused(channeld); |
| 207 | |
| 208 | switch (op->type) { |
| 209 | default: |
| 210 | /* pass control up or down the stack depending on op->dir */ |
| 211 | grpc_channel_next_op(elem, op); |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* Constructor for call_data */ |
| 217 | static void init_call_elem(grpc_call_element *elem, |
| 218 | const void *server_transport_data) { |
| 219 | /* grab pointers to our data from the call element */ |
| 220 | call_data *calld = elem->call_data; |
| 221 | channel_data *channeld = elem->channel_data; |
| 222 | |
| 223 | ignore_unused(channeld); |
| 224 | |
| 225 | /* initialize members */ |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 226 | calld->path = NULL; |
klempner | 1e27328 | 2014-12-10 13:55:41 -0800 | [diff] [blame] | 227 | calld->sent_status = 0; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 228 | calld->seen_scheme = 0; |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 229 | calld->seen_method = NOT_RECEIVED; |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 230 | calld->seen_te_trailers = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* Destructor for call_data */ |
| 234 | static void destroy_call_elem(grpc_call_element *elem) { |
| 235 | /* grab pointers to our data from the call element */ |
| 236 | call_data *calld = elem->call_data; |
| 237 | channel_data *channeld = elem->channel_data; |
| 238 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | ignore_unused(channeld); |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 240 | |
| 241 | if (calld->path) { |
| 242 | grpc_mdelem_unref(calld->path); |
| 243 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* Constructor for channel_data */ |
| 247 | static void init_channel_elem(grpc_channel_element *elem, |
| 248 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 249 | int is_first, int is_last) { |
| 250 | /* grab pointers to our data from the channel element */ |
| 251 | channel_data *channeld = elem->channel_data; |
| 252 | |
| 253 | /* The first and the last filters tend to be implemented differently to |
| 254 | handle the case that there's no 'next' filter to call on the up or down |
| 255 | path */ |
| 256 | GPR_ASSERT(!is_first); |
| 257 | GPR_ASSERT(!is_last); |
| 258 | |
| 259 | /* initialize members */ |
| 260 | channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers"); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 261 | channeld->status = grpc_mdelem_from_strings(mdctx, ":status", "200"); |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 262 | channeld->method_post = grpc_mdelem_from_strings(mdctx, ":method", "POST"); |
| 263 | channeld->method_get = grpc_mdelem_from_strings(mdctx, ":method", "GET"); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 264 | channeld->http_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "http"); |
| 265 | channeld->https_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "https"); |
| 266 | channeld->grpc_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "grpc"); |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 267 | channeld->path_key = grpc_mdstr_from_string(mdctx, ":path"); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 268 | channeld->content_type = |
| 269 | grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* Destructor for channel data */ |
| 273 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 274 | /* grab pointers to our data from the channel element */ |
| 275 | channel_data *channeld = elem->channel_data; |
| 276 | |
| 277 | grpc_mdelem_unref(channeld->te_trailers); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 278 | grpc_mdelem_unref(channeld->status); |
Craig Tiller | c923cd7 | 2015-01-19 16:14:31 -0800 | [diff] [blame] | 279 | grpc_mdelem_unref(channeld->method_post); |
| 280 | grpc_mdelem_unref(channeld->method_get); |
ctiller | d74729d | 2015-01-12 13:31:36 -0800 | [diff] [blame] | 281 | grpc_mdelem_unref(channeld->http_scheme); |
| 282 | grpc_mdelem_unref(channeld->https_scheme); |
| 283 | grpc_mdelem_unref(channeld->grpc_scheme); |
| 284 | grpc_mdelem_unref(channeld->content_type); |
Craig Tiller | d2d0a11 | 2015-01-19 16:07:52 -0800 | [diff] [blame] | 285 | grpc_mdstr_unref(channeld->path_key); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | const grpc_channel_filter grpc_http_server_filter = { |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 289 | call_op, channel_op, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 290 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 291 | sizeof(call_data), init_call_elem, destroy_call_elem, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 292 | |
| 293 | sizeof(channel_data), init_channel_elem, destroy_channel_elem, |
| 294 | |
| 295 | "http-server"}; |