mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
Do not handle special trailing slash case for partial prefix (#1741)
* Add tests for issue #1739 * Handle special trailing slash case only for a matching prefix Only handle the special trailing slash case if the whole prefix matches to avoid matching a wrong route for overlapping prefixes, e.g. /users/* for the path /users_prefix/ where the route is only a partial prefix of the requested path.
This commit is contained in:
@ -372,14 +372,14 @@ func (r *Router) Find(method, path string, c Context) {
|
|||||||
if search == "" && (nn == nil || cn.parent == nil || cn.ppath != "") {
|
if search == "" && (nn == nil || cn.parent == nil || cn.ppath != "") {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
// Handle special case of trailing slash route with existing any route (see #1526)
|
||||||
|
if search == "" && path[len(path)-1] == '/' && cn.anyChildren != nil {
|
||||||
|
goto Any
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to go back up the tree on no matching prefix or no remaining search
|
// Attempt to go back up the tree on no matching prefix or no remaining search
|
||||||
if l != pl || search == "" {
|
if l != pl || search == "" {
|
||||||
// Handle special case of trailing slash route with existing any route (see #1526)
|
|
||||||
if path[len(path)-1] == '/' && cn.anyChildren != nil {
|
|
||||||
goto Any
|
|
||||||
}
|
|
||||||
if nn == nil { // Issue #1348
|
if nn == nil { // Issue #1348
|
||||||
return // Not found
|
return // Not found
|
||||||
}
|
}
|
||||||
|
@ -750,6 +750,47 @@ func TestRouterMatchAny(t *testing.T) {
|
|||||||
assert.Equal(t, "joe", c.Param("*"))
|
assert.Equal(t, "joe", c.Param("*"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue #1739
|
||||||
|
func TestRouterMatchAnyPrefixIssue(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
r := e.router
|
||||||
|
|
||||||
|
// Routes
|
||||||
|
r.Add(http.MethodGet, "/*", func(c Context) error {
|
||||||
|
c.Set("path", c.Path())
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
r.Add(http.MethodGet, "/users/*", func(c Context) error {
|
||||||
|
c.Set("path", c.Path())
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
c := e.NewContext(nil, nil).(*context)
|
||||||
|
r.Find(http.MethodGet, "/", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "", c.Param("*"))
|
||||||
|
|
||||||
|
r.Find(http.MethodGet, "/users", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "users", c.Param("*"))
|
||||||
|
|
||||||
|
r.Find(http.MethodGet, "/users/", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/users/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "", c.Param("*"))
|
||||||
|
|
||||||
|
r.Find(http.MethodGet, "/users_prefix", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "users_prefix", c.Param("*"))
|
||||||
|
|
||||||
|
r.Find(http.MethodGet, "/users_prefix/", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "users_prefix/", c.Param("*"))
|
||||||
|
}
|
||||||
|
|
||||||
// TestRouterMatchAnySlash shall verify finding the best route
|
// TestRouterMatchAnySlash shall verify finding the best route
|
||||||
// for any routes with trailing slash requests
|
// for any routes with trailing slash requests
|
||||||
func TestRouterMatchAnySlash(t *testing.T) {
|
func TestRouterMatchAnySlash(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user