1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00

Fixed catch all param

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-26 09:48:49 -07:00
parent 4e69d8d4f5
commit 0a075ce7c5
3 changed files with 16 additions and 14 deletions

View File

@ -13,7 +13,6 @@ type (
Response *response Response *response
pnames []string pnames []string
pvalues []string pvalues []string
pn int // Param count
store store store store
echo *Echo echo *Echo
} }
@ -22,7 +21,7 @@ type (
// P returns path parameter by index. // P returns path parameter by index.
func (c *Context) P(i int) (value string) { func (c *Context) P(i int) (value string) {
if i <= c.pn { if i <= len(c.pnames) {
value = c.pvalues[i] value = c.pvalues[i]
} }
return return
@ -30,8 +29,9 @@ func (c *Context) P(i int) (value string) {
// Param returns path parameter by name. // Param returns path parameter by name.
func (c *Context) Param(name string) (value string) { func (c *Context) Param(name string) (value string) {
l := len(c.pnames)
for i, n := range c.pnames { for i, n := range c.pnames {
if n == name && i <= c.pn { if n == name && i <= l {
value = c.pvalues[i] value = c.pvalues[i]
break break
} }

View File

@ -64,8 +64,10 @@ func (r *router) Add(method, path string, h HandlerFunc, echo *Echo) {
} }
r.insert(method, path[:i], nil, ptype, pnames, echo) r.insert(method, path[:i], nil, ptype, pnames, echo)
} else if path[i] == '*' { } else if path[i] == '*' {
r.insert(method, path[:i], nil, ctype, nil, echo) r.insert(method, path[:i], nil, stype, nil, echo)
r.insert(method, path[:l], h, ctype, nil, echo) pnames = append(pnames, "_name")
r.insert(method, path[:l], h, ctype, pnames, echo)
return
} }
} }
r.insert(method, path, h, stype, nil, echo) r.insert(method, path, h, stype, nil, echo)
@ -203,7 +205,7 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
cn := r.trees[method] // Current node as root cn := r.trees[method] // Current node as root
search := path search := path
chn := new(node) // Child node chn := new(node) // Child node
c.pn = 0 // Param count n := 0 // Param counter
// Search order static > param > catch-all // Search order static > param > catch-all
for { for {
@ -239,8 +241,8 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
i, l := 0, len(search) i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ { for ; i < l && search[i] != '/'; i++ {
} }
c.pvalues[c.pn] = search[:i] c.pvalues[n] = search[:i]
c.pn++ n++
search = search[i:] search = search[i:]
continue continue
} }
@ -249,8 +251,7 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
chn = cn.findCchild() chn = cn.findCchild()
if chn != nil { if chn != nil {
cn = chn cn = chn
c.pnames[c.pn] = "_name" c.pvalues[n] = search
c.pvalues[c.pn] = search
search = "" // End search search = "" // End search
continue continue
} }

View File

@ -340,15 +340,16 @@ func TestRouterTwoParam(t *testing.T) {
func TestRouterCatchAll(t *testing.T) { func TestRouterCatchAll(t *testing.T) {
r := New().Router r := New().Router
r.Add(GET, "/static/*", func(*Context) error { r.Add(GET, "/users/*", func(*Context) error {
return nil return nil
}, nil) }, nil)
h, _ := r.Find(GET, "/static/echo.gif", context)
h, _ := r.Find(GET, "/users/joe", context)
if h == nil { if h == nil {
t.Fatal("handler not found") t.Fatal("handler not found")
} }
if context.pvalues[0] != "echo.gif" { if context.pvalues[0] != "joe" {
t.Error("value should be echo.gif") t.Error("value should be joe")
} }
} }