Skip to content

Commit

Permalink
Closes labstack#235, Closes labstack#203
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Nov 13, 2015
1 parent e48ebd3 commit 56954bc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
7 changes: 7 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
request *http.Request
response *Response
socket *websocket.Conn
path string
pnames []string
pvalues []string
query url.Values
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
17 changes: 16 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"

"encoding/xml"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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")
Expand All @@ -239,7 +255,6 @@ func TestContextQuery(t *testing.T) {
c := NewContext(req, nil, New())
assert.Equal(t, "joe", c.Query("name"))
assert.Equal(t, "[email protected]", c.Query("email"))

}

func TestContextForm(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 21 additions & 13 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type (
prefix string
parent *node
children children
methodHandler *methodHandler
ppath string
pnames []string
methodHandler *methodHandler
echo *Echo
}
kind uint8
Expand Down Expand Up @@ -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++ {
}

Expand All @@ -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 {
Expand Down Expand Up @@ -113,19 +115,21 @@ 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
cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l]
cn.children = nil
cn.methodHandler = new(methodHandler)
cn.ppath = ""
cn.pnames = nil
cn.echo = nil

Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions website/content/guide/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 56954bc

Please sign in to comment.