mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
Tested and fixed bug with reused context (path parameters).
This commit is contained in:
parent
22012e0e9d
commit
f0e3f950cf
@ -275,7 +275,7 @@ func (c *context) SetParamNames(names ...string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) ParamValues() []string {
|
func (c *context) ParamValues() []string {
|
||||||
return c.pvalues
|
return c.pvalues[:len(c.pnames)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetParamValues(values ...string) {
|
func (c *context) SetParamValues(values ...string) {
|
||||||
@ -553,4 +553,7 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
|
|||||||
c.query = nil
|
c.query = nil
|
||||||
c.handler = NotFoundHandler
|
c.handler = NotFoundHandler
|
||||||
c.store = nil
|
c.store = nil
|
||||||
|
c.pnames = nil
|
||||||
|
// WARNING: Don't reset because it has to have length c.echo.maxParam at all times: c.pvalues = nil
|
||||||
|
c.path = ""
|
||||||
}
|
}
|
||||||
|
@ -190,7 +190,16 @@ func TestContext(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
||||||
|
|
||||||
// Reset
|
// 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())
|
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) {
|
func TestContextCookie(t *testing.T) {
|
||||||
|
34
router.go
34
router.go
@ -296,18 +296,18 @@ func (n *node) checkMethodNotAllowed() HandlerFunc {
|
|||||||
// - Get context from `Echo#AcquireContext()`
|
// - Get context from `Echo#AcquireContext()`
|
||||||
// - Reset it `Context#Reset()`
|
// - Reset it `Context#Reset()`
|
||||||
// - Return it `Echo#ReleaseContext()`.
|
// - Return it `Echo#ReleaseContext()`.
|
||||||
func (r *Router) Find(method, path string, context Context) {
|
func (r *Router) Find(method, path string, ctx Context) {
|
||||||
context.SetPath(path)
|
ctx.SetPath(path)
|
||||||
cn := r.tree // Current node as root
|
cn := r.tree // Current node as root
|
||||||
|
|
||||||
var (
|
var (
|
||||||
search = path
|
search = path
|
||||||
c *node // Child node
|
c *node // Child node
|
||||||
n int // Param counter
|
n int // Param counter
|
||||||
nk kind // Next kind
|
nk kind // Next kind
|
||||||
nn *node // Next node
|
nn *node // Next node
|
||||||
ns string // Next search
|
ns string // Next search
|
||||||
pvalues = context.ParamValues()
|
pvalues = ctx.(*context).pvalues // use the internal slice so the interface can keep the illusion of a dynamic slice
|
||||||
)
|
)
|
||||||
|
|
||||||
// Search order static > param > any
|
// Search order static > param > any
|
||||||
@ -409,13 +409,13 @@ func (r *Router) Find(method, path string, context Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
End:
|
End:
|
||||||
context.SetHandler(cn.findHandler(method))
|
ctx.SetHandler(cn.findHandler(method))
|
||||||
context.SetPath(cn.ppath)
|
ctx.SetPath(cn.ppath)
|
||||||
context.SetParamNames(cn.pnames...)
|
ctx.SetParamNames(cn.pnames...)
|
||||||
|
|
||||||
// NOTE: Slow zone...
|
// NOTE: Slow zone...
|
||||||
if context.Handler() == nil {
|
if ctx.Handler() == nil {
|
||||||
context.SetHandler(cn.checkMethodNotAllowed())
|
ctx.SetHandler(cn.checkMethodNotAllowed())
|
||||||
|
|
||||||
// Dig further for 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.
|
||||||
@ -423,12 +423,12 @@ End:
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if h := cn.findHandler(method); h != nil {
|
if h := cn.findHandler(method); h != nil {
|
||||||
context.SetHandler(h)
|
ctx.SetHandler(h)
|
||||||
} else {
|
} else {
|
||||||
context.SetHandler(cn.checkMethodNotAllowed())
|
ctx.SetHandler(cn.checkMethodNotAllowed())
|
||||||
}
|
}
|
||||||
context.SetPath(cn.ppath)
|
ctx.SetPath(cn.ppath)
|
||||||
context.SetParamNames(cn.pnames...)
|
ctx.SetParamNames(cn.pnames...)
|
||||||
pvalues[len(cn.pnames)-1] = ""
|
pvalues[len(cn.pnames)-1] = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user