Skip to content

Commit d5ba76e

Browse files
committed
Router fix, #217, #371, #372.
Signed-off-by: Vishal Rana <[email protected]>
1 parent 12e2c74 commit d5ba76e

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

router.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type (
3434
const (
3535
skind kind = iota
3636
pkind
37-
mkind
37+
akind
3838
)
3939

4040
func NewRouter(e *Echo) *Router {
@@ -84,7 +84,7 @@ func (r *Router) Add(method, path string, h Handler, e *Echo) {
8484
} else if path[i] == '*' {
8585
r.insert(method, path[:i], nil, skind, "", nil, e)
8686
pnames = append(pnames, "_*")
87-
r.insert(method, path[:i+1], h, mkind, ppath, pnames, e)
87+
r.insert(method, path[:i+1], h, akind, ppath, pnames, e)
8888
return
8989
}
9090
}
@@ -295,7 +295,7 @@ func (r *Router) Find(method, path string, context Context) {
295295
ns string // Next search
296296
)
297297

298-
// Search order static > param > match-any
298+
// Search order static > param > any
299299
for {
300300
if search == "" {
301301
goto End
@@ -325,21 +325,19 @@ func (r *Router) Find(method, path string, context Context) {
325325
search = ns
326326
if nk == pkind {
327327
goto Param
328-
} else if nk == mkind {
329-
goto MatchAny
330-
} else {
331-
// Not found
332-
return
328+
} else if nk == akind {
329+
goto Any
333330
}
331+
// Not found
332+
return
334333
}
335334

336335
if search == "" {
337336
goto End
338337
}
339338

340339
// Static node
341-
c = cn.findChild(search[0], skind)
342-
if c != nil {
340+
if c = cn.findChild(search[0], skind); c != nil {
343341
// Save next
344342
if cn.label == '/' {
345343
nk = pkind
@@ -352,11 +350,10 @@ func (r *Router) Find(method, path string, context Context) {
352350

353351
// Param node
354352
Param:
355-
c = cn.findChildByKind(pkind)
356-
if c != nil {
353+
if c = cn.findChildByKind(pkind); c != nil {
357354
// Save next
358355
if cn.label == '/' {
359-
nk = mkind
356+
nk = akind
360357
nn = cn
361358
ns = search
362359
}
@@ -370,10 +367,19 @@ func (r *Router) Find(method, path string, context Context) {
370367
continue
371368
}
372369

373-
// Match-any node
374-
MatchAny:
375-
// c = cn.getChild()
376-
if cn = cn.findChildByKind(mkind); cn == nil {
370+
// Any node
371+
Any:
372+
if cn = cn.findChildByKind(akind); cn == nil {
373+
if nn != nil {
374+
cn = nn
375+
nn = nil // Next
376+
search = ns
377+
if nk == pkind {
378+
goto Param
379+
} else if nk == akind {
380+
goto Any
381+
}
382+
}
377383
// Not found
378384
return
379385
}
@@ -390,9 +396,9 @@ End:
390396
if ctx.handler == nil {
391397
ctx.handler = cn.check405()
392398

393-
// Dig further for match-any, might have an empty value for *, e.g.
399+
// Dig further for any, might have an empty value for *, e.g.
394400
// serving a directory. Issue #207.
395-
if cn = cn.findChildByKind(mkind); cn == nil {
401+
if cn = cn.findChildByKind(akind); cn == nil {
396402
return
397403
}
398404
ctx.pvalues[len(cn.pnames)-1] = ""

router_test.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,8 @@ func TestRouterMultiRoute(t *testing.T) {
391391
// Route > /user
392392
c = NewContext(nil, nil, e)
393393
r.Find(GET, "/user", c)
394-
if assert.IsType(t, new(HTTPError), c.Handle(c)) {
395-
he := c.Handle(c).(*HTTPError)
396-
assert.Equal(t, http.StatusNotFound, he.code)
397-
}
394+
he := c.Handle(c).(*HTTPError)
395+
assert.Equal(t, http.StatusNotFound, he.code)
398396
}
399397

400398
func TestRouterPriority(t *testing.T) {
@@ -469,6 +467,37 @@ func TestRouterPriority(t *testing.T) {
469467
assert.Equal(t, "joe/books", c.Param("_*"))
470468
}
471469

470+
// Issue #372
471+
func TestRouterPriorityNotFound(t *testing.T) {
472+
e := New()
473+
r := e.router
474+
c := NewContext(nil, nil, e)
475+
476+
// Add
477+
r.Add(GET, "/a/foo", HandlerFunc(func(c Context) error {
478+
c.Set("a", 1)
479+
return nil
480+
}), e)
481+
r.Add(GET, "/a/bar", HandlerFunc(func(c Context) error {
482+
c.Set("b", 2)
483+
return nil
484+
}), e)
485+
486+
// Find
487+
r.Find(GET, "/a/foo", c)
488+
c.Handle(c)
489+
assert.Equal(t, 1, c.Get("a"))
490+
491+
r.Find(GET, "/a/bar", c)
492+
c.Handle(c)
493+
assert.Equal(t, 2, c.Get("b"))
494+
495+
c = NewContext(nil, nil, e)
496+
r.Find(GET, "/abc/def", c)
497+
he := c.Handle(c).(*HTTPError)
498+
assert.Equal(t, http.StatusNotFound, he.Code())
499+
}
500+
472501
func TestRouterParamNames(t *testing.T) {
473502
e := New()
474503
r := e.router
@@ -521,7 +550,6 @@ func TestRouterAPI(t *testing.T) {
521550
assert.Equal(t, ":"+n, c.P(i))
522551
}
523552
}
524-
c.Handle(c)
525553
}
526554
}
527555

0 commit comments

Comments
 (0)