@@ -3,24 +3,22 @@ package client
3
3
import (
4
4
"fmt"
5
5
"io"
6
- "math/rand"
6
+ "math/rand/v2 "
7
7
"mime/multipart"
8
8
"os"
9
9
"path/filepath"
10
10
"regexp"
11
11
"strconv"
12
12
"strings"
13
- "time"
14
13
15
14
"github.com/gofiber/utils/v2"
16
15
"github.com/valyala/fasthttp"
17
16
)
18
17
19
- var (
20
- protocolCheck = regexp .MustCompile (`^https?://.*$` )
21
-
22
- headerAccept = "Accept"
18
+ var protocolCheck = regexp .MustCompile (`^https?://.*$` )
23
19
20
+ const (
21
+ headerAccept = "Accept"
24
22
applicationJSON = "application/json"
25
23
applicationCBOR = "application/cbor"
26
24
applicationXML = "application/xml"
@@ -30,25 +28,26 @@ var (
30
28
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
31
29
letterIdxBits = 6 // 6 bits to represent a letter index
32
30
letterIdxMask = 1 << letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
33
- letterIdxMax = 63 / letterIdxBits // # of letter indices fitting into 63 bits
31
+ letterIdxMax = 64 / letterIdxBits // # of letter indices fitting into 64 bits
34
32
)
35
33
36
- // randString returns a random string of length n.
37
- func randString (n int ) string {
34
+ // unsafeRandString returns a random string of length n.
35
+ func unsafeRandString (n int ) string {
38
36
b := make ([]byte , n )
39
- length := len (letterBytes )
40
- src := rand .NewSource (time .Now ().UnixNano ())
37
+ const length = uint64 (len (letterBytes ))
41
38
42
- for i , cache , remain := n - 1 , src .Int63 (), letterIdxMax ; i >= 0 ; {
39
+ //nolint:gosec // Not a concern
40
+ for i , cache , remain := n - 1 , rand .Uint64 (), letterIdxMax ; i >= 0 ; {
43
41
if remain == 0 {
44
- cache , remain = src .Int63 (), letterIdxMax
42
+ //nolint:gosec // Not a concern
43
+ cache , remain = rand .Uint64 (), letterIdxMax
45
44
}
46
45
47
- if idx := int ( cache & int64 ( letterIdxMask )) ; idx < length {
46
+ if idx := cache & letterIdxMask ; idx < length {
48
47
b [i ] = letterBytes [idx ]
49
48
i --
50
49
}
51
- cache >>= int64 ( letterIdxBits )
50
+ cache >>= letterIdxBits
52
51
remain --
53
52
}
54
53
@@ -134,7 +133,7 @@ func parserRequestHeader(c *Client, req *Request) error {
134
133
req .RawRequest .Header .SetContentType (multipartFormData )
135
134
// If boundary is default, append a random string to it.
136
135
if req .boundary == boundary {
137
- req .boundary += randString (16 )
136
+ req .boundary += unsafeRandString (16 )
138
137
}
139
138
req .RawRequest .Header .SetMultipartFormBoundary (req .boundary )
140
139
default :
0 commit comments