Fix prefix conflict in exact paths

Signed-off-by: Yue Yang <g1enyy0ung@gmail.com>
This commit is contained in:
Yue Yang 2021-04-28 14:56:07 +08:00
parent 39fa5a345f
commit 7c9e3b0e29
2 changed files with 16 additions and 3 deletions

16
tree.go
View File

@ -414,11 +414,21 @@ walk: // Outer loop for walking the tree
childWillWalkTo := n.children[i] childWillWalkTo := n.children[i]
if strings.Split(childWillWalkTo.path, "/")[0] != strings.Split(path, "/")[0] && strings.HasPrefix(n.children[len(n.children)-1].path, ":") { if strings.Split(childWillWalkTo.path, "/")[0] != strings.Split(path, "/")[0] && strings.HasPrefix(n.children[len(n.children)-1].path, ":") {
for _, child := range childWillWalkTo.children {
cPath := string(c) + child.path
if cPath == path {
child.path = cPath
n = child
continue walk
}
}
continue continue
} else {
n = childWillWalkTo
continue walk
} }
n = childWillWalkTo
continue walk
} }
} }

View File

@ -143,6 +143,7 @@ func TestTreeWildcard(t *testing.T) {
"/search/", "/search/",
"/search/:query", "/search/:query",
"/search/gin-gonic", "/search/gin-gonic",
"/search/google",
"/user_:name", "/user_:name",
"/user_:name/about", "/user_:name/about",
"/files/:dir/*filepath", "/files/:dir/*filepath",
@ -171,6 +172,8 @@ func TestTreeWildcard(t *testing.T) {
{"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}}, {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}},
{"/search/someth!ng+in+ünìcodé/", true, "", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}}, {"/search/someth!ng+in+ünìcodé/", true, "", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}},
{"/search/gin", false, "/search/:query", Params{Param{"query", "gin"}}}, {"/search/gin", false, "/search/:query", Params{Param{"query", "gin"}}},
{"/search/gin-gonic", false, "/search/gin-gonic", nil},
{"/search/google", false, "/search/google", nil},
{"/user_gopher", false, "/user_:name", Params{Param{Key: "name", Value: "gopher"}}}, {"/user_gopher", false, "/user_:name", Params{Param{Key: "name", Value: "gopher"}}},
{"/user_gopher/about", false, "/user_:name/about", Params{Param{Key: "name", Value: "gopher"}}}, {"/user_gopher/about", false, "/user_:name/about", Params{Param{Key: "name", Value: "gopher"}}},
{"/files/js/inc/framework.js", false, "/files/:dir/*filepath", Params{Param{Key: "dir", Value: "js"}, Param{Key: "filepath", Value: "/inc/framework.js"}}}, {"/files/js/inc/framework.js", false, "/files/:dir/*filepath", Params{Param{Key: "dir", Value: "js"}, Param{Key: "filepath", Value: "/inc/framework.js"}}},