Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 2 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include "src/core/channel/http_client_filter.h" |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 34 | #include <string.h> |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 35 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | #include <grpc/support/log.h> |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 37 | #include <grpc/support/string_util.h> |
| 38 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 40 | typedef struct call_data { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 41 | grpc_linked_mdelem method; |
| 42 | grpc_linked_mdelem scheme; |
| 43 | grpc_linked_mdelem te_trailers; |
| 44 | grpc_linked_mdelem content_type; |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 45 | grpc_linked_mdelem user_agent; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 46 | int sent_initial_metadata; |
| 47 | |
| 48 | int got_initial_metadata; |
| 49 | grpc_stream_op_buffer *recv_ops; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 50 | |
Craig Tiller | 98bf7e6 | 2015-06-24 08:47:07 -0700 | [diff] [blame] | 51 | /** Closure to call when finished with the hc_on_recv hook */ |
| 52 | grpc_iomgr_closure *on_done_recv; |
| 53 | /** Receive closures are chained: we inject this closure as the on_done_recv |
| 54 | up-call on transport_op, and remember to call our on_done_recv member |
| 55 | after handling it. */ |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 56 | grpc_iomgr_closure hc_on_recv; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 57 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 59 | typedef struct channel_data { |
| 60 | grpc_mdelem *te_trailers; |
| 61 | grpc_mdelem *method; |
| 62 | grpc_mdelem *scheme; |
| 63 | grpc_mdelem *content_type; |
Craig Tiller | 5b9efed | 2015-02-03 20:13:06 -0800 | [diff] [blame] | 64 | grpc_mdelem *status; |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 65 | /** complete user agent mdelem */ |
| 66 | grpc_mdelem *user_agent; |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 67 | } channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | |
| 69 | /* used to silence 'variable not used' warnings */ |
| 70 | static void ignore_unused(void *ignored) {} |
| 71 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 72 | static grpc_mdelem *client_filter(void *user_data, grpc_mdelem *md) { |
| 73 | grpc_call_element *elem = user_data; |
| 74 | channel_data *channeld = elem->channel_data; |
| 75 | if (md == channeld->status) { |
| 76 | return NULL; |
| 77 | } else if (md->key == channeld->status->key) { |
| 78 | grpc_call_element_send_cancel(elem); |
| 79 | return NULL; |
| 80 | } |
| 81 | return md; |
| 82 | } |
| 83 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 84 | static void hc_on_recv(void *user_data, int success) { |
| 85 | grpc_call_element *elem = user_data; |
| 86 | call_data *calld = elem->call_data; |
| 87 | if (success) { |
| 88 | size_t i; |
| 89 | size_t nops = calld->recv_ops->nops; |
| 90 | grpc_stream_op *ops = calld->recv_ops->ops; |
| 91 | for (i = 0; i < nops; i++) { |
| 92 | grpc_stream_op *op = &ops[i]; |
| 93 | if (op->type != GRPC_OP_METADATA) continue; |
| 94 | calld->got_initial_metadata = 1; |
| 95 | grpc_metadata_batch_filter(&op->data.metadata, client_filter, elem); |
| 96 | } |
| 97 | } |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 98 | calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 101 | static void hc_mutate_op(grpc_call_element *elem, |
| 102 | grpc_transport_stream_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | /* grab pointers to our data from the call element */ |
| 104 | call_data *calld = elem->call_data; |
| 105 | channel_data *channeld = elem->channel_data; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 106 | size_t i; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 107 | if (op->send_ops && !calld->sent_initial_metadata) { |
| 108 | size_t nops = op->send_ops->nops; |
| 109 | grpc_stream_op *ops = op->send_ops->ops; |
| 110 | for (i = 0; i < nops; i++) { |
| 111 | grpc_stream_op *op = &ops[i]; |
| 112 | if (op->type != GRPC_OP_METADATA) continue; |
| 113 | calld->sent_initial_metadata = 1; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 114 | /* Send : prefixed headers, which have to be before any application |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 115 | layer headers. */ |
Craig Tiller | 205aee1 | 2015-04-16 14:46:41 -0700 | [diff] [blame] | 116 | grpc_metadata_batch_add_head(&op->data.metadata, &calld->method, |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 117 | GRPC_MDELEM_REF(channeld->method)); |
Craig Tiller | 205aee1 | 2015-04-16 14:46:41 -0700 | [diff] [blame] | 118 | grpc_metadata_batch_add_head(&op->data.metadata, &calld->scheme, |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 119 | GRPC_MDELEM_REF(channeld->scheme)); |
Craig Tiller | 205aee1 | 2015-04-16 14:46:41 -0700 | [diff] [blame] | 120 | grpc_metadata_batch_add_tail(&op->data.metadata, &calld->te_trailers, |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 121 | GRPC_MDELEM_REF(channeld->te_trailers)); |
Craig Tiller | 205aee1 | 2015-04-16 14:46:41 -0700 | [diff] [blame] | 122 | grpc_metadata_batch_add_tail(&op->data.metadata, &calld->content_type, |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 123 | GRPC_MDELEM_REF(channeld->content_type)); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 124 | grpc_metadata_batch_add_tail(&op->data.metadata, &calld->user_agent, |
| 125 | GRPC_MDELEM_REF(channeld->user_agent)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | break; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 127 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 129 | |
| 130 | if (op->recv_ops && !calld->got_initial_metadata) { |
| 131 | /* substitute our callback for the higher callback */ |
| 132 | calld->recv_ops = op->recv_ops; |
| 133 | calld->on_done_recv = op->on_done_recv; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 134 | op->on_done_recv = &calld->hc_on_recv; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 135 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 136 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 137 | |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 138 | static void hc_start_transport_op(grpc_call_element *elem, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 139 | grpc_transport_stream_op *op) { |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 140 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 141 | hc_mutate_op(elem, op); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 142 | grpc_call_next_op(elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | /* Constructor for call_data */ |
| 146 | static void init_call_elem(grpc_call_element *elem, |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 147 | const void *server_transport_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 148 | grpc_transport_stream_op *initial_op) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 149 | call_data *calld = elem->call_data; |
| 150 | calld->sent_initial_metadata = 0; |
| 151 | calld->got_initial_metadata = 0; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 152 | calld->on_done_recv = NULL; |
| 153 | grpc_iomgr_closure_init(&calld->hc_on_recv, hc_on_recv, elem); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 154 | if (initial_op) hc_mutate_op(elem, initial_op); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 155 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | |
| 157 | /* Destructor for call_data */ |
| 158 | static void destroy_call_elem(grpc_call_element *elem) { |
| 159 | /* grab pointers to our data from the call element */ |
| 160 | call_data *calld = elem->call_data; |
| 161 | channel_data *channeld = elem->channel_data; |
| 162 | |
| 163 | ignore_unused(calld); |
| 164 | ignore_unused(channeld); |
| 165 | } |
| 166 | |
David Klempner | ed0cbc8 | 2015-01-14 14:46:10 -0800 | [diff] [blame] | 167 | static const char *scheme_from_args(const grpc_channel_args *args) { |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 168 | unsigned i; |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 169 | if (args != NULL) { |
| 170 | for (i = 0; i < args->num_args; ++i) { |
| 171 | if (args->args[i].type == GRPC_ARG_STRING && |
David Klempner | ed0cbc8 | 2015-01-14 14:46:10 -0800 | [diff] [blame] | 172 | strcmp(args->args[i].key, GRPC_ARG_HTTP2_SCHEME) == 0) { |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 173 | return args->args[i].value.string; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | return "http"; |
| 178 | } |
| 179 | |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 180 | static grpc_mdstr *user_agent_from_args(grpc_mdctx *mdctx, |
| 181 | const grpc_channel_args *args) { |
| 182 | gpr_strvec v; |
| 183 | size_t i; |
| 184 | int is_first = 1; |
| 185 | char *tmp; |
| 186 | grpc_mdstr *result; |
| 187 | |
| 188 | gpr_strvec_init(&v); |
| 189 | |
| 190 | for (i = 0; args && i < args->num_args; i++) { |
| 191 | if (0 == strcmp(args->args[i].key, GRPC_ARG_PRIMARY_USER_AGENT_STRING)) { |
| 192 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 193 | gpr_log(GPR_ERROR, "Channel argument '%s' should be a string", |
| 194 | GRPC_ARG_PRIMARY_USER_AGENT_STRING); |
| 195 | } else { |
| 196 | if (!is_first) gpr_strvec_add(&v, gpr_strdup(" ")); |
| 197 | is_first = 0; |
| 198 | gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string)); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | gpr_asprintf(&tmp, "%sgrpc-c/%s (%s)", is_first ? "" : " ", |
| 204 | grpc_version_string(), GPR_PLATFORM_STRING); |
| 205 | is_first = 0; |
| 206 | gpr_strvec_add(&v, tmp); |
| 207 | |
| 208 | for (i = 0; args && i < args->num_args; i++) { |
| 209 | if (0 == strcmp(args->args[i].key, GRPC_ARG_SECONDARY_USER_AGENT_STRING)) { |
| 210 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 211 | gpr_log(GPR_ERROR, "Channel argument '%s' should be a string", |
| 212 | GRPC_ARG_SECONDARY_USER_AGENT_STRING); |
| 213 | } else { |
| 214 | if (!is_first) gpr_strvec_add(&v, gpr_strdup(" ")); |
| 215 | is_first = 0; |
| 216 | gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string)); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | tmp = gpr_strvec_flatten(&v, NULL); |
| 222 | gpr_strvec_destroy(&v); |
| 223 | result = grpc_mdstr_from_string(mdctx, tmp); |
| 224 | gpr_free(tmp); |
| 225 | |
| 226 | return result; |
| 227 | } |
| 228 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 229 | /* Constructor for channel_data */ |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 230 | static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 231 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 232 | int is_first, int is_last) { |
| 233 | /* grab pointers to our data from the channel element */ |
| 234 | channel_data *channeld = elem->channel_data; |
| 235 | |
| 236 | /* The first and the last filters tend to be implemented differently to |
| 237 | handle the case that there's no 'next' filter to call on the up or down |
| 238 | path */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | GPR_ASSERT(!is_last); |
| 240 | |
| 241 | /* initialize members */ |
| 242 | channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers"); |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 243 | channeld->method = grpc_mdelem_from_strings(mdctx, ":method", "POST"); |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 244 | channeld->scheme = |
| 245 | grpc_mdelem_from_strings(mdctx, ":scheme", scheme_from_args(args)); |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 246 | channeld->content_type = |
| 247 | grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc"); |
Craig Tiller | 5b9efed | 2015-02-03 20:13:06 -0800 | [diff] [blame] | 248 | channeld->status = grpc_mdelem_from_strings(mdctx, ":status", "200"); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 249 | channeld->user_agent = grpc_mdelem_from_metadata_strings( |
| 250 | mdctx, grpc_mdstr_from_string(mdctx, "user-agent"), |
| 251 | user_agent_from_args(mdctx, args)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* Destructor for channel data */ |
| 255 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 256 | /* grab pointers to our data from the channel element */ |
| 257 | channel_data *channeld = elem->channel_data; |
| 258 | |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 259 | GRPC_MDELEM_UNREF(channeld->te_trailers); |
| 260 | GRPC_MDELEM_UNREF(channeld->method); |
| 261 | GRPC_MDELEM_UNREF(channeld->scheme); |
| 262 | GRPC_MDELEM_UNREF(channeld->content_type); |
| 263 | GRPC_MDELEM_UNREF(channeld->status); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 264 | GRPC_MDELEM_UNREF(channeld->user_agent); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | const grpc_channel_filter grpc_http_client_filter = { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 268 | hc_start_transport_op, grpc_channel_next_op, sizeof(call_data), |
| 269 | init_call_elem, destroy_call_elem, sizeof(channel_data), |
| 270 | init_channel_elem, destroy_channel_elem, "http-client"}; |