mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
Merge pull request #258 from labstack/issue-203
Closes #235, Closes #203
This commit is contained in:
commit
ec1e0bb753
@ -22,6 +22,7 @@ type (
|
||||
request *http.Request
|
||||
response *Response
|
||||
socket *websocket.Conn
|
||||
path string
|
||||
pnames []string
|
||||
pvalues []string
|
||||
query url.Values
|
||||
@ -57,6 +58,11 @@ func (c *Context) Socket() *websocket.Conn {
|
||||
return c.socket
|
||||
}
|
||||
|
||||
// Path returns the registered path for a handler.
|
||||
func (c *Context) Path() string {
|
||||
return c.path
|
||||
}
|
||||
|
||||
// P returns path parameter by index.
|
||||
func (c *Context) P(i int) (value string) {
|
||||
l := len(c.pnames)
|
||||
@ -266,5 +272,6 @@ func (c *Context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
|
||||
c.response.reset(w)
|
||||
c.query = nil
|
||||
c.store = nil
|
||||
c.path = ""
|
||||
c.echo = e
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
"net/url"
|
||||
|
||||
"encoding/xml"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -227,6 +228,21 @@ func TestContext(t *testing.T) {
|
||||
c.reset(req, NewResponse(httptest.NewRecorder()), New())
|
||||
}
|
||||
|
||||
func TestContextPath(t *testing.T) {
|
||||
e := New()
|
||||
r := e.Router()
|
||||
|
||||
r.Add(GET, "/users/:id", nil, e)
|
||||
c := NewContext(nil, nil, e)
|
||||
r.Find(GET, "/users/1", c)
|
||||
assert.Equal(t, c.Path(), "/users/:id")
|
||||
|
||||
r.Add(GET, "/users/:uid/files/:fid", nil, e)
|
||||
c = NewContext(nil, nil, e)
|
||||
r.Find(GET, "/users/1/files/1", c)
|
||||
assert.Equal(t, c.Path(), "/users/:uid/files/:fid")
|
||||
}
|
||||
|
||||
func TestContextQuery(t *testing.T) {
|
||||
q := make(url.Values)
|
||||
q.Set("name", "joe")
|
||||
@ -239,7 +255,6 @@ func TestContextQuery(t *testing.T) {
|
||||
c := NewContext(req, nil, New())
|
||||
assert.Equal(t, "joe", c.Query("name"))
|
||||
assert.Equal(t, "joe@labstack.com", c.Query("email"))
|
||||
|
||||
}
|
||||
|
||||
func TestContextForm(t *testing.T) {
|
||||
|
@ -84,8 +84,3 @@ func (r *Response) reset(w http.ResponseWriter) {
|
||||
r.status = http.StatusOK
|
||||
r.committed = false
|
||||
}
|
||||
|
||||
//func (r *Response) clear() {
|
||||
// r.Header().Del(ContentType)
|
||||
// r.committed = false
|
||||
//}
|
||||
|
34
router.go
34
router.go
@ -14,8 +14,9 @@ type (
|
||||
prefix string
|
||||
parent *node
|
||||
children children
|
||||
methodHandler *methodHandler
|
||||
ppath string
|
||||
pnames []string
|
||||
methodHandler *methodHandler
|
||||
echo *Echo
|
||||
}
|
||||
kind uint8
|
||||
@ -50,13 +51,14 @@ func NewRouter(e *Echo) *Router {
|
||||
}
|
||||
|
||||
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
||||
ppath := path // Pristine path
|
||||
pnames := []string{} // Param names
|
||||
|
||||
for i, l := 0, len(path); i < l; i++ {
|
||||
if path[i] == ':' {
|
||||
j := i + 1
|
||||
|
||||
r.insert(method, path[:i], nil, skind, nil, e)
|
||||
r.insert(method, path[:i], nil, skind, "", nil, e)
|
||||
for ; i < l && path[i] != '/'; i++ {
|
||||
}
|
||||
|
||||
@ -65,22 +67,22 @@ func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
||||
i, l = j, len(path)
|
||||
|
||||
if i == l {
|
||||
r.insert(method, path[:i], h, pkind, pnames, e)
|
||||
r.insert(method, path[:i], h, pkind, ppath, pnames, e)
|
||||
return
|
||||
}
|
||||
r.insert(method, path[:i], nil, pkind, pnames, e)
|
||||
r.insert(method, path[:i], nil, pkind, ppath, pnames, e)
|
||||
} 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, "_*")
|
||||
r.insert(method, path[:i+1], h, mkind, pnames, e)
|
||||
r.insert(method, path[:i+1], h, mkind, ppath, pnames, e)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
r.insert(method, path, h, skind, pnames, e)
|
||||
r.insert(method, path, h, skind, ppath, pnames, e)
|
||||
}
|
||||
|
||||
func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []string, e *Echo) {
|
||||
func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string, e *Echo) {
|
||||
// Adjust max param
|
||||
l := len(pnames)
|
||||
if *e.maxParam < l {
|
||||
@ -113,12 +115,13 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
||||
if h != nil {
|
||||
cn.kind = t
|
||||
cn.addHandler(method, h)
|
||||
cn.ppath = ppath
|
||||
cn.pnames = pnames
|
||||
cn.echo = e
|
||||
}
|
||||
} else if l < pl {
|
||||
// Split node
|
||||
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.pnames, cn.echo)
|
||||
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.ppath, cn.pnames, cn.echo)
|
||||
|
||||
// Reset parent node
|
||||
cn.kind = skind
|
||||
@ -126,6 +129,7 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
||||
cn.prefix = cn.prefix[:l]
|
||||
cn.children = nil
|
||||
cn.methodHandler = new(methodHandler)
|
||||
cn.ppath = ""
|
||||
cn.pnames = nil
|
||||
cn.echo = nil
|
||||
|
||||
@ -135,11 +139,12 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
||||
// At parent node
|
||||
cn.kind = t
|
||||
cn.addHandler(method, h)
|
||||
cn.ppath = ppath
|
||||
cn.pnames = pnames
|
||||
cn.echo = e
|
||||
} else {
|
||||
// Create child node
|
||||
n = newNode(t, search[l:], cn, nil, new(methodHandler), pnames, e)
|
||||
n = newNode(t, search[l:], cn, nil, new(methodHandler), ppath, pnames, e)
|
||||
n.addHandler(method, h)
|
||||
cn.addChild(n)
|
||||
}
|
||||
@ -152,13 +157,14 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
||||
continue
|
||||
}
|
||||
// Create child node
|
||||
n := newNode(t, search, cn, nil, new(methodHandler), pnames, e)
|
||||
n := newNode(t, search, cn, nil, new(methodHandler), ppath, pnames, e)
|
||||
n.addHandler(method, h)
|
||||
cn.addChild(n)
|
||||
} else {
|
||||
// Node already exists
|
||||
if h != nil {
|
||||
cn.addHandler(method, h)
|
||||
cn.ppath = path
|
||||
cn.pnames = pnames
|
||||
cn.echo = e
|
||||
}
|
||||
@ -167,15 +173,16 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
||||
}
|
||||
}
|
||||
|
||||
func newNode(t kind, pre string, p *node, c children, mh *methodHandler, pnames []string, e *Echo) *node {
|
||||
func newNode(t kind, pre string, p *node, c children, mh *methodHandler, ppath string, pnames []string, e *Echo) *node {
|
||||
return &node{
|
||||
kind: t,
|
||||
label: pre[0],
|
||||
prefix: pre,
|
||||
parent: p,
|
||||
children: c,
|
||||
methodHandler: mh,
|
||||
ppath: ppath,
|
||||
pnames: pnames,
|
||||
methodHandler: mh,
|
||||
echo: e,
|
||||
}
|
||||
}
|
||||
@ -368,6 +375,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
||||
}
|
||||
|
||||
End:
|
||||
ctx.path = cn.ppath
|
||||
ctx.pnames = cn.pnames
|
||||
h = cn.findHandler(method)
|
||||
if cn.echo != nil {
|
||||
|
@ -6,6 +6,22 @@ menu:
|
||||
weight: 5
|
||||
---
|
||||
|
||||
### Handler path
|
||||
|
||||
`Context#Path()` returns the registered path for a handler, it can be used in the middleware for logging purpose.
|
||||
|
||||
*Example*
|
||||
|
||||
```go
|
||||
e.Use(func(c *echo.Context) error {
|
||||
log.Println(c.Path()) // Prints `/users/:name`
|
||||
return nil
|
||||
})
|
||||
e.Get("/users/:name", func(c *echo.Context) error) {
|
||||
return c.String(http.StatusOK, name)
|
||||
})
|
||||
```
|
||||
|
||||
### Path parameter
|
||||
|
||||
Path parameter can be retrieved either by name `Context.Param(name string) string`
|
||||
|
Loading…
Reference in New Issue
Block a user