diff --git a/src/shrpx.cc b/src/shrpx.cc index 24290891..ee05263b 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -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().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)); } diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 23b480b3..9dd5c444 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -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)); } diff --git a/src/shrpx_config_test.cc b/src/shrpx_config_test.cc index fa8c1391..53b1bc30 100644 --- a/src/shrpx_config_test.cc +++ b/src/shrpx_config_test.cc @@ -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, diff --git a/src/shrpx_router.cc b/src/shrpx_router.cc index e0404252..4324748a 100644 --- a/src/shrpx_router.cc +++ b/src/shrpx_router.cc @@ -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; } diff --git a/src/shrpx_router.h b/src/shrpx_router.h index 07aadf69..e4d9a465 100644 --- a/src/shrpx_router.h +++ b/src/shrpx_router.h @@ -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;