From ead3f654d6fb066f3274f7351e4ad8f7e9b5bbe4 Mon Sep 17 00:00:00 2001 From: zhaotong Date: Fri, 21 Sep 2018 15:24:45 +0800 Subject: [PATCH] feat:add func BeforeHandle to Engine --- gin.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gin.go b/gin.go index 11f6d94a..303e5a70 100644 --- a/gin.go +++ b/gin.go @@ -104,6 +104,11 @@ type Engine struct { noMethod HandlersChain pool sync.Pool 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{} @@ -342,9 +347,15 @@ func (engine *Engine) HandleContext(c *Context) { func (engine *Engine) handleHTTPRequest(c *Context) { httpMethod := c.Request.Method path := c.Request.URL.Path + rawPath := c.Request.URL.RawPath + if engine.BeforeHandle != nil { + engine.BeforeHandle(c, &httpMethod, &path, &rawPath) + } + 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 }