mirror of https://github.com/gofiber/fiber.git
♻️ Refactor: Migrate randString to rand v2 (#3329)
* ♻️ Refactor: migrate randString to rand/v2 * 🩹 Fix: golangci-lintpull/3309/head
parent
3f935551ea
commit
435fa42360
|
@ -3,24 +3,22 @@ package client
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/utils/v2"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var (
|
||||
protocolCheck = regexp.MustCompile(`^https?://.*$`)
|
||||
|
||||
headerAccept = "Accept"
|
||||
var protocolCheck = regexp.MustCompile(`^https?://.*$`)
|
||||
|
||||
const (
|
||||
headerAccept = "Accept"
|
||||
applicationJSON = "application/json"
|
||||
applicationCBOR = "application/cbor"
|
||||
applicationXML = "application/xml"
|
||||
|
@ -30,25 +28,26 @@ var (
|
|||
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
letterIdxBits = 6 // 6 bits to represent a letter index
|
||||
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
||||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting into 63 bits
|
||||
letterIdxMax = 64 / letterIdxBits // # of letter indices fitting into 64 bits
|
||||
)
|
||||
|
||||
// randString returns a random string of length n.
|
||||
func randString(n int) string {
|
||||
// unsafeRandString returns a random string of length n.
|
||||
func unsafeRandString(n int) string {
|
||||
b := make([]byte, n)
|
||||
length := len(letterBytes)
|
||||
src := rand.NewSource(time.Now().UnixNano())
|
||||
const length = uint64(len(letterBytes))
|
||||
|
||||
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
||||
//nolint:gosec // Not a concern
|
||||
for i, cache, remain := n-1, rand.Uint64(), letterIdxMax; i >= 0; {
|
||||
if remain == 0 {
|
||||
cache, remain = src.Int63(), letterIdxMax
|
||||
//nolint:gosec // Not a concern
|
||||
cache, remain = rand.Uint64(), letterIdxMax
|
||||
}
|
||||
|
||||
if idx := int(cache & int64(letterIdxMask)); idx < length {
|
||||
if idx := cache & letterIdxMask; idx < length {
|
||||
b[i] = letterBytes[idx]
|
||||
i--
|
||||
}
|
||||
cache >>= int64(letterIdxBits)
|
||||
cache >>= letterIdxBits
|
||||
remain--
|
||||
}
|
||||
|
||||
|
@ -134,7 +133,7 @@ func parserRequestHeader(c *Client, req *Request) error {
|
|||
req.RawRequest.Header.SetContentType(multipartFormData)
|
||||
// If boundary is default, append a random string to it.
|
||||
if req.boundary == boundary {
|
||||
req.boundary += randString(16)
|
||||
req.boundary += unsafeRandString(16)
|
||||
}
|
||||
req.RawRequest.Header.SetMultipartFormBoundary(req.boundary)
|
||||
default:
|
||||
|
|
|
@ -38,7 +38,7 @@ func Test_Rand_String(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got := randString(tt.args)
|
||||
got := unsafeRandString(tt.args)
|
||||
require.Len(t, got, tt.args)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue