@@ -49,28 +49,26 @@ const userContextKey contextKey = 0 // __local_user_context__
49
49
//
50
50
//go:generate ifacemaker --file ctx.go --struct DefaultCtx --iface Ctx --pkg fiber --output ctx_interface_gen.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
51
51
type DefaultCtx struct {
52
- app * App // Reference to *App
53
- route * Route // Reference to *Route
54
- fasthttp * fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
55
- bind * Bind // Default bind reference
56
- redirect * Redirect // Default redirect reference
57
- req * DefaultReq // Default request api reference
58
- res * DefaultRes // Default response api reference
59
- values [maxParams ]string // Route parameter values
60
- viewBindMap sync.Map // Default view map to bind template engine
61
- method string // HTTP method
62
- baseURI string // HTTP base uri
63
- path string // HTTP path with the modifications by the configuration -> string copy from pathBuffer
64
- detectionPath string // Route detection path -> string copy from detectionPathBuffer
65
- treePath string // Path for the search in the tree
66
- pathOriginal string // Original HTTP path
67
- pathBuffer []byte // HTTP path buffer
68
- detectionPathBuffer []byte // HTTP detectionPath buffer
69
- flashMessages redirectionMsgs // Flash messages
70
- indexRoute int // Index of the current route
71
- indexHandler int // Index of the current handler
72
- methodINT int // HTTP method INT equivalent
73
- matched bool // Non use route matched
52
+ app * App // Reference to *App
53
+ route * Route // Reference to *Route
54
+ fasthttp * fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
55
+ bind * Bind // Default bind reference
56
+ redirect * Redirect // Default redirect reference
57
+ req * DefaultReq // Default request api reference
58
+ res * DefaultRes // Default response api reference
59
+ values [maxParams ]string // Route parameter values
60
+ viewBindMap sync.Map // Default view map to bind template engine
61
+ method string // HTTP method
62
+ baseURI string // HTTP base uri
63
+ treePath string // Path for the search in the tree
64
+ pathOriginal string // Original HTTP path
65
+ flashMessages redirectionMsgs // Flash messages
66
+ path []byte // HTTP path with the modifications by the configuration
67
+ detectionPath []byte // Route detection path
68
+ indexRoute int // Index of the current route
69
+ indexHandler int // Index of the current handler
70
+ methodINT int // HTTP method INT equivalent
71
+ matched bool // Non use route matched
74
72
}
75
73
76
74
// SendFile defines configuration options when to transfer file with SendFile.
@@ -1123,8 +1121,9 @@ func Params[V GenericType](c Ctx, key string, defaultValue ...V) V {
1123
1121
1124
1122
// Path returns the path part of the request URL.
1125
1123
// Optionally, you could override the path.
1124
+ // Make copies or use the Immutable setting to use the value outside the Handler.
1126
1125
func (c * DefaultCtx ) Path (override ... string ) string {
1127
- if len (override ) != 0 && c .path != override [0 ] {
1126
+ if len (override ) != 0 && string ( c .path ) != override [0 ] {
1128
1127
// Set new path to context
1129
1128
c .pathOriginal = override [0 ]
1130
1129
@@ -1133,7 +1132,7 @@ func (c *DefaultCtx) Path(override ...string) string {
1133
1132
// Prettify path
1134
1133
c .configDependentPaths ()
1135
1134
}
1136
- return c .path
1135
+ return c .app . getString ( c . path )
1137
1136
}
1138
1137
1139
1138
// Scheme contains the request protocol string: http or https for TLS requests.
@@ -1832,32 +1831,32 @@ func (c *DefaultCtx) XHR() bool {
1832
1831
// configDependentPaths set paths for route recognition and prepared paths for the user,
1833
1832
// here the features for caseSensitive, decoded paths, strict paths are evaluated
1834
1833
func (c * DefaultCtx ) configDependentPaths () {
1835
- c .pathBuffer = append (c .pathBuffer [ 0 :0 ], c .pathOriginal ... )
1834
+ c .path = append (c .path [ :0 ], c .pathOriginal ... )
1836
1835
// If UnescapePath enabled, we decode the path and save it for the framework user
1837
1836
if c .app .config .UnescapePath {
1838
- c .pathBuffer = fasthttp .AppendUnquotedArg (c .pathBuffer [:0 ], c .pathBuffer )
1837
+ c .path = fasthttp .AppendUnquotedArg (c .path [:0 ], c .path )
1839
1838
}
1840
- c .path = c .app .getString (c .pathBuffer )
1841
1839
1842
1840
// another path is specified which is for routing recognition only
1843
1841
// use the path that was changed by the previous configuration flags
1844
- c .detectionPathBuffer = append (c .detectionPathBuffer [ 0 :0 ], c .pathBuffer ... )
1842
+ c .detectionPath = append (c .detectionPath [ :0 ], c .path ... )
1845
1843
// If CaseSensitive is disabled, we lowercase the original path
1846
1844
if ! c .app .config .CaseSensitive {
1847
- c .detectionPathBuffer = utils .ToLowerBytes (c .detectionPathBuffer )
1845
+ c .detectionPath = utils .ToLowerBytes (c .detectionPath )
1848
1846
}
1849
1847
// If StrictRouting is disabled, we strip all trailing slashes
1850
- if ! c .app .config .StrictRouting && len (c .detectionPathBuffer ) > 1 && c .detectionPathBuffer [len (c .detectionPathBuffer )- 1 ] == '/' {
1851
- c .detectionPathBuffer = utils .TrimRight (c .detectionPathBuffer , '/' )
1848
+ if ! c .app .config .StrictRouting && len (c .detectionPath ) > 1 && c .detectionPath [len (c .detectionPath )- 1 ] == '/' {
1849
+ c .detectionPath = utils .TrimRight (c .detectionPath , '/' )
1852
1850
}
1853
- c .detectionPath = c .app .getString (c .detectionPathBuffer )
1854
1851
1855
1852
// Define the path for dividing routes into areas for fast tree detection, so that fewer routes need to be traversed,
1856
1853
// since the first three characters area select a list of routes
1857
- c .treePath = c . treePath [ 0 : 0 ]
1854
+ c .treePath = ""
1858
1855
const maxDetectionPaths = 3
1859
1856
if len (c .detectionPath ) >= maxDetectionPaths {
1860
- c .treePath = c .detectionPath [:maxDetectionPaths ]
1857
+ // c.treePath is only used by Fiber and is not exposed to the user
1858
+ // so we can use utils.UnsafeString instead of c.app.getString
1859
+ c .treePath = utils .UnsafeString (c .detectionPath [:maxDetectionPaths ])
1861
1860
}
1862
1861
}
1863
1862
@@ -1963,7 +1962,7 @@ func (c *DefaultCtx) getTreePath() string {
1963
1962
}
1964
1963
1965
1964
func (c * DefaultCtx ) getDetectionPath () string {
1966
- return c .detectionPath
1965
+ return c .app . getString ( c . detectionPath )
1967
1966
}
1968
1967
1969
1968
func (c * DefaultCtx ) getPathOriginal () string {
0 commit comments