diff --git a/context.go b/context.go index 724ded79..1360cc1c 100644 --- a/context.go +++ b/context.go @@ -57,6 +57,9 @@ type Context struct { // Accepted defines a list of manually accepted formats for content negotiation. Accepted []string + + // save the route's relativePath + RelativePath string } /************************************/ diff --git a/gin.go b/gin.go index 3ee8018d..35b6b5e2 100644 --- a/gin.go +++ b/gin.go @@ -352,7 +352,8 @@ 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, relativePath, tsr := root.getValue(path, c.Params, unescape) + c.RelativePath = relativePath if handlers != nil { c.handlers = handlers c.Params = params diff --git a/tree.go b/tree.go index b6530665..4766177b 100644 --- a/tree.go +++ b/tree.go @@ -362,8 +362,9 @@ 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, relativePath string, tsr bool) { p = po + relativePath = path walk: // Outer loop for walking the tree for { if len(path) > len(n.path) { @@ -415,6 +416,9 @@ walk: // Outer loop for walking the tree p[i].Value = val } + // replace p.value with p.key (pattern :) + relativePath = strings.Replace(relativePath, p[i].Value, ":"+p[i].Key, 1) + // we need to go deeper! if end < len(path) { if len(n.children) > 0 { @@ -457,6 +461,9 @@ walk: // Outer loop for walking the tree p[i].Value = path } + // replace p.value with p.key (pattern *) + relativePath = strings.Replace(relativePath, p[i].Value, "/*"+p[i].Key, 1) + handlers = n.handlers return