Skip to content

Commit

Permalink
⚡️ fix: case sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjq4214 committed Aug 20, 2022
1 parent adcc067 commit 4c97ad8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
5 changes: 3 additions & 2 deletions ctx.go
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions ctx_test.go
Expand Up @@ -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",
},
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions router.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)))

Expand Down
10 changes: 10 additions & 0 deletions utils/strings.go
Expand Up @@ -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
}

0 comments on commit 4c97ad8

Please sign in to comment.