Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (c *context) SetParamNames(names ...string) {
}

func (c *context) ParamValues() []string {
return c.pvalues
return c.pvalues[:len(c.pnames)]
}

func (c *context) SetParamValues(values ...string) {
Expand Down Expand Up @@ -553,4 +553,8 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
c.query = nil
c.handler = NotFoundHandler
c.store = nil
c.path = ""
c.pnames = nil
// NOTE: Don't reset because it has to have length c.echo.maxParam at all times
// c.pvalues = nil
}
9 changes: 9 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,16 @@ func TestContext(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, rec.Code)

// Reset
c.SetParamNames("foo")
c.SetParamValues("bar")
c.Set("foe", "ban")
c.query = url.Values(map[string][]string{"fon": []string{"baz"}})
c.Reset(req, httptest.NewRecorder())
assert.Equal(t, 0, len(c.ParamValues()))
assert.Equal(t, 0, len(c.ParamNames()))
assert.Equal(t, 0, len(c.store))
assert.Equal(t, "", c.Path())
assert.Equal(t, 0, len(c.QueryParams()))
}

func TestContextCookie(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ func TestLoggerTemplate(t *testing.T) {
"hexvalue": false,
"GET": true,
"127.0.0.1": true,
"\"path\":\"/\"": true,
"\"uri\":\"/\"": true,
"\"status\":200": true,
"\"bytes_in\":0": true,
`"path":"/"`: true,
`"uri":"/"`: true,
`"status":200`: true,
`"bytes_in":0`: true,
"google.com": true,
"echo-tests-agent": true,
"6ba7b810-9dad-11d1-80b4-00c04fd430c8": true,
Expand Down
43 changes: 22 additions & 21 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,19 @@ func (n *node) checkMethodNotAllowed() HandlerFunc {
// - Get context from `Echo#AcquireContext()`
// - Reset it `Context#Reset()`
// - Return it `Echo#ReleaseContext()`.
func (r *Router) Find(method, path string, context Context) {
context.SetPath(path)
func (r *Router) Find(method, path string, c Context) {
ctx := c.(*context)
ctx.path = path
cn := r.tree // Current node as root

var (
search = path
c *node // Child node
n int // Param counter
nk kind // Next kind
nn *node // Next node
ns string // Next search
pvalues = context.ParamValues()
child *node // Child node
n int // Param counter
nk kind // Next kind
nn *node // Next node
ns string // Next search
pvalues = ctx.pvalues // Use the internal slice so the interface can keep the illusion of a dynamic slice
)

// Search order static > param > any
Expand Down Expand Up @@ -352,20 +353,20 @@ func (r *Router) Find(method, path string, context Context) {
}

// Static node
if c = cn.findChild(search[0], skind); c != nil {
if child = cn.findChild(search[0], skind); child != nil {
// Save next
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
nk = pkind
nn = cn
ns = search
}
cn = c
cn = child
continue
}

// Param node
Param:
if c = cn.findChildByKind(pkind); c != nil {
if child = cn.findChildByKind(pkind); child != nil {
// Issue #378
if len(pvalues) == n {
continue
Expand All @@ -378,7 +379,7 @@ func (r *Router) Find(method, path string, context Context) {
ns = search
}

cn = c
cn = child
i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ {
}
Expand Down Expand Up @@ -409,26 +410,26 @@ func (r *Router) Find(method, path string, context Context) {
}

End:
context.SetHandler(cn.findHandler(method))
context.SetPath(cn.ppath)
context.SetParamNames(cn.pnames...)
ctx.handler = cn.findHandler(method)
ctx.path = cn.ppath
ctx.pnames = cn.pnames

// NOTE: Slow zone...
if context.Handler() == nil {
context.SetHandler(cn.checkMethodNotAllowed())
if ctx.handler == nil {
ctx.handler = cn.checkMethodNotAllowed()

// Dig further for any, might have an empty value for *, e.g.
// serving a directory. Issue #207.
if cn = cn.findChildByKind(akind); cn == nil {
return
}
if h := cn.findHandler(method); h != nil {
context.SetHandler(h)
ctx.handler = h
} else {
context.SetHandler(cn.checkMethodNotAllowed())
ctx.handler = cn.checkMethodNotAllowed()
}
context.SetPath(cn.ppath)
context.SetParamNames(cn.pnames...)
ctx.path = cn.ppath
ctx.pnames = cn.pnames
pvalues[len(cn.pnames)-1] = ""
}

Expand Down