mirror of
https://github.com/labstack/echo.git
synced 2025-06-08 23:56:20 +02:00
refs #1412: Fix multi level match any routes
This commit is contained in:
parent
d286e285dc
commit
8d47b34cdc
40
router.go
40
router.go
@ -1,6 +1,9 @@
|
|||||||
package echo
|
package echo
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Router is the registry of all registered routes for an `Echo` instance for
|
// Router is the registry of all registered routes for an `Echo` instance for
|
||||||
@ -20,8 +23,8 @@ type (
|
|||||||
pnames []string
|
pnames []string
|
||||||
methodHandler *methodHandler
|
methodHandler *methodHandler
|
||||||
}
|
}
|
||||||
kind uint8
|
kind uint8
|
||||||
children []*node
|
children []*node
|
||||||
methodHandler struct {
|
methodHandler struct {
|
||||||
connect HandlerFunc
|
connect HandlerFunc
|
||||||
delete HandlerFunc
|
delete HandlerFunc
|
||||||
@ -336,7 +339,6 @@ func (r *Router) Find(method, path string, c Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if l == pl {
|
if l == pl {
|
||||||
// Continue search
|
// Continue search
|
||||||
search = search[l:]
|
search = search[l:]
|
||||||
@ -398,16 +400,28 @@ func (r *Router) Find(method, path string, c Context) {
|
|||||||
Any:
|
Any:
|
||||||
if cn = cn.findChildByKind(akind); cn == nil {
|
if cn = cn.findChildByKind(akind); cn == nil {
|
||||||
if nn != nil {
|
if nn != nil {
|
||||||
cn = nn
|
// No next node to go down in routing (issue #954)
|
||||||
nn = cn.parent // Next (Issue #954)
|
// Find nearest "any" route going up the routing tree
|
||||||
if nn != nil {
|
|
||||||
nk = nn.kind
|
|
||||||
}
|
|
||||||
search = ns
|
search = ns
|
||||||
if nk == pkind {
|
np := nn.parent
|
||||||
goto Param
|
// Consider param route one level up only
|
||||||
} else if nk == akind {
|
// if no slash is remaining in search string
|
||||||
goto Any
|
if cn = nn.findChildByKind(pkind); cn != nil && strings.IndexByte(ns, '/') == -1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
np = nn.parent
|
||||||
|
if cn = nn.findChildByKind(akind); cn != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if np == nil {
|
||||||
|
break // no further parent nodes in tree, abort
|
||||||
|
}
|
||||||
|
nn = np
|
||||||
|
}
|
||||||
|
if cn != nil { // use the found "any" route and update path
|
||||||
|
pvalues[len(cn.pnames)-1] = search
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return // Not found
|
return // Not found
|
||||||
|
@ -595,20 +595,41 @@ func TestRouterMatchAnyMultiLevel(t *testing.T) {
|
|||||||
// Routes
|
// Routes
|
||||||
r.Add(http.MethodGet, "/api/users/jack", handler)
|
r.Add(http.MethodGet, "/api/users/jack", handler)
|
||||||
r.Add(http.MethodGet, "/api/users/jill", handler)
|
r.Add(http.MethodGet, "/api/users/jill", handler)
|
||||||
|
r.Add(http.MethodGet, "/api/users/*", handler)
|
||||||
|
r.Add(http.MethodGet, "/api/*", handler)
|
||||||
|
r.Add(http.MethodGet, "/other/*", handler)
|
||||||
r.Add(http.MethodGet, "/*", handler)
|
r.Add(http.MethodGet, "/*", handler)
|
||||||
|
|
||||||
c := e.NewContext(nil, nil).(*context)
|
c := e.NewContext(nil, nil).(*context)
|
||||||
r.Find(http.MethodGet, "/api/users/jack", c)
|
r.Find(http.MethodGet, "/api/users/jack", c)
|
||||||
c.handler(c)
|
c.handler(c)
|
||||||
assert.Equal(t, "/api/users/jack", c.Get("path"))
|
assert.Equal(t, "/api/users/jack", c.Get("path"))
|
||||||
|
assert.Equal(t, "", c.Param("*"))
|
||||||
|
|
||||||
r.Find(http.MethodGet, "/api/users/jill", c)
|
r.Find(http.MethodGet, "/api/users/jill", c)
|
||||||
c.handler(c)
|
c.handler(c)
|
||||||
assert.Equal(t, "/api/users/jill", c.Get("path"))
|
assert.Equal(t, "/api/users/jill", c.Get("path"))
|
||||||
|
assert.Equal(t, "", c.Param("*"))
|
||||||
|
|
||||||
r.Find(http.MethodGet, "/api/users/joe", c)
|
r.Find(http.MethodGet, "/api/users/joe", c)
|
||||||
c.handler(c)
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/api/users/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "joe", c.Param("*"))
|
||||||
|
|
||||||
|
r.Find(http.MethodGet, "/api/nousers/joe", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/api/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "nousers/joe", c.Param("*"))
|
||||||
|
|
||||||
|
r.Find(http.MethodGet, "/api/none", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, "/api/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "none", c.Param("*"))
|
||||||
|
|
||||||
|
r.Find(http.MethodGet, "/noapi/users/jim", c)
|
||||||
|
c.handler(c)
|
||||||
assert.Equal(t, "/*", c.Get("path"))
|
assert.Equal(t, "/*", c.Get("path"))
|
||||||
|
assert.Equal(t, "noapi/users/jim", c.Param("*"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterMicroParam(t *testing.T) {
|
func TestRouterMicroParam(t *testing.T) {
|
||||||
@ -702,6 +723,15 @@ func TestRouterPriority(t *testing.T) {
|
|||||||
c.Set("g", 7)
|
c.Set("g", 7)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
r.Add(http.MethodGet, "/users/new/*", func(c Context) error {
|
||||||
|
c.Set("h", 8)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
r.Add(http.MethodGet, "/*", func(c Context) error {
|
||||||
|
c.Set("i", 9)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
c := e.NewContext(nil, nil).(*context)
|
c := e.NewContext(nil, nil).(*context)
|
||||||
|
|
||||||
// Route > /users
|
// Route > /users
|
||||||
@ -734,11 +764,46 @@ func TestRouterPriority(t *testing.T) {
|
|||||||
c.handler(c)
|
c.handler(c)
|
||||||
assert.Equal(t, 3, c.Get("c"))
|
assert.Equal(t, 3, c.Get("c"))
|
||||||
|
|
||||||
|
// Route > /users/newsee
|
||||||
|
r.Find(http.MethodGet, "/users/newsee", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, 6, c.Get("f"))
|
||||||
|
|
||||||
// Route > /users/*
|
// Route > /users/*
|
||||||
r.Find(http.MethodGet, "/users/joe/books", c)
|
r.Find(http.MethodGet, "/users/joe/books", c)
|
||||||
c.handler(c)
|
c.handler(c)
|
||||||
assert.Equal(t, 7, c.Get("g"))
|
assert.Equal(t, 7, c.Get("g"))
|
||||||
assert.Equal(t, "joe/books", c.Param("*"))
|
assert.Equal(t, "joe/books", c.Param("*"))
|
||||||
|
|
||||||
|
// Route > /users/new/* should be matched
|
||||||
|
r.Find(http.MethodGet, "/users/new/someone", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, 8, c.Get("h"))
|
||||||
|
assert.Equal(t, "someone", c.Param("*"))
|
||||||
|
|
||||||
|
// Route > /users/* should be matched although /users/dew exists
|
||||||
|
r.Find(http.MethodGet, "/users/dew/someone", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, 7, c.Get("g"))
|
||||||
|
assert.Equal(t, "dew/someone", c.Param("*"))
|
||||||
|
|
||||||
|
// Route > /users/* should be matched although /users/dew exists
|
||||||
|
r.Find(http.MethodGet, "/users/notexists/someone/else", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, 7, c.Get("g"))
|
||||||
|
assert.Equal(t, "notexists/someone/else", c.Param("*"))
|
||||||
|
|
||||||
|
// Route > *
|
||||||
|
r.Find(http.MethodGet, "/nousers", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, 9, c.Get("i"))
|
||||||
|
assert.Equal(t, "nousers", c.Param("*"))
|
||||||
|
|
||||||
|
// Route > *
|
||||||
|
r.Find(http.MethodGet, "/nousers/new", c)
|
||||||
|
c.handler(c)
|
||||||
|
assert.Equal(t, 9, c.Get("i"))
|
||||||
|
assert.Equal(t, "nousers/new", c.Param("*"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterIssue1348(t *testing.T) {
|
func TestRouterIssue1348(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user