From 7adbb643072af7b6dc0f09298fa44b988118df01 Mon Sep 17 00:00:00 2001 From: Hongyu Chen Date: Tue, 11 Aug 2015 16:00:32 -0700 Subject: Build file changes --- src/core/channel/census_filter.c | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) (limited to 'src/core/channel') diff --git a/src/core/channel/census_filter.c b/src/core/channel/census_filter.c index d996c3475e..86bde85d12 100644 --- a/src/core/channel/census_filter.c +++ b/src/core/channel/census_filter.c @@ -47,13 +47,12 @@ typedef struct call_data { census_op_id op_id; - census_rpc_stats stats; + /*census_rpc_stats stats;*/ gpr_timespec start_ts; /* recv callback */ grpc_stream_op_buffer* recv_ops; - void (*on_done_recv)(void* user_data, int success); - void* recv_user_data; + grpc_iomgr_closure* on_done_recv; } call_data; typedef struct channel_data { @@ -108,7 +107,7 @@ static void server_on_done_recv(void* ptr, int success) { if (success) { extract_and_annotate_method_tag(calld->recv_ops, calld, chand); } - calld->on_done_recv(calld->recv_user_data, success); + calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); } static void server_mutate_op(grpc_call_element* elem, @@ -118,9 +117,7 @@ static void server_mutate_op(grpc_call_element* elem, /* substitute our callback for the op callback */ calld->recv_ops = op->recv_ops; calld->on_done_recv = op->on_done_recv; - calld->recv_user_data = op->recv_user_data; - op->on_done_recv = server_on_done_recv; - op->recv_user_data = elem; + op->on_done_recv = calld->on_done_recv; } } @@ -132,19 +129,6 @@ static void server_start_transport_op(grpc_call_element* elem, grpc_call_next_op(elem, op); } -static void channel_op(grpc_channel_element* elem, - grpc_channel_element* from_elem, grpc_channel_op* op) { - switch (op->type) { - case GRPC_TRANSPORT_CLOSED: - /* TODO(hongyu): Annotate trace information for all calls of the channel - */ - break; - default: - break; - } - grpc_channel_next_op(elem, op); -} - static void client_init_call_elem(grpc_call_element* elem, const void* server_transport_data, grpc_transport_stream_op* initial_op) { @@ -171,6 +155,7 @@ static void server_init_call_elem(grpc_call_element* elem, init_rpc_stats(&d->stats); d->start_ts = gpr_now(GPR_CLOCK_REALTIME); d->op_id = census_tracing_start_op(); + grpc_iomgr_closure_init(d->on_done_recv, server_on_done_recv, elem); if (initial_op) server_mutate_op(elem, initial_op); } @@ -179,18 +164,19 @@ static void server_destroy_call_elem(grpc_call_element* elem) { GPR_ASSERT(d != NULL); d->stats.elapsed_time_ms = gpr_timespec_to_micros( gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), d->start_ts)); - census_record_rpc_server_stats(d->op_id, &d->stats); + census_record_stats(d->ctxt, stats, nstats); + /*census_record_rpc_server_stats(d->op_id, &d->stats);*/ census_tracing_end_op(d->op_id); } -static void init_channel_elem(grpc_channel_element* elem, +static void init_channel_elem(grpc_channel_element* elem, grpc_channel* master, const grpc_channel_args* args, grpc_mdctx* mdctx, int is_first, int is_last) { channel_data* chand = elem->channel_data; GPR_ASSERT(chand != NULL); GPR_ASSERT(!is_first); GPR_ASSERT(!is_last); - chand->path_str = grpc_mdstr_from_string(mdctx, ":path"); + chand->path_str = grpc_mdstr_from_string(mdctx, ":path", 0); } static void destroy_channel_elem(grpc_channel_element* elem) { @@ -203,22 +189,24 @@ static void destroy_channel_elem(grpc_channel_element* elem) { const grpc_channel_filter grpc_client_census_filter = { client_start_transport_op, - channel_op, + grpc_channel_next_op, sizeof(call_data), client_init_call_elem, client_destroy_call_elem, sizeof(channel_data), init_channel_elem, destroy_channel_elem, + grpc_call_next_get_peer, "census-client"}; const grpc_channel_filter grpc_server_census_filter = { server_start_transport_op, - channel_op, + grpc_channel_next_op, sizeof(call_data), server_init_call_elem, server_destroy_call_elem, sizeof(channel_data), init_channel_elem, destroy_channel_elem, + grpc_call_next_get_peer, "census-server"}; -- cgit v1.2.3 From 3c9b87396f17b48e714f07432b733b45ee612812 Mon Sep 17 00:00:00 2001 From: Hongyu Chen Date: Thu, 13 Aug 2015 17:01:02 -0700 Subject: Make census filter mostly no-op --- src/core/channel/census_filter.c | 48 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'src/core/channel') diff --git a/src/core/channel/census_filter.c b/src/core/channel/census_filter.c index 86bde85d12..8713d606b4 100644 --- a/src/core/channel/census_filter.c +++ b/src/core/channel/census_filter.c @@ -36,6 +36,8 @@ #include #include +#include "include/grpc/census.h" +#include "src/core/census/rpc_stat_id.h" #include "src/core/channel/channel_stack.h" #include "src/core/channel/noop_filter.h" #include "src/core/statistics/census_interface.h" @@ -47,8 +49,9 @@ typedef struct call_data { census_op_id op_id; - /*census_rpc_stats stats;*/ + census_context* ctxt; gpr_timespec start_ts; + int error; /* recv callback */ grpc_stream_op_buffer* recv_ops; @@ -59,11 +62,6 @@ typedef struct channel_data { grpc_mdstr* path_str; /* pointer to meta data str with key == ":path" */ } channel_data; -static void init_rpc_stats(census_rpc_stats* stats) { - memset(stats, 0, sizeof(census_rpc_stats)); - stats->cnt = 1; -} - static void extract_and_annotate_method_tag(grpc_stream_op_buffer* sopb, call_data* calld, channel_data* chand) { @@ -76,8 +74,7 @@ static void extract_and_annotate_method_tag(grpc_stream_op_buffer* sopb, if (m->md->key == chand->path_str) { gpr_log(GPR_DEBUG, "%s", (const char*)GPR_SLICE_START_PTR(m->md->value->slice)); - census_add_method_tag(calld->op_id, (const char*)GPR_SLICE_START_PTR( - m->md->value->slice)); + /* Add method tag here */ } } } @@ -94,8 +91,6 @@ static void client_mutate_op(grpc_call_element* elem, static void client_start_transport_op(grpc_call_element* elem, grpc_transport_stream_op* op) { - call_data* calld = elem->call_data; - GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0)); client_mutate_op(elem, op); grpc_call_next_op(elem, op); } @@ -134,17 +129,24 @@ static void client_init_call_elem(grpc_call_element* elem, grpc_transport_stream_op* initial_op) { call_data* d = elem->call_data; GPR_ASSERT(d != NULL); - init_rpc_stats(&d->stats); d->start_ts = gpr_now(GPR_CLOCK_REALTIME); - d->op_id = census_tracing_start_op(); if (initial_op) client_mutate_op(elem, initial_op); } static void client_destroy_call_elem(grpc_call_element* elem) { call_data* d = elem->call_data; + census_stat stats[3]; GPR_ASSERT(d != NULL); - census_record_rpc_client_stats(d->op_id, &d->stats); - census_tracing_end_op(d->op_id); + stats[0].id = CENSUS_RPC_CLIENT_REQUESTS; + stats[0].value = 1.0; + stats[1].id = CENSUS_RPC_CLIENT_ERRORS; + stats[1].value = 0.0; /* TODO(hongyu): add rpc error recording */ + stats[2].id = CENSUS_RPC_CLIENT_LATENCY; + /* Temporarily using census_filter invoke time as the start time of rpc. */ + stats[2].value = gpr_timespec_to_micros( + gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), d->start_ts)); + census_record_stat(d->ctxt, stats, 3); + /* TODO(hongyu): call census_rpc_end_op here */ } static void server_init_call_elem(grpc_call_element* elem, @@ -152,21 +154,25 @@ static void server_init_call_elem(grpc_call_element* elem, grpc_transport_stream_op* initial_op) { call_data* d = elem->call_data; GPR_ASSERT(d != NULL); - init_rpc_stats(&d->stats); d->start_ts = gpr_now(GPR_CLOCK_REALTIME); - d->op_id = census_tracing_start_op(); + /* TODO(hongyu): call census_tracing_start_op here. */ grpc_iomgr_closure_init(d->on_done_recv, server_on_done_recv, elem); if (initial_op) server_mutate_op(elem, initial_op); } static void server_destroy_call_elem(grpc_call_element* elem) { call_data* d = elem->call_data; + census_stat stats[3]; GPR_ASSERT(d != NULL); - d->stats.elapsed_time_ms = gpr_timespec_to_micros( + stats[0].id = CENSUS_RPC_SERVER_REQUESTS; + stats[0].value = 1.0; + stats[1].id = CENSUS_RPC_SERVER_ERRORS; + stats[1].value = 0.0; + stats[2].id = CENSUS_RPC_SERVER_LATENCY; + stats[2].value = gpr_timespec_to_micros( gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), d->start_ts)); - census_record_stats(d->ctxt, stats, nstats); - /*census_record_rpc_server_stats(d->op_id, &d->stats);*/ - census_tracing_end_op(d->op_id); + census_record_stat(d->ctxt, stats, 3); + /* TODO(hongyu): call census_tracing_end_op here */ } static void init_channel_elem(grpc_channel_element* elem, grpc_channel* master, @@ -174,8 +180,6 @@ static void init_channel_elem(grpc_channel_element* elem, grpc_channel* master, int is_first, int is_last) { channel_data* chand = elem->channel_data; GPR_ASSERT(chand != NULL); - GPR_ASSERT(!is_first); - GPR_ASSERT(!is_last); chand->path_str = grpc_mdstr_from_string(mdctx, ":path", 0); } -- cgit v1.2.3 From 58c927cede72cfdda7bf4173b09e8313f954412d Mon Sep 17 00:00:00 2001 From: Hongyu Chen Date: Fri, 14 Aug 2015 15:19:13 -0700 Subject: Make census_filter more of a noop. --- src/core/channel/census_filter.c | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) (limited to 'src/core/channel') diff --git a/src/core/channel/census_filter.c b/src/core/channel/census_filter.c index 8713d606b4..53d70be356 100644 --- a/src/core/channel/census_filter.c +++ b/src/core/channel/census_filter.c @@ -135,18 +135,8 @@ static void client_init_call_elem(grpc_call_element* elem, static void client_destroy_call_elem(grpc_call_element* elem) { call_data* d = elem->call_data; - census_stat stats[3]; GPR_ASSERT(d != NULL); - stats[0].id = CENSUS_RPC_CLIENT_REQUESTS; - stats[0].value = 1.0; - stats[1].id = CENSUS_RPC_CLIENT_ERRORS; - stats[1].value = 0.0; /* TODO(hongyu): add rpc error recording */ - stats[2].id = CENSUS_RPC_CLIENT_LATENCY; - /* Temporarily using census_filter invoke time as the start time of rpc. */ - stats[2].value = gpr_timespec_to_micros( - gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), d->start_ts)); - census_record_stat(d->ctxt, stats, 3); - /* TODO(hongyu): call census_rpc_end_op here */ + /* TODO(hongyu): record rpc client stats and census_rpc_end_op here */ } static void server_init_call_elem(grpc_call_element* elem, @@ -162,17 +152,8 @@ static void server_init_call_elem(grpc_call_element* elem, static void server_destroy_call_elem(grpc_call_element* elem) { call_data* d = elem->call_data; - census_stat stats[3]; GPR_ASSERT(d != NULL); - stats[0].id = CENSUS_RPC_SERVER_REQUESTS; - stats[0].value = 1.0; - stats[1].id = CENSUS_RPC_SERVER_ERRORS; - stats[1].value = 0.0; - stats[2].id = CENSUS_RPC_SERVER_LATENCY; - stats[2].value = gpr_timespec_to_micros( - gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), d->start_ts)); - census_record_stat(d->ctxt, stats, 3); - /* TODO(hongyu): call census_tracing_end_op here */ + /* TODO(hongyu): record rpc server stats and census_tracing_end_op here */ } static void init_channel_elem(grpc_channel_element* elem, grpc_channel* master, -- cgit v1.2.3 From fdc1dc744baa194f220a5d103ca73dde290c7d4b Mon Sep 17 00:00:00 2001 From: Hongyu Chen Date: Wed, 19 Aug 2015 16:58:12 -0700 Subject: Move census_filters from .../channel to .../census --- BUILD | 12 +- Makefile | 4 +- build.json | 4 +- gRPC.podspec | 6 +- src/core/census/census_filter.c | 197 +++++++++++++++++++++ src/core/census/census_filter.h | 44 +++++ src/core/channel/census_filter.c | 197 --------------------- src/core/channel/census_filter.h | 44 ----- src/core/surface/channel_create.c | 2 +- src/core/surface/secure_channel_create.c | 2 +- src/core/surface/server.c | 2 +- tools/doxygen/Doxyfile.core.internal | 4 +- tools/run_tests/sources_and_headers.json | 12 +- vsprojects/grpc/grpc.vcxproj | 6 +- vsprojects/grpc/grpc.vcxproj.filters | 10 +- vsprojects/grpc_unsecure/grpc_unsecure.vcxproj | 6 +- .../grpc_unsecure/grpc_unsecure.vcxproj.filters | 10 +- 17 files changed, 281 insertions(+), 281 deletions(-) create mode 100644 src/core/census/census_filter.c create mode 100644 src/core/census/census_filter.h delete mode 100644 src/core/channel/census_filter.c delete mode 100644 src/core/channel/census_filter.h (limited to 'src/core/channel') diff --git a/BUILD b/BUILD index d38b9cee10..b24ba311e2 100644 --- a/BUILD +++ b/BUILD @@ -145,7 +145,7 @@ cc_library( "src/core/tsi/ssl_transport_security.h", "src/core/tsi/transport_security.h", "src/core/tsi/transport_security_interface.h", - "src/core/channel/census_filter.h", + "src/core/census/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", "src/core/channel/client_channel.h", @@ -267,8 +267,8 @@ cc_library( "src/core/tsi/fake_transport_security.c", "src/core/tsi/ssl_transport_security.c", "src/core/tsi/transport_security.c", + "src/core/census/census_filter.c", "src/core/census/grpc_context.c", - "src/core/channel/census_filter.c", "src/core/channel/channel_args.c", "src/core/channel/channel_stack.c", "src/core/channel/client_channel.c", @@ -409,7 +409,7 @@ cc_library( cc_library( name = "grpc_unsecure", srcs = [ - "src/core/channel/census_filter.h", + "src/core/census/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", "src/core/channel/client_channel.h", @@ -511,8 +511,8 @@ cc_library( "src/core/census/context.h", "src/core/census/rpc_stat_id.h", "src/core/surface/init_unsecure.c", + "src/core/census/census_filter.c", "src/core/census/grpc_context.c", - "src/core/channel/census_filter.c", "src/core/channel/channel_args.c", "src/core/channel/channel_stack.c", "src/core/channel/client_channel.c", @@ -997,8 +997,8 @@ objc_library( "src/core/tsi/fake_transport_security.c", "src/core/tsi/ssl_transport_security.c", "src/core/tsi/transport_security.c", + "src/core/census/census_filter.c", "src/core/census/grpc_context.c", - "src/core/channel/census_filter.c", "src/core/channel/channel_args.c", "src/core/channel/channel_stack.c", "src/core/channel/client_channel.c", @@ -1137,7 +1137,7 @@ objc_library( "src/core/tsi/ssl_transport_security.h", "src/core/tsi/transport_security.h", "src/core/tsi/transport_security_interface.h", - "src/core/channel/census_filter.h", + "src/core/census/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", "src/core/channel/client_channel.h", diff --git a/Makefile b/Makefile index aabf1bbd04..995a4954e4 100644 --- a/Makefile +++ b/Makefile @@ -3976,8 +3976,8 @@ LIBGRPC_SRC = \ src/core/tsi/fake_transport_security.c \ src/core/tsi/ssl_transport_security.c \ src/core/tsi/transport_security.c \ + src/core/census/census_filter.c \ src/core/census/grpc_context.c \ - src/core/channel/census_filter.c \ src/core/channel/channel_args.c \ src/core/channel/channel_stack.c \ src/core/channel/client_channel.c \ @@ -4249,8 +4249,8 @@ endif LIBGRPC_UNSECURE_SRC = \ src/core/surface/init_unsecure.c \ + src/core/census/census_filter.c \ src/core/census/grpc_context.c \ - src/core/channel/census_filter.c \ src/core/channel/channel_args.c \ src/core/channel/channel_stack.c \ src/core/channel/client_channel.c \ diff --git a/build.json b/build.json index ab6a39a20f..3e3e71a684 100644 --- a/build.json +++ b/build.json @@ -114,7 +114,7 @@ "include/grpc/status.h" ], "headers": [ - "src/core/channel/census_filter.h", + "src/core/census/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", "src/core/channel/client_channel.h", @@ -215,8 +215,8 @@ "src/core/transport/transport_impl.h" ], "src": [ + "src/core/census/census_filter.c", "src/core/census/grpc_context.c", - "src/core/channel/census_filter.c", "src/core/channel/channel_args.c", "src/core/channel/channel_stack.c", "src/core/channel/client_channel.c", diff --git a/gRPC.podspec b/gRPC.podspec index d1e95a1652..b57960a896 100644 --- a/gRPC.podspec +++ b/gRPC.podspec @@ -147,7 +147,7 @@ Pod::Spec.new do |s| 'src/core/tsi/ssl_transport_security.h', 'src/core/tsi/transport_security.h', 'src/core/tsi/transport_security_interface.h', - 'src/core/channel/census_filter.h', + 'src/core/census/census_filter.h', 'src/core/channel/channel_args.h', 'src/core/channel/channel_stack.h', 'src/core/channel/client_channel.h', @@ -276,8 +276,8 @@ Pod::Spec.new do |s| 'src/core/tsi/fake_transport_security.c', 'src/core/tsi/ssl_transport_security.c', 'src/core/tsi/transport_security.c', + 'src/core/census/census_filter.c', 'src/core/census/grpc_context.c', - 'src/core/channel/census_filter.c', 'src/core/channel/channel_args.c', 'src/core/channel/channel_stack.c', 'src/core/channel/client_channel.c', @@ -415,7 +415,7 @@ Pod::Spec.new do |s| 'src/core/tsi/ssl_transport_security.h', 'src/core/tsi/transport_security.h', 'src/core/tsi/transport_security_interface.h', - 'src/core/channel/census_filter.h', + 'src/core/census/census_filter.h', 'src/core/channel/channel_args.h', 'src/core/channel/channel_stack.h', 'src/core/channel/client_channel.h', diff --git a/src/core/census/census_filter.c b/src/core/census/census_filter.c new file mode 100644 index 0000000000..31db686cf3 --- /dev/null +++ b/src/core/census/census_filter.c @@ -0,0 +1,197 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "src/core/census/census_filter.h" + +#include +#include + +#include "include/grpc/census.h" +#include "src/core/census/rpc_stat_id.h" +#include "src/core/channel/channel_stack.h" +#include "src/core/channel/noop_filter.h" +#include "src/core/statistics/census_interface.h" +#include "src/core/statistics/census_rpc_stats.h" +#include +#include +#include +#include + +typedef struct call_data { + census_op_id op_id; + census_context* ctxt; + gpr_timespec start_ts; + int error; + + /* recv callback */ + grpc_stream_op_buffer* recv_ops; + grpc_iomgr_closure* on_done_recv; +} call_data; + +typedef struct channel_data { + grpc_mdstr* path_str; /* pointer to meta data str with key == ":path" */ +} channel_data; + +static void extract_and_annotate_method_tag(grpc_stream_op_buffer* sopb, + call_data* calld, + channel_data* chand) { + grpc_linked_mdelem* m; + size_t i; + for (i = 0; i < sopb->nops; i++) { + grpc_stream_op* op = &sopb->ops[i]; + if (op->type != GRPC_OP_METADATA) continue; + for (m = op->data.metadata.list.head; m != NULL; m = m->next) { + if (m->md->key == chand->path_str) { + gpr_log(GPR_DEBUG, "%s", + (const char*)GPR_SLICE_START_PTR(m->md->value->slice)); + /* Add method tag here */ + } + } + } +} + +static void client_mutate_op(grpc_call_element* elem, + grpc_transport_stream_op* op) { + call_data* calld = elem->call_data; + channel_data* chand = elem->channel_data; + if (op->send_ops) { + extract_and_annotate_method_tag(op->send_ops, calld, chand); + } +} + +static void client_start_transport_op(grpc_call_element* elem, + grpc_transport_stream_op* op) { + client_mutate_op(elem, op); + grpc_call_next_op(elem, op); +} + +static void server_on_done_recv(void* ptr, int success) { + grpc_call_element* elem = ptr; + call_data* calld = elem->call_data; + channel_data* chand = elem->channel_data; + if (success) { + extract_and_annotate_method_tag(calld->recv_ops, calld, chand); + } + calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); +} + +static void server_mutate_op(grpc_call_element* elem, + grpc_transport_stream_op* op) { + call_data* calld = elem->call_data; + if (op->recv_ops) { + /* substitute our callback for the op callback */ + calld->recv_ops = op->recv_ops; + calld->on_done_recv = op->on_done_recv; + op->on_done_recv = calld->on_done_recv; + } +} + +static void server_start_transport_op(grpc_call_element* elem, + grpc_transport_stream_op* op) { + call_data* calld = elem->call_data; + GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0)); + server_mutate_op(elem, op); + grpc_call_next_op(elem, op); +} + +static void client_init_call_elem(grpc_call_element* elem, + const void* server_transport_data, + grpc_transport_stream_op* initial_op) { + call_data* d = elem->call_data; + GPR_ASSERT(d != NULL); + d->start_ts = gpr_now(GPR_CLOCK_REALTIME); + if (initial_op) client_mutate_op(elem, initial_op); +} + +static void client_destroy_call_elem(grpc_call_element* elem) { + call_data* d = elem->call_data; + GPR_ASSERT(d != NULL); + /* TODO(hongyu): record rpc client stats and census_rpc_end_op here */ +} + +static void server_init_call_elem(grpc_call_element* elem, + const void* server_transport_data, + grpc_transport_stream_op* initial_op) { + call_data* d = elem->call_data; + GPR_ASSERT(d != NULL); + d->start_ts = gpr_now(GPR_CLOCK_REALTIME); + /* TODO(hongyu): call census_tracing_start_op here. */ + grpc_iomgr_closure_init(d->on_done_recv, server_on_done_recv, elem); + if (initial_op) server_mutate_op(elem, initial_op); +} + +static void server_destroy_call_elem(grpc_call_element* elem) { + call_data* d = elem->call_data; + GPR_ASSERT(d != NULL); + /* TODO(hongyu): record rpc server stats and census_tracing_end_op here */ +} + +static void init_channel_elem(grpc_channel_element* elem, grpc_channel* master, + const grpc_channel_args* args, grpc_mdctx* mdctx, + int is_first, int is_last) { + channel_data* chand = elem->channel_data; + GPR_ASSERT(chand != NULL); + chand->path_str = grpc_mdstr_from_string(mdctx, ":path", 0); +} + +static void destroy_channel_elem(grpc_channel_element* elem) { + channel_data* chand = elem->channel_data; + GPR_ASSERT(chand != NULL); + if (chand->path_str != NULL) { + GRPC_MDSTR_UNREF(chand->path_str); + } +} + +const grpc_channel_filter grpc_client_census_filter = { + client_start_transport_op, + grpc_channel_next_op, + sizeof(call_data), + client_init_call_elem, + client_destroy_call_elem, + sizeof(channel_data), + init_channel_elem, + destroy_channel_elem, + grpc_call_next_get_peer, + "census-client"}; + +const grpc_channel_filter grpc_server_census_filter = { + server_start_transport_op, + grpc_channel_next_op, + sizeof(call_data), + server_init_call_elem, + server_destroy_call_elem, + sizeof(channel_data), + init_channel_elem, + destroy_channel_elem, + grpc_call_next_get_peer, + "census-server"}; diff --git a/src/core/census/census_filter.h b/src/core/census/census_filter.h new file mode 100644 index 0000000000..4f9759f0db --- /dev/null +++ b/src/core/census/census_filter.h @@ -0,0 +1,44 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_INTERNAL_CORE_CHANNEL_CENSUS_FILTER_H +#define GRPC_INTERNAL_CORE_CHANNEL_CENSUS_FILTER_H + +#include "src/core/channel/channel_stack.h" + +/* Census filters: provides tracing and stats collection functionalities. It + needs to reside right below the surface filter in the channel stack. */ +extern const grpc_channel_filter grpc_client_census_filter; +extern const grpc_channel_filter grpc_server_census_filter; + +#endif /* GRPC_INTERNAL_CORE_CHANNEL_CENSUS_FILTER_H */ diff --git a/src/core/channel/census_filter.c b/src/core/channel/census_filter.c deleted file mode 100644 index 53d70be356..0000000000 --- a/src/core/channel/census_filter.c +++ /dev/null @@ -1,197 +0,0 @@ -/* - * - * Copyright 2015, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include "src/core/channel/census_filter.h" - -#include -#include - -#include "include/grpc/census.h" -#include "src/core/census/rpc_stat_id.h" -#include "src/core/channel/channel_stack.h" -#include "src/core/channel/noop_filter.h" -#include "src/core/statistics/census_interface.h" -#include "src/core/statistics/census_rpc_stats.h" -#include -#include -#include -#include - -typedef struct call_data { - census_op_id op_id; - census_context* ctxt; - gpr_timespec start_ts; - int error; - - /* recv callback */ - grpc_stream_op_buffer* recv_ops; - grpc_iomgr_closure* on_done_recv; -} call_data; - -typedef struct channel_data { - grpc_mdstr* path_str; /* pointer to meta data str with key == ":path" */ -} channel_data; - -static void extract_and_annotate_method_tag(grpc_stream_op_buffer* sopb, - call_data* calld, - channel_data* chand) { - grpc_linked_mdelem* m; - size_t i; - for (i = 0; i < sopb->nops; i++) { - grpc_stream_op* op = &sopb->ops[i]; - if (op->type != GRPC_OP_METADATA) continue; - for (m = op->data.metadata.list.head; m != NULL; m = m->next) { - if (m->md->key == chand->path_str) { - gpr_log(GPR_DEBUG, "%s", - (const char*)GPR_SLICE_START_PTR(m->md->value->slice)); - /* Add method tag here */ - } - } - } -} - -static void client_mutate_op(grpc_call_element* elem, - grpc_transport_stream_op* op) { - call_data* calld = elem->call_data; - channel_data* chand = elem->channel_data; - if (op->send_ops) { - extract_and_annotate_method_tag(op->send_ops, calld, chand); - } -} - -static void client_start_transport_op(grpc_call_element* elem, - grpc_transport_stream_op* op) { - client_mutate_op(elem, op); - grpc_call_next_op(elem, op); -} - -static void server_on_done_recv(void* ptr, int success) { - grpc_call_element* elem = ptr; - call_data* calld = elem->call_data; - channel_data* chand = elem->channel_data; - if (success) { - extract_and_annotate_method_tag(calld->recv_ops, calld, chand); - } - calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); -} - -static void server_mutate_op(grpc_call_element* elem, - grpc_transport_stream_op* op) { - call_data* calld = elem->call_data; - if (op->recv_ops) { - /* substitute our callback for the op callback */ - calld->recv_ops = op->recv_ops; - calld->on_done_recv = op->on_done_recv; - op->on_done_recv = calld->on_done_recv; - } -} - -static void server_start_transport_op(grpc_call_element* elem, - grpc_transport_stream_op* op) { - call_data* calld = elem->call_data; - GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0)); - server_mutate_op(elem, op); - grpc_call_next_op(elem, op); -} - -static void client_init_call_elem(grpc_call_element* elem, - const void* server_transport_data, - grpc_transport_stream_op* initial_op) { - call_data* d = elem->call_data; - GPR_ASSERT(d != NULL); - d->start_ts = gpr_now(GPR_CLOCK_REALTIME); - if (initial_op) client_mutate_op(elem, initial_op); -} - -static void client_destroy_call_elem(grpc_call_element* elem) { - call_data* d = elem->call_data; - GPR_ASSERT(d != NULL); - /* TODO(hongyu): record rpc client stats and census_rpc_end_op here */ -} - -static void server_init_call_elem(grpc_call_element* elem, - const void* server_transport_data, - grpc_transport_stream_op* initial_op) { - call_data* d = elem->call_data; - GPR_ASSERT(d != NULL); - d->start_ts = gpr_now(GPR_CLOCK_REALTIME); - /* TODO(hongyu): call census_tracing_start_op here. */ - grpc_iomgr_closure_init(d->on_done_recv, server_on_done_recv, elem); - if (initial_op) server_mutate_op(elem, initial_op); -} - -static void server_destroy_call_elem(grpc_call_element* elem) { - call_data* d = elem->call_data; - GPR_ASSERT(d != NULL); - /* TODO(hongyu): record rpc server stats and census_tracing_end_op here */ -} - -static void init_channel_elem(grpc_channel_element* elem, grpc_channel* master, - const grpc_channel_args* args, grpc_mdctx* mdctx, - int is_first, int is_last) { - channel_data* chand = elem->channel_data; - GPR_ASSERT(chand != NULL); - chand->path_str = grpc_mdstr_from_string(mdctx, ":path", 0); -} - -static void destroy_channel_elem(grpc_channel_element* elem) { - channel_data* chand = elem->channel_data; - GPR_ASSERT(chand != NULL); - if (chand->path_str != NULL) { - GRPC_MDSTR_UNREF(chand->path_str); - } -} - -const grpc_channel_filter grpc_client_census_filter = { - client_start_transport_op, - grpc_channel_next_op, - sizeof(call_data), - client_init_call_elem, - client_destroy_call_elem, - sizeof(channel_data), - init_channel_elem, - destroy_channel_elem, - grpc_call_next_get_peer, - "census-client"}; - -const grpc_channel_filter grpc_server_census_filter = { - server_start_transport_op, - grpc_channel_next_op, - sizeof(call_data), - server_init_call_elem, - server_destroy_call_elem, - sizeof(channel_data), - init_channel_elem, - destroy_channel_elem, - grpc_call_next_get_peer, - "census-server"}; diff --git a/src/core/channel/census_filter.h b/src/core/channel/census_filter.h deleted file mode 100644 index 4f9759f0db..0000000000 --- a/src/core/channel/census_filter.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * - * Copyright 2015, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#ifndef GRPC_INTERNAL_CORE_CHANNEL_CENSUS_FILTER_H -#define GRPC_INTERNAL_CORE_CHANNEL_CENSUS_FILTER_H - -#include "src/core/channel/channel_stack.h" - -/* Census filters: provides tracing and stats collection functionalities. It - needs to reside right below the surface filter in the channel stack. */ -extern const grpc_channel_filter grpc_client_census_filter; -extern const grpc_channel_filter grpc_server_census_filter; - -#endif /* GRPC_INTERNAL_CORE_CHANNEL_CENSUS_FILTER_H */ diff --git a/src/core/surface/channel_create.c b/src/core/surface/channel_create.c index 4379b3d016..4d01be3d7d 100644 --- a/src/core/surface/channel_create.c +++ b/src/core/surface/channel_create.c @@ -38,7 +38,7 @@ #include -#include "src/core/channel/census_filter.h" +#include "src/core/census/census_filter.h" #include "src/core/channel/channel_args.h" #include "src/core/channel/client_channel.h" #include "src/core/channel/compress_filter.h" diff --git a/src/core/surface/secure_channel_create.c b/src/core/surface/secure_channel_create.c index 93a372928f..943bddfc0c 100644 --- a/src/core/surface/secure_channel_create.c +++ b/src/core/surface/secure_channel_create.c @@ -38,7 +38,7 @@ #include -#include "src/core/channel/census_filter.h" +#include "src/core/census/census_filter.h" #include "src/core/channel/channel_args.h" #include "src/core/channel/client_channel.h" #include "src/core/channel/compress_filter.h" diff --git a/src/core/surface/server.c b/src/core/surface/server.c index 27070836f3..22d399cf1f 100644 --- a/src/core/surface/server.c +++ b/src/core/surface/server.c @@ -41,7 +41,7 @@ #include #include -#include "src/core/channel/census_filter.h" +#include "src/core/census/census_filter.h" #include "src/core/channel/channel_args.h" #include "src/core/channel/connected_channel.h" #include "src/core/iomgr/iomgr.h" diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index 0619f76e27..f5d5dc0f80 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -780,7 +780,7 @@ src/core/tsi/fake_transport_security.h \ src/core/tsi/ssl_transport_security.h \ src/core/tsi/transport_security.h \ src/core/tsi/transport_security_interface.h \ -src/core/channel/census_filter.h \ +src/core/census/census_filter.h \ src/core/channel/channel_args.h \ src/core/channel/channel_stack.h \ src/core/channel/client_channel.h \ @@ -902,8 +902,8 @@ src/core/surface/secure_channel_create.c \ src/core/tsi/fake_transport_security.c \ src/core/tsi/ssl_transport_security.c \ src/core/tsi/transport_security.c \ +src/core/census/census_filter.c \ src/core/census/grpc_context.c \ -src/core/channel/census_filter.c \ src/core/channel/channel_args.c \ src/core/channel/channel_stack.c \ src/core/channel/client_channel.c \ diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 38bf532f44..cc74397cea 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -12222,9 +12222,9 @@ "include/grpc/grpc.h", "include/grpc/grpc_security.h", "include/grpc/status.h", + "src/core/census/census_filter.h", "src/core/census/context.h", "src/core/census/rpc_stat_id.h", - "src/core/channel/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", "src/core/channel/client_channel.h", @@ -12347,14 +12347,14 @@ "include/grpc/grpc.h", "include/grpc/grpc_security.h", "include/grpc/status.h", + "src/core/census/census_filter.c", + "src/core/census/census_filter.h", "src/core/census/context.c", "src/core/census/context.h", "src/core/census/grpc_context.c", "src/core/census/initialize.c", "src/core/census/record_stat.c", "src/core/census/rpc_stat_id.h", - "src/core/channel/census_filter.c", - "src/core/channel/census_filter.h", "src/core/channel/channel_args.c", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.c", @@ -12694,9 +12694,9 @@ "include/grpc/compression.h", "include/grpc/grpc.h", "include/grpc/status.h", + "src/core/census/census_filter.h", "src/core/census/context.h", "src/core/census/rpc_stat_id.h", - "src/core/channel/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", "src/core/channel/client_channel.h", @@ -12805,14 +12805,14 @@ "include/grpc/compression.h", "include/grpc/grpc.h", "include/grpc/status.h", + "src/core/census/census_filter.c", + "src/core/census/census_filter.h", "src/core/census/context.c", "src/core/census/context.h", "src/core/census/grpc_context.c", "src/core/census/initialize.c", "src/core/census/record_stat.c", "src/core/census/rpc_stat_id.h", - "src/core/channel/census_filter.c", - "src/core/channel/census_filter.h", "src/core/channel/channel_args.c", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.c", diff --git a/vsprojects/grpc/grpc.vcxproj b/vsprojects/grpc/grpc.vcxproj index a87ad29d49..950e385ee7 100644 --- a/vsprojects/grpc/grpc.vcxproj +++ b/vsprojects/grpc/grpc.vcxproj @@ -242,7 +242,7 @@ - + @@ -387,9 +387,9 @@ - + - + diff --git a/vsprojects/grpc/grpc.vcxproj.filters b/vsprojects/grpc/grpc.vcxproj.filters index c8812faa85..d6ecdbba41 100644 --- a/vsprojects/grpc/grpc.vcxproj.filters +++ b/vsprojects/grpc/grpc.vcxproj.filters @@ -64,11 +64,11 @@ src\core\tsi - + src\core\census - - src\core\channel + + src\core\census src\core\channel @@ -482,8 +482,8 @@ src\core\tsi - - src\core\channel + + src\core\census src\core\channel diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj index 06da784323..acd83c6080 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj @@ -225,7 +225,7 @@ - + @@ -330,9 +330,9 @@ - + - + diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters index 2d10960a79..12da3fa1e3 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters @@ -4,11 +4,11 @@ src\core\surface - + src\core\census - - src\core\channel + + src\core\census src\core\channel @@ -380,8 +380,8 @@ - - src\core\channel + + src\core\census src\core\channel -- cgit v1.2.3 From 49772e00eb9606c3192b88f348d9cbcb22eb93c2 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 21 Aug 2015 08:08:37 -0700 Subject: Outlaw illegal metadata characters --- Makefile | 32 +++++++++- build.json | 10 +++ src/core/channel/compress_filter.h | 2 +- src/core/surface/call.c | 5 +- src/core/transport/metadata.c | 26 +++++++- src/core/transport/metadata.h | 1 + tools/codegen/core/gen_legal_metadata_characters.c | 73 ++++++++++++++++++++++ tools/run_tests/sources_and_headers.json | 12 ++++ tools/run_tests/tests.json | 1 + vsprojects/Grpc.mak | 8 +++ 10 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 tools/codegen/core/gen_legal_metadata_characters.c (limited to 'src/core/channel') diff --git a/Makefile b/Makefile index 31628b4412..f7ace3186a 100644 --- a/Makefile +++ b/Makefile @@ -793,6 +793,7 @@ fling_server: $(BINDIR)/$(CONFIG)/fling_server fling_stream_test: $(BINDIR)/$(CONFIG)/fling_stream_test fling_test: $(BINDIR)/$(CONFIG)/fling_test gen_hpack_tables: $(BINDIR)/$(CONFIG)/gen_hpack_tables +gen_legal_metadata_characters: $(BINDIR)/$(CONFIG)/gen_legal_metadata_characters gpr_cmdline_test: $(BINDIR)/$(CONFIG)/gpr_cmdline_test gpr_env_test: $(BINDIR)/$(CONFIG)/gpr_env_test gpr_file_test: $(BINDIR)/$(CONFIG)/gpr_file_test @@ -3386,7 +3387,7 @@ test_python: static_c tools: tools_c tools_cxx -tools_c: privatelibs_c $(BINDIR)/$(CONFIG)/gen_hpack_tables $(BINDIR)/$(CONFIG)/grpc_create_jwt $(BINDIR)/$(CONFIG)/grpc_fetch_oauth2 $(BINDIR)/$(CONFIG)/grpc_print_google_default_creds_token $(BINDIR)/$(CONFIG)/grpc_verify_jwt +tools_c: privatelibs_c $(BINDIR)/$(CONFIG)/gen_hpack_tables $(BINDIR)/$(CONFIG)/gen_legal_metadata_characters $(BINDIR)/$(CONFIG)/grpc_create_jwt $(BINDIR)/$(CONFIG)/grpc_fetch_oauth2 $(BINDIR)/$(CONFIG)/grpc_print_google_default_creds_token $(BINDIR)/$(CONFIG)/grpc_verify_jwt tools_cxx: privatelibs_cxx @@ -7122,6 +7123,35 @@ endif endif +GEN_LEGAL_METADATA_CHARACTERS_SRC = \ + tools/codegen/core/gen_legal_metadata_characters.c \ + +GEN_LEGAL_METADATA_CHARACTERS_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(GEN_LEGAL_METADATA_CHARACTERS_SRC)))) +ifeq ($(NO_SECURE),true) + +# You can't build secure targets if you don't have OpenSSL. + +$(BINDIR)/$(CONFIG)/gen_legal_metadata_characters: openssl_dep_error + +else + +$(BINDIR)/$(CONFIG)/gen_legal_metadata_characters: $(GEN_LEGAL_METADATA_CHARACTERS_OBJS) $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc.a + $(E) "[LD] Linking $@" + $(Q) mkdir -p `dirname $@` + $(Q) $(LD) $(LDFLAGS) $(GEN_LEGAL_METADATA_CHARACTERS_OBJS) $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/gen_legal_metadata_characters + +endif + +$(OBJDIR)/$(CONFIG)/tools/codegen/core/gen_legal_metadata_characters.o: $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc.a +deps_gen_legal_metadata_characters: $(GEN_LEGAL_METADATA_CHARACTERS_OBJS:.o=.dep) + +ifneq ($(NO_SECURE),true) +ifneq ($(NO_DEPS),true) +-include $(GEN_LEGAL_METADATA_CHARACTERS_OBJS:.o=.dep) +endif +endif + + GPR_CMDLINE_TEST_SRC = \ test/core/support/cmdline_test.c \ diff --git a/build.json b/build.json index bd707d2e34..c6a670970b 100644 --- a/build.json +++ b/build.json @@ -1143,6 +1143,16 @@ "grpc" ] }, + { + "name": "gen_legal_metadata_characters", + "build": "tool", + "language": "c", + "src": [ + "tools/codegen/core/gen_legal_metadata_characters.c" + ], + "deps": [ + ] + }, { "name": "gpr_cmdline_test", "build": "test", diff --git a/src/core/channel/compress_filter.h b/src/core/channel/compress_filter.h index 0917e81ca4..415459bca6 100644 --- a/src/core/channel/compress_filter.h +++ b/src/core/channel/compress_filter.h @@ -36,7 +36,7 @@ #include "src/core/channel/channel_stack.h" -#define GRPC_COMPRESS_REQUEST_ALGORITHM_KEY "internal:grpc-encoding-request" +#define GRPC_COMPRESS_REQUEST_ALGORITHM_KEY "grpc-internal-encoding-request" /** Compression filter for outgoing data. * diff --git a/src/core/surface/call.c b/src/core/surface/call.c index 33f277da46..4426bbbce9 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -1046,10 +1046,11 @@ static int prepare_application_metadata(grpc_call *call, size_t count, (const gpr_uint8 *)md->value, md->value_length, 1); if (!grpc_mdstr_is_legal_header(l->md->key)) { - gpr_log(GPR_ERROR, "attempt to send invalid metadata key"); + gpr_log(GPR_ERROR, "attempt to send invalid metadata key: %s", + grpc_mdstr_as_c_string(l->md->key)); return 0; } else if (!grpc_mdstr_is_bin_suffixed(l->md->key) && - !grpc_mdstr_is_legal_header(l->md->value)) { + !grpc_mdstr_is_legal_nonbin_header(l->md->value)) { gpr_log(GPR_ERROR, "attempt to send invalid metadata value"); return 0; } diff --git a/src/core/transport/metadata.c b/src/core/transport/metadata.c index f92e87e9dd..d758351feb 100644 --- a/src/core/transport/metadata.c +++ b/src/core/transport/metadata.c @@ -681,16 +681,36 @@ void grpc_mdctx_locked_mdelem_unref(grpc_mdctx *ctx, void grpc_mdctx_unlock(grpc_mdctx *ctx) { unlock(ctx); } -int grpc_mdstr_is_legal_header(grpc_mdstr *s) { - /* TODO(ctiller): consider caching this, or computing it on construction */ +static int conforms_to(grpc_mdstr *s, const gpr_uint8 *legal_bits) { const gpr_uint8 *p = GPR_SLICE_START_PTR(s->slice); const gpr_uint8 *e = GPR_SLICE_END_PTR(s->slice); for (; p != e; p++) { - if (*p < 32 || *p > 126) return 0; + int idx = *p; + int byte = idx / 8; + int bit = idx % 8; + if ((legal_bits[byte] & (1 << bit)) == 0) return 0; } return 1; } +int grpc_mdstr_is_legal_header(grpc_mdstr *s) { + static const gpr_uint8 legal_header_bits[256 / 8] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xff, 0x03, 0xfe, 0xff, 0xff, + 0x07, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + /* TODO(ctiller): consider caching this, or computing it on construction */ + return conforms_to(s, legal_header_bits); +} + +int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s) { + static const gpr_uint8 legal_header_bits[256 / 8] = { + 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + return conforms_to(s, legal_header_bits); +} + int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s) { /* TODO(ctiller): consider caching this */ return grpc_is_binary_header((const char *)GPR_SLICE_START_PTR(s->slice), diff --git a/src/core/transport/metadata.h b/src/core/transport/metadata.h index a7af49ba55..eb17747be7 100644 --- a/src/core/transport/metadata.h +++ b/src/core/transport/metadata.h @@ -154,6 +154,7 @@ void grpc_mdelem_unref(grpc_mdelem *md); const char *grpc_mdstr_as_c_string(grpc_mdstr *s); int grpc_mdstr_is_legal_header(grpc_mdstr *s); +int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s); int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s); /* Batch mode metadata functions. diff --git a/tools/codegen/core/gen_legal_metadata_characters.c b/tools/codegen/core/gen_legal_metadata_characters.c new file mode 100644 index 0000000000..5e32d91d50 --- /dev/null +++ b/tools/codegen/core/gen_legal_metadata_characters.c @@ -0,0 +1,73 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* generates constant table for metadata.c */ + +#include +#include + +static unsigned char legal_bits[256 / 8]; + +static void legal(int x) { + int byte = x / 8; + int bit = x % 8; + legal_bits[byte] |= 1 << bit; +} + +static void dump(void) { + int i; + + printf("static const gpr_uint8 legal_header_bits[256/8] = "); + for (i = 0; i < 256 / 8; i++) + printf("%c 0x%02x", i ? ',' : '{', legal_bits[i]); + printf(" };\n"); +} + +static void clear(void) { memset(legal_bits, 0, sizeof(legal_bits)); } + +int main(void) { + int i; + + clear(); + for (i = 'a'; i <= 'z'; i++) legal(i); + for (i = 'A'; i <= 'Z'; i++) legal(i); + for (i = '0'; i <= '9'; i++) legal(i); + legal('-'); + dump(); + + clear(); + for (i = 32; i <= 126; i++) legal(i); + dump(); + + return 0; +} diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 50f078586d..142c84901d 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -237,6 +237,18 @@ "tools/codegen/core/gen_hpack_tables.c" ] }, + { + "deps": [ + "gpr", + "grpc" + ], + "headers": [], + "language": "c", + "name": "gen_legal_metadata_characters", + "src": [ + "tools/codegen/core/gen_legal_metadata_characters.c" + ] + }, { "deps": [ "gpr", diff --git a/tools/run_tests/tests.json b/tools/run_tests/tests.json index 6c06d74834..127b1dfc40 100644 --- a/tools/run_tests/tests.json +++ b/tools/run_tests/tests.json @@ -1538,6 +1538,7 @@ "posix", "windows" ], + "exclude_configs": [], "flaky": false, "language": "c++", "name": "status_test", diff --git a/vsprojects/Grpc.mak b/vsprojects/Grpc.mak index 662de784f7..3d853d9c76 100644 --- a/vsprojects/Grpc.mak +++ b/vsprojects/Grpc.mak @@ -183,6 +183,14 @@ gen_hpack_tables: gen_hpack_tables.exe echo Running gen_hpack_tables $(OUT_DIR)\gen_hpack_tables.exe +gen_legal_metadata_characters.exe: build_gpr build_grpc $(OUT_DIR) + echo Building gen_legal_metadata_characters + $(CC) $(CFLAGS) /Fo:$(OUT_DIR)\ $(REPO_ROOT)\tools\codegen\core\gen_legal_metadata_characters.c + $(LINK) $(LFLAGS) /OUT:"$(OUT_DIR)\gen_legal_metadata_characters.exe" Debug\gpr.lib Debug\grpc.lib $(LIBS) $(OUT_DIR)\gen_legal_metadata_characters.obj +gen_legal_metadata_characters: gen_legal_metadata_characters.exe + echo Running gen_legal_metadata_characters + $(OUT_DIR)\gen_legal_metadata_characters.exe + gpr_cmdline_test.exe: build_gpr_test_util build_gpr $(OUT_DIR) echo Building gpr_cmdline_test $(CC) $(CFLAGS) /Fo:$(OUT_DIR)\ $(REPO_ROOT)\test\core\support\cmdline_test.c -- cgit v1.2.3 From bc85be10ef420f35d660bc12afb47580acec3143 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 24 Aug 2015 10:36:39 -0700 Subject: Refactor default host name resolution Rephrase API's such that resolvers are constrained to be able to provide a default host given just the text of the URI channel target. This avoids needing to rewrite such details in the core library during retries, and generally makes things much saner to debug. --- src/core/channel/http_client_filter.c | 33 --------- src/core/client_config/resolver_factory.c | 6 ++ src/core/client_config/resolver_factory.h | 9 +++ src/core/client_config/resolver_registry.c | 79 ++++++++++++---------- src/core/client_config/resolver_registry.h | 15 ++-- src/core/client_config/resolvers/dns_resolver.c | 25 +++---- .../client_config/resolvers/sockaddr_resolver.c | 49 ++++++++++---- .../client_config/resolvers/zookeeper_resolver.c | 11 ++- src/core/surface/channel.c | 24 +++++++ src/core/surface/init.c | 8 +-- test/core/end2end/tests/default_host.c | 2 +- 11 files changed, 148 insertions(+), 113 deletions(-) (limited to 'src/core/channel') diff --git a/src/core/channel/http_client_filter.c b/src/core/channel/http_client_filter.c index 48c623d359..2b61d33c29 100644 --- a/src/core/channel/http_client_filter.c +++ b/src/core/channel/http_client_filter.c @@ -45,7 +45,6 @@ typedef struct call_data { grpc_linked_mdelem content_type; grpc_linked_mdelem user_agent; int sent_initial_metadata; - int sent_authority; int got_initial_metadata; grpc_stream_op_buffer *recv_ops; @@ -64,7 +63,6 @@ typedef struct channel_data { grpc_mdelem *scheme; grpc_mdelem *content_type; grpc_mdelem *status; - grpc_mdelem *default_authority; /** complete user agent mdelem */ grpc_mdelem *user_agent; } channel_data; @@ -103,7 +101,6 @@ static void hc_on_recv(void *user_data, int success) { static grpc_mdelem *client_strip_filter(void *user_data, grpc_mdelem *md) { grpc_call_element *elem = user_data; - call_data *calld = elem->call_data; channel_data *channeld = elem->channel_data; /* eat the things we'd like to set ourselves */ if (md->key == channeld->method->key) return NULL; @@ -111,10 +108,6 @@ static grpc_mdelem *client_strip_filter(void *user_data, grpc_mdelem *md) { if (md->key == channeld->te_trailers->key) return NULL; if (md->key == channeld->content_type->key) return NULL; if (md->key == channeld->user_agent->key) return NULL; - if (channeld->default_authority && - channeld->default_authority->key == md->key) { - calld->sent_authority = 1; - } return md; } @@ -138,11 +131,6 @@ static void hc_mutate_op(grpc_call_element *elem, GRPC_MDELEM_REF(channeld->method)); grpc_metadata_batch_add_head(&op->data.metadata, &calld->scheme, GRPC_MDELEM_REF(channeld->scheme)); - if (channeld->default_authority && !calld->sent_authority) { - grpc_metadata_batch_add_head( - &op->data.metadata, &calld->authority, - GRPC_MDELEM_REF(channeld->default_authority)); - } grpc_metadata_batch_add_tail(&op->data.metadata, &calld->te_trailers, GRPC_MDELEM_REF(channeld->te_trailers)); grpc_metadata_batch_add_tail(&op->data.metadata, &calld->content_type, @@ -175,7 +163,6 @@ static void init_call_elem(grpc_call_element *elem, call_data *calld = elem->call_data; calld->sent_initial_metadata = 0; calld->got_initial_metadata = 0; - calld->sent_authority = 0; calld->on_done_recv = NULL; grpc_iomgr_closure_init(&calld->hc_on_recv, hc_on_recv, elem); if (initial_op) hc_mutate_op(elem, initial_op); @@ -257,8 +244,6 @@ static grpc_mdstr *user_agent_from_args(grpc_mdctx *mdctx, static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, const grpc_channel_args *channel_args, grpc_mdctx *mdctx, int is_first, int is_last) { - size_t i; - /* grab pointers to our data from the channel element */ channel_data *channeld = elem->channel_data; @@ -267,21 +252,6 @@ static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, path */ GPR_ASSERT(!is_last); - channeld->default_authority = NULL; - if (channel_args) { - for (i = 0; i < channel_args->num_args; i++) { - if (0 == strcmp(channel_args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) { - if (channel_args->args[i].type != GRPC_ARG_STRING) { - gpr_log(GPR_ERROR, "%s: must be an string", - GRPC_ARG_DEFAULT_AUTHORITY); - } else { - channeld->default_authority = grpc_mdelem_from_strings( - mdctx, ":authority", channel_args->args[i].value.string); - } - } - } - } - /* initialize members */ channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers"); channeld->method = grpc_mdelem_from_strings(mdctx, ":method", "POST"); @@ -306,9 +276,6 @@ static void destroy_channel_elem(grpc_channel_element *elem) { GRPC_MDELEM_UNREF(channeld->content_type); GRPC_MDELEM_UNREF(channeld->status); GRPC_MDELEM_UNREF(channeld->user_agent); - if (channeld->default_authority) { - GRPC_MDELEM_UNREF(channeld->default_authority); - } } const grpc_channel_filter grpc_http_client_filter = { diff --git a/src/core/client_config/resolver_factory.c b/src/core/client_config/resolver_factory.c index 6721977e21..bf631cefd1 100644 --- a/src/core/client_config/resolver_factory.c +++ b/src/core/client_config/resolver_factory.c @@ -48,3 +48,9 @@ grpc_resolver *grpc_resolver_factory_create_resolver( if (!factory) return NULL; return factory->vtable->create_resolver(factory, uri, subchannel_factory); } + +char *grpc_resolver_factory_get_default_authority( + grpc_resolver_factory *factory, grpc_uri *uri) { + if (!factory) return NULL; + return factory->vtable->get_default_authority(factory, uri); +} diff --git a/src/core/client_config/resolver_factory.h b/src/core/client_config/resolver_factory.h index c5d85499c6..73a0564230 100644 --- a/src/core/client_config/resolver_factory.h +++ b/src/core/client_config/resolver_factory.h @@ -54,6 +54,10 @@ struct grpc_resolver_factory_vtable { grpc_resolver *(*create_resolver)( grpc_resolver_factory *factory, grpc_uri *uri, grpc_subchannel_factory *subchannel_factory); + + char *(*get_default_authority)(grpc_resolver_factory *factory, grpc_uri *uri); + + const char *scheme; }; void grpc_resolver_factory_ref(grpc_resolver_factory *resolver); @@ -64,4 +68,9 @@ grpc_resolver *grpc_resolver_factory_create_resolver( grpc_resolver_factory *factory, grpc_uri *uri, grpc_subchannel_factory *subchannel_factory); +/** Return a (freshly allocated with gpr_malloc) string representing + the default authority to use for this scheme. */ +char *grpc_resolver_factory_get_default_authority( + grpc_resolver_factory *factory, grpc_uri *uri); + #endif /* GRPC_INTERNAL_CORE_CONFIG_RESOLVER_FACTORY_H */ diff --git a/src/core/client_config/resolver_registry.c b/src/core/client_config/resolver_registry.c index 16be2da994..3aff4630fb 100644 --- a/src/core/client_config/resolver_registry.c +++ b/src/core/client_config/resolver_registry.c @@ -41,41 +41,33 @@ #define MAX_RESOLVERS 10 -typedef struct { - char *scheme; - grpc_resolver_factory *factory; -} registered_resolver; - -static registered_resolver g_all_of_the_resolvers[MAX_RESOLVERS]; +static grpc_resolver_factory *g_all_of_the_resolvers[MAX_RESOLVERS]; static int g_number_of_resolvers = 0; -static char *g_default_resolver_scheme; +static char *g_default_resolver_prefix; -void grpc_resolver_registry_init(const char *default_resolver_scheme) { +void grpc_resolver_registry_init(const char *default_resolver_prefix) { g_number_of_resolvers = 0; - g_default_resolver_scheme = gpr_strdup(default_resolver_scheme); + g_default_resolver_prefix = gpr_strdup(default_resolver_prefix); } void grpc_resolver_registry_shutdown(void) { int i; for (i = 0; i < g_number_of_resolvers; i++) { - gpr_free(g_all_of_the_resolvers[i].scheme); - grpc_resolver_factory_unref(g_all_of_the_resolvers[i].factory); + grpc_resolver_factory_unref(g_all_of_the_resolvers[i]); } - gpr_free(g_default_resolver_scheme); + gpr_free(g_default_resolver_prefix); } -void grpc_register_resolver_type(const char *scheme, - grpc_resolver_factory *factory) { +void grpc_register_resolver_type(grpc_resolver_factory *factory) { int i; for (i = 0; i < g_number_of_resolvers; i++) { - GPR_ASSERT(0 != strcmp(scheme, g_all_of_the_resolvers[i].scheme)); + GPR_ASSERT(0 != strcmp(factory->vtable->scheme, + g_all_of_the_resolvers[i]->vtable->scheme)); } GPR_ASSERT(g_number_of_resolvers != MAX_RESOLVERS); - g_all_of_the_resolvers[g_number_of_resolvers].scheme = gpr_strdup(scheme); grpc_resolver_factory_ref(factory); - g_all_of_the_resolvers[g_number_of_resolvers].factory = factory; - g_number_of_resolvers++; + g_all_of_the_resolvers[g_number_of_resolvers++] = factory; } static grpc_resolver_factory *lookup_factory(grpc_uri *uri) { @@ -85,40 +77,53 @@ static grpc_resolver_factory *lookup_factory(grpc_uri *uri) { if (!uri) return NULL; for (i = 0; i < g_number_of_resolvers; i++) { - if (0 == strcmp(uri->scheme, g_all_of_the_resolvers[i].scheme)) { - return g_all_of_the_resolvers[i].factory; + if (0 == strcmp(uri->scheme, g_all_of_the_resolvers[i]->vtable->scheme)) { + return g_all_of_the_resolvers[i]; } } return NULL; } -grpc_resolver *grpc_resolver_create( - const char *name, grpc_subchannel_factory *subchannel_factory) { - grpc_uri *uri; +static grpc_resolver_factory *resolve_factory(const char *target, + grpc_uri **uri) { char *tmp; grpc_resolver_factory *factory = NULL; - grpc_resolver *resolver; - - uri = grpc_uri_parse(name, 1); - factory = lookup_factory(uri); - if (factory == NULL && g_default_resolver_scheme != NULL) { - grpc_uri_destroy(uri); - gpr_asprintf(&tmp, "%s%s", g_default_resolver_scheme, name); - uri = grpc_uri_parse(tmp, 1); - factory = lookup_factory(uri); + + *uri = grpc_uri_parse(target, 1); + factory = lookup_factory(*uri); + if (factory == NULL && g_default_resolver_prefix != NULL) { + grpc_uri_destroy(*uri); + gpr_asprintf(&tmp, "%s%s", g_default_resolver_prefix, target); + *uri = grpc_uri_parse(tmp, 1); + factory = lookup_factory(*uri); if (factory == NULL) { - grpc_uri_destroy(grpc_uri_parse(name, 0)); + grpc_uri_destroy(grpc_uri_parse(target, 0)); grpc_uri_destroy(grpc_uri_parse(tmp, 0)); - gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", name, tmp); + gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", target, tmp); } gpr_free(tmp); } else if (factory == NULL) { - grpc_uri_destroy(grpc_uri_parse(name, 0)); - gpr_log(GPR_ERROR, "don't know how to resolve '%s'", name); + grpc_uri_destroy(grpc_uri_parse(target, 0)); + gpr_log(GPR_ERROR, "don't know how to resolve '%s'", target); } - resolver = + return factory; +} + +grpc_resolver *grpc_resolver_create( + const char *target, grpc_subchannel_factory *subchannel_factory) { + grpc_uri *uri = NULL; + grpc_resolver_factory *factory = resolve_factory(target, &uri); + grpc_resolver *resolver = grpc_resolver_factory_create_resolver(factory, uri, subchannel_factory); grpc_uri_destroy(uri); return resolver; } + +char *grpc_get_default_authority(const char *target) { + grpc_uri *uri = NULL; + grpc_resolver_factory *factory = resolve_factory(target, &uri); + char *authority = grpc_resolver_factory_get_default_authority(factory, uri); + grpc_uri_destroy(uri); + return authority; +} diff --git a/src/core/client_config/resolver_registry.h b/src/core/client_config/resolver_registry.h index 31aa47620a..5a7193b7ae 100644 --- a/src/core/client_config/resolver_registry.h +++ b/src/core/client_config/resolver_registry.h @@ -44,19 +44,22 @@ void grpc_resolver_registry_shutdown(void); If \a priority is greater than zero, then the resolver will be eligible to resolve names that are passed in with no scheme. Higher priority resolvers will be tried before lower priority schemes. */ -void grpc_register_resolver_type(const char *scheme, - grpc_resolver_factory *factory); +void grpc_register_resolver_type(grpc_resolver_factory *factory); -/** Create a resolver given \a name. - First tries to parse \a name as a URI. If this succeeds, tries +/** Create a resolver given \a target. + First tries to parse \a target as a URI. If this succeeds, tries to locate a registered resolver factory based on the URI scheme. If parsing or location fails, prefixes default_prefix from - grpc_resolver_registry_init to name, and tries again (if default_prefix + grpc_resolver_registry_init to target, and tries again (if default_prefix was not NULL). If a resolver factory was found, use it to instantiate a resolver and return it. If a resolver factory was not found, return NULL. */ grpc_resolver *grpc_resolver_create( - const char *name, grpc_subchannel_factory *subchannel_factory); + const char *target, grpc_subchannel_factory *subchannel_factory); + +/** Given a target, return a (freshly allocated with gpr_malloc) string + representing the default authority to pass from a client. */ +char *grpc_get_default_authority(const char *target); #endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVER_REGISTRY_H */ diff --git a/src/core/client_config/resolvers/dns_resolver.c b/src/core/client_config/resolvers/dns_resolver.c index 7b35b7902f..84643c464a 100644 --- a/src/core/client_config/resolvers/dns_resolver.c +++ b/src/core/client_config/resolvers/dns_resolver.c @@ -203,9 +203,6 @@ static grpc_resolver *dns_create( grpc_subchannel_factory *subchannel_factory) { dns_resolver *r; const char *path = uri->path; - grpc_arg default_host_arg; - char *host; - char *port; if (0 != strcmp(uri->authority, "")) { gpr_log(GPR_ERROR, "authority based uri's not supported"); @@ -214,17 +211,6 @@ static grpc_resolver *dns_create( if (path[0] == '/') ++path; - gpr_split_host_port(path, &host, &port); - - default_host_arg.type = GRPC_ARG_STRING; - default_host_arg.key = GRPC_ARG_DEFAULT_AUTHORITY; - default_host_arg.value.string = host; - subchannel_factory = grpc_subchannel_factory_add_channel_arg( - subchannel_factory, &default_host_arg); - - gpr_free(host); - gpr_free(port); - r = gpr_malloc(sizeof(dns_resolver)); memset(r, 0, sizeof(*r)); gpr_ref_init(&r->refs, 1); @@ -233,6 +219,7 @@ static grpc_resolver *dns_create( r->name = gpr_strdup(path); r->default_port = gpr_strdup(default_port); r->subchannel_factory = subchannel_factory; + grpc_subchannel_factory_ref(subchannel_factory); r->lb_policy_factory = lb_policy_factory; return &r->base; } @@ -252,8 +239,16 @@ static grpc_resolver *dns_factory_create_resolver( subchannel_factory); } +char *dns_factory_get_default_host_name(grpc_resolver_factory *factory, + grpc_uri *uri) { + const char *path = uri->path; + if (path[0] == '/') ++path; + return gpr_strdup(path); +} + static const grpc_resolver_factory_vtable dns_factory_vtable = { - dns_factory_ref, dns_factory_unref, dns_factory_create_resolver}; + dns_factory_ref, dns_factory_unref, dns_factory_create_resolver, + dns_factory_get_default_host_name, "dns"}; static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable}; grpc_resolver_factory *grpc_dns_resolver_factory_create() { diff --git a/src/core/client_config/resolvers/sockaddr_resolver.c b/src/core/client_config/resolvers/sockaddr_resolver.c index 74584e7e2c..eeafef4174 100644 --- a/src/core/client_config/resolvers/sockaddr_resolver.c +++ b/src/core/client_config/resolvers/sockaddr_resolver.c @@ -156,8 +156,29 @@ static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { return 1; } + +static char *unix_get_default_authority(grpc_resolver_factory *factory, + grpc_uri *uri) { + return gpr_strdup("localhost"); +} #endif +static char *ip_get_default_authority(grpc_uri *uri) { + const char *path = uri->path; + if (path[0] == '/') ++path; + return gpr_strdup(path); +} + +static char *ipv4_get_default_authority(grpc_resolver_factory *factory, + grpc_uri *uri) { + return ip_get_default_authority(uri); +} + +static char *ipv6_get_default_authority(grpc_resolver_factory *factory, + grpc_uri *uri) { + return ip_get_default_authority(uri); +} + static int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { const char *host_port = uri->path; char *host; @@ -276,20 +297,20 @@ static void sockaddr_factory_ref(grpc_resolver_factory *factory) {} static void sockaddr_factory_unref(grpc_resolver_factory *factory) {} -#define DECL_FACTORY(name) \ - static grpc_resolver *name##_factory_create_resolver( \ - grpc_resolver_factory *factory, grpc_uri *uri, \ - grpc_subchannel_factory *subchannel_factory) { \ - return sockaddr_create(uri, grpc_create_pick_first_lb_policy, \ - subchannel_factory, parse_##name); \ - } \ - static const grpc_resolver_factory_vtable name##_factory_vtable = { \ - sockaddr_factory_ref, sockaddr_factory_unref, \ - name##_factory_create_resolver}; \ - static grpc_resolver_factory name##_resolver_factory = { \ - &name##_factory_vtable}; \ - grpc_resolver_factory *grpc_##name##_resolver_factory_create() { \ - return &name##_resolver_factory; \ +#define DECL_FACTORY(name) \ + static grpc_resolver *name##_factory_create_resolver( \ + grpc_resolver_factory *factory, grpc_uri *uri, \ + grpc_subchannel_factory *subchannel_factory) { \ + return sockaddr_create(uri, grpc_create_pick_first_lb_policy, \ + subchannel_factory, parse_##name); \ + } \ + static const grpc_resolver_factory_vtable name##_factory_vtable = { \ + sockaddr_factory_ref, sockaddr_factory_unref, \ + name##_factory_create_resolver, name##_get_default_authority, #name}; \ + static grpc_resolver_factory name##_resolver_factory = { \ + &name##_factory_vtable}; \ + grpc_resolver_factory *grpc_##name##_resolver_factory_create() { \ + return &name##_resolver_factory; \ } #ifdef GPR_POSIX_SOCKET diff --git a/src/core/client_config/resolvers/zookeeper_resolver.c b/src/core/client_config/resolvers/zookeeper_resolver.c index acb2ba136e..da399f9954 100644 --- a/src/core/client_config/resolvers/zookeeper_resolver.c +++ b/src/core/client_config/resolvers/zookeeper_resolver.c @@ -467,8 +467,7 @@ static grpc_resolver *zookeeper_create( } static void zookeeper_plugin_init() { - grpc_register_resolver_type("zookeeper", - grpc_zookeeper_resolver_factory_create()); + grpc_register_resolver_type(grpc_zookeeper_resolver_factory_create()); } void grpc_zookeeper_register() { @@ -483,6 +482,11 @@ static void zookeeper_factory_ref(grpc_resolver_factory *factory) {} static void zookeeper_factory_unref(grpc_resolver_factory *factory) {} +static char *zookeeper_factory_get_default_hostname( + grpc_resolver_factory *factory, grpc_uri *uri) { + return NULL; +} + static grpc_resolver *zookeeper_factory_create_resolver( grpc_resolver_factory *factory, grpc_uri *uri, grpc_subchannel_factory *subchannel_factory) { @@ -492,7 +496,8 @@ static grpc_resolver *zookeeper_factory_create_resolver( static const grpc_resolver_factory_vtable zookeeper_factory_vtable = { zookeeper_factory_ref, zookeeper_factory_unref, - zookeeper_factory_create_resolver}; + zookeeper_factory_create_resolver, zookeeper_factory_get_default_hostname, + "zookeeper"}; static grpc_resolver_factory zookeeper_resolver_factory = { &zookeeper_factory_vtable}; diff --git a/src/core/surface/channel.c b/src/core/surface/channel.c index e50251566d..7378929ca4 100644 --- a/src/core/surface/channel.c +++ b/src/core/surface/channel.c @@ -40,6 +40,7 @@ #include #include +#include "src/core/client_config/resolver_registry.h" #include "src/core/iomgr/iomgr.h" #include "src/core/support/string.h" #include "src/core/surface/call.h" @@ -70,6 +71,7 @@ struct grpc_channel { grpc_mdstr *grpc_message_string; grpc_mdstr *path_string; grpc_mdstr *authority_string; + grpc_mdelem *default_authority; /** mdelem for grpc-status: 0 thru grpc-status: 2 */ grpc_mdelem *grpc_status_elem[NUM_CACHED_STATUS_ELEMS]; @@ -134,10 +136,27 @@ grpc_channel *grpc_channel_create_from_filters( } else { channel->max_message_length = args->args[i].value.integer; } + } else if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) { + if (args->args[i].type != GRPC_ARG_STRING) { + gpr_log(GPR_ERROR, "%s: must be an string", + GRPC_ARG_DEFAULT_AUTHORITY); + } else { + channel->default_authority = grpc_mdelem_from_strings( + mdctx, ":authority", args->args[i].value.string); + } } } } + if (channel->is_client && channel->default_authority == NULL) { + char *default_authority = grpc_get_default_authority(target); + if (default_authority) { + channel->default_authority = grpc_mdelem_from_strings( + channel->metadata_context, ":authority", default_authority); + } + gpr_free(default_authority); + } + grpc_channel_stack_init(filters, num_filters, channel, args, channel->metadata_context, CHANNEL_STACK_FROM_CHANNEL(channel)); @@ -161,6 +180,8 @@ static grpc_call *grpc_channel_create_call_internal( send_metadata[num_metadata++] = path_mdelem; if (authority_mdelem != NULL) { send_metadata[num_metadata++] = authority_mdelem; + } else if (channel->default_authority != NULL) { + send_metadata[num_metadata++] = GRPC_MDELEM_REF(channel->default_authority); } return grpc_call_create(channel, parent_call, propagation_mask, cq, NULL, @@ -251,6 +272,9 @@ static void destroy_channel(void *p, int ok) { } gpr_free(rc); } + if (channel->default_authority != NULL) { + GRPC_MDELEM_UNREF(channel->default_authority); + } grpc_mdctx_unref(channel->metadata_context); gpr_mu_destroy(&channel->registered_call_mu); gpr_free(channel->target); diff --git a/src/core/surface/init.c b/src/core/surface/init.c index d9044549f2..0d48cd42d7 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -86,11 +86,11 @@ void grpc_init(void) { if (++g_initializations == 1) { gpr_time_init(); grpc_resolver_registry_init("dns:///"); - grpc_register_resolver_type("dns", grpc_dns_resolver_factory_create()); - grpc_register_resolver_type("ipv4", grpc_ipv4_resolver_factory_create()); - grpc_register_resolver_type("ipv6", grpc_ipv6_resolver_factory_create()); + grpc_register_resolver_type(grpc_dns_resolver_factory_create()); + grpc_register_resolver_type(grpc_ipv4_resolver_factory_create()); + grpc_register_resolver_type(grpc_ipv6_resolver_factory_create()); #ifdef GPR_POSIX_SOCKET - grpc_register_resolver_type("unix", grpc_unix_resolver_factory_create()); + grpc_register_resolver_type(grpc_unix_resolver_factory_create()); #endif grpc_register_tracer("channel", &grpc_trace_channel); grpc_register_tracer("surface", &grpc_surface_trace); diff --git a/test/core/end2end/tests/default_host.c b/test/core/end2end/tests/default_host.c index 97c19db331..57f65b834b 100644 --- a/test/core/end2end/tests/default_host.c +++ b/test/core/end2end/tests/default_host.c @@ -201,7 +201,7 @@ static void simple_request_body(grpc_end2end_test_fixture f) { GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); GPR_ASSERT(0 == strcmp(details, "xyz")); GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "localhost")); + GPR_ASSERT(0 == strncmp(call_details.host, "localhost", 9)); GPR_ASSERT(was_cancelled == 1); gpr_free(details); -- cgit v1.2.3 From 37b0044844a82a43ef64b581340db00bc3af5095 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 4 Sep 2015 08:39:14 -0700 Subject: Fix a case where :status could be returned to applications --- src/core/channel/http_client_filter.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'src/core/channel') diff --git a/src/core/channel/http_client_filter.c b/src/core/channel/http_client_filter.c index 2b61d33c29..ec832a0367 100644 --- a/src/core/channel/http_client_filter.c +++ b/src/core/channel/http_client_filter.c @@ -85,16 +85,14 @@ static grpc_mdelem *client_filter(void *user_data, grpc_mdelem *md) { static void hc_on_recv(void *user_data, int success) { grpc_call_element *elem = user_data; call_data *calld = elem->call_data; - if (success) { - size_t i; - size_t nops = calld->recv_ops->nops; - grpc_stream_op *ops = calld->recv_ops->ops; - for (i = 0; i < nops; i++) { - grpc_stream_op *op = &ops[i]; - if (op->type != GRPC_OP_METADATA) continue; - calld->got_initial_metadata = 1; - grpc_metadata_batch_filter(&op->data.metadata, client_filter, elem); - } + size_t i; + size_t nops = calld->recv_ops->nops; + grpc_stream_op *ops = calld->recv_ops->ops; + for (i = 0; i < nops; i++) { + grpc_stream_op *op = &ops[i]; + if (op->type != GRPC_OP_METADATA) continue; + calld->got_initial_metadata = 1; + grpc_metadata_batch_filter(&op->data.metadata, client_filter, elem); } calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); } -- cgit v1.2.3