mirror of https://github.com/gofiber/fiber.git
Use utils Trim functions instead of the strings/bytes functions (#3087)
* Use utils Trim functions instead of the strings/bytes functions * rename Test and Benchmark functions with same namepull/3076/head
parent
401d96d1aa
commit
d1865491b6
|
@ -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.
|
||||
|
|
10
ctx.go
10
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)
|
||||
|
||||
|
|
2
go.mod
2
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
|
||||
|
|
4
go.sum
4
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=
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 + "/"
|
||||
}
|
||||
|
|
7
mount.go
7
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 = "/"
|
||||
}
|
||||
|
|
6
path.go
6
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -286,10 +286,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:]
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue