From 4c97ad843b3fdd13d09625a64eba6a821faf1237 Mon Sep 17 00:00:00 2001 From: Jinquan Wang Date: Sat, 20 Aug 2022 14:44:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20fix:=20case=20sensitivity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ctx.go | 5 +++-- ctx_test.go | 7 +++++-- router.go | 11 +++++++++++ utils/strings.go | 10 ++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/ctx.go b/ctx.go index 4bacb473045..38b8b6f45f1 100644 --- a/ctx.go +++ b/ctx.go @@ -857,11 +857,12 @@ func (c *Ctx) Params(key string, defaultValue ...string) string { if key == "*" || key == "+" { key += "1" } - for i := range c.route.Params { + + for i := 0; i < len(c.route.Params); i++ { if len(key) != len(c.route.Params[i]) { continue } - if c.route.Params[i] == key || (!c.app.config.CaseSensitive && utils.EqualFold(c.route.Params[i], key)) { + if c.route.Params[i] == key || (!c.app.config.CaseSensitive && utils.EqualWithUpper(key, c.route.Params[i], c.route.upParams[i])) { // in case values are not here if len(c.values) <= i || len(c.values[i]) == 0 { break diff --git a/ctx_test.go b/ctx_test.go index f5a64c27e81..44d8a5008e3 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -1505,6 +1505,9 @@ func Benchmark_Ctx_Params(b *testing.B) { c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) c.route = &Route{ + upParams: []string{ + "PARAM1", "PARAM2", "PARAM3", "PARAM4", + }, Params: []string{ "param1", "param2", "param3", "param4", }, @@ -2963,8 +2966,8 @@ func Test_Ctx_Get_Location_From_Route_name(t *testing.T) { utils.AssertEqual(t, nil, err) utils.AssertEqual(t, "/user/fiber", location) }) - - t.Run("case sensitive",func(t *testing.T) { + + t.Run("case sensitive", func(t *testing.T) { app := New(Config{CaseSensitive: true}) c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) diff --git a/router.go b/router.go index 69cc3c00da2..8ab224ffb95 100644 --- a/router.go +++ b/router.go @@ -51,6 +51,7 @@ type Route struct { star bool // Path equals '*' root bool // Path equals '/' path string // Prettified path + upParams []string // Case insensitive param keys routeParser routeParser // Parameter parser // Public fields @@ -267,6 +268,16 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Router { Method: method, Handlers: handlers, } + + if !app.config.CaseSensitive { + route.Params = parsedPretty.params + + route.upParams = make([]string, len(route.Params)) + for i := range route.Params { + route.upParams[i] = utils.ToUpper(route.Params[i]) + } + } + // Increment global handler count atomic.AddUint32(&app.handlersCount, uint32(len(handlers))) diff --git a/utils/strings.go b/utils/strings.go index 109d132f1ea..c171e085809 100644 --- a/utils/strings.go +++ b/utils/strings.go @@ -73,3 +73,13 @@ func EqualFold(b, s string) bool { } return true } + +func EqualWithUpper(key, low, up string) bool { + for i := len(key) - 1; i >= 0; i-- { + if key[i] != low[i] && key[i] != up[i] { + return false + } + } + + return true +}