Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 33 | #include "src/core/lib/channel/http_client_filter.h" |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 34 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | #include <grpc/support/log.h> |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 36 | #include <grpc/support/string_util.h> |
yang-g | d88e1d8 | 2015-12-02 13:23:33 -0800 | [diff] [blame] | 37 | #include <string.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 38 | #include "src/core/lib/profiling/timers.h" |
| 39 | #include "src/core/lib/support/string.h" |
| 40 | #include "src/core/lib/transport/static_metadata.h" |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 41 | #include "src/core/lib/transport/transport_impl.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
David Garcia Quintas | 8ba60db | 2016-05-20 13:53:14 -0700 | [diff] [blame] | 43 | #define EXPECTED_CONTENT_TYPE "application/grpc" |
| 44 | #define EXPECTED_CONTENT_TYPE_LENGTH sizeof(EXPECTED_CONTENT_TYPE) - 1 |
| 45 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 46 | typedef struct call_data { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 47 | grpc_linked_mdelem method; |
| 48 | grpc_linked_mdelem scheme; |
Craig Tiller | 26b3714 | 2015-07-26 12:53:27 -0700 | [diff] [blame] | 49 | grpc_linked_mdelem authority; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 50 | grpc_linked_mdelem te_trailers; |
| 51 | grpc_linked_mdelem content_type; |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 52 | grpc_linked_mdelem user_agent; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 53 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 54 | grpc_metadata_batch *recv_initial_metadata; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 55 | |
Craig Tiller | 98bf7e6 | 2015-06-24 08:47:07 -0700 | [diff] [blame] | 56 | /** Closure to call when finished with the hc_on_recv hook */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 57 | grpc_closure *on_done_recv; |
Craig Tiller | 98bf7e6 | 2015-06-24 08:47:07 -0700 | [diff] [blame] | 58 | /** Receive closures are chained: we inject this closure as the on_done_recv |
| 59 | up-call on transport_op, and remember to call our on_done_recv member |
| 60 | after handling it. */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 61 | grpc_closure hc_on_recv; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 62 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | typedef struct channel_data { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 65 | grpc_mdelem *static_scheme; |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 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 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 69 | typedef struct { |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 70 | grpc_call_element *elem; |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 71 | grpc_exec_ctx *exec_ctx; |
Craig Tiller | 10ee274 | 2015-09-22 09:25:57 -0700 | [diff] [blame] | 72 | } client_recv_filter_args; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 74 | static grpc_mdelem *client_recv_filter(void *user_data, grpc_mdelem *md) { |
Craig Tiller | 10ee274 | 2015-09-22 09:25:57 -0700 | [diff] [blame] | 75 | client_recv_filter_args *a = user_data; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 76 | if (md == GRPC_MDELEM_STATUS_200) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | return NULL; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 78 | } else if (md->key == GRPC_MDSTR_STATUS) { |
Yuchen Zeng | ec066b3 | 2016-06-13 18:10:23 -0700 | [diff] [blame^] | 79 | char *message_string; |
| 80 | gpr_asprintf(&message_string, "Received http2 header with status: %s", |
| 81 | grpc_mdstr_as_c_string(md->value)); |
| 82 | gpr_slice message = gpr_slice_from_copied_string(message_string); |
| 83 | gpr_free(message_string); |
| 84 | grpc_call_element_send_cancel_with_message(a->exec_ctx, a->elem, |
| 85 | GRPC_STATUS_CANCELLED, &message); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | return NULL; |
David Garcia Quintas | 8ba60db | 2016-05-20 13:53:14 -0700 | [diff] [blame] | 87 | } else if (md == GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC) { |
| 88 | return NULL; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 89 | } else if (md->key == GRPC_MDSTR_CONTENT_TYPE) { |
David Garcia Quintas | 8ba60db | 2016-05-20 13:53:14 -0700 | [diff] [blame] | 90 | const char *value_str = grpc_mdstr_as_c_string(md->value); |
| 91 | if (strncmp(value_str, EXPECTED_CONTENT_TYPE, |
| 92 | EXPECTED_CONTENT_TYPE_LENGTH) == 0 && |
| 93 | (value_str[EXPECTED_CONTENT_TYPE_LENGTH] == '+' || |
| 94 | value_str[EXPECTED_CONTENT_TYPE_LENGTH] == ';')) { |
| 95 | /* Although the C implementation doesn't (currently) generate them, |
| 96 | any custom +-suffix is explicitly valid. */ |
| 97 | /* TODO(klempner): We should consider preallocating common values such |
| 98 | as +proto or +json, or at least stashing them if we see them. */ |
| 99 | /* TODO(klempner): Should we be surfacing this to application code? */ |
| 100 | } else { |
| 101 | /* TODO(klempner): We're currently allowing this, but we shouldn't |
| 102 | see it without a proxy so log for now. */ |
| 103 | gpr_log(GPR_INFO, "Unexpected content-type '%s'", value_str); |
| 104 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 107 | return md; |
| 108 | } |
| 109 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 110 | static void hc_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, bool success) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 111 | grpc_call_element *elem = user_data; |
| 112 | call_data *calld = elem->call_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 113 | client_recv_filter_args a; |
| 114 | a.elem = elem; |
| 115 | a.exec_ctx = exec_ctx; |
| 116 | grpc_metadata_batch_filter(calld->recv_initial_metadata, client_recv_filter, |
| 117 | &a); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | static grpc_mdelem *client_strip_filter(void *user_data, grpc_mdelem *md) { |
Craig Tiller | d9ddc77 | 2015-07-10 13:00:05 -0700 | [diff] [blame] | 122 | /* eat the things we'd like to set ourselves */ |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 123 | if (md->key == GRPC_MDSTR_METHOD) return NULL; |
| 124 | if (md->key == GRPC_MDSTR_SCHEME) return NULL; |
| 125 | if (md->key == GRPC_MDSTR_TE) return NULL; |
| 126 | if (md->key == GRPC_MDSTR_CONTENT_TYPE) return NULL; |
| 127 | if (md->key == GRPC_MDSTR_USER_AGENT) return NULL; |
Craig Tiller | d9ddc77 | 2015-07-10 13:00:05 -0700 | [diff] [blame] | 128 | return md; |
| 129 | } |
| 130 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | static void hc_mutate_op(grpc_call_element *elem, |
| 132 | grpc_transport_stream_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | /* grab pointers to our data from the call element */ |
| 134 | call_data *calld = elem->call_data; |
| 135 | channel_data *channeld = elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 136 | if (op->send_initial_metadata != NULL) { |
| 137 | grpc_metadata_batch_filter(op->send_initial_metadata, client_strip_filter, |
| 138 | elem); |
| 139 | /* Send : prefixed headers, which have to be before any application |
| 140 | layer headers. */ |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 141 | grpc_metadata_batch_add_head( |
| 142 | op->send_initial_metadata, &calld->method, |
| 143 | op->send_initial_metadata_flags & |
| 144 | GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST |
| 145 | ? GRPC_MDELEM_METHOD_PUT |
| 146 | : GRPC_MDELEM_METHOD_POST); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 147 | grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->scheme, |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 148 | channeld->static_scheme); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 149 | grpc_metadata_batch_add_tail(op->send_initial_metadata, &calld->te_trailers, |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 150 | GRPC_MDELEM_TE_TRAILERS); |
| 151 | grpc_metadata_batch_add_tail( |
| 152 | op->send_initial_metadata, &calld->content_type, |
| 153 | GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 154 | grpc_metadata_batch_add_tail(op->send_initial_metadata, &calld->user_agent, |
| 155 | GRPC_MDELEM_REF(channeld->user_agent)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 157 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 158 | if (op->recv_initial_metadata != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | /* substitute our callback for the higher callback */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 160 | calld->recv_initial_metadata = op->recv_initial_metadata; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 161 | calld->on_done_recv = op->recv_initial_metadata_ready; |
| 162 | op->recv_initial_metadata_ready = &calld->hc_on_recv; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 163 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 164 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 165 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 166 | static void hc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 167 | grpc_call_element *elem, |
| 168 | grpc_transport_stream_op *op) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 169 | GPR_TIMER_BEGIN("hc_start_transport_op", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 171 | hc_mutate_op(elem, op); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 172 | GPR_TIMER_END("hc_start_transport_op", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 173 | grpc_call_next_op(exec_ctx, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | /* Constructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 178 | grpc_call_element_args *args) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 179 | call_data *calld = elem->call_data; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 180 | calld->on_done_recv = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 181 | grpc_closure_init(&calld->hc_on_recv, hc_on_recv, elem); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 182 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 183 | |
| 184 | /* Destructor for call_data */ |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 185 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | e293b64 | 2016-05-02 10:29:51 -0700 | [diff] [blame] | 186 | const grpc_call_stats *stats, void *ignored) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 188 | static grpc_mdelem *scheme_from_args(const grpc_channel_args *args) { |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 189 | unsigned i; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 190 | size_t j; |
| 191 | grpc_mdelem *valid_schemes[] = {GRPC_MDELEM_SCHEME_HTTP, |
| 192 | GRPC_MDELEM_SCHEME_HTTPS}; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | if (args != NULL) { |
| 194 | for (i = 0; i < args->num_args; ++i) { |
| 195 | if (args->args[i].type == GRPC_ARG_STRING && |
| 196 | strcmp(args->args[i].key, GRPC_ARG_HTTP2_SCHEME) == 0) { |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 197 | for (j = 0; j < GPR_ARRAY_SIZE(valid_schemes); j++) { |
| 198 | if (0 == strcmp(grpc_mdstr_as_c_string(valid_schemes[j]->value), |
| 199 | args->args[i].value.string)) { |
| 200 | return valid_schemes[j]; |
| 201 | } |
| 202 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 203 | } |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 204 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 205 | } |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 206 | return GRPC_MDELEM_SCHEME_HTTP; |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 209 | static grpc_mdstr *user_agent_from_args(const grpc_channel_args *args, |
| 210 | const char *transport_name) { |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 211 | gpr_strvec v; |
| 212 | size_t i; |
| 213 | int is_first = 1; |
| 214 | char *tmp; |
| 215 | grpc_mdstr *result; |
| 216 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | gpr_strvec_init(&v); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 218 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | for (i = 0; args && i < args->num_args; i++) { |
| 220 | if (0 == strcmp(args->args[i].key, GRPC_ARG_PRIMARY_USER_AGENT_STRING)) { |
| 221 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 222 | gpr_log(GPR_ERROR, "Channel argument '%s' should be a string", |
| 223 | GRPC_ARG_PRIMARY_USER_AGENT_STRING); |
| 224 | } else { |
| 225 | if (!is_first) gpr_strvec_add(&v, gpr_strdup(" ")); |
| 226 | is_first = 0; |
| 227 | gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string)); |
| 228 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 229 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 230 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 231 | |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 232 | gpr_asprintf(&tmp, "%sgrpc-c/%s (%s; %s)", is_first ? "" : " ", |
| 233 | grpc_version_string(), GPR_PLATFORM_STRING, transport_name); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 234 | is_first = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | gpr_strvec_add(&v, tmp); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 236 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 237 | for (i = 0; args && i < args->num_args; i++) { |
| 238 | if (0 == strcmp(args->args[i].key, GRPC_ARG_SECONDARY_USER_AGENT_STRING)) { |
| 239 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 240 | gpr_log(GPR_ERROR, "Channel argument '%s' should be a string", |
| 241 | GRPC_ARG_SECONDARY_USER_AGENT_STRING); |
| 242 | } else { |
| 243 | if (!is_first) gpr_strvec_add(&v, gpr_strdup(" ")); |
| 244 | is_first = 0; |
| 245 | gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string)); |
| 246 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 247 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 248 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 249 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 250 | tmp = gpr_strvec_flatten(&v, NULL); |
| 251 | gpr_strvec_destroy(&v); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 252 | result = grpc_mdstr_from_string(tmp); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | gpr_free(tmp); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 254 | |
| 255 | return result; |
| 256 | } |
| 257 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 258 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 259 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 260 | grpc_channel_element *elem, |
| 261 | grpc_channel_element_args *args) { |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 262 | channel_data *chand = elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 263 | GPR_ASSERT(!args->is_last); |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 264 | GPR_ASSERT(args->optional_transport != NULL); |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 265 | chand->static_scheme = scheme_from_args(args->channel_args); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 266 | chand->user_agent = grpc_mdelem_from_metadata_strings( |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 267 | GRPC_MDSTR_USER_AGENT, |
| 268 | user_agent_from_args(args->channel_args, |
| 269 | args->optional_transport->vtable->name)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* Destructor for channel data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 273 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 274 | grpc_channel_element *elem) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 275 | channel_data *chand = elem->channel_data; |
| 276 | GRPC_MDELEM_UNREF(chand->user_agent); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | const grpc_channel_filter grpc_http_client_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 280 | hc_start_transport_op, |
| 281 | grpc_channel_next_op, |
| 282 | sizeof(call_data), |
| 283 | init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 284 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 285 | destroy_call_elem, |
| 286 | sizeof(channel_data), |
| 287 | init_channel_elem, |
| 288 | destroy_channel_elem, |
| 289 | grpc_call_next_get_peer, |
| 290 | "http-client"}; |