forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpUtils.go
234 lines (204 loc) · 6.67 KB
/
HttpUtils.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
package goex
//http request 工具函数
import (
"encoding/json"
"errors"
"fmt"
"github.com/soulsplit/goex/internal/logger"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
var (
fastHttpClient = &fasthttp.Client{
Name: "goex-http-utils",
MaxConnsPerHost: 16,
MaxIdleConnDuration: 20 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
socksDialer fasthttp.DialFunc
)
func NewHttpRequestWithFasthttp(client *http.Client, reqMethod, reqUrl, postData string, headers map[string]string) ([]byte, error) {
logger.Log.Debug("use fasthttp client")
transport := client.Transport
if transport != nil {
if proxy, err := transport.(*http.Transport).Proxy(nil); err == nil && proxy != nil {
proxyUrl := proxy.String()
logger.Log.Debug("proxy url: ", proxyUrl)
if proxy.Scheme != "socks5" {
logger.Log.Error("fasthttp only support the socks5 proxy")
} else if socksDialer == nil {
socksDialer = fasthttpproxy.FasthttpSocksDialer(strings.TrimPrefix(proxyUrl, proxy.Scheme+"://"))
fastHttpClient.Dial = socksDialer
}
}
}
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}()
for k, v := range headers {
req.Header.Set(k, v)
}
req.Header.SetMethod(reqMethod)
req.SetRequestURI(reqUrl)
req.SetBodyString(postData)
err := fastHttpClient.Do(req, resp)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode(), string(resp.Body())))
}
return resp.Body(), nil
}
func NewHttpRequest(client *http.Client, reqType string, reqUrl string, postData string, requstHeaders map[string]string) ([]byte, error) {
logger.Log.Debugf("[%s] request url: %s", reqType, reqUrl)
lib := os.Getenv("HTTP_LIB")
if lib == "fasthttp" {
return NewHttpRequestWithFasthttp(client, reqType, reqUrl, postData, requstHeaders)
}
req, _ := http.NewRequest(reqType, reqUrl, strings.NewReader(postData))
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
}
if requstHeaders != nil {
for k, v := range requstHeaders {
req.Header.Add(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode, string(bodyData)))
}
return bodyData, nil
}
func HttpGet(client *http.Client, reqUrl string) (map[string]interface{}, error) {
respData, err := NewHttpRequest(client, "GET", reqUrl, "", nil)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println(string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet2(client *http.Client, reqUrl string, headers map[string]string) (map[string]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println("respData", string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet3(client *http.Client, reqUrl string, headers map[string]string) ([]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
//println(string(respData))
var bodyDataMap []interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println("respData", string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet4(client *http.Client, reqUrl string, headers map[string]string, result interface{}) error {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return err
}
err = json.Unmarshal(respData, result)
if err != nil {
log.Printf("HttpGet4 - json.Unmarshal failed : %v, resp %s", err, string(respData))
return err
}
return nil
}
func HttpGet5(client *http.Client, reqUrl string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
return respData, nil
}
func HttpPostForm(client *http.Client, reqUrl string, postData url.Values) ([]byte, error) {
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded"}
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm2(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm3(client *http.Client, reqUrl string, postData string, headers map[string]string) ([]byte, error) {
return NewHttpRequest(client, "POST", reqUrl, postData, headers)
}
func HttpPostForm4(client *http.Client, reqUrl string, postData map[string]string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/json"
data, _ := json.Marshal(postData)
return NewHttpRequest(client, "POST", reqUrl, string(data), headers)
}
func HttpDeleteForm(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "DELETE", reqUrl, postData.Encode(), headers)
}
func HttpPut(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "PUT", reqUrl, postData.Encode(), headers)
}