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