mirror of
https://github.com/labstack/echo.git
synced 2025-04-25 12:24:55 +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
|
request *http.Request
|
||||||
response *Response
|
response *Response
|
||||||
socket *websocket.Conn
|
socket *websocket.Conn
|
||||||
|
path string
|
||||||
pnames []string
|
pnames []string
|
||||||
pvalues []string
|
pvalues []string
|
||||||
query url.Values
|
query url.Values
|
||||||
@ -57,6 +58,11 @@ func (c *Context) Socket() *websocket.Conn {
|
|||||||
return c.socket
|
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.
|
// P returns path parameter by index.
|
||||||
func (c *Context) P(i int) (value string) {
|
func (c *Context) P(i int) (value string) {
|
||||||
l := len(c.pnames)
|
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.response.reset(w)
|
||||||
c.query = nil
|
c.query = nil
|
||||||
c.store = nil
|
c.store = nil
|
||||||
|
c.path = ""
|
||||||
c.echo = e
|
c.echo = e
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -227,6 +228,21 @@ func TestContext(t *testing.T) {
|
|||||||
c.reset(req, NewResponse(httptest.NewRecorder()), New())
|
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) {
|
func TestContextQuery(t *testing.T) {
|
||||||
q := make(url.Values)
|
q := make(url.Values)
|
||||||
q.Set("name", "joe")
|
q.Set("name", "joe")
|
||||||
@ -239,7 +255,6 @@ func TestContextQuery(t *testing.T) {
|
|||||||
c := NewContext(req, nil, New())
|
c := NewContext(req, nil, New())
|
||||||
assert.Equal(t, "joe", c.Query("name"))
|
assert.Equal(t, "joe", c.Query("name"))
|
||||||
assert.Equal(t, "joe@labstack.com", c.Query("email"))
|
assert.Equal(t, "joe@labstack.com", c.Query("email"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextForm(t *testing.T) {
|
func TestContextForm(t *testing.T) {
|
||||||
|
@ -84,8 +84,3 @@ func (r *Response) reset(w http.ResponseWriter) {
|
|||||||
r.status = http.StatusOK
|
r.status = http.StatusOK
|
||||||
r.committed = false
|
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
|
prefix string
|
||||||
parent *node
|
parent *node
|
||||||
children children
|
children children
|
||||||
methodHandler *methodHandler
|
ppath string
|
||||||
pnames []string
|
pnames []string
|
||||||
|
methodHandler *methodHandler
|
||||||
echo *Echo
|
echo *Echo
|
||||||
}
|
}
|
||||||
kind uint8
|
kind uint8
|
||||||
@ -50,13 +51,14 @@ func NewRouter(e *Echo) *Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
||||||
|
ppath := path // Pristine path
|
||||||
pnames := []string{} // Param names
|
pnames := []string{} // Param names
|
||||||
|
|
||||||
for i, l := 0, len(path); i < l; i++ {
|
for i, l := 0, len(path); i < l; i++ {
|
||||||
if path[i] == ':' {
|
if path[i] == ':' {
|
||||||
j := i + 1
|
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++ {
|
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)
|
i, l = j, len(path)
|
||||||
|
|
||||||
if i == l {
|
if i == l {
|
||||||
r.insert(method, path[:i], h, pkind, pnames, e)
|
r.insert(method, path[:i], h, pkind, ppath, pnames, e)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.insert(method, path[:i], nil, pkind, pnames, e)
|
r.insert(method, path[:i], nil, pkind, ppath, pnames, e)
|
||||||
} else if path[i] == '*' {
|
} 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, "_*")
|
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
|
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
|
// Adjust max param
|
||||||
l := len(pnames)
|
l := len(pnames)
|
||||||
if *e.maxParam < l {
|
if *e.maxParam < l {
|
||||||
@ -113,12 +115,13 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
|||||||
if h != nil {
|
if h != nil {
|
||||||
cn.kind = t
|
cn.kind = t
|
||||||
cn.addHandler(method, h)
|
cn.addHandler(method, h)
|
||||||
|
cn.ppath = ppath
|
||||||
cn.pnames = pnames
|
cn.pnames = pnames
|
||||||
cn.echo = e
|
cn.echo = e
|
||||||
}
|
}
|
||||||
} else if l < pl {
|
} else if l < pl {
|
||||||
// Split node
|
// 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
|
// Reset parent node
|
||||||
cn.kind = skind
|
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.prefix = cn.prefix[:l]
|
||||||
cn.children = nil
|
cn.children = nil
|
||||||
cn.methodHandler = new(methodHandler)
|
cn.methodHandler = new(methodHandler)
|
||||||
|
cn.ppath = ""
|
||||||
cn.pnames = nil
|
cn.pnames = nil
|
||||||
cn.echo = nil
|
cn.echo = nil
|
||||||
|
|
||||||
@ -135,11 +139,12 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
|||||||
// At parent node
|
// At parent node
|
||||||
cn.kind = t
|
cn.kind = t
|
||||||
cn.addHandler(method, h)
|
cn.addHandler(method, h)
|
||||||
|
cn.ppath = ppath
|
||||||
cn.pnames = pnames
|
cn.pnames = pnames
|
||||||
cn.echo = e
|
cn.echo = e
|
||||||
} else {
|
} else {
|
||||||
// Create child node
|
// 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)
|
n.addHandler(method, h)
|
||||||
cn.addChild(n)
|
cn.addChild(n)
|
||||||
}
|
}
|
||||||
@ -152,13 +157,14 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, pnames []str
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Create child node
|
// 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)
|
n.addHandler(method, h)
|
||||||
cn.addChild(n)
|
cn.addChild(n)
|
||||||
} else {
|
} else {
|
||||||
// Node already exists
|
// Node already exists
|
||||||
if h != nil {
|
if h != nil {
|
||||||
cn.addHandler(method, h)
|
cn.addHandler(method, h)
|
||||||
|
cn.ppath = path
|
||||||
cn.pnames = pnames
|
cn.pnames = pnames
|
||||||
cn.echo = e
|
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{
|
return &node{
|
||||||
kind: t,
|
kind: t,
|
||||||
label: pre[0],
|
label: pre[0],
|
||||||
prefix: pre,
|
prefix: pre,
|
||||||
parent: p,
|
parent: p,
|
||||||
children: c,
|
children: c,
|
||||||
methodHandler: mh,
|
ppath: ppath,
|
||||||
pnames: pnames,
|
pnames: pnames,
|
||||||
|
methodHandler: mh,
|
||||||
echo: e,
|
echo: e,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -368,6 +375,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
}
|
}
|
||||||
|
|
||||||
End:
|
End:
|
||||||
|
ctx.path = cn.ppath
|
||||||
ctx.pnames = cn.pnames
|
ctx.pnames = cn.pnames
|
||||||
h = cn.findHandler(method)
|
h = cn.findHandler(method)
|
||||||
if cn.echo != nil {
|
if cn.echo != nil {
|
||||||
|
@ -6,6 +6,22 @@ menu:
|
|||||||
weight: 5
|
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
|
||||||
|
|
||||||
Path parameter can be retrieved either by name `Context.Param(name string) string`
|
Path parameter can be retrieved either by name `Context.Param(name string) string`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user