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