Repository: labstack/echo (go)

Issue:
"any" routing bug if path and any route share prefix
### Issue Description

#1563 introduced a bug where the wrong route is selected if there's an any route for a folder e.g. `/users/*` and a requested path has the folder as a prefix e.g. `/users_prefix/`.

Having the following two any routes registered:
`/*`
`/users/*`

### Expected behaviour

GET `/users` => `/*`
GET `/users/` => `/users/*`
GET `/users_prefix` => `/*`
GET `/users_prefix/` => `/*`

### Actual behaviour

GET `/users` => `/*`
GET `/users/` => `/users/*`
GET `/users_prefix` => `/*`
GET `/users_prefix/` => `/users/*` which is wrong

### Version/commit

6119aecb16b39eae121ef620d665ca0ff00b21ba


Pull request:
Do not handle special trailing slash case for partial prefix
Only handle the special trailing slash case if the whole prefix
matches. Otherwise we might be using a completely wrong
route, e.g. /users/* for the path /users_prefix/ where
the route is only a partial prefix of the requested path.

Fixes #1739

Patch:
diff --git a/router.go b/router.go
index 749dbf4..5010659 100644
--- a/router.go
+++ b/router.go
@@ -372,14 +372,14 @@ func (r *Router) Find(method, path string, c Context) {
 			if search == "" && (nn == nil || cn.parent == nil || cn.ppath != "") {
 				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
 		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
 				return // Not found
 			}


Test patch:
diff --git a/router_test.go b/router_test.go
index aafc622..a5e53c0 100644
--- a/router_test.go
+++ b/router_test.go
@@ -750,6 +750,47 @@ func TestRouterMatchAny(t *testing.T) {
 	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
 // for any routes with trailing slash requests
 func TestRouterMatchAnySlash(t *testing.T) {