mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 10:02:10 +08:00
add RelativePath
This commit is contained in:
parent
220e8d3453
commit
ff434e2667
@ -57,6 +57,9 @@ type Context struct {
|
|||||||
|
|
||||||
// Accepted defines a list of manually accepted formats for content negotiation.
|
// Accepted defines a list of manually accepted formats for content negotiation.
|
||||||
Accepted []string
|
Accepted []string
|
||||||
|
|
||||||
|
// save the route's relativePath
|
||||||
|
RelativePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
|
3
gin.go
3
gin.go
@ -352,7 +352,8 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
if t[i].method == httpMethod {
|
if t[i].method == httpMethod {
|
||||||
root := t[i].root
|
root := t[i].root
|
||||||
// Find route in tree
|
// 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 {
|
if handlers != nil {
|
||||||
c.handlers = handlers
|
c.handlers = handlers
|
||||||
c.Params = params
|
c.Params = params
|
||||||
|
9
tree.go
9
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
|
// 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
|
// made if a handle exists with an extra (without the) trailing slash for the
|
||||||
// given path.
|
// 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
|
p = po
|
||||||
|
relativePath = path
|
||||||
walk: // Outer loop for walking the tree
|
walk: // Outer loop for walking the tree
|
||||||
for {
|
for {
|
||||||
if len(path) > len(n.path) {
|
if len(path) > len(n.path) {
|
||||||
@ -415,6 +416,9 @@ walk: // Outer loop for walking the tree
|
|||||||
p[i].Value = val
|
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!
|
// we need to go deeper!
|
||||||
if end < len(path) {
|
if end < len(path) {
|
||||||
if len(n.children) > 0 {
|
if len(n.children) > 0 {
|
||||||
@ -457,6 +461,9 @@ walk: // Outer loop for walking the tree
|
|||||||
p[i].Value = path
|
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
|
handlers = n.handlers
|
||||||
return
|
return
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user