get matched route in context

This commit is contained in:
zhoujun 2018-06-12 12:36:54 +08:00
parent caf3e350a5
commit 4a380288d1
4 changed files with 18 additions and 7 deletions

View File

@ -43,6 +43,7 @@ type Context struct {
Request *http.Request
Writer ResponseWriter
Route string
Params Params
handlers HandlersChain
index int8
@ -65,6 +66,7 @@ type Context struct {
func (c *Context) reset() {
c.Writer = &c.writermem
c.Route = ""
c.Params = c.Params[0:0]
c.handlers = nil
c.index = -1

5
gin.go
View File

@ -352,10 +352,11 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
if t[i].method == httpMethod {
root := t[i].root
// Find route in tree
handlers, params, tsr := root.getValue(path, c.Params, unescape)
handlers, params, route, tsr := root.getValue(path, c.Params, unescape)
if handlers != nil {
c.handlers = handlers
c.Params = params
c.Route = route
c.Next()
c.writermem.WriteHeaderNow()
return
@ -376,7 +377,7 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
if engine.HandleMethodNotAllowed {
for _, tree := range engine.trees {
if tree.method != httpMethod {
if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
if handlers, _, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
c.handlers = engine.allNoMethod
serveError(c, 405, default405Body)
return

View File

@ -362,12 +362,13 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
// made if a handle exists with an extra (without the) trailing slash for the
// given path.
func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, tsr bool) {
func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, route string, tsr bool) {
p = po
walk: // Outer loop for walking the tree
for {
if len(path) > len(n.path) {
if path[:len(n.path)] == n.path {
route += n.path
path = path[len(n.path):]
// If this node does not have a wildcard (param or catchAll)
// child, we can just look up the next child node and continue
@ -418,6 +419,7 @@ walk: // Outer loop for walking the tree
// we need to go deeper!
if end < len(path) {
if len(n.children) > 0 {
route += n.path
path = path[end:]
n = n.children[0]
continue walk
@ -429,6 +431,7 @@ walk: // Outer loop for walking the tree
}
if handlers = n.handlers; handlers != nil {
route += n.path
return
}
if len(n.children) == 1 {
@ -457,6 +460,7 @@ walk: // Outer loop for walking the tree
p[i].Value = path
}
route += n.path
handlers = n.handlers
return
@ -468,6 +472,7 @@ walk: // Outer loop for walking the tree
// We should have reached the node containing the handle.
// Check if this node has a handle registered.
if handlers = n.handlers; handlers != nil {
route += n.path
return
}

View File

@ -33,7 +33,7 @@ func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes ..
}
for _, request := range requests {
handler, ps, _ := tree.getValue(request.path, nil, unescape)
handler, ps, route, _ := tree.getValue(request.path, nil, unescape)
if handler == nil {
if !request.nilHandler {
@ -46,6 +46,9 @@ func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes ..
if fakeHandlerValue != request.route {
t.Errorf("handle mismatch for route '%s': Wrong handle (%s != %s)", request.path, fakeHandlerValue, request.route)
}
if route != request.route {
t.Errorf("handle mismatch for route '%s': Wrong route (%s != %s)", request.path, route, request.route)
}
}
if !reflect.DeepEqual(ps, request.ps) {
@ -463,7 +466,7 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/doc/",
}
for _, route := range tsrRoutes {
handler, _, tsr := tree.getValue(route, nil, false)
handler, _, _, tsr := tree.getValue(route, nil, false)
if handler != nil {
t.Fatalf("non-nil handler for TSR route '%s", route)
} else if !tsr {
@ -480,7 +483,7 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/api/world/abc",
}
for _, route := range noTsrRoutes {
handler, _, tsr := tree.getValue(route, nil, false)
handler, _, _, tsr := tree.getValue(route, nil, false)
if handler != nil {
t.Fatalf("non-nil handler for No-TSR route '%s", route)
} else if tsr {
@ -499,7 +502,7 @@ func TestTreeRootTrailingSlashRedirect(t *testing.T) {
t.Fatalf("panic inserting test route: %v", recv)
}
handler, _, tsr := tree.getValue("/", nil, false)
handler, _, _, tsr := tree.getValue("/", nil, false)
if handler != nil {
t.Fatalf("non-nil handler")
} else if tsr {