/
httpclient.go
310 lines (276 loc) · 9.63 KB
/
httpclient.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
/*
* httpclient.go - HTTP client
*
* bitfinex_borrow_catcher - Automatic borrow catcher for open positions in
* the Bitfinex exchange
* Copyright (C) 2021 Mateusz Szpakowski
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package main
import (
"bytes"
"fmt"
"math"
"time"
"github.com/matszpk/godec64"
"github.com/valyala/fasthttp"
"github.com/valyala/fastjson"
)
func HttpPanic(msg string, statusCode int) {
panic(fmt.Sprint(msg, ": status code: ", fasthttp.StatusMessage(statusCode),
" (", statusCode, ")"))
}
var jsonContentType []byte = []byte("application/json")
// check whether is content-type application/json ?
func CheckJsonContentType(respContentType []byte) bool {
rlen := len(respContentType)
if rlen<16 || !bytes.Equal(respContentType[:16], jsonContentType) {
return false
}
if rlen==16 { return true }
i := 16
for ; i<rlen && respContentType[i]==' '; i++ { } // skip spaces
// no semicolon
if i>=rlen || respContentType[i]!=';' { return false }
return true
}
var UserAgentBytes []byte = []byte("cryptospeculator")
var JsonParserPool fastjson.ParserPool
var JsonArenaPool fastjson.ArenaPool
type RequestHandle struct {
JsonParser *fastjson.Parser
Response *fasthttp.Response
}
// handle http get with json. it returns json value and http status code.
func (rh *RequestHandle) HandleHttpGetJson(httpClient *fasthttp.HostClient,
host, uri []byte, args *fasthttp.Args) (*fastjson.Value, int) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
if args!=nil {
// append arguments
dstUri := make([]byte, 0, len(uri)+10)
dstUri = append(dstUri, uri...)
dstUri = append(dstUri, '?')
dstUri = append(dstUri, args.QueryString()...)
req.SetRequestURIBytes(dstUri)
} else {
req.SetRequestURIBytes(uri)
}
if httpClient.IsTLS { // fix for new fasthttp versions
req.URI().SetScheme("https")
}
req.Header.SetMethod(fasthttp.MethodGet)
req.SetHostBytes(host)
req.Header.SetUserAgentBytes(UserAgentBytes)
req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Encoding", "utf-8")
rh.Response = fasthttp.AcquireResponse()
if err := httpClient.Do(req, rh.Response); err!=nil {
ErrorPanic("Error while doing HTTP request", err)
}
status := rh.Response.Header.StatusCode()
if !CheckJsonContentType(rh.Response.Header.ContentType()) {
// wrong content type (must be json encoded in utf-8
panic("HTTP response have wrong content-type")
}
// parse json
rh.JsonParser = JsonParserPool.Get()
v, err := rh.JsonParser.ParseBytes(rh.Response.Body())
if err!=nil {
ErrorPanic("Error while parsing response", err)
}
return v, status
}
// headers - array of string-bytes, even elements are keys, odd are value
func (rh *RequestHandle) HandleHttpPostJson(httpClient *fasthttp.HostClient,
host, uri, query []byte, body []byte,
headers [][]byte) (*fastjson.Value, int) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
uriWithQuery := make([]byte, 0, len(uri)+len(query))
uriWithQuery = append(uriWithQuery, uri...)
uriWithQuery = append(uriWithQuery, query...)
req.SetRequestURIBytes(uriWithQuery)
if httpClient.IsTLS { // fix for new fasthttp versions
req.URI().SetScheme("https")
}
req.Header.SetMethod(fasthttp.MethodPost)
req.SetHostBytes(host)
req.Header.SetUserAgentBytes(UserAgentBytes)
req.Header.SetContentType("application/json")
req.Header.SetContentLength(len(body))
req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Encoding", "utf-8")
// set extra headers
hlen := len(headers)
for i:=0; i < hlen; i+=2 {
req.Header.AddBytesKV(headers[i], headers[i+1])
}
req.SetBody(body)
rh.Response = fasthttp.AcquireResponse()
if err := httpClient.Do(req, rh.Response); err!=nil {
ErrorPanic("Error while doing HTTP request", err)
}
status := rh.Response.Header.StatusCode()
if !CheckJsonContentType(rh.Response.Header.ContentType()) {
// wrong content type (must be json encoded in utf-8
panic("HTTP response have wrong content-type")
}
// parse json
rh.JsonParser = JsonParserPool.Get()
v, err := rh.JsonParser.ParseBytes(rh.Response.Body())
if err!=nil {
ErrorPanic("Error while parsing response", err)
}
return v, status
}
// should be called after using request handle
func (rh *RequestHandle) Release() {
if rh.JsonParser!=nil {
JsonParserPool.Put(rh.JsonParser)
rh.JsonParser = nil
}
if rh.Response!=nil {
fasthttp.ReleaseResponse(rh.Response)
rh.Response = nil
}
}
/* fastjson utilities */
func FastjsonGetObjectRequired(vx *fastjson.Value) *fastjson.Object {
if o, err := vx.Object(); err==nil {
return o
}
panic("Wrong json body: no object field")
}
func FastjsonGetString(vx *fastjson.Value) string {
if vx.Type()==fastjson.TypeNull { return "" }
if s, err := vx.StringBytes(); err==nil {
return string(s)
}
panic("Wrong json body: no string field")
}
func FastjsonGetBool(vx *fastjson.Value) bool {
if vx.Type()==fastjson.TypeNull { return false }
if b, err := vx.Bool(); err==nil {
return b
}
panic("Wrong json body: no bool field")
}
func FastjsonGetStringBytes(vx *fastjson.Value) []byte {
if vx.Type()==fastjson.TypeNull { return nil }
if s, err := vx.StringBytes(); err==nil {
return s
}
panic("Wrong json body: no string field")
}
func FastjsonCheckString(vx *fastjson.Value, expected []byte) bool {
if s, err := vx.StringBytes(); err==nil {
return bytes.Equal(s, expected)
}
panic("Wrong json body: no string field")
}
func FastjsonGetInt(vx *fastjson.Value) int {
if vx.Type()==fastjson.TypeNull { return 0 }
if iv, err := vx.Int(); err==nil {
return iv
}
panic("Wrong json body: no integer field")
}
func FastjsonGetUInt(vx *fastjson.Value) uint {
if vx.Type()==fastjson.TypeNull { return 0 }
if iv, err := vx.Uint(); err==nil {
return iv
}
panic("Wrong json body: no integer field")
}
func FastjsonGetUInt32(vx *fastjson.Value) uint32 {
if vx.Type()==fastjson.TypeNull { return 0 }
if iv, err := vx.Uint(); err==nil {
if iv > math.MaxUint32 {
panic("Unsigned integer overflow in json")
}
return uint32(iv)
}
panic("Wrong json body: no integer field")
}
func FastjsonGetUInt64(vx *fastjson.Value) uint64 {
if vx.Type()==fastjson.TypeNull { return 0 }
if iv, err := vx.Uint64(); err==nil {
return iv
}
panic("Wrong json body: no integer field")
}
func FastjsonGetFloat64(vx *fastjson.Value) float64 {
if vx.Type()==fastjson.TypeNull { return 0 }
if fv, err := vx.Float64(); err==nil {
return fv
}
panic("Wrong json body: no float field")
}
func FastjsonGetArray(vx *fastjson.Value) []*fastjson.Value {
if vx.Type()==fastjson.TypeNull { return nil }
if arr, err := vx.Array(); err==nil {
return arr
}
panic("Wrong json body: no array field")
}
func FastjsonGetUDec64(vx *fastjson.Value, precision uint) godec64.UDec64 {
if vx.Type()==fastjson.TypeNull { return 0 }
if vx.Type()==fastjson.TypeNumber {
ud, err := godec64.ParseUDec64Bytes(vx.MarshalTo(nil), precision, false)
if err!=nil {
panic("Wrong json body: no udec64 field")
}
return ud
}
panic("Wrong json body: no udec64 field")
}
func FastjsonGetUDec64Signed(vx *fastjson.Value,
precision uint) (godec64.UDec64, bool) {
if vx.Type()==fastjson.TypeNull { return 0, false }
neg := false
if vx.Type()==fastjson.TypeNumber {
str := vx.MarshalTo(nil)
if len(str)>0 && (str[0]=='-' || str[0]=='+') {
if str[0]=='-' { neg = true }
str = str[1:]
}
ud, err := godec64.ParseUDec64Bytes(str, precision, false)
if err!=nil {
panic("Wrong json body: no signed udec64 field")
}
return ud, neg
}
panic("Wrong json body: no signed udec64 field")
}
func FastjsonGetUnixTimeMilli(vx *fastjson.Value) time.Time {
if vx.Type()==fastjson.TypeNull { return time.Time{} }
if iv, err := vx.Int64(); err==nil {
return time.Unix(iv/1000, (iv%1000)*1000000)
}
panic("Wrong json body: no unix time")
}
func FastjsonGetDuration(vx *fastjson.Value) time.Duration {
if vx.Type()==fastjson.TypeNull { return 0 }
if s, err := vx.StringBytes(); err==nil {
if d, err := time.ParseDuration(string(s)); err!=nil {
panic("Wrong json body: no time duration field")
} else {
return d
}
}
panic("Wrong json body: no time duration field")
}