Skip to content

Commit

Permalink
Adding Fake headers for header matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
donnadionne committed Jul 15, 2020
1 parent 9b215e9 commit 8c013bf
Show file tree
Hide file tree
Showing 20 changed files with 413 additions and 42 deletions.
16 changes: 16 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,20 @@ grpc_cc_library(
],
)

grpc_cc_library(
name = "grpc_http_util",
srcs = [
"src/core/ext/filters/http/client/util.cc",
],
hdrs = [
"src/core/ext/filters/http/client/util.h",
],
language = "c++",
deps = [
"grpc_base",
],
)

grpc_cc_library(
name = "grpc_http_filters",
srcs = [
Expand All @@ -1183,6 +1197,7 @@ grpc_cc_library(
language = "c++",
deps = [
"grpc_base",
"grpc_http_util",
"grpc_message_size_filter",
],
)
Expand Down Expand Up @@ -1462,6 +1477,7 @@ grpc_cc_library(
deps = [
"grpc_base",
"grpc_client_channel",
"grpc_http_util",
"grpc_resolver_xds_header",
"grpc_xds_api_header",
],
Expand Down
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ config("grpc_config") {
"src/core/ext/filters/deadline/deadline_filter.h",
"src/core/ext/filters/http/client/http_client_filter.cc",
"src/core/ext/filters/http/client/http_client_filter.h",
"src/core/ext/filters/http/client/util.cc",
"src/core/ext/filters/http/client/util.h",
"src/core/ext/filters/http/client_authority_filter.cc",
"src/core/ext/filters/http/client_authority_filter.h",
"src/core/ext/filters/http/http_filters_plugin.cc",
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ add_library(grpc
src/core/ext/filters/client_idle/client_idle_filter.cc
src/core/ext/filters/deadline/deadline_filter.cc
src/core/ext/filters/http/client/http_client_filter.cc
src/core/ext/filters/http/client/util.cc
src/core/ext/filters/http/client_authority_filter.cc
src/core/ext/filters/http/http_filters_plugin.cc
src/core/ext/filters/http/message_compress/message_compress_filter.cc
Expand Down Expand Up @@ -2140,6 +2141,7 @@ add_library(grpc_unsecure
src/core/ext/filters/client_idle/client_idle_filter.cc
src/core/ext/filters/deadline/deadline_filter.cc
src/core/ext/filters/http/client/http_client_filter.cc
src/core/ext/filters/http/client/util.cc
src/core/ext/filters/http/client_authority_filter.cc
src/core/ext/filters/http/http_filters_plugin.cc
src/core/ext/filters/http/message_compress/message_compress_filter.cc
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3462,6 +3462,7 @@ LIBGRPC_SRC = \
src/core/ext/filters/client_idle/client_idle_filter.cc \
src/core/ext/filters/deadline/deadline_filter.cc \
src/core/ext/filters/http/client/http_client_filter.cc \
src/core/ext/filters/http/client/util.cc \
src/core/ext/filters/http/client_authority_filter.cc \
src/core/ext/filters/http/http_filters_plugin.cc \
src/core/ext/filters/http/message_compress/message_compress_filter.cc \
Expand Down Expand Up @@ -4175,6 +4176,7 @@ LIBGRPC_UNSECURE_SRC = \
src/core/ext/filters/client_idle/client_idle_filter.cc \
src/core/ext/filters/deadline/deadline_filter.cc \
src/core/ext/filters/http/client/http_client_filter.cc \
src/core/ext/filters/http/client/util.cc \
src/core/ext/filters/http/client_authority_filter.cc \
src/core/ext/filters/http/http_filters_plugin.cc \
src/core/ext/filters/http/message_compress/message_compress_filter.cc \
Expand Down
4 changes: 4 additions & 0 deletions build_autogenerated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ libs:
- src/core/ext/filters/client_channel/xds/xds_client_stats.h
- src/core/ext/filters/deadline/deadline_filter.h
- src/core/ext/filters/http/client/http_client_filter.h
- src/core/ext/filters/http/client/util.h
- src/core/ext/filters/http/client_authority_filter.h
- src/core/ext/filters/http/message_compress/message_compress_filter.h
- src/core/ext/filters/http/message_compress/message_decompress_filter.h
Expand Down Expand Up @@ -874,6 +875,7 @@ libs:
- src/core/ext/filters/client_idle/client_idle_filter.cc
- src/core/ext/filters/deadline/deadline_filter.cc
- src/core/ext/filters/http/client/http_client_filter.cc
- src/core/ext/filters/http/client/util.cc
- src/core/ext/filters/http/client_authority_filter.cc
- src/core/ext/filters/http/http_filters_plugin.cc
- src/core/ext/filters/http/message_compress/message_compress_filter.cc
Expand Down Expand Up @@ -1487,6 +1489,7 @@ libs:
- src/core/ext/filters/client_channel/xds/xds_client_stats.h
- src/core/ext/filters/deadline/deadline_filter.h
- src/core/ext/filters/http/client/http_client_filter.h
- src/core/ext/filters/http/client/util.h
- src/core/ext/filters/http/client_authority_filter.h
- src/core/ext/filters/http/message_compress/message_compress_filter.h
- src/core/ext/filters/http/message_compress/message_decompress_filter.h
Expand Down Expand Up @@ -1871,6 +1874,7 @@ libs:
- src/core/ext/filters/client_idle/client_idle_filter.cc
- src/core/ext/filters/deadline/deadline_filter.cc
- src/core/ext/filters/http/client/http_client_filter.cc
- src/core/ext/filters/http/client/util.cc
- src/core/ext/filters/http/client_authority_filter.cc
- src/core/ext/filters/http/http_filters_plugin.cc
- src/core/ext/filters/http/message_compress/message_compress_filter.cc
Expand Down
1 change: 1 addition & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ if test "$PHP_GRPC" != "no"; then
src/core/ext/filters/client_idle/client_idle_filter.cc \
src/core/ext/filters/deadline/deadline_filter.cc \
src/core/ext/filters/http/client/http_client_filter.cc \
src/core/ext/filters/http/client/util.cc \
src/core/ext/filters/http/client_authority_filter.cc \
src/core/ext/filters/http/http_filters_plugin.cc \
src/core/ext/filters/http/message_compress/message_compress_filter.cc \
Expand Down
1 change: 1 addition & 0 deletions config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ if (PHP_GRPC != "no") {
"src\\core\\ext\\filters\\client_idle\\client_idle_filter.cc " +
"src\\core\\ext\\filters\\deadline\\deadline_filter.cc " +
"src\\core\\ext\\filters\\http\\client\\http_client_filter.cc " +
"src\\core\\ext\\filters\\http\\client\\util.cc " +
"src\\core\\ext\\filters\\http\\client_authority_filter.cc " +
"src\\core\\ext\\filters\\http\\http_filters_plugin.cc " +
"src\\core\\ext\\filters\\http\\message_compress\\message_compress_filter.cc " +
Expand Down
2 changes: 2 additions & 0 deletions gRPC-C++.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ Pod::Spec.new do |s|
'src/core/ext/filters/client_channel/xds/xds_client_stats.h',
'src/core/ext/filters/deadline/deadline_filter.h',
'src/core/ext/filters/http/client/http_client_filter.h',
'src/core/ext/filters/http/client/util.h',
'src/core/ext/filters/http/client_authority_filter.h',
'src/core/ext/filters/http/message_compress/message_compress_filter.h',
'src/core/ext/filters/http/message_compress/message_decompress_filter.h',
Expand Down Expand Up @@ -829,6 +830,7 @@ Pod::Spec.new do |s|
'src/core/ext/filters/client_channel/xds/xds_client_stats.h',
'src/core/ext/filters/deadline/deadline_filter.h',
'src/core/ext/filters/http/client/http_client_filter.h',
'src/core/ext/filters/http/client/util.h',
'src/core/ext/filters/http/client_authority_filter.h',
'src/core/ext/filters/http/message_compress/message_compress_filter.h',
'src/core/ext/filters/http/message_compress/message_decompress_filter.h',
Expand Down
3 changes: 3 additions & 0 deletions gRPC-Core.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ Pod::Spec.new do |s|
'src/core/ext/filters/deadline/deadline_filter.h',
'src/core/ext/filters/http/client/http_client_filter.cc',
'src/core/ext/filters/http/client/http_client_filter.h',
'src/core/ext/filters/http/client/util.cc',
'src/core/ext/filters/http/client/util.h',
'src/core/ext/filters/http/client_authority_filter.cc',
'src/core/ext/filters/http/client_authority_filter.h',
'src/core/ext/filters/http/http_filters_plugin.cc',
Expand Down Expand Up @@ -1293,6 +1295,7 @@ Pod::Spec.new do |s|
'src/core/ext/filters/client_channel/xds/xds_client_stats.h',
'src/core/ext/filters/deadline/deadline_filter.h',
'src/core/ext/filters/http/client/http_client_filter.h',
'src/core/ext/filters/http/client/util.h',
'src/core/ext/filters/http/client_authority_filter.h',
'src/core/ext/filters/http/message_compress/message_compress_filter.h',
'src/core/ext/filters/http/message_compress/message_decompress_filter.h',
Expand Down
2 changes: 2 additions & 0 deletions grpc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ Gem::Specification.new do |s|
s.files += %w( src/core/ext/filters/deadline/deadline_filter.h )
s.files += %w( src/core/ext/filters/http/client/http_client_filter.cc )
s.files += %w( src/core/ext/filters/http/client/http_client_filter.h )
s.files += %w( src/core/ext/filters/http/client/util.cc )
s.files += %w( src/core/ext/filters/http/client/util.h )
s.files += %w( src/core/ext/filters/http/client_authority_filter.cc )
s.files += %w( src/core/ext/filters/http/client_authority_filter.h )
s.files += %w( src/core/ext/filters/http/http_filters_plugin.cc )
Expand Down
2 changes: 2 additions & 0 deletions grpc.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@
'src/core/ext/filters/client_idle/client_idle_filter.cc',
'src/core/ext/filters/deadline/deadline_filter.cc',
'src/core/ext/filters/http/client/http_client_filter.cc',
'src/core/ext/filters/http/client/util.cc',
'src/core/ext/filters/http/client_authority_filter.cc',
'src/core/ext/filters/http/http_filters_plugin.cc',
'src/core/ext/filters/http/message_compress/message_compress_filter.cc',
Expand Down Expand Up @@ -1078,6 +1079,7 @@
'src/core/ext/filters/client_idle/client_idle_filter.cc',
'src/core/ext/filters/deadline/deadline_filter.cc',
'src/core/ext/filters/http/client/http_client_filter.cc',
'src/core/ext/filters/http/client/util.cc',
'src/core/ext/filters/http/client_authority_filter.cc',
'src/core/ext/filters/http/http_filters_plugin.cc',
'src/core/ext/filters/http/message_compress/message_compress_filter.cc',
Expand Down
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@
<file baseinstalldir="/" name="src/core/ext/filters/deadline/deadline_filter.h" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/http/client/http_client_filter.cc" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/http/client/http_client_filter.h" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/http/client/util.cc" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/http/client/util.h" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/http/client_authority_filter.cc" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/http/client_authority_filter.h" role="src" />
<file baseinstalldir="/" name="src/core/ext/filters/http/http_filters_plugin.cc" role="src" />
Expand Down
56 changes: 44 additions & 12 deletions src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "src/core/ext/filters/client_channel/lb_policy_registry.h"
#include "src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h"
#include "src/core/ext/filters/client_channel/xds/xds_api.h"
#include "src/core/ext/filters/http/client/util.h"
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gprpp/orphanable.h"
Expand Down Expand Up @@ -117,14 +118,18 @@ class XdsRoutingLb : public LoadBalancingPolicy {
// Maintains an ordered xds route table as provided by RDS response.
using RouteTable = std::vector<Route>;

explicit RoutePicker(RouteTable route_table,
RefCountedPtr<XdsRoutingLbConfig> config)
: route_table_(std::move(route_table)), config_(std::move(config)) {}
RoutePicker(RouteTable route_table, std::string user_agent,
RefCountedPtr<XdsRoutingLbConfig> config)
: route_table_(std::move(route_table)),
user_agent_(std::move(user_agent)),
config_(std::move(config)) {}

PickResult Pick(PickArgs args) override;

private:
RouteTable route_table_;
// Storing user_agent generated from args from http layer.
std::string user_agent_;
// Take a reference to config so that we can use
// XdsApi::RdsUpdate::RdsRoute::Matchers from it.
RefCountedPtr<XdsRoutingLbConfig> config_;
Expand Down Expand Up @@ -213,6 +218,9 @@ class XdsRoutingLb : public LoadBalancingPolicy {

// Children.
std::map<std::string, OrphanablePtr<XdsRoutingChild>> actions_;

// Storing user_agent generated from args from http layer.
std::string user_agent_;
};

//
Expand Down Expand Up @@ -254,8 +262,22 @@ bool PathMatch(

bool HeaderMatchHelper(
const XdsApi::RdsUpdate::RdsRoute::Matchers::HeaderMatcher& header_matcher,
LoadBalancingPolicy::MetadataInterface* initial_metadata) {
auto value = GetMetadataValue(header_matcher.name, initial_metadata);
LoadBalancingPolicy::MetadataInterface* initial_metadata,
const std::string& user_agent, absl::string_view deadline) {
absl::optional<absl::string_view> value;
if (header_matcher.name == "grpc-tags-bin" ||
header_matcher.name == "grpc-trace-bin" ||
header_matcher.name == "grpc-previous-rpc-attempts") {
value = absl::nullopt;
} else if (header_matcher.name == "content-type") {
value = "application/grpc";
} else if (header_matcher.name == "user-agent") {
value = user_agent;
} else if (header_matcher.name == "grpc-timeout") {
value = deadline;
} else {
value = GetMetadataValue(header_matcher.name, initial_metadata);
}
if (!value.has_value()) {
if (header_matcher.type == XdsApi::RdsUpdate::RdsRoute::Matchers::
HeaderMatcher::HeaderMatcherType::PRESENT) {
Expand Down Expand Up @@ -293,11 +315,13 @@ bool HeaderMatchHelper(
}

bool HeadersMatch(
LoadBalancingPolicy::PickArgs args,
const std::vector<XdsApi::RdsUpdate::RdsRoute::Matchers::HeaderMatcher>&
header_matchers) {
header_matchers,
LoadBalancingPolicy::MetadataInterface* initial_metadata,
const std::string& user_agent, absl::string_view deadline) {
for (const auto& header_matcher : header_matchers) {
bool match = HeaderMatchHelper(header_matcher, args.initial_metadata);
bool match = HeaderMatchHelper(header_matcher, initial_metadata, user_agent,
deadline);
if (header_matcher.invert_match) match = !match;
if (!match) return false;
}
Expand All @@ -315,11 +339,16 @@ XdsRoutingLb::PickResult XdsRoutingLb::RoutePicker::Pick(PickArgs args) {
// Path matching.
if (!PathMatch(args.path, route.matchers->path_matcher)) continue;
// Header Matching.
if (!HeadersMatch(args, route.matchers->header_matchers)) continue;
if (!HeadersMatch(route.matchers->header_matchers, args.initial_metadata,
user_agent_,
args.call_state->ExperimentalGetCallAttribute(
kCallAttributeDeadline)))
continue;
// Match fraction check
if (route.matchers->fraction_per_million.has_value() &&
!UnderFraction(route.matchers->fraction_per_million.value()))
!UnderFraction(route.matchers->fraction_per_million.value())) {
continue;
}
// Found a match
return route.picker->Pick(args);
}
Expand All @@ -336,7 +365,9 @@ XdsRoutingLb::PickResult XdsRoutingLb::RoutePicker::Pick(PickArgs args) {
// XdsRoutingLb
//

XdsRoutingLb::XdsRoutingLb(Args args) : LoadBalancingPolicy(std::move(args)) {}
XdsRoutingLb::XdsRoutingLb(Args args)
: LoadBalancingPolicy(std::move(args)),
user_agent_(GenerateUserAgentFromArgs(args.args, "chttp2")) {}

XdsRoutingLb::~XdsRoutingLb() {
if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_routing_lb_trace)) {
Expand Down Expand Up @@ -461,7 +492,8 @@ void XdsRoutingLb::UpdateStateLocked() {
}
route_table.push_back(std::move(route));
}
picker = absl::make_unique<RoutePicker>(std::move(route_table), config_);
picker = absl::make_unique<RoutePicker>(std::move(route_table),
user_agent_, config_);
break;
}
case GRPC_CHANNEL_CONNECTING:
Expand Down
33 changes: 3 additions & 30 deletions src/core/ext/filters/http/client/http_client_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <grpc/support/log.h>

#include "src/core/ext/filters/http/client/http_client_filter.h"
#include "src/core/ext/filters/http/client/util.h"
#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gprpp/manual_constructor.h"
#include "src/core/lib/profiling/timers.h"
Expand Down Expand Up @@ -528,36 +529,8 @@ static size_t max_payload_size_from_args(const grpc_channel_args* args) {

static grpc_core::ManagedMemorySlice user_agent_from_args(
const grpc_channel_args* args, const char* transport_name) {
std::vector<std::string> user_agent_fields;

for (size_t i = 0; args && i < args->num_args; i++) {
if (0 == strcmp(args->args[i].key, GRPC_ARG_PRIMARY_USER_AGENT_STRING)) {
if (args->args[i].type != GRPC_ARG_STRING) {
gpr_log(GPR_ERROR, "Channel argument '%s' should be a string",
GRPC_ARG_PRIMARY_USER_AGENT_STRING);
} else {
user_agent_fields.push_back(args->args[i].value.string);
}
}
}

user_agent_fields.push_back(
absl::StrFormat("grpc-c/%s (%s; %s)", grpc_version_string(),
GPR_PLATFORM_STRING, transport_name));

for (size_t i = 0; args && i < args->num_args; i++) {
if (0 == strcmp(args->args[i].key, GRPC_ARG_SECONDARY_USER_AGENT_STRING)) {
if (args->args[i].type != GRPC_ARG_STRING) {
gpr_log(GPR_ERROR, "Channel argument '%s' should be a string",
GRPC_ARG_SECONDARY_USER_AGENT_STRING);
} else {
user_agent_fields.push_back(args->args[i].value.string);
}
}
}

std::string user_agent_string = absl::StrJoin(user_agent_fields, " ");
return grpc_core::ManagedMemorySlice(user_agent_string.c_str());
return grpc_core::ManagedMemorySlice(
grpc_core::GenerateUserAgentFromArgs(args, transport_name).c_str());
}

/* Constructor for channel_data */
Expand Down
Loading

0 comments on commit 8c013bf

Please sign in to comment.