nghttpx: Use StringRef for pattern paramter in Router::add_route

This commit is contained in:
Tatsuhiro Tsujikawa 2016-02-14 18:55:53 +09:00
parent a53f0f0a17
commit 2d273f8237
5 changed files with 12 additions and 13 deletions

View File

@ -2111,7 +2111,7 @@ void process_options(
DownstreamAddrGroup g(StringRef::from_lit("/"));
g.addrs.push_back(std::move(addr));
mod_config()->router.add_route(g.pattern.c_str(), 1, addr_groups.size());
mod_config()->router.add_route(StringRef{g.pattern}, 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
@ -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.c_str(), 1,
mod_config()->router.add_route(StringRef{catch_all.pattern},
addr_groups.size());
addr_groups.push_back(std::move(catch_all));
}

View File

@ -609,8 +609,7 @@ void parse_mapping(const DownstreamAddr &addr, const char *src) {
DownstreamAddrGroup g(StringRef{pattern});
g.addrs.push_back(addr);
mod_config()->router.add_route(g.pattern.c_str(), g.pattern.size(),
addr_groups.size());
mod_config()->router.add_route(StringRef{g.pattern}, addr_groups.size());
addr_groups.push_back(std::move(g));
}

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.c_str(), g.pattern.size(), i);
router.add_route(StringRef{g.pattern}, i);
}
CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org", "/", groups,

View File

@ -66,21 +66,21 @@ void Router::add_node(RNode *node, const char *pattern, size_t patlen,
add_next_node(node, std::move(new_node));
}
bool Router::add_route(const char *pattern, size_t patlen, size_t index) {
bool Router::add_route(const StringRef &pattern, size_t index) {
auto node = &root_;
size_t i = 0;
for (;;) {
auto next_node = find_next_node(node, pattern[i]);
if (next_node == nullptr) {
add_node(node, pattern + i, patlen - i, index);
add_node(node, pattern.c_str() + i, pattern.size() - i, index);
return true;
}
node = next_node;
auto slen = patlen - i;
auto s = pattern + i;
auto slen = pattern.size() - i;
auto s = pattern.c_str() + i;
auto n = std::min(node->len, slen);
size_t j;
for (j = 0; j < n && node->s[j] == s[j]; ++j)
@ -125,8 +125,8 @@ bool Router::add_route(const char *pattern, size_t patlen, size_t index) {
i += j;
assert(patlen > i);
add_node(node, pattern + i, patlen - i, index);
assert(pattern.size() > i);
add_node(node, pattern.c_str() + i, pattern.size() - i, index);
return true;
}

View File

@ -55,8 +55,8 @@ struct RNode {
class Router {
public:
Router();
// Adds route |pattern| of size |patlen| with its |index|.
bool add_route(const char *pattern, size_t patlen, size_t index);
// Adds route |pattern| with its |index|.
bool add_route(const StringRef &pattern, size_t index);
// Returns the matched index of pattern. -1 if there is no match.
ssize_t match(const std::string &host, const char *path,
size_t pathlen) const;