nghttpx: Use StringRef for pattern paramter in Router::add_route
This commit is contained in:
parent
a53f0f0a17
commit
2d273f8237
|
@ -2111,7 +2111,7 @@ void process_options(
|
||||||
|
|
||||||
DownstreamAddrGroup g(StringRef::from_lit("/"));
|
DownstreamAddrGroup g(StringRef::from_lit("/"));
|
||||||
g.addrs.push_back(std::move(addr));
|
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));
|
addr_groups.push_back(std::move(g));
|
||||||
} else if (get_config()->http2_proxy || get_config()->client_proxy) {
|
} else if (get_config()->http2_proxy || get_config()->client_proxy) {
|
||||||
// We don't support host mapping in these cases. Move all
|
// We don't support host mapping in these cases. Move all
|
||||||
|
@ -2124,7 +2124,7 @@ void process_options(
|
||||||
std::vector<DownstreamAddrGroup>().swap(addr_groups);
|
std::vector<DownstreamAddrGroup>().swap(addr_groups);
|
||||||
// maybe not necessary?
|
// maybe not necessary?
|
||||||
mod_config()->router = Router();
|
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.size());
|
||||||
addr_groups.push_back(std::move(catch_all));
|
addr_groups.push_back(std::move(catch_all));
|
||||||
}
|
}
|
||||||
|
|
|
@ -609,8 +609,7 @@ void parse_mapping(const DownstreamAddr &addr, const char *src) {
|
||||||
DownstreamAddrGroup g(StringRef{pattern});
|
DownstreamAddrGroup g(StringRef{pattern});
|
||||||
g.addrs.push_back(addr);
|
g.addrs.push_back(addr);
|
||||||
|
|
||||||
mod_config()->router.add_route(g.pattern.c_str(), g.pattern.size(),
|
mod_config()->router.add_route(StringRef{g.pattern}, addr_groups.size());
|
||||||
addr_groups.size());
|
|
||||||
|
|
||||||
addr_groups.push_back(std::move(g));
|
addr_groups.push_back(std::move(g));
|
||||||
}
|
}
|
||||||
|
|
|
@ -256,7 +256,7 @@ void test_shrpx_config_match_downstream_addr_group(void) {
|
||||||
|
|
||||||
for (size_t i = 0; i < groups.size(); ++i) {
|
for (size_t i = 0; i < groups.size(); ++i) {
|
||||||
auto &g = groups[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,
|
CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org", "/", groups,
|
||||||
|
|
|
@ -66,21 +66,21 @@ void Router::add_node(RNode *node, const char *pattern, size_t patlen,
|
||||||
add_next_node(node, std::move(new_node));
|
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_;
|
auto node = &root_;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto next_node = find_next_node(node, pattern[i]);
|
auto next_node = find_next_node(node, pattern[i]);
|
||||||
if (next_node == nullptr) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
node = next_node;
|
node = next_node;
|
||||||
|
|
||||||
auto slen = patlen - i;
|
auto slen = pattern.size() - i;
|
||||||
auto s = pattern + i;
|
auto s = pattern.c_str() + i;
|
||||||
auto n = std::min(node->len, slen);
|
auto n = std::min(node->len, slen);
|
||||||
size_t j;
|
size_t j;
|
||||||
for (j = 0; j < n && node->s[j] == s[j]; ++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;
|
i += j;
|
||||||
|
|
||||||
assert(patlen > i);
|
assert(pattern.size() > i);
|
||||||
add_node(node, pattern + i, patlen - i, index);
|
add_node(node, pattern.c_str() + i, pattern.size() - i, index);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,8 +55,8 @@ struct RNode {
|
||||||
class Router {
|
class Router {
|
||||||
public:
|
public:
|
||||||
Router();
|
Router();
|
||||||
// Adds route |pattern| of size |patlen| with its |index|.
|
// Adds route |pattern| with its |index|.
|
||||||
bool add_route(const char *pattern, size_t patlen, size_t index);
|
bool add_route(const StringRef &pattern, size_t index);
|
||||||
// Returns the matched index of pattern. -1 if there is no match.
|
// Returns the matched index of pattern. -1 if there is no match.
|
||||||
ssize_t match(const std::string &host, const char *path,
|
ssize_t match(const std::string &host, const char *path,
|
||||||
size_t pathlen) const;
|
size_t pathlen) const;
|
||||||
|
|
Loading…
Reference in New Issue