Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header matched routing #110

Merged
merged 12 commits into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions source/common/router/config_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,21 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHost& vhost, const Json::Obj
}

if (route.hasObject("headers")) {
config_headers_ = route.getObjectArray("headers");
std::vector<Json::Object> config_headers = route.getObjectArray("headers");
for (const Json::Object& header_map : config_headers) {
HeaderData header_data;

if (!header_map.hasObject("header_name")) {
throw EnvoyException("headers in the route config must have a header name");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't bother with this check here. Just let JsonException be thrown if value does not exist

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will let JsonException be thrown.

} else {
header_data.header_name_ = header_map.getString("header_name");
}

// allow header value to be empty, allows matching to be only based on header presence.
header_data.header_value_ = header_map.getString("header_value");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs default value of ""

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.


config_headers_.push_back(header_data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

push_back struct directly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can emplace_back(header_map.getString("header_name"), header_map.getString("header_value", ""))

Copy link
Member Author

@junr03 junr03 Sep 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like emplace_back would not allow me to do uniform initialization

Important edit: @RomanDzhabarov you were right all along!

}
}
}

Expand All @@ -84,8 +98,8 @@ bool RouteEntryImplBase::matches(const Http::HeaderMap& headers, uint64_t random
}

if (!config_headers_.empty()) {
for (const Json::Object& header_map : config_headers_) {
matches &= headers.get(Http::LowerCaseString(header_map.getString("header_name"))) == header_map.getString("header_value");
for (const HeaderData header_data : config_headers_) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const HeaderData&

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

matches &= headers.get(Http::LowerCaseString(header_data.header_name_)) == header_data.header_value_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store as lower case string in the struct

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storing as Http::LowerCaseString

if (!matches) {
break;
}
Expand Down
7 changes: 6 additions & 1 deletion source/common/router/config_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class RouteEntryImplBase : public RouteEntry, public Matchable, public RedirectE
uint64_t default_;
};

struct HeaderData {
std::string header_name_;
std::string header_value_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

Copy link
Member Author

@junr03 junr03 Sep 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

};

static Optional<RuntimeData> loadRuntimeData(const Json::Object& route);

// Default timeout is 15s if nothing is specified in the route config.
Expand All @@ -211,7 +216,7 @@ class RouteEntryImplBase : public RouteEntry, public Matchable, public RedirectE
const RateLimitPolicyImpl rate_limit_policy_;
const ShadowPolicyImpl shadow_policy_;
const Upstream::ResourcePriority priority_;
std::vector<Json::Object> config_headers_;
std::vector<HeaderData> config_headers_;
};

/**
Expand Down