nghttpx: Refactor DownstreamAddrGroup and router API

This commit is contained in:
Tatsuhiro Tsujikawa 2016-02-14 18:47:24 +09:00
parent 1bd98dcf4f
commit a53f0f0a17
4 changed files with 16 additions and 34 deletions

View File

@ -2109,14 +2109,14 @@ void process_options(
addr.host = ImmutableString::from_lit(DEFAULT_DOWNSTREAM_HOST);
addr.port = DEFAULT_DOWNSTREAM_PORT;
DownstreamAddrGroup g("/");
DownstreamAddrGroup g(StringRef::from_lit("/"));
g.addrs.push_back(std::move(addr));
mod_config()->router.add_route(g.pattern.get(), 1, addr_groups.size());
mod_config()->router.add_route(g.pattern.c_str(), 1, addr_groups.size());
addr_groups.push_back(std::move(g));
} else if (get_config()->http2_proxy || get_config()->client_proxy) {
// We don't support host mapping in these cases. Move all
// non-catch-all patterns to catch-all pattern.
DownstreamAddrGroup catch_all("/");
DownstreamAddrGroup catch_all(StringRef::from_lit("/"));
for (auto &g : addr_groups) {
std::move(std::begin(g.addrs), std::end(g.addrs),
std::back_inserter(catch_all.addrs));
@ -2124,7 +2124,7 @@ void process_options(
std::vector<DownstreamAddrGroup>().swap(addr_groups);
// maybe not necessary?
mod_config()->router = Router();
mod_config()->router.add_route(catch_all.pattern.get(), 1,
mod_config()->router.add_route(catch_all.pattern.c_str(), 1,
addr_groups.size());
addr_groups.push_back(std::move(catch_all));
}
@ -2136,11 +2136,11 @@ void process_options(
ssize_t catch_all_group = -1;
for (size_t i = 0; i < addr_groups.size(); ++i) {
auto &g = addr_groups[i];
if (util::streq(g.pattern.get(), "/")) {
if (g.pattern == "/") {
catch_all_group = i;
}
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Host-path pattern: group " << i << ": '" << g.pattern.get()
LOG(INFO) << "Host-path pattern: group " << i << ": '" << g.pattern
<< "'";
for (auto &addr : g.addrs) {
LOG(INFO) << "group " << i << " -> " << addr.host.c_str()

View File

@ -78,21 +78,6 @@ TicketKeys::~TicketKeys() {
}
}
DownstreamAddrGroup::DownstreamAddrGroup(const DownstreamAddrGroup &other)
: pattern(strcopy(other.pattern)), addrs(other.addrs) {}
DownstreamAddrGroup &DownstreamAddrGroup::
operator=(const DownstreamAddrGroup &other) {
if (this == &other) {
return *this;
}
pattern = strcopy(other.pattern);
addrs = other.addrs;
return *this;
}
namespace {
int split_host_port(char *host, size_t hostlen, uint16_t *port_ptr,
const char *hostport, size_t hostportlen) {
@ -612,7 +597,7 @@ void parse_mapping(const DownstreamAddr &addr, const char *src) {
pattern += http2::normalize_path(slash, raw_pattern.second);
}
for (auto &g : addr_groups) {
if (g.pattern.get() == pattern) {
if (g.pattern == pattern) {
g.addrs.push_back(addr);
done = true;
break;
@ -621,10 +606,10 @@ void parse_mapping(const DownstreamAddr &addr, const char *src) {
if (done) {
continue;
}
DownstreamAddrGroup g(pattern);
DownstreamAddrGroup g(StringRef{pattern});
g.addrs.push_back(addr);
mod_config()->router.add_route(g.pattern.get(), strlen(g.pattern.get()),
mod_config()->router.add_route(g.pattern.c_str(), g.pattern.size(),
addr_groups.size());
addr_groups.push_back(std::move(g));
@ -2529,7 +2514,7 @@ match_downstream_addr_group_host(const Router &router, const std::string &host,
if (group != -1) {
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Found pattern with query " << host
<< ", matched pattern=" << groups[group].pattern.get();
<< ", matched pattern=" << groups[group].pattern;
}
return group;
}
@ -2546,7 +2531,7 @@ match_downstream_addr_group_host(const Router &router, const std::string &host,
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Found pattern with query " << host
<< std::string(path, pathlen)
<< ", matched pattern=" << groups[group].pattern.get();
<< ", matched pattern=" << groups[group].pattern;
}
return group;
}
@ -2555,7 +2540,7 @@ match_downstream_addr_group_host(const Router &router, const std::string &host,
if (group != -1) {
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Found pattern with query " << std::string(path, pathlen)
<< ", matched pattern=" << groups[group].pattern.get();
<< ", matched pattern=" << groups[group].pattern;
}
return group;
}

View File

@ -301,13 +301,10 @@ struct DownstreamAddr {
};
struct DownstreamAddrGroup {
DownstreamAddrGroup(const std::string &pattern) : pattern(strcopy(pattern)) {}
DownstreamAddrGroup(const DownstreamAddrGroup &other);
DownstreamAddrGroup(DownstreamAddrGroup &&) = default;
DownstreamAddrGroup &operator=(const DownstreamAddrGroup &other);
DownstreamAddrGroup &operator=(DownstreamAddrGroup &&) = default;
DownstreamAddrGroup(const StringRef &pattern)
: pattern(pattern.c_str(), pattern.size()) {}
std::unique_ptr<char[]> pattern;
ImmutableString pattern;
std::vector<DownstreamAddr> addrs;
};

View File

@ -256,7 +256,7 @@ void test_shrpx_config_match_downstream_addr_group(void) {
for (size_t i = 0; i < groups.size(); ++i) {
auto &g = groups[i];
router.add_route(g.pattern.get(), strlen(g.pattern.get()), i);
router.add_route(g.pattern.c_str(), g.pattern.size(), i);
}
CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org", "/", groups,