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; |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 53 | grpc_linked_mdelem payload_bin; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 54 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 55 | grpc_metadata_batch *recv_initial_metadata; |
Makdharma | 6a523f0 | 2016-08-26 10:02:00 -0700 | [diff] [blame] | 56 | uint8_t *payload_bytes; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 57 | |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 58 | /* Vars to read data off of send_message */ |
| 59 | grpc_transport_stream_op send_op; |
| 60 | uint32_t send_length; |
| 61 | uint32_t send_flags; |
| 62 | gpr_slice incoming_slice; |
| 63 | grpc_slice_buffer_stream replacement_stream; |
| 64 | gpr_slice_buffer slices; |
| 65 | /* flag that indicates that all slices of send_messages aren't availble */ |
| 66 | bool send_message_blocked; |
| 67 | |
Craig Tiller | 98bf7e6 | 2015-06-24 08:47:07 -0700 | [diff] [blame] | 68 | /** Closure to call when finished with the hc_on_recv hook */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 69 | grpc_closure *on_done_recv; |
Makdharma | 7f0abf3 | 2016-08-26 13:34:54 -0700 | [diff] [blame] | 70 | grpc_closure *on_complete; |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 71 | grpc_closure *post_send; |
| 72 | |
Craig Tiller | 98bf7e6 | 2015-06-24 08:47:07 -0700 | [diff] [blame] | 73 | /** Receive closures are chained: we inject this closure as the on_done_recv |
| 74 | up-call on transport_op, and remember to call our on_done_recv member |
| 75 | after handling it. */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 76 | grpc_closure hc_on_recv; |
Makdharma | 7f0abf3 | 2016-08-26 13:34:54 -0700 | [diff] [blame] | 77 | grpc_closure hc_on_complete; |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 78 | grpc_closure got_slice; |
| 79 | grpc_closure send_done; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 80 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | typedef struct channel_data { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 83 | grpc_mdelem *static_scheme; |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 84 | grpc_mdelem *user_agent; |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 85 | size_t max_payload_size_for_get; |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 86 | } channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | typedef struct { |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 89 | grpc_call_element *elem; |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 90 | grpc_exec_ctx *exec_ctx; |
Craig Tiller | 10ee274 | 2015-09-22 09:25:57 -0700 | [diff] [blame] | 91 | } client_recv_filter_args; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 92 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | static grpc_mdelem *client_recv_filter(void *user_data, grpc_mdelem *md) { |
Craig Tiller | 10ee274 | 2015-09-22 09:25:57 -0700 | [diff] [blame] | 94 | client_recv_filter_args *a = user_data; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 95 | if (md == GRPC_MDELEM_STATUS_200) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | return NULL; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 97 | } else if (md->key == GRPC_MDSTR_STATUS) { |
Yuchen Zeng | ec066b3 | 2016-06-13 18:10:23 -0700 | [diff] [blame] | 98 | char *message_string; |
| 99 | gpr_asprintf(&message_string, "Received http2 header with status: %s", |
| 100 | grpc_mdstr_as_c_string(md->value)); |
| 101 | gpr_slice message = gpr_slice_from_copied_string(message_string); |
| 102 | gpr_free(message_string); |
| 103 | grpc_call_element_send_cancel_with_message(a->exec_ctx, a->elem, |
| 104 | GRPC_STATUS_CANCELLED, &message); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | return NULL; |
David Garcia Quintas | 8ba60db | 2016-05-20 13:53:14 -0700 | [diff] [blame] | 106 | } else if (md == GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC) { |
| 107 | return NULL; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 108 | } else if (md->key == GRPC_MDSTR_CONTENT_TYPE) { |
David Garcia Quintas | 8ba60db | 2016-05-20 13:53:14 -0700 | [diff] [blame] | 109 | const char *value_str = grpc_mdstr_as_c_string(md->value); |
| 110 | if (strncmp(value_str, EXPECTED_CONTENT_TYPE, |
| 111 | EXPECTED_CONTENT_TYPE_LENGTH) == 0 && |
| 112 | (value_str[EXPECTED_CONTENT_TYPE_LENGTH] == '+' || |
| 113 | value_str[EXPECTED_CONTENT_TYPE_LENGTH] == ';')) { |
| 114 | /* Although the C implementation doesn't (currently) generate them, |
| 115 | any custom +-suffix is explicitly valid. */ |
| 116 | /* TODO(klempner): We should consider preallocating common values such |
| 117 | as +proto or +json, or at least stashing them if we see them. */ |
| 118 | /* TODO(klempner): Should we be surfacing this to application code? */ |
| 119 | } else { |
| 120 | /* TODO(klempner): We're currently allowing this, but we shouldn't |
| 121 | see it without a proxy so log for now. */ |
| 122 | gpr_log(GPR_INFO, "Unexpected content-type '%s'", value_str); |
| 123 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 126 | return md; |
| 127 | } |
| 128 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 129 | static void hc_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, |
| 130 | grpc_error *error) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 131 | grpc_call_element *elem = user_data; |
| 132 | call_data *calld = elem->call_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 133 | client_recv_filter_args a; |
| 134 | a.elem = elem; |
| 135 | a.exec_ctx = exec_ctx; |
| 136 | grpc_metadata_batch_filter(calld->recv_initial_metadata, client_recv_filter, |
| 137 | &a); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 138 | calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, error); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Makdharma | 7f0abf3 | 2016-08-26 13:34:54 -0700 | [diff] [blame] | 141 | static void hc_on_complete(grpc_exec_ctx *exec_ctx, void *user_data, |
Makdharma | b305452 | 2016-08-26 13:36:26 -0700 | [diff] [blame] | 142 | grpc_error *error) { |
Makdharma | 7f0abf3 | 2016-08-26 13:34:54 -0700 | [diff] [blame] | 143 | grpc_call_element *elem = user_data; |
| 144 | call_data *calld = elem->call_data; |
| 145 | if (calld->payload_bytes) { |
| 146 | gpr_free(calld->payload_bytes); |
| 147 | calld->payload_bytes = NULL; |
| 148 | } |
| 149 | calld->on_complete->cb(exec_ctx, calld->on_complete->cb_arg, error); |
| 150 | } |
| 151 | |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 152 | static void send_done(grpc_exec_ctx *exec_ctx, void *elemp, grpc_error *error) { |
| 153 | grpc_call_element *elem = elemp; |
| 154 | call_data *calld = elem->call_data; |
| 155 | gpr_slice_buffer_reset_and_unref(&calld->slices); |
| 156 | calld->post_send->cb(exec_ctx, calld->post_send->cb_arg, error); |
| 157 | } |
| 158 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | static grpc_mdelem *client_strip_filter(void *user_data, grpc_mdelem *md) { |
Craig Tiller | d9ddc77 | 2015-07-10 13:00:05 -0700 | [diff] [blame] | 160 | /* eat the things we'd like to set ourselves */ |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 161 | if (md->key == GRPC_MDSTR_METHOD) return NULL; |
| 162 | if (md->key == GRPC_MDSTR_SCHEME) return NULL; |
| 163 | if (md->key == GRPC_MDSTR_TE) return NULL; |
| 164 | if (md->key == GRPC_MDSTR_CONTENT_TYPE) return NULL; |
| 165 | if (md->key == GRPC_MDSTR_USER_AGENT) return NULL; |
Craig Tiller | d9ddc77 | 2015-07-10 13:00:05 -0700 | [diff] [blame] | 166 | return md; |
| 167 | } |
| 168 | |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 169 | static void continue_send_message(grpc_exec_ctx *exec_ctx, |
Makarand Dharmapurikar | 432d1db | 2016-08-31 11:46:23 -0700 | [diff] [blame] | 170 | grpc_call_element *elem) { |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 171 | call_data *calld = elem->call_data; |
| 172 | uint8_t *wrptr = calld->payload_bytes; |
| 173 | while (grpc_byte_stream_next(exec_ctx, calld->send_op.send_message, |
Makarand Dharmapurikar | 432d1db | 2016-08-31 11:46:23 -0700 | [diff] [blame] | 174 | &calld->incoming_slice, ~(size_t)0, |
| 175 | &calld->got_slice)) { |
| 176 | memcpy(wrptr, GPR_SLICE_START_PTR(calld->incoming_slice), |
| 177 | GPR_SLICE_LENGTH(calld->incoming_slice)); |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 178 | wrptr += GPR_SLICE_LENGTH(calld->incoming_slice); |
| 179 | gpr_slice_buffer_add(&calld->slices, calld->incoming_slice); |
| 180 | if (calld->send_length == calld->slices.length) { |
| 181 | calld->send_message_blocked = false; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void got_slice(grpc_exec_ctx *exec_ctx, void *elemp, grpc_error *error) { |
| 188 | grpc_call_element *elem = elemp; |
| 189 | call_data *calld = elem->call_data; |
| 190 | calld->send_message_blocked = false; |
| 191 | gpr_slice_buffer_add(&calld->slices, calld->incoming_slice); |
| 192 | if (calld->send_length == calld->slices.length) { |
| 193 | /* Pass down the original send_message op that was blocked.*/ |
| 194 | grpc_slice_buffer_stream_init(&calld->replacement_stream, &calld->slices, |
Makarand Dharmapurikar | 432d1db | 2016-08-31 11:46:23 -0700 | [diff] [blame] | 195 | calld->send_flags); |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 196 | calld->send_op.send_message = &calld->replacement_stream.base; |
| 197 | calld->post_send = calld->send_op.on_complete; |
| 198 | calld->send_op.on_complete = &calld->send_done; |
| 199 | grpc_call_next_op(exec_ctx, elem, &calld->send_op); |
Makarand Dharmapurikar | 432d1db | 2016-08-31 11:46:23 -0700 | [diff] [blame] | 200 | } else { |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 201 | continue_send_message(exec_ctx, elem); |
| 202 | } |
| 203 | } |
| 204 | |
Makarand Dharmapurikar | 432d1db | 2016-08-31 11:46:23 -0700 | [diff] [blame] | 205 | static void hc_mutate_op(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | grpc_transport_stream_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 207 | /* grab pointers to our data from the call element */ |
| 208 | call_data *calld = elem->call_data; |
| 209 | channel_data *channeld = elem->channel_data; |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 210 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 211 | if (op->send_initial_metadata != NULL) { |
Makarand Dharmapurikar | b94656d | 2016-08-31 15:42:44 -0700 | [diff] [blame^] | 212 | /* Decide which HTTP VERB to use. We use GET if the request is marked |
| 213 | cacheable, and the operation contains both initial metadata and send |
| 214 | message, and the payload is below the size threshold, and all the data |
| 215 | for this request is immediately available. */ |
| 216 | grpc_mdelem *method = GRPC_MDELEM_METHOD_POST; |
| 217 | calld->send_message_blocked = false; |
| 218 | if ((op->send_initial_metadata_flags & |
| 219 | GRPC_INITIAL_METADATA_CACHEABLE_REQUEST) && |
| 220 | op->send_message != NULL && |
| 221 | op->send_message->length < channeld->max_payload_size_for_get) { |
| 222 | method = GRPC_MDELEM_METHOD_GET; |
| 223 | calld->send_message_blocked = true; |
| 224 | } else if (op->send_initial_metadata_flags & |
| 225 | GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) { |
| 226 | method = GRPC_MDELEM_METHOD_PUT; |
| 227 | } |
| 228 | |
| 229 | /* Attempt to read the data from send_message and create a header field. */ |
| 230 | if (method == GRPC_MDELEM_METHOD_GET) { |
| 231 | /* allocate memory to hold the entire payload */ |
| 232 | calld->payload_bytes = gpr_malloc(op->send_message->length); |
| 233 | GPR_ASSERT(calld->payload_bytes); |
| 234 | |
| 235 | /* read slices of send_message and copy into payload_bytes */ |
| 236 | calld->send_op = *op; |
| 237 | calld->send_length = op->send_message->length; |
| 238 | calld->send_flags = op->send_message->flags; |
| 239 | continue_send_message(exec_ctx, elem); |
| 240 | |
| 241 | if (calld->send_message_blocked == false) { |
| 242 | /* when all the send_message data is available, then create a MDELEM and |
| 243 | append to headers */ |
| 244 | grpc_mdelem *payload_bin = grpc_mdelem_from_metadata_strings( |
| 245 | GRPC_MDSTR_GRPC_PAYLOAD_BIN, |
| 246 | grpc_mdstr_from_buffer(calld->payload_bytes, |
| 247 | op->send_message->length)); |
| 248 | grpc_metadata_batch_add_tail(op->send_initial_metadata, |
| 249 | &calld->payload_bin, payload_bin); |
| 250 | calld->on_complete = op->on_complete; |
| 251 | op->on_complete = &calld->hc_on_complete; |
| 252 | op->send_message = NULL; |
| 253 | } else { |
| 254 | /* Not all data is available. Fall back to POST. */ |
| 255 | gpr_log(GPR_DEBUG, |
| 256 | "Request is marked Cacheable but not all data is available.\ |
| 257 | Falling back to POST"); |
| 258 | method = GRPC_MDELEM_METHOD_POST; |
| 259 | } |
| 260 | } |
| 261 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 262 | grpc_metadata_batch_filter(op->send_initial_metadata, client_strip_filter, |
| 263 | elem); |
| 264 | /* Send : prefixed headers, which have to be before any application |
| 265 | layer headers. */ |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 266 | grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->method, |
| 267 | method); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 268 | grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->scheme, |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 269 | channeld->static_scheme); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 270 | grpc_metadata_batch_add_tail(op->send_initial_metadata, &calld->te_trailers, |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 271 | GRPC_MDELEM_TE_TRAILERS); |
| 272 | grpc_metadata_batch_add_tail( |
| 273 | op->send_initial_metadata, &calld->content_type, |
| 274 | GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 275 | grpc_metadata_batch_add_tail(op->send_initial_metadata, &calld->user_agent, |
| 276 | GRPC_MDELEM_REF(channeld->user_agent)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 278 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 279 | if (op->recv_initial_metadata != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 280 | /* substitute our callback for the higher callback */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 281 | calld->recv_initial_metadata = op->recv_initial_metadata; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 282 | calld->on_done_recv = op->recv_initial_metadata_ready; |
| 283 | op->recv_initial_metadata_ready = &calld->hc_on_recv; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 284 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 285 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 286 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | static void hc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 288 | grpc_call_element *elem, |
| 289 | grpc_transport_stream_op *op) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 290 | GPR_TIMER_BEGIN("hc_start_transport_op", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 291 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 292 | hc_mutate_op(exec_ctx, elem, op); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 293 | GPR_TIMER_END("hc_start_transport_op", 0); |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 294 | call_data *calld = elem->call_data; |
| 295 | if (op->send_message != NULL && calld->send_message_blocked) { |
| 296 | /* Don't forward the op. send_message contains slices that aren't ready |
| 297 | yet. The call will be forwarded by the op_complete of slice read call. |
| 298 | */ |
| 299 | } else { |
| 300 | grpc_call_next_op(exec_ctx, elem, op); |
| 301 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 304 | /* Constructor for call_data */ |
Mark D. Roth | 76d2442 | 2016-06-23 13:22:10 -0700 | [diff] [blame] | 305 | static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx, |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 306 | grpc_call_element *elem, |
| 307 | grpc_call_element_args *args) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 308 | call_data *calld = elem->call_data; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 309 | calld->on_done_recv = NULL; |
Makdharma | 7f0abf3 | 2016-08-26 13:34:54 -0700 | [diff] [blame] | 310 | calld->on_complete = NULL; |
| 311 | calld->payload_bytes = NULL; |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 312 | gpr_slice_buffer_init(&calld->slices); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 313 | grpc_closure_init(&calld->hc_on_recv, hc_on_recv, elem); |
Makdharma | 7f0abf3 | 2016-08-26 13:34:54 -0700 | [diff] [blame] | 314 | grpc_closure_init(&calld->hc_on_complete, hc_on_complete, elem); |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 315 | grpc_closure_init(&calld->got_slice, got_slice, elem); |
| 316 | grpc_closure_init(&calld->send_done, send_done, elem); |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 317 | return GRPC_ERROR_NONE; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 318 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 319 | |
| 320 | /* Destructor for call_data */ |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 321 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | 5dde14c | 2016-07-28 17:29:27 -0700 | [diff] [blame] | 322 | const grpc_call_final_info *final_info, |
Makarand Dharmapurikar | a6babb0 | 2016-08-30 16:59:26 -0700 | [diff] [blame] | 323 | void *ignored) { |
| 324 | call_data *calld = elem->call_data; |
| 325 | gpr_slice_buffer_destroy(&calld->slices); |
| 326 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 327 | |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 328 | static grpc_mdelem *scheme_from_args(const grpc_channel_args *args) { |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 329 | unsigned i; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 330 | size_t j; |
| 331 | grpc_mdelem *valid_schemes[] = {GRPC_MDELEM_SCHEME_HTTP, |
| 332 | GRPC_MDELEM_SCHEME_HTTPS}; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 333 | if (args != NULL) { |
| 334 | for (i = 0; i < args->num_args; ++i) { |
| 335 | if (args->args[i].type == GRPC_ARG_STRING && |
| 336 | strcmp(args->args[i].key, GRPC_ARG_HTTP2_SCHEME) == 0) { |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 337 | for (j = 0; j < GPR_ARRAY_SIZE(valid_schemes); j++) { |
| 338 | if (0 == strcmp(grpc_mdstr_as_c_string(valid_schemes[j]->value), |
| 339 | args->args[i].value.string)) { |
| 340 | return valid_schemes[j]; |
| 341 | } |
| 342 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | } |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 344 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | } |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 346 | return GRPC_MDELEM_SCHEME_HTTP; |
David Klempner | a1e8693 | 2015-01-13 18:13:59 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 349 | static size_t max_payload_size_from_args(const grpc_channel_args *args) { |
| 350 | if (args != NULL) { |
| 351 | for (size_t i = 0; i < args->num_args; ++i) { |
| 352 | if (0 == strcmp(args->args[i].key, GRPC_ARG_MAX_PAYLOAD_SIZE_FOR_GET)) { |
| 353 | if (args->args[i].type != GRPC_ARG_INTEGER) { |
| 354 | gpr_log(GPR_ERROR, "%s: must be an integer", |
| 355 | GRPC_ARG_MAX_PAYLOAD_SIZE_FOR_GET); |
| 356 | } else { |
Makdharma | 6a523f0 | 2016-08-26 10:02:00 -0700 | [diff] [blame] | 357 | return (size_t)args->args[i].value.integer; |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | return kMaxPayloadSizeForGet; |
| 363 | } |
| 364 | |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 365 | static grpc_mdstr *user_agent_from_args(const grpc_channel_args *args, |
| 366 | const char *transport_name) { |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 367 | gpr_strvec v; |
| 368 | size_t i; |
| 369 | int is_first = 1; |
| 370 | char *tmp; |
| 371 | grpc_mdstr *result; |
| 372 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 373 | gpr_strvec_init(&v); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 374 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 375 | for (i = 0; args && i < args->num_args; i++) { |
| 376 | if (0 == strcmp(args->args[i].key, GRPC_ARG_PRIMARY_USER_AGENT_STRING)) { |
| 377 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 378 | gpr_log(GPR_ERROR, "Channel argument '%s' should be a string", |
| 379 | GRPC_ARG_PRIMARY_USER_AGENT_STRING); |
| 380 | } else { |
| 381 | if (!is_first) gpr_strvec_add(&v, gpr_strdup(" ")); |
| 382 | is_first = 0; |
| 383 | gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string)); |
| 384 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 385 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 386 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 387 | |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 388 | gpr_asprintf(&tmp, "%sgrpc-c/%s (%s; %s)", is_first ? "" : " ", |
| 389 | grpc_version_string(), GPR_PLATFORM_STRING, transport_name); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 390 | is_first = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 391 | gpr_strvec_add(&v, tmp); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 392 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | for (i = 0; args && i < args->num_args; i++) { |
| 394 | if (0 == strcmp(args->args[i].key, GRPC_ARG_SECONDARY_USER_AGENT_STRING)) { |
| 395 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 396 | gpr_log(GPR_ERROR, "Channel argument '%s' should be a string", |
| 397 | GRPC_ARG_SECONDARY_USER_AGENT_STRING); |
| 398 | } else { |
| 399 | if (!is_first) gpr_strvec_add(&v, gpr_strdup(" ")); |
| 400 | is_first = 0; |
| 401 | gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string)); |
| 402 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 403 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 404 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 405 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 406 | tmp = gpr_strvec_flatten(&v, NULL); |
| 407 | gpr_strvec_destroy(&v); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 408 | result = grpc_mdstr_from_string(tmp); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 409 | gpr_free(tmp); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 410 | |
| 411 | return result; |
| 412 | } |
| 413 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 414 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 415 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 416 | grpc_channel_element *elem, |
| 417 | grpc_channel_element_args *args) { |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 418 | channel_data *chand = elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 419 | GPR_ASSERT(!args->is_last); |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 420 | GPR_ASSERT(args->optional_transport != NULL); |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 421 | chand->static_scheme = scheme_from_args(args->channel_args); |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 422 | chand->max_payload_size_for_get = |
| 423 | max_payload_size_from_args(args->channel_args); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 424 | chand->user_agent = grpc_mdelem_from_metadata_strings( |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 425 | GRPC_MDSTR_USER_AGENT, |
| 426 | user_agent_from_args(args->channel_args, |
| 427 | args->optional_transport->vtable->name)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* Destructor for channel data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 431 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 432 | grpc_channel_element *elem) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 433 | channel_data *chand = elem->channel_data; |
| 434 | GRPC_MDELEM_UNREF(chand->user_agent); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | const grpc_channel_filter grpc_http_client_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 438 | hc_start_transport_op, |
| 439 | grpc_channel_next_op, |
| 440 | sizeof(call_data), |
| 441 | init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 442 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 443 | destroy_call_elem, |
| 444 | sizeof(channel_data), |
| 445 | init_channel_elem, |
| 446 | destroy_channel_elem, |
| 447 | grpc_call_next_get_peer, |
| 448 | "http-client"}; |