blob: 9805f325a644b36fcbf0561f866277947308cd14 [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>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035#include <grpc/support/log.h>
36
Yang Gao5fd0d292015-01-26 00:19:48 -080037typedef struct call_data {
Craig Tiller6902ad22015-04-16 08:01:49 -070038 grpc_linked_mdelem method;
39 grpc_linked_mdelem scheme;
40 grpc_linked_mdelem te_trailers;
41 grpc_linked_mdelem content_type;
Craig Tiller83f88d92015-04-21 16:02:05 -070042 int sent_initial_metadata;
43
44 int got_initial_metadata;
45 grpc_stream_op_buffer *recv_ops;
46 void (*on_done_recv)(void *user_data, int success);
47 void *recv_user_data;
Yang Gao5fd0d292015-01-26 00:19:48 -080048} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
klempnerc463f742014-12-19 13:03:35 -080050typedef struct channel_data {
51 grpc_mdelem *te_trailers;
52 grpc_mdelem *method;
53 grpc_mdelem *scheme;
54 grpc_mdelem *content_type;
Craig Tiller5b9efed2015-02-03 20:13:06 -080055 grpc_mdelem *status;
klempnerc463f742014-12-19 13:03:35 -080056} channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057
58/* used to silence 'variable not used' warnings */
59static void ignore_unused(void *ignored) {}
60
Craig Tiller6902ad22015-04-16 08:01:49 -070061static grpc_mdelem *client_filter(void *user_data, grpc_mdelem *md) {
62 grpc_call_element *elem = user_data;
63 channel_data *channeld = elem->channel_data;
64 if (md == channeld->status) {
65 return NULL;
66 } else if (md->key == channeld->status->key) {
67 grpc_call_element_send_cancel(elem);
68 return NULL;
69 }
70 return md;
71}
72
Craig Tiller83f88d92015-04-21 16:02:05 -070073static void hc_on_recv(void *user_data, int success) {
74 grpc_call_element *elem = user_data;
75 call_data *calld = elem->call_data;
76 if (success) {
77 size_t i;
78 size_t nops = calld->recv_ops->nops;
79 grpc_stream_op *ops = calld->recv_ops->ops;
80 for (i = 0; i < nops; i++) {
81 grpc_stream_op *op = &ops[i];
82 if (op->type != GRPC_OP_METADATA) continue;
83 calld->got_initial_metadata = 1;
84 grpc_metadata_batch_filter(&op->data.metadata, client_filter, elem);
85 }
86 }
87 calld->on_done_recv(calld->recv_user_data, success);
88}
89
Craig Tiller50d9db52015-04-23 10:52:14 -070090static void hc_mutate_op(grpc_call_element *elem, grpc_transport_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091 /* grab pointers to our data from the call element */
92 call_data *calld = elem->call_data;
93 channel_data *channeld = elem->channel_data;
Craig Tiller83f88d92015-04-21 16:02:05 -070094 size_t i;
Craig Tiller83f88d92015-04-21 16:02:05 -070095 if (op->send_ops && !calld->sent_initial_metadata) {
96 size_t nops = op->send_ops->nops;
97 grpc_stream_op *ops = op->send_ops->ops;
98 for (i = 0; i < nops; i++) {
99 grpc_stream_op *op = &ops[i];
100 if (op->type != GRPC_OP_METADATA) continue;
101 calld->sent_initial_metadata = 1;
Craig Tiller6902ad22015-04-16 08:01:49 -0700102 /* Send : prefixed headers, which have to be before any application
Craig Tiller83f88d92015-04-21 16:02:05 -0700103 layer headers. */
Craig Tiller205aee12015-04-16 14:46:41 -0700104 grpc_metadata_batch_add_head(&op->data.metadata, &calld->method,
Craig Tiller1b5062c2015-04-21 11:53:02 -0700105 grpc_mdelem_ref(channeld->method));
Craig Tiller205aee12015-04-16 14:46:41 -0700106 grpc_metadata_batch_add_head(&op->data.metadata, &calld->scheme,
Craig Tiller1b5062c2015-04-21 11:53:02 -0700107 grpc_mdelem_ref(channeld->scheme));
Craig Tiller205aee12015-04-16 14:46:41 -0700108 grpc_metadata_batch_add_tail(&op->data.metadata, &calld->te_trailers,
Craig Tiller1b5062c2015-04-21 11:53:02 -0700109 grpc_mdelem_ref(channeld->te_trailers));
Craig Tiller205aee12015-04-16 14:46:41 -0700110 grpc_metadata_batch_add_tail(&op->data.metadata, &calld->content_type,
Craig Tiller1b5062c2015-04-21 11:53:02 -0700111 grpc_mdelem_ref(channeld->content_type));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800112 break;
Craig Tiller83f88d92015-04-21 16:02:05 -0700113 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114 }
Craig Tiller83f88d92015-04-21 16:02:05 -0700115
116 if (op->recv_ops && !calld->got_initial_metadata) {
117 /* substitute our callback for the higher callback */
118 calld->recv_ops = op->recv_ops;
119 calld->on_done_recv = op->on_done_recv;
120 calld->recv_user_data = op->recv_user_data;
121 op->on_done_recv = hc_on_recv;
122 op->recv_user_data = elem;
123 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700124}
Craig Tiller83f88d92015-04-21 16:02:05 -0700125
Craig Tiller06aeea72015-04-23 10:54:45 -0700126static void hc_start_transport_op(grpc_call_element *elem,
127 grpc_transport_op *op) {
Craig Tiller50d9db52015-04-23 10:52:14 -0700128 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
129 hc_mutate_op(elem, op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700130 grpc_call_next_op(elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131}
132
133/* Called on special channel events, such as disconnection or new incoming
134 calls on the server */
ctillerf962f522014-12-10 15:28:27 -0800135static void channel_op(grpc_channel_element *elem,
136 grpc_channel_element *from_elem, grpc_channel_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800137 /* grab pointers to our data from the channel element */
138 channel_data *channeld = elem->channel_data;
139
140 ignore_unused(channeld);
141
142 switch (op->type) {
143 default:
144 /* pass control up or down the stack depending on op->dir */
145 grpc_channel_next_op(elem, op);
146 break;
147 }
148}
149
150/* Constructor for call_data */
151static void init_call_elem(grpc_call_element *elem,
Craig Tiller50d9db52015-04-23 10:52:14 -0700152 const void *server_transport_data,
153 grpc_transport_op *initial_op) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700154 call_data *calld = elem->call_data;
155 calld->sent_initial_metadata = 0;
156 calld->got_initial_metadata = 0;
Craig Tiller50d9db52015-04-23 10:52:14 -0700157 if (initial_op) hc_mutate_op(elem, initial_op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700158}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800159
160/* Destructor for call_data */
161static void destroy_call_elem(grpc_call_element *elem) {
162 /* grab pointers to our data from the call element */
163 call_data *calld = elem->call_data;
164 channel_data *channeld = elem->channel_data;
165
166 ignore_unused(calld);
167 ignore_unused(channeld);
168}
169
David Klempnered0cbc82015-01-14 14:46:10 -0800170static const char *scheme_from_args(const grpc_channel_args *args) {
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100171 unsigned i;
David Klempnera1e86932015-01-13 18:13:59 -0800172 if (args != NULL) {
173 for (i = 0; i < args->num_args; ++i) {
174 if (args->args[i].type == GRPC_ARG_STRING &&
David Klempnered0cbc82015-01-14 14:46:10 -0800175 strcmp(args->args[i].key, GRPC_ARG_HTTP2_SCHEME) == 0) {
David Klempnera1e86932015-01-13 18:13:59 -0800176 return args->args[i].value.string;
177 }
178 }
179 }
180 return "http";
181}
182
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800183/* Constructor for channel_data */
184static void init_channel_elem(grpc_channel_element *elem,
185 const grpc_channel_args *args, grpc_mdctx *mdctx,
186 int is_first, int is_last) {
187 /* grab pointers to our data from the channel element */
188 channel_data *channeld = elem->channel_data;
189
190 /* The first and the last filters tend to be implemented differently to
191 handle the case that there's no 'next' filter to call on the up or down
192 path */
193 GPR_ASSERT(!is_first);
194 GPR_ASSERT(!is_last);
195
196 /* initialize members */
197 channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers");
klempnerc463f742014-12-19 13:03:35 -0800198 channeld->method = grpc_mdelem_from_strings(mdctx, ":method", "POST");
David Klempnera1e86932015-01-13 18:13:59 -0800199 channeld->scheme =
200 grpc_mdelem_from_strings(mdctx, ":scheme", scheme_from_args(args));
klempnerc463f742014-12-19 13:03:35 -0800201 channeld->content_type =
202 grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc");
Craig Tiller5b9efed2015-02-03 20:13:06 -0800203 channeld->status = grpc_mdelem_from_strings(mdctx, ":status", "200");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204}
205
206/* Destructor for channel data */
207static void destroy_channel_elem(grpc_channel_element *elem) {
208 /* grab pointers to our data from the channel element */
209 channel_data *channeld = elem->channel_data;
210
211 grpc_mdelem_unref(channeld->te_trailers);
klempnerc463f742014-12-19 13:03:35 -0800212 grpc_mdelem_unref(channeld->method);
213 grpc_mdelem_unref(channeld->scheme);
214 grpc_mdelem_unref(channeld->content_type);
Craig Tiller5b9efed2015-02-03 20:13:06 -0800215 grpc_mdelem_unref(channeld->status);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800216}
217
218const grpc_channel_filter grpc_http_client_filter = {
Craig Tiller06aeea72015-04-23 10:54:45 -0700219 hc_start_transport_op, channel_op, sizeof(call_data), init_call_elem,
220 destroy_call_elem, sizeof(channel_data), init_channel_elem,
221 destroy_channel_elem, "http-client"};