-
Notifications
You must be signed in to change notification settings - Fork 30
/
http_utils.go
350 lines (297 loc) · 6.65 KB
/
http_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
gourl "net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
"text/template"
"time"
)
func usageAndExit(msg string) {
if msg != "" {
fmt.Println(msg)
}
flag.Usage()
fmt.Println("")
os.Exit(1)
}
type flagSlice []string
func (h *flagSlice) String() string {
return fmt.Sprintf("%s", *h)
}
func (h *flagSlice) Set(value string) error {
*h = append(*h, value)
return nil
}
func verbosePrint(level int, vfmt string, args ...interface{}) {
if *verbose > level {
return
}
switch level {
case vTRACE:
println("[TRACE] "+vfmt, args...)
case vDEBUG:
println("[DEBUG] "+vfmt, args...)
case vINFO:
println("[INFO] "+vfmt, args...)
default:
println("[ERROR] "+vfmt, args...)
}
}
const (
IntMax = int(^uint(0) >> 1)
IntMin = ^IntMax
)
const (
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 in 63 bits
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterNumBytes = "0123456789"
httpContentTypeJSON = "application/json"
httpWorkerApiPath = "/api"
)
var (
ErrInitWsClient = errors.New("init ws client error")
ErrInitHttpClient = errors.New("init http client error")
ErrInitTcpClient = errors.New("init tcp client error")
ErrUrl = errors.New("check url error")
)
var (
fnSrc = rand.NewSource(time.Now().UnixNano()) // for functions
fnMap = template.FuncMap{
"intSum": intSum,
"random": random,
"randomDate": randomDate,
"randomString": randomString,
"randomNum": randomNum,
"date": date,
"UUID": uuid,
"escape": escape,
"getEnv": getEnv,
"hexToString": hexToString,
"stringToHex": stringToHex,
"toString": toString,
}
fnUUID = randomString(10)
)
// template functions
func intSum(v ...int64) int64 {
var r int64
for _, r1 := range v {
r += int64(r1)
}
return r
}
func random(min, max int64) int64 {
rand.Seed(time.Now().UnixNano())
return rand.Int63n(max-min) + min
}
func formatTime(now time.Time, fmt string) string {
switch fmt {
case "YMD":
return now.Format("20060201")
case "HMS":
return now.Format("150405")
default:
return now.Format("20060201-150405")
}
}
// YMD = yyyyMMdd, HMS = HHmmss, YMDHMS = yyyyMMdd-HHmmss
func date(fmt string) string {
return formatTime(time.Now(), fmt)
}
func randomDate(fmt string) string {
return formatTime(time.Unix(rand.Int63n(time.Now().Unix()-94608000)+94608000, 0), fmt)
}
func escape(u string) string {
return gourl.QueryEscape(u)
}
func randomN(n int, letter string) string {
b := make([]byte, n)
for i, cache, remain := n-1, fnSrc.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = fnSrc.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letter) {
b[i] = letter[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func randomString(n int) string {
return randomN(n, letterBytes)
}
func randomNum(n int) string {
return randomN(n, letterNumBytes)
}
func uuid() string {
return fnUUID
}
func getEnv(key string) string {
return os.Getenv(key)
}
func hexToString(hexStr string) string {
data, _ := hex.DecodeString(hexStr)
return string(data)
}
func stringToHex(s string) string {
data := []byte(s)
return hex.EncodeToString(data)
}
func toString(args ...interface{}) string {
return fmt.Sprintf(`"%v"`, args...)
}
func parseTime(timeStr string) int64 {
var multi int64 = 1
if timeStrLen := len(timeStr) - 1; timeStrLen > 0 {
switch timeStr[timeStrLen] {
case 's':
timeStr = timeStr[:timeStrLen]
case 'm':
timeStr = timeStr[:timeStrLen]
multi = 60
case 'h':
timeStr = timeStr[:timeStrLen]
multi = 3600
}
}
t, err := strconv.ParseInt(timeStr, 10, 64)
if err != nil || t <= 0 {
usageAndExit("Duration parse err: " + err.Error())
}
return multi * t
}
type byteBlock struct {
block []byte
cap int
}
var bytePool = sync.Pool{
New: func() interface{} {
return &byteBlock{
block: make([]byte, 0, 10240),
cap: 10240,
}
},
}
// the purpose of this function is to reduce processing
func fastRead(r io.Reader, cycleRead bool) (int64, error) {
var (
n = int64(0)
b = bytePool.Get().(*byteBlock)
bsize int
err error
)
defer bytePool.Put(b)
for {
bsize, err = r.Read(b.block[0:b.cap])
verbosePrint(vDEBUG, "fastRead: %v, bsize: %v", b, bsize)
if err != nil {
if err == io.EOF {
err = nil
} else {
return n, err
}
}
n += int64(bsize)
// TODO: cycleRead isn't support
if !cycleRead || bsize == 0 {
return n, err
}
}
}
func parseInputWithRegexp(input, regx string) ([]string, error) {
re := regexp.MustCompile(regx)
matches := re.FindStringSubmatch(input)
if len(matches) < 1 {
return nil, fmt.Errorf("could not parse the provided input; input = %v", input)
}
return matches, nil
}
func parseFile(fileName string, delimiter []rune) ([]string, error) {
var contentList []string
file, err := os.Open(fileName)
if err != nil {
return contentList, err
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
return contentList, err
}
if delimiter == nil {
return []string{string(content)}, nil
}
lines := strings.FieldsFunc(string(content), func(r rune) bool {
for _, v := range delimiter {
if r == v {
return true
}
}
return false
})
for _, line := range lines {
if len(line) > 0 {
contentList = append(contentList, line)
}
}
return contentList, nil
}
type ConnOption struct {
timeout time.Duration
disableKeepAlives bool
}
type tcpConn struct {
tcpClient net.Conn
uri string
option ConnOption
}
func DialTCP(uri string, option ConnOption) (*tcpConn, error) {
conn, err := net.Dial("tcp", uri)
if err != nil {
verbosePrint(vERROR, "DialTCP Dial err: %v", err)
return nil, err
}
err = conn.SetDeadline(time.Now().Add(time.Millisecond * time.Duration(option.timeout)))
if err != nil {
verbosePrint(vERROR, "DialTCP SetDeadline err: %v", err)
return nil, err
}
tcp := &tcpConn{
tcpClient: conn,
uri: uri,
option: option,
}
return tcp, nil
}
func (tcp *tcpConn) Do(body []byte) (int64, error) {
if tcp.tcpClient == nil {
return 0, ErrInitTcpClient
}
_, err := tcp.tcpClient.Write(body)
if err != nil {
return 0, err
}
return fastRead(tcp.tcpClient, false) // !tcp.option.disableKeepAlives
}
func (tcp *tcpConn) Close() error {
if tcp.tcpClient == nil {
return ErrInitTcpClient
}
err := tcp.tcpClient.Close()
tcp.tcpClient = nil
return err
}