mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 01:57:55 +08:00
feat:add func BeforeHandle to Engine
This commit is contained in:
parent
a210eea3bd
commit
ead3f654d6
15
gin.go
15
gin.go
@ -104,6 +104,11 @@ type Engine struct {
|
|||||||
noMethod HandlersChain
|
noMethod HandlersChain
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
trees methodTrees
|
trees methodTrees
|
||||||
|
|
||||||
|
// If defiend, invoke before `handleHTTPRequest`
|
||||||
|
// Change `httpMethod`, `path`, `rawPath` for complex route match,it will not change origin `Context.Request`
|
||||||
|
// Note: If necessary, DON'T MODIFIY `Context`
|
||||||
|
BeforeHandle func(c *Context, httpMethod, path, rawPath *string)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ IRouter = &Engine{}
|
var _ IRouter = &Engine{}
|
||||||
@ -342,9 +347,15 @@ func (engine *Engine) HandleContext(c *Context) {
|
|||||||
func (engine *Engine) handleHTTPRequest(c *Context) {
|
func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||||
httpMethod := c.Request.Method
|
httpMethod := c.Request.Method
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
|
rawPath := c.Request.URL.RawPath
|
||||||
|
if engine.BeforeHandle != nil {
|
||||||
|
engine.BeforeHandle(c, &httpMethod, &path, &rawPath)
|
||||||
|
}
|
||||||
|
|
||||||
unescape := false
|
unescape := false
|
||||||
if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {
|
|
||||||
path = c.Request.URL.RawPath
|
if engine.UseRawPath && len(rawPath) > 0 {
|
||||||
|
path = rawPath
|
||||||
unescape = engine.UnescapePathValues
|
unescape = engine.UnescapePathValues
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user