-
Notifications
You must be signed in to change notification settings - Fork 8
/
request.go
297 lines (271 loc) · 6.22 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
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
package ghttp
import (
"bufio"
"bytes"
"crypto/tls"
"errors"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
)
var (
// DefaultDialer
DefaultDialer = &net.Dialer{Timeout: 2000 * time.Millisecond}
// DefaultTransport
DefaultTransport http.RoundTripper = &http.Transport{Dial: DefaultDialer.Dial, Proxy: http.ProxyFromEnvironment}
// DefaultClient
DefaultClient = &http.Client{Transport: DefaultTransport}
// proxyTransport
proxyTransport http.RoundTripper
// proxyClient
proxyClient *http.Client
)
// Request ghttp request lib
type Request struct {
headers []headerElements
Method string
Url string
Body interface{}
Query interface{}
cookies []*http.Cookie
Timeout time.Duration
ContentType string
XForwardedFor string
Accept string
UserAgent string
Host string
Insecure bool
TlsConfig *tls.Config
MaxRedirects int
RedirectHeaders bool
Proxy string
Compression *compression
BasicAuthUsername string
BasicAuthPassword string
CookieJar http.CookieJar
ShowDebug bool
OnBeforeRequest func(goxhttp *Request, httpreq *http.Request)
}
// transportRequestCanceler
type transportRequestCanceler interface {
CancelRequest(*http.Request)
}
// NewRequest new request before do()
func (r Request) NewRequest() (*http.Request, error) {
b, e := prepareRequestBody(r.Body)
if e != nil {
return nil, &Error{Err: e}
}
if r.Query != nil {
param, e := paramParse(r.Query)
if e != nil {
return nil, &Error{Err: e}
}
// http://127.0.0.1?user={}
r.Url = r.Url + "?" + param
}
var (
bodyReader io.Reader
)
if b != nil && r.Compression != nil {
buffer := bytes.NewBuffer([]byte{})
readBuffer := bufio.NewReader(b)
writer, err := r.Compression.writer(buffer)
if err != nil {
return nil, &Error{Err: err}
}
_, e = readBuffer.WriteTo(writer)
writer.Close()
if e != nil {
return nil, &Error{Err: e}
}
bodyReader = buffer
} else {
bodyReader = b
}
req, err := http.NewRequest(r.Method, r.Url, bodyReader)
if err != nil {
return nil, err
}
// add headers to the request
req.Host = r.Host
r.addHeaders(req.Header)
if r.Compression != nil {
req.Header.Add("Content-Encoding", r.Compression.ContentEncoding)
req.Header.Add("Accept-Encoding", r.Compression.ContentEncoding)
}
if r.headers != nil {
for _, header := range r.headers {
req.Header.Add(header.key, header.value)
}
}
//use basic auth if required
if r.BasicAuthUsername != "" {
req.SetBasicAuth(r.BasicAuthUsername, r.BasicAuthPassword)
}
for _, c := range r.cookies {
req.AddCookie(c)
}
return req, nil
}
// Do Initiate a request
func (r Request) Do() (*Response, error) {
var (
client = DefaultClient
transport = DefaultTransport
resURL string
redirectFailed bool
)
r.Method = valueOrDefault(r.Method, "GET")
// use old cookiejar
if r.CookieJar != nil {
client = &http.Client{
Transport: transport,
Jar: r.CookieJar,
}
}
if len(r.Proxy) != 0 {
proxyUrl, err := url.Parse(r.Proxy)
if err != nil {
return nil, &Error{Err: err}
}
// 如果指定,则需要重新构建
if proxyTransport == nil || client.Jar != nil {
proxyTransport = &http.Transport{Dial: DefaultDialer.Dial, Proxy: http.ProxyURL(proxyUrl)}
proxyClient = &http.Client{Transport: proxyTransport, Jar: client.Jar}
} else if proxyTransport, ok := proxyTransport.(*http.Transport); ok {
proxyTransport.Proxy = http.ProxyURL(proxyUrl)
}
transport = proxyTransport
client = proxyClient
}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) > r.MaxRedirects {
redirectFailed = true
return errors.New("Error redirecting. MaxRedirects reached")
}
resURL = req.URL.String()
// 默认不会重定向请求头,重新设置
if r.RedirectHeaders {
for key, val := range via[0].Header {
req.Header[key] = val
}
}
return nil
}
if transport, ok := transport.(*http.Transport); ok {
if r.Insecure {
if r.TlsConfig != nil {
transport.TLSClientConfig = r.TlsConfig
} else {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
} else if transport.TLSClientConfig != nil {
// default
transport.TLSClientConfig.InsecureSkipVerify = false
}
}
req, err := r.NewRequest()
if err != nil {
return nil, &Error{Err: err}
}
timeout := false
if r.Timeout > 0 {
client.Timeout = r.Timeout
}
if r.ShowDebug {
dump, err := httputil.DumpRequest(req, true)
if err != nil {
log.Println(err)
}
log.Println(string(dump))
}
if r.OnBeforeRequest != nil {
r.OnBeforeRequest(&r, req)
}
res, err := client.Do(req)
if err != nil {
if !timeout {
if t, ok := err.(reqtimeout); ok {
timeout = t.Timeout()
}
if ue, ok := err.(*url.Error); ok {
if t, ok := ue.Err.(reqtimeout); ok {
timeout = t.Timeout()
}
}
}
var (
response *Response
)
// response when redirectFailed
if redirectFailed {
if res != nil {
response = &Response{
res,
resURL,
&Body{
reader: res.Body,
},
req,
}
} else {
response = &Response{
res,
resURL,
nil,
req,
}
}
}
// redirectFailed and MaxRedirects==0 return nil(no err)
if redirectFailed && r.MaxRedirects == 0 {
return response, nil
}
return response, &Error{
timeout: timeout,
Err: err,
}
}
if r.Compression != nil && strings.Contains(res.Header.Get("Content-Encoding"), r.Compression.ContentEncoding) {
compressedReader, err := r.Compression.reader(res.Body)
if err != nil {
return nil, &Error{Err: err}
}
return &Response{
res, resURL,
&Body{
reader: res.Body,
compressedReader: compressedReader,
},
req,
},
nil
}
return &Response{
res,
resURL,
&Body{
reader: res.Body,
},
req,
},
nil
}
// CancelRequest cancel like postman
func (r Response) CancelRequest() {
cancelRequest(DefaultTransport, r.req)
}
// cancelRequest
func cancelRequest(transport interface{}, r *http.Request) {
if tp, ok := transport.(transportRequestCanceler); ok {
tp.CancelRequest(r)
}
}