-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
261 lines (221 loc) · 5.39 KB
/
request.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
package okhttp
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"time"
"github.com/mredencom/okhttp/log"
"golang.org/x/net/publicsuffix"
)
// DefaultRequestTimeOut set a default request time out
var DefaultRequestTimeOut = 5 * time.Second
type Request struct {
method string
url *url.URL
header http.Header
cookies []*http.Cookie
body io.Reader
timeout time.Duration
proxy func(*http.Request) (*url.URL, error)
allowRedirect bool
debug bool
isPrintBody bool
l *log.Logger
}
//SetDebug set debug mode
func (r *Request) SetDebug(d bool) *Request {
r.debug = d
return r
}
// SetPrintBody set debug mode
// rely Debug
func (r *Request) SetPrintBody(d bool) *Request {
r.isPrintBody = d
return r
}
// Method get http method
func (r *Request) Method() string {
return r.method
}
// URL url object
func (r *Request) URL() *url.URL {
return r.url
}
// URLString url string
func (r *Request) URLString() string {
return r.url.String()
}
// SetTimeOut set default request time
func (r *Request) SetTimeOut(d time.Duration) *Request {
if d > 0 {
r.timeout = d
return r
}
r.timeout = DefaultRequestTimeOut
return r
}
// SetRedirects set default request allow redirects
func (r *Request) SetRedirects(i bool) *Request {
r.allowRedirect = i
return r
}
// GetHeaders get all request header
func (r *Request) GetHeaders() H {
return doHeader(r.header)
}
// SetHeader set request header
func (r *Request) SetHeader(key, value string) *Request {
r.header.Set(key, value)
return r
}
// SetHeaders multi set request header
func (r *Request) SetHeaders(headers map[string]string) *Request {
if headers == nil {
return r
}
for k, v := range headers {
r.SetHeader(k, v)
}
return r
}
// AddHeader add request header
func (r *Request) AddHeader(key, value string) *Request {
r.header.Add(key, value)
return r
}
// SetUserAgent set user-agent
func (r *Request) SetUserAgent(value string) *Request {
r.SetHeader("User-Agent", value)
return r
}
// SetReferer set a referer request header
func (r *Request) SetReferer(referer string) *Request {
r.SetHeader("Referer", referer)
return r
}
// SetProxy set a referer request header
func (r *Request) SetProxy(proxyURL string) *Request {
parse, err := url.Parse(proxyURL)
if err != nil {
panic("illegal url")
}
r.proxy = http.ProxyURL(parse)
return r
}
// SetCookie set a cookie request header
func (r *Request) SetCookie(cookie *http.Cookie) *Request {
r.cookies = append(r.cookies, cookie)
return r
}
// SetBody sets request body
func (r *Request) SetBody(body io.Reader) *Request {
r.body = body
return r
}
// SetForm sets request form and returns response
func (r *Request) SetForm(v url.Values) *Request {
r.SetHeader("Content-Type", "application/x-www-form-urlencoded")
// todo err
return r.SetBody(bytes.NewBuffer([]byte(v.Encode())))
}
// SetJSON sets request JSON and returns response
func (r *Request) SetJSON(v interface{}) *Request {
r.SetHeader("Content-Type", "application/json")
body, err := json.Marshal(v)
if err != nil {
panic("json encode err " + err.Error())
}
return r.SetBody(bytes.NewBuffer(body))
}
// SetBasicAuth set username and password to header
func (r *Request) SetBasicAuth(username, password string) *Request {
r.SetHeader("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
return r
}
// Do returns response
func (r *Request) Do() (*Response, error) {
client, err := r.client()
if err != nil {
return nil, err
}
request, err := http.NewRequest(r.method, r.url.String(), r.body)
if err != nil {
return nil, err
}
for k := range r.header {
request.Header.Add(k, r.header.Get(k))
}
if r.debug {
dumpRequest, _ := httputil.DumpRequest(request, r.isPrintBody)
r.l.Info(string(dumpRequest))
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
return r.response(response)
}
// client create a request client
func (r *Request) client() (*http.Client, error) {
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
return nil, err
}
jar.SetCookies(r.url, r.cookies)
client := &http.Client{
Transport: &http.Transport{
// 设置代理
Proxy: r.proxy,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Jar: jar,
Timeout: r.timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if !r.allowRedirect {
return http.ErrUseLastResponse
}
return nil
},
}
if len(r.cookies) > 0 {
client.Jar.SetCookies(r.url, r.cookies)
}
return client, nil
}
// response response data
func (r *Request) response(response *http.Response) (*Response, error) {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
res := &Response{
request: r,
status: response.StatusCode,
headers: response.Header,
cookies: response.Cookies(),
body: body,
}
if r.debug {
dumpResponse, _ := httputil.DumpResponse(response, r.isPrintBody)
r.l.Info(string(dumpResponse))
}
return res, nil
}