diff --git a/client/response.go b/client/response.go index 84710768..a8a032b6 100644 --- a/client/response.go +++ b/client/response.go @@ -8,7 +8,6 @@ import ( "io/fs" "os" "path/filepath" - "strings" "sync" "github.com/gofiber/utils/v2" @@ -68,7 +67,7 @@ func (r *Response) Body() []byte { // String method returns the body of the server response as String. func (r *Response) String() string { - return strings.TrimSpace(string(r.Body())) + return utils.Trim(string(r.Body()), ' ') } // JSON method will unmarshal body to json. diff --git a/ctx.go b/ctx.go index eabfa7e1..b10a5e83 100644 --- a/ctx.go +++ b/ctx.go @@ -778,7 +778,7 @@ iploop: i++ } - s := strings.TrimRight(headerValue[i:j], " ") + s := utils.TrimRight(headerValue[i:j], ' ') if c.app.config.EnableIPValidation { // Skip validation if IP is clearly not IPv4/IPv6, otherwise validate without allocations @@ -828,7 +828,7 @@ func (c *DefaultCtx) extractIPFromHeader(header string) string { i++ } - s := strings.TrimRight(headerValue[i:j], " ") + s := utils.TrimRight(headerValue[i:j], ' ') if c.app.config.EnableIPValidation { if (!v6 && !v4) || (v6 && !utils.IsIPv6(s)) || (v4 && !utils.IsIPv4(s)) { @@ -862,7 +862,7 @@ func (c *DefaultCtx) Is(extension string) bool { } return strings.HasPrefix( - strings.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), " "), + utils.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), ' '), extensionHeader, ) } @@ -939,7 +939,7 @@ func (c *DefaultCtx) Links(link ...string) { bb.WriteString(`; rel="` + link[i] + `",`) } } - c.setCanonical(HeaderLink, strings.TrimRight(c.app.getString(bb.Bytes()), ",")) + c.setCanonical(HeaderLink, utils.TrimRight(c.app.getString(bb.Bytes()), ',')) bytebufferpool.Put(bb) } @@ -1810,7 +1810,7 @@ func (c *DefaultCtx) configDependentPaths() { } // If StrictRouting is disabled, we strip all trailing slashes if !c.app.config.StrictRouting && len(c.detectionPathBuffer) > 1 && c.detectionPathBuffer[len(c.detectionPathBuffer)-1] == '/' { - c.detectionPathBuffer = bytes.TrimRight(c.detectionPathBuffer, "/") + c.detectionPathBuffer = utils.TrimRight(c.detectionPathBuffer, '/') } c.detectionPath = c.app.getString(c.detectionPathBuffer) diff --git a/go.mod b/go.mod index 7d01cbec..0f53ba14 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/gofiber/fiber/v3 go 1.21 require ( - github.com/gofiber/utils/v2 v2.0.0-beta.5 + github.com/gofiber/utils/v2 v2.0.0-beta.6 github.com/google/uuid v1.6.0 github.com/mattn/go-colorable v0.1.13 github.com/mattn/go-isatty v0.0.20 diff --git a/go.sum b/go.sum index f64b064c..48410522 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1 github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gofiber/utils/v2 v2.0.0-beta.5 h1:zbDIU8gVAlZ2Ak9Fk8APlis4S7wUiQFbcvv6UASkm6A= -github.com/gofiber/utils/v2 v2.0.0-beta.5/go.mod h1:3Kz8Px3jInKFvqxDzDeoSygwEOO+3uyubTmUa6PqY+0= +github.com/gofiber/utils/v2 v2.0.0-beta.6 h1:ED62bOmpRXdgviPlfTmf0Q+AXzhaTUAFtdWjgx+XkYI= +github.com/gofiber/utils/v2 v2.0.0-beta.6/go.mod h1:3Kz8Px3jInKFvqxDzDeoSygwEOO+3uyubTmUa6PqY+0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= diff --git a/helpers.go b/helpers.go index 804ab78f..2a2a2cba 100644 --- a/helpers.go +++ b/helpers.go @@ -222,7 +222,7 @@ func getGroupPath(prefix, path string) string { path = "/" + path } - return strings.TrimRight(prefix, "/") + path + return utils.TrimRight(prefix, '/') + path } // acceptsOffer This function determines if an offer matches a given specification. @@ -336,7 +336,7 @@ func getSplicedStrList(headerValue string, dst []string) []string { dst = make([]string, len(dst)+(len(dst)>>1)+2) copy(dst, oldSlice) } - dst[insertIndex] = strings.TrimLeft(headerValue[lastElementEndsAt:index], " ") + dst[insertIndex] = utils.TrimLeft(headerValue[lastElementEndsAt:index], ' ') lastElementEndsAt = uint8(index + 1) insertIndex++ } @@ -356,7 +356,7 @@ func forEachMediaRange(header []byte, functor func([]byte)) { for len(header) > 0 { n := 0 - header = bytes.TrimLeft(header, " ") + header = utils.TrimLeft(header, ' ') quotes := 0 escaping := false @@ -459,7 +459,7 @@ func getOffer(header []byte, isAccepted func(spec, offer string, specParams head } } - spec = bytes.TrimSpace(spec) + spec = utils.Trim(spec, ' ') // Determine specificity var specificity int diff --git a/middleware/cache/manager_msgp_test.go b/middleware/cache/manager_msgp_test.go index ab4d912b..2b7fafc9 100644 --- a/middleware/cache/manager_msgp_test.go +++ b/middleware/cache/manager_msgp_test.go @@ -8,7 +8,7 @@ import ( "github.com/tinylib/msgp/msgp" ) -func Test_MarshalUnmarshalitem(t *testing.T) { +func Test_Cache_MarshalUnmarshalitem(t *testing.T) { v := item{} bts, err := v.MarshalMsg(nil) if err != nil { @@ -31,7 +31,7 @@ func Test_MarshalUnmarshalitem(t *testing.T) { } } -func Benchmark_MarshalMsgitem(b *testing.B) { +func Benchmark_Cache_MarshalMsgitem(b *testing.B) { v := item{} b.ReportAllocs() b.ResetTimer() @@ -40,7 +40,7 @@ func Benchmark_MarshalMsgitem(b *testing.B) { } } -func Benchmark_AppendMsgitem(b *testing.B) { +func Benchmark_Cache_AppendMsgitem(b *testing.B) { v := item{} bts := make([]byte, 0, v.Msgsize()) bts, _ = v.MarshalMsg(bts[0:0]) @@ -52,7 +52,7 @@ func Benchmark_AppendMsgitem(b *testing.B) { } } -func Benchmark_Unmarshalitem(b *testing.B) { +func Benchmark_Cache_Unmarshalitem(b *testing.B) { v := item{} bts, _ := v.MarshalMsg(nil) b.ReportAllocs() diff --git a/middleware/cors/cors.go b/middleware/cors/cors.go index e0fac917..7b17e143 100644 --- a/middleware/cors/cors.go +++ b/middleware/cors/cors.go @@ -6,6 +6,7 @@ import ( "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/log" + "github.com/gofiber/utils/v2" ) // New creates a new middleware handler @@ -44,7 +45,7 @@ func New(config ...Config) fiber.Handler { break } if i := strings.Index(origin, "://*."); i != -1 { - trimmedOrigin := strings.TrimSpace(origin[:i+3] + origin[i+4:]) + trimmedOrigin := utils.Trim(origin[:i+3]+origin[i+4:], ' ') isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin) if !isValid { panic("[CORS] Invalid origin format in configuration: " + trimmedOrigin) @@ -52,7 +53,7 @@ func New(config ...Config) fiber.Handler { sd := subdomain{prefix: normalizedOrigin[:i+3], suffix: normalizedOrigin[i+3:]} allowSOrigins = append(allowSOrigins, sd) } else { - trimmedOrigin := strings.TrimSpace(origin) + trimmedOrigin := utils.Trim(origin, ' ') isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin) if !isValid { panic("[CORS] Invalid origin format in configuration: " + trimmedOrigin) diff --git a/middleware/csrf/csrf.go b/middleware/csrf/csrf.go index 182cbaea..d4177304 100644 --- a/middleware/csrf/csrf.go +++ b/middleware/csrf/csrf.go @@ -8,6 +8,7 @@ import ( "time" "github.com/gofiber/fiber/v3" + "github.com/gofiber/utils/v2" ) var ( @@ -62,7 +63,7 @@ func New(config ...Config) fiber.Handler { for _, origin := range cfg.TrustedOrigins { if i := strings.Index(origin, "://*."); i != -1 { - trimmedOrigin := strings.TrimSpace(origin[:i+3] + origin[i+4:]) + trimmedOrigin := utils.Trim(origin[:i+3]+origin[i+4:], ' ') isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin) if !isValid { panic("[CSRF] Invalid origin format in configuration:" + origin) @@ -70,7 +71,7 @@ func New(config ...Config) fiber.Handler { sd := subdomain{prefix: normalizedOrigin[:i+3], suffix: normalizedOrigin[i+3:]} trustedSubOrigins = append(trustedSubOrigins, sd) } else { - trimmedOrigin := strings.TrimSpace(origin) + trimmedOrigin := utils.Trim(origin, ' ') isValid, normalizedOrigin := normalizeOrigin(trimmedOrigin) if !isValid { panic("[CSRF] Invalid origin format in configuration:" + origin) diff --git a/middleware/csrf/csrf_test.go b/middleware/csrf/csrf_test.go index e98902be..82252549 100644 --- a/middleware/csrf/csrf_test.go +++ b/middleware/csrf/csrf_test.go @@ -139,7 +139,7 @@ func Test_CSRF_WithSession(t *testing.T) { h(ctx) token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie)) for _, header := range strings.Split(token, ";") { - if strings.Split(strings.TrimSpace(header), "=")[0] == ConfigDefault.CookieName { + if strings.Split(utils.Trim(header, ' '), "=")[0] == ConfigDefault.CookieName { token = strings.Split(header, "=")[1] break } @@ -248,7 +248,7 @@ func Test_CSRF_ExpiredToken_WithSession(t *testing.T) { h(ctx) token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie)) for _, header := range strings.Split(token, ";") { - if strings.Split(strings.TrimSpace(header), "=")[0] == ConfigDefault.CookieName { + if strings.Split(utils.Trim(header, ' '), "=")[0] == ConfigDefault.CookieName { token = strings.Split(header, "=")[1] break } diff --git a/middleware/csrf/manager_msgp_test.go b/middleware/csrf/manager_msgp_test.go index 4d2bd237..e83d55d5 100644 --- a/middleware/csrf/manager_msgp_test.go +++ b/middleware/csrf/manager_msgp_test.go @@ -8,7 +8,7 @@ import ( "github.com/tinylib/msgp/msgp" ) -func Test_MarshalUnmarshalitem(t *testing.T) { +func Test_Csrf_MarshalUnmarshalitem(t *testing.T) { v := item{} bts, err := v.MarshalMsg(nil) if err != nil { @@ -31,7 +31,7 @@ func Test_MarshalUnmarshalitem(t *testing.T) { } } -func Benchmark_MarshalMsgitem(b *testing.B) { +func Benchmark_Csrf_MarshalMsgitem(b *testing.B) { v := item{} b.ReportAllocs() b.ResetTimer() @@ -40,7 +40,7 @@ func Benchmark_MarshalMsgitem(b *testing.B) { } } -func Benchmark_AppendMsgitem(b *testing.B) { +func Benchmark_Csrf_AppendMsgitem(b *testing.B) { v := item{} bts := make([]byte, 0, v.Msgsize()) bts, _ = v.MarshalMsg(bts[0:0]) @@ -52,7 +52,7 @@ func Benchmark_AppendMsgitem(b *testing.B) { } } -func Benchmark_Unmarshalitem(b *testing.B) { +func Benchmark_Csrf_Unmarshalitem(b *testing.B) { v := item{} bts, _ := v.MarshalMsg(nil) b.ReportAllocs() diff --git a/middleware/limiter/manager_msgp_test.go b/middleware/limiter/manager_msgp_test.go index 39562df3..56d9ec50 100644 --- a/middleware/limiter/manager_msgp_test.go +++ b/middleware/limiter/manager_msgp_test.go @@ -8,7 +8,7 @@ import ( "github.com/tinylib/msgp/msgp" ) -func Test_MarshalUnmarshalitem(t *testing.T) { +func Test_Limiter_MarshalUnmarshalitem(t *testing.T) { v := item{} bts, err := v.MarshalMsg(nil) if err != nil { @@ -31,7 +31,7 @@ func Test_MarshalUnmarshalitem(t *testing.T) { } } -func Benchmark_MarshalMsgitem(b *testing.B) { +func Benchmark_Limiter_MarshalMsgitem(b *testing.B) { v := item{} b.ReportAllocs() b.ResetTimer() @@ -40,7 +40,7 @@ func Benchmark_MarshalMsgitem(b *testing.B) { } } -func Benchmark_AppendMsgitem(b *testing.B) { +func Benchmark_Limiter_AppendMsgitem(b *testing.B) { v := item{} bts := make([]byte, 0, v.Msgsize()) bts, _ = v.MarshalMsg(bts[0:0]) @@ -52,7 +52,7 @@ func Benchmark_AppendMsgitem(b *testing.B) { } } -func Benchmark_Unmarshalitem(b *testing.B) { +func Benchmark_Limiter_Unmarshalitem(b *testing.B) { v := item{} bts, _ := v.MarshalMsg(nil) b.ReportAllocs() diff --git a/middleware/pprof/pprof.go b/middleware/pprof/pprof.go index bd6a58e7..13b01b53 100644 --- a/middleware/pprof/pprof.go +++ b/middleware/pprof/pprof.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/gofiber/fiber/v3" + "github.com/gofiber/utils/v2" "github.com/valyala/fasthttp/fasthttpadaptor" ) @@ -71,7 +72,7 @@ func New(config ...Config) fiber.Handler { default: // pprof index only works with trailing slash if strings.HasSuffix(path, "/") { - path = strings.TrimRight(path, "/") + path = utils.TrimRight(path, '/') } else { path = prefix + "/" } diff --git a/mount.go b/mount.go index 07d77648..61614a2f 100644 --- a/mount.go +++ b/mount.go @@ -6,9 +6,10 @@ package fiber import ( "sort" - "strings" "sync" "sync/atomic" + + "github.com/gofiber/utils/v2" ) // Put fields related to mounting. @@ -39,7 +40,7 @@ func newMountFields(app *App) *mountFields { // any of the fiber's sub apps are added to the application's error handlers // to be invoked on errors that happen within the prefix route. func (app *App) mount(prefix string, subApp *App) Router { - prefix = strings.TrimRight(prefix, "/") + prefix = utils.TrimRight(prefix, '/') if prefix == "" { prefix = "/" } @@ -69,7 +70,7 @@ func (app *App) mount(prefix string, subApp *App) Router { // compose them as a single service using Mount. func (grp *Group) mount(prefix string, subApp *App) Router { groupPath := getGroupPath(grp.Prefix, prefix) - groupPath = strings.TrimRight(groupPath, "/") + groupPath = utils.TrimRight(groupPath, '/') if groupPath == "" { groupPath = "/" } diff --git a/path.go b/path.go index 53b80b9f..e4d32c9b 100644 --- a/path.go +++ b/path.go @@ -51,7 +51,7 @@ const ( optionalParam byte = '?' // concludes a parameter by name and makes it optional paramStarterChar byte = ':' // start character for a parameter with name slashDelimiter byte = '/' // separator for the route, unlike the other delimiters this character at the end can be optional - slashDelimiterStr = "/" // separator for the route, unlike the other delimiters this character at the end can be optional + slashDelimiterStr byte = '/' // separator for the route, unlike the other delimiters this character at the end can be optional escapeChar byte = '\\' // escape character paramConstraintStart byte = '<' // start of type constraint for a parameter paramConstraintEnd byte = '>' // end of type constraint for a parameter @@ -161,7 +161,7 @@ func RoutePatternMatch(path, pattern string, cfg ...Config) bool { } // Strict routing, remove trailing slashes if !config.StrictRouting && len(patternPretty) > 1 { - patternPretty = strings.TrimRight(patternPretty, "/") + patternPretty = utils.TrimRight(patternPretty, '/') } parser := parseRoute(patternPretty) @@ -233,7 +233,7 @@ func addParameterMetaInfo(segs []*routeSegment) []*routeSegment { } else { comparePart = segs[i].Const if len(comparePart) > 1 { - comparePart = strings.TrimRight(comparePart, slashDelimiterStr) + comparePart = utils.TrimRight(comparePart, slashDelimiterStr) } } } diff --git a/redirect.go b/redirect.go index 02311859..c59a00a3 100644 --- a/redirect.go +++ b/redirect.go @@ -249,10 +249,10 @@ func (r *Redirect) parseAndClearFlashMessages() { for { commaPos = findNextNonEscapedCharsetPosition(cookieValue, []byte(CookieDataSeparator)) if commaPos == -1 { - r.c.redirectionMessages = append(r.c.redirectionMessages, strings.Trim(cookieValue, " ")) + r.c.redirectionMessages = append(r.c.redirectionMessages, utils.Trim(cookieValue, ' ')) break } - r.c.redirectionMessages = append(r.c.redirectionMessages, strings.Trim(cookieValue[:commaPos], " ")) + r.c.redirectionMessages = append(r.c.redirectionMessages, utils.Trim(cookieValue[:commaPos], ' ')) cookieValue = cookieValue[commaPos+1:] } diff --git a/router.go b/router.go index e0c9a5d6..5b06ae3e 100644 --- a/router.go +++ b/router.go @@ -253,7 +253,7 @@ func (app *App) addPrefixToRoute(prefix string, route *Route) *Route { } // Strict routing, remove trailing slashes if !app.config.StrictRouting && len(prettyPath) > 1 { - prettyPath = strings.TrimRight(prettyPath, "/") + prettyPath = utils.TrimRight(prettyPath, '/') } route.Path = prefixedPath @@ -324,7 +324,7 @@ func (app *App) register(methods []string, pathRaw string, group *Group, handler } // Strict routing, remove trailing slashes if !app.config.StrictRouting && len(pathPretty) > 1 { - pathPretty = strings.TrimRight(pathPretty, "/") + pathPretty = utils.TrimRight(pathPretty, '/') } // Is layer a middleware? isUse := method == methodUse