blob: ec7791656fae6264aeffc1602533fdccd772f505 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
Craig Tiller06059952015-02-18 08:34:56 -08002 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33#include "src/core/channel/http_client_filter.h"
David Klempnera1e86932015-01-13 18:13:59 -080034#include <string.h>
Craig Tiller0dc5e6c2015-07-10 10:07:53 -070035#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <grpc/support/log.h>
Craig Tiller0dc5e6c2015-07-10 10:07:53 -070037#include <grpc/support/string_util.h>
38#include "src/core/support/string.h"
Craig Tiller1f41b6b2015-10-09 15:07:02 -070039#include "src/core/profiling/timers.h"
Craig Tillerebdef9d2015-11-19 17:09:49 -080040#include "src/core/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
Craig Tillera82950e2015-09-22 12:33:20 -070042typedef struct call_data {
Craig Tiller6902ad22015-04-16 08:01:49 -070043 grpc_linked_mdelem method;
44 grpc_linked_mdelem scheme;
Craig Tiller26b37142015-07-26 12:53:27 -070045 grpc_linked_mdelem authority;
Craig Tiller6902ad22015-04-16 08:01:49 -070046 grpc_linked_mdelem te_trailers;
47 grpc_linked_mdelem content_type;
Craig Tiller0dc5e6c2015-07-10 10:07:53 -070048 grpc_linked_mdelem user_agent;
Craig Tiller83f88d92015-04-21 16:02:05 -070049
Craig Tiller577c9b22015-11-02 14:11:15 -080050 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller1e6facb2015-06-11 22:47:11 -070051
Craig Tiller98bf7e62015-06-24 08:47:07 -070052 /** Closure to call when finished with the hc_on_recv hook */
Craig Tiller33825112015-09-18 07:44:19 -070053 grpc_closure *on_done_recv;
Craig Tiller98bf7e62015-06-24 08:47:07 -070054 /** Receive closures are chained: we inject this closure as the on_done_recv
55 up-call on transport_op, and remember to call our on_done_recv member
56 after handling it. */
Craig Tiller33825112015-09-18 07:44:19 -070057 grpc_closure hc_on_recv;
Craig Tillerebdef9d2015-11-19 17:09:49 -080058
59 grpc_mdctx *mdctx;
Yang Gao5fd0d292015-01-26 00:19:48 -080060} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
Craig Tillerebdef9d2015-11-19 17:09:49 -080062typedef struct channel_data { grpc_mdelem *static_scheme; } channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
Craig Tillera82950e2015-09-22 12:33:20 -070064typedef struct {
Craig Tillerd1bec032015-09-18 17:29:00 -070065 grpc_call_element *elem;
Craig Tiller8af4c332015-09-22 12:32:31 -070066 grpc_exec_ctx *exec_ctx;
Craig Tiller10ee2742015-09-22 09:25:57 -070067} client_recv_filter_args;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068
Craig Tillera82950e2015-09-22 12:33:20 -070069static grpc_mdelem *client_recv_filter(void *user_data, grpc_mdelem *md) {
Craig Tiller10ee2742015-09-22 09:25:57 -070070 client_recv_filter_args *a = user_data;
Craig Tillerebdef9d2015-11-19 17:09:49 -080071 if (md == GRPC_MDELEM_STATUS_200) {
Craig Tillera82950e2015-09-22 12:33:20 -070072 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -080073 } else if (md->key == GRPC_MDSTR_STATUS) {
74 grpc_call_element_send_cancel(a->exec_ctx, a->elem);
Craig Tillera82950e2015-09-22 12:33:20 -070075 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -080076 } else if (md->key == GRPC_MDSTR_CONTENT_TYPE) {
Craig Tillera82950e2015-09-22 12:33:20 -070077 return NULL;
78 }
Craig Tiller6902ad22015-04-16 08:01:49 -070079 return md;
80}
81
Craig Tillera82950e2015-09-22 12:33:20 -070082static void hc_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
Craig Tiller83f88d92015-04-21 16:02:05 -070083 grpc_call_element *elem = user_data;
84 call_data *calld = elem->call_data;
Craig Tiller577c9b22015-11-02 14:11:15 -080085 client_recv_filter_args a;
86 a.elem = elem;
87 a.exec_ctx = exec_ctx;
88 grpc_metadata_batch_filter(calld->recv_initial_metadata, client_recv_filter,
89 &a);
Craig Tillera82950e2015-09-22 12:33:20 -070090 calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
Craig Tiller83f88d92015-04-21 16:02:05 -070091}
92
Craig Tillera82950e2015-09-22 12:33:20 -070093static grpc_mdelem *client_strip_filter(void *user_data, grpc_mdelem *md) {
Craig Tillerd9ddc772015-07-10 13:00:05 -070094 /* eat the things we'd like to set ourselves */
Craig Tillerebdef9d2015-11-19 17:09:49 -080095 if (md->key == GRPC_MDSTR_METHOD) return NULL;
96 if (md->key == GRPC_MDSTR_SCHEME) return NULL;
97 if (md->key == GRPC_MDSTR_TE) return NULL;
98 if (md->key == GRPC_MDSTR_CONTENT_TYPE) return NULL;
99 if (md->key == GRPC_MDSTR_USER_AGENT) return NULL;
Craig Tillerd9ddc772015-07-10 13:00:05 -0700100 return md;
101}
102
Craig Tillera82950e2015-09-22 12:33:20 -0700103static void hc_mutate_op(grpc_call_element *elem,
104 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105 /* grab pointers to our data from the call element */
106 call_data *calld = elem->call_data;
107 channel_data *channeld = elem->channel_data;
Craig Tiller577c9b22015-11-02 14:11:15 -0800108 if (op->send_initial_metadata != NULL) {
109 grpc_metadata_batch_filter(op->send_initial_metadata, client_strip_filter,
110 elem);
111 /* Send : prefixed headers, which have to be before any application
112 layer headers. */
113 grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->method,
Craig Tillerebdef9d2015-11-19 17:09:49 -0800114 GRPC_MDELEM_METHOD_POST);
Craig Tiller577c9b22015-11-02 14:11:15 -0800115 grpc_metadata_batch_add_head(op->send_initial_metadata, &calld->scheme,
Craig Tillerebdef9d2015-11-19 17:09:49 -0800116 channeld->static_scheme);
Craig Tiller577c9b22015-11-02 14:11:15 -0800117 grpc_metadata_batch_add_tail(op->send_initial_metadata, &calld->te_trailers,
Craig Tillerebdef9d2015-11-19 17:09:49 -0800118 GRPC_MDELEM_TE_TRAILERS);
119 grpc_metadata_batch_add_tail(
120 op->send_initial_metadata, &calld->content_type,
121 GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC);
122 grpc_metadata_batch_add_tail(
123 op->send_initial_metadata, &calld->user_agent,
124 GRPC_MDELEM_REF(grpc_mdelem_from_cache(calld->mdctx,
125 GRPC_MDELEM_CACHED_USER_AGENT)));
Craig Tillera82950e2015-09-22 12:33:20 -0700126 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700127
Craig Tiller577c9b22015-11-02 14:11:15 -0800128 if (op->recv_initial_metadata != NULL) {
Craig Tillera82950e2015-09-22 12:33:20 -0700129 /* substitute our callback for the higher callback */
Craig Tiller577c9b22015-11-02 14:11:15 -0800130 calld->recv_initial_metadata = op->recv_initial_metadata;
131 calld->on_done_recv = op->on_complete;
132 op->on_complete = &calld->hc_on_recv;
Craig Tillera82950e2015-09-22 12:33:20 -0700133 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700134}
Craig Tiller83f88d92015-04-21 16:02:05 -0700135
Craig Tillera82950e2015-09-22 12:33:20 -0700136static void hc_start_transport_op(grpc_exec_ctx *exec_ctx,
137 grpc_call_element *elem,
138 grpc_transport_stream_op *op) {
Craig Tiller0ba432d2015-10-09 16:57:11 -0700139 GPR_TIMER_BEGIN("hc_start_transport_op", 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700140 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
141 hc_mutate_op(elem, op);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700142 GPR_TIMER_END("hc_start_transport_op", 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700143 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144}
145
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146/* Constructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700147static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800148 grpc_call_element_args *args) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700149 call_data *calld = elem->call_data;
Craig Tiller1e6facb2015-06-11 22:47:11 -0700150 calld->on_done_recv = NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800151 calld->mdctx = args->metadata_context;
Craig Tillera82950e2015-09-22 12:33:20 -0700152 grpc_closure_init(&calld->hc_on_recv, hc_on_recv, elem);
Craig Tiller83f88d92015-04-21 16:02:05 -0700153}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800154
155/* Destructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700156static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
157 grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158
Craig Tillerebdef9d2015-11-19 17:09:49 -0800159static grpc_mdelem *scheme_from_args(const grpc_channel_args *args) {
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100160 unsigned i;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800161 size_t j;
162 grpc_mdelem *valid_schemes[] = {GRPC_MDELEM_SCHEME_HTTP,
163 GRPC_MDELEM_SCHEME_HTTPS};
Craig Tillera82950e2015-09-22 12:33:20 -0700164 if (args != NULL) {
165 for (i = 0; i < args->num_args; ++i) {
166 if (args->args[i].type == GRPC_ARG_STRING &&
167 strcmp(args->args[i].key, GRPC_ARG_HTTP2_SCHEME) == 0) {
Craig Tillerebdef9d2015-11-19 17:09:49 -0800168 for (j = 0; j < GPR_ARRAY_SIZE(valid_schemes); j++) {
169 if (0 == strcmp(grpc_mdstr_as_c_string(valid_schemes[j]->value),
170 args->args[i].value.string)) {
171 return valid_schemes[j];
172 }
173 }
Craig Tillera82950e2015-09-22 12:33:20 -0700174 }
David Klempnera1e86932015-01-13 18:13:59 -0800175 }
Craig Tillera82950e2015-09-22 12:33:20 -0700176 }
Craig Tillerebdef9d2015-11-19 17:09:49 -0800177 return GRPC_MDELEM_SCHEME_HTTP;
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700178}
179
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180/* Constructor for channel_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700181static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800182 grpc_channel_element *elem,
183 grpc_channel_element_args *args) {
Craig Tillerebdef9d2015-11-19 17:09:49 -0800184 channel_data *chand = elem->channel_data;
Craig Tiller577c9b22015-11-02 14:11:15 -0800185 GPR_ASSERT(!args->is_last);
Craig Tillerebdef9d2015-11-19 17:09:49 -0800186 chand->static_scheme = scheme_from_args(args->channel_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187}
188
189/* Destructor for channel data */
Craig Tillera82950e2015-09-22 12:33:20 -0700190static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
191 grpc_channel_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192}
193
194const grpc_channel_filter grpc_http_client_filter = {
Craig Tillera82950e2015-09-22 12:33:20 -0700195 hc_start_transport_op, grpc_channel_next_op, sizeof(call_data),
Craig Tiller577c9b22015-11-02 14:11:15 -0800196 init_call_elem, grpc_call_stack_ignore_set_pollset, destroy_call_elem,
197 sizeof(channel_data), init_channel_elem, destroy_channel_elem,
198 grpc_call_next_get_peer, "http-client"};