mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
parent
33c227045a
commit
d105270d58
42
router.go
42
router.go
@ -43,7 +43,7 @@ type (
|
|||||||
const (
|
const (
|
||||||
skind kind = iota
|
skind kind = iota
|
||||||
pkind
|
pkind
|
||||||
mkind
|
akind
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewRouter returns a new Router instance.
|
// NewRouter returns a new Router instance.
|
||||||
@ -82,7 +82,7 @@ func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
|||||||
} else if path[i] == '*' {
|
} else if path[i] == '*' {
|
||||||
r.insert(method, path[:i], nil, skind, "", nil, e)
|
r.insert(method, path[:i], nil, skind, "", nil, e)
|
||||||
pnames = append(pnames, "_*")
|
pnames = append(pnames, "_*")
|
||||||
r.insert(method, path[:i+1], h, mkind, ppath, pnames, e)
|
r.insert(method, path[:i+1], h, akind, ppath, pnames, e)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -286,6 +286,7 @@ func (n *node) check405() HandlerFunc {
|
|||||||
// Find dispatches the request to the handler whos route is matched with the
|
// Find dispatches the request to the handler whos route is matched with the
|
||||||
// specified request path.
|
// specified request path.
|
||||||
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
|
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
|
||||||
|
// r.tree.printTree("", true)
|
||||||
h = notFoundHandler
|
h = notFoundHandler
|
||||||
e = r.echo
|
e = r.echo
|
||||||
cn := r.tree // Current node as root
|
cn := r.tree // Current node as root
|
||||||
@ -299,7 +300,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
ns string // Next search
|
ns string // Next search
|
||||||
)
|
)
|
||||||
|
|
||||||
// Search order static > param > match-any
|
// Search order static > param > any
|
||||||
for {
|
for {
|
||||||
if search == "" {
|
if search == "" {
|
||||||
goto End
|
goto End
|
||||||
@ -329,12 +330,11 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
search = ns
|
search = ns
|
||||||
if nk == pkind {
|
if nk == pkind {
|
||||||
goto Param
|
goto Param
|
||||||
} else if nk == mkind {
|
} else if nk == akind {
|
||||||
goto MatchAny
|
goto Any
|
||||||
} else {
|
|
||||||
// Not found
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
// Not found
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if search == "" {
|
if search == "" {
|
||||||
@ -342,8 +342,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Static node
|
// Static node
|
||||||
c = cn.findChild(search[0], skind)
|
if c = cn.findChild(search[0], skind); c != nil {
|
||||||
if c != nil {
|
|
||||||
// Save next
|
// Save next
|
||||||
if cn.label == '/' {
|
if cn.label == '/' {
|
||||||
nk = pkind
|
nk = pkind
|
||||||
@ -356,11 +355,10 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
|
|
||||||
// Param node
|
// Param node
|
||||||
Param:
|
Param:
|
||||||
c = cn.findChildByKind(pkind)
|
if c = cn.findChildByKind(pkind); c != nil {
|
||||||
if c != nil {
|
|
||||||
// Save next
|
// Save next
|
||||||
if cn.label == '/' {
|
if cn.label == '/' {
|
||||||
nk = mkind
|
nk = akind
|
||||||
nn = cn
|
nn = cn
|
||||||
ns = search
|
ns = search
|
||||||
}
|
}
|
||||||
@ -374,10 +372,16 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match-any node
|
// Any node
|
||||||
MatchAny:
|
Any:
|
||||||
// c = cn.getChild()
|
if cn = cn.findChildByKind(akind); cn == nil {
|
||||||
if cn = cn.findChildByKind(mkind); cn == nil {
|
cn = nn
|
||||||
|
search = ns
|
||||||
|
if nk == pkind {
|
||||||
|
goto Param
|
||||||
|
} else if nk == akind {
|
||||||
|
goto Any
|
||||||
|
}
|
||||||
// Not found
|
// Not found
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -397,9 +401,9 @@ End:
|
|||||||
if h == nil {
|
if h == nil {
|
||||||
h = cn.check405()
|
h = cn.check405()
|
||||||
|
|
||||||
// Dig further for match-any, might have an empty value for *, e.g.
|
// Dig further for any, might have an empty value for *, e.g.
|
||||||
// serving a directory. Issue #207.
|
// serving a directory. Issue #207.
|
||||||
if cn = cn.findChildByKind(mkind); cn == nil {
|
if cn = cn.findChildByKind(akind); cn == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.pvalues[len(cn.pnames)-1] = ""
|
ctx.pvalues[len(cn.pnames)-1] = ""
|
||||||
|
@ -503,6 +503,70 @@ func TestRouterPriority(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue #217
|
||||||
|
func TestRouterPriorityNext(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
r := e.router
|
||||||
|
|
||||||
|
// Routes
|
||||||
|
r.Add(GET, "/aa", func(c *Context) error {
|
||||||
|
c.Set("a", 1)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
r.Add(GET, "/ab", func(c *Context) error {
|
||||||
|
c.Set("b", 2)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
r.Add(GET, "/abc", func(c *Context) error {
|
||||||
|
c.Set("c", 3)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
r.Add(GET, "/abd", func(c *Context) error {
|
||||||
|
c.Set("d", 4)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
r.Add(GET, "/*", func(c *Context) error {
|
||||||
|
c.Set("e", 5)
|
||||||
|
return nil
|
||||||
|
}, e)
|
||||||
|
c := NewContext(nil, nil, e)
|
||||||
|
|
||||||
|
// Route > /aa
|
||||||
|
h, _ := r.Find(GET, "/aa", c)
|
||||||
|
if assert.NotNil(t, h) {
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 1, c.Get("a"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route > /ab
|
||||||
|
h, _ = r.Find(GET, "/ab", c)
|
||||||
|
if assert.NotNil(t, h) {
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 2, c.Get("b"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route > /abc
|
||||||
|
h, _ = r.Find(GET, "/abc", c)
|
||||||
|
if assert.NotNil(t, h) {
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 3, c.Get("c"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route > /abd
|
||||||
|
h, _ = r.Find(GET, "/abd", c)
|
||||||
|
if assert.NotNil(t, h) {
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 4, c.Get("d"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route > /*
|
||||||
|
h, _ = r.Find(GET, "/abc.html", c)
|
||||||
|
if assert.NotNil(t, h) {
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, 5, c.Get("e"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRouterParamNames(t *testing.T) {
|
func TestRouterParamNames(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
|
Loading…
Reference in New Issue
Block a user