From 0a2d1965df6ff1a55702b649489da3bf8c207230 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 18 Apr 2017 21:03:50 +0900 Subject: [PATCH] nghttpx: Fix path matching bug Previously, if path is empty or path does not start with "/", nghttpx did not try to match with wildcard pattern. This commit fixes it. --- src/shrpx_worker.cc | 16 ++++------------ src/shrpx_worker_test.cc | 12 ++++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index 69a574fc..d58a9e0e 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -505,18 +505,6 @@ size_t match_downstream_addr_group_host( const auto &rev_wildcard_router = routerconf.rev_wildcard_router; const auto &wildcard_patterns = routerconf.wildcard_patterns; - if (path.empty() || path[0] != '/') { - auto group = router.match(host, StringRef::from_lit("/")); - if (group != -1) { - if (LOG_ENABLED(INFO)) { - LOG(INFO) << "Found pattern with query " << host - << ", matched pattern=" << groups[group]->pattern; - } - return group; - } - return catch_all; - } - if (LOG_ENABLED(INFO)) { LOG(INFO) << "Perform mapping selection, using host=" << host << ", path=" << path; @@ -602,6 +590,10 @@ size_t match_downstream_addr_group( auto query = std::find(std::begin(raw_path), fragment, '?'); auto path = StringRef{std::begin(raw_path), query}; + if (path.empty() || path[0] != '/') { + path = StringRef::from_lit("/"); + } + if (hostport.empty()) { return match_downstream_addr_group_host(routerconf, hostport, path, groups, catch_all, balloc); diff --git a/src/shrpx_worker_test.cc b/src/shrpx_worker_test.cc index ee6919ff..d907b327 100644 --- a/src/shrpx_worker_test.cc +++ b/src/shrpx_worker_test.cc @@ -197,6 +197,10 @@ void test_shrpx_worker_match_downstream_addr_group(void) { g2->pattern = ImmutableString::from_lit(".nghttp2.org"); groups.push_back(std::move(g2)); + auto g3 = std::make_shared(); + g3->pattern = ImmutableString::from_lit(".local"); + groups.push_back(std::move(g3)); + wp.emplace_back(StringRef::from_lit("git.nghttp2.org")); wcrouter.add_route(StringRef::from_lit("gro.2ptthgn.tig"), 0); wp.back().router.add_route(StringRef::from_lit("/echo/"), 10); @@ -206,6 +210,10 @@ void test_shrpx_worker_match_downstream_addr_group(void) { wp.back().router.add_route(StringRef::from_lit("/echo/"), 11); wp.back().router.add_route(StringRef::from_lit("/echo/foxtrot"), 12); + wp.emplace_back(StringRef::from_lit(".local")); + wcrouter.add_route(StringRef::from_lit("lacol."), 2); + wp.back().router.add_route(StringRef::from_lit("/"), 13); + CU_ASSERT(11 == match_downstream_addr_group( routerconf, StringRef::from_lit("git.nghttp2.org"), StringRef::from_lit("/echo"), groups, 255, balloc)); @@ -230,6 +238,10 @@ void test_shrpx_worker_match_downstream_addr_group(void) { CU_ASSERT(0 == match_downstream_addr_group( routerconf, StringRef::from_lit("nghttp2.org"), StringRef::from_lit("/echo"), groups, 255, balloc)); + + CU_ASSERT(13 == match_downstream_addr_group( + routerconf, StringRef::from_lit("test.local"), + StringRef{}, groups, 255, balloc)); } } // namespace shrpx