1
|
-// Copyright 2014 beego Author. All Rights Reserved.
|
|
|
2
|
-//
|
|
|
3
|
-// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4
|
-// you may not use this file except in compliance with the License.
|
|
|
5
|
-// You may obtain a copy of the License at
|
|
|
6
|
-//
|
|
|
7
|
-// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8
|
-//
|
|
|
9
|
-// Unless required by applicable law or agreed to in writing, software
|
|
|
10
|
-// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11
|
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12
|
-// See the License for the specific language governing permissions and
|
|
|
13
|
-// limitations under the License.
|
|
|
14
|
-
|
|
|
15
|
-// Package httplib is used as http.Client
|
|
|
16
|
-// Usage:
|
|
|
17
|
-//
|
|
|
18
|
-// import "github.com/astaxie/beego/httplib"
|
|
|
19
|
-//
|
|
|
20
|
-// b := httplib.Post("http://beego.me/")
|
|
|
21
|
-// b.Param("username","astaxie")
|
|
|
22
|
-// b.Param("password","123456")
|
|
|
23
|
-// b.PostFile("uploadfile1", "httplib.pdf")
|
|
|
24
|
-// b.PostFile("uploadfile2", "httplib.txt")
|
|
|
25
|
-// str, err := b.String()
|
|
|
26
|
-// if err != nil {
|
|
|
27
|
-// t.Fatal(err)
|
|
|
28
|
-// }
|
|
|
29
|
-// fmt.Println(str)
|
|
|
30
|
-//
|
|
|
31
|
-// more docs http://beego.me/docs/module/httplib.md
|
|
|
32
|
-package httplib
|
|
|
33
|
-
|
|
|
34
|
-import (
|
|
|
35
|
- "bytes"
|
|
|
36
|
- "compress/gzip"
|
|
|
37
|
- "crypto/tls"
|
|
|
38
|
- "encoding/json"
|
|
|
39
|
- "encoding/xml"
|
|
|
40
|
- "io"
|
|
|
41
|
- "io/ioutil"
|
|
|
42
|
- "log"
|
|
|
43
|
- "mime/multipart"
|
|
|
44
|
- "net"
|
|
|
45
|
- "net/http"
|
|
|
46
|
- "net/http/cookiejar"
|
|
|
47
|
- "net/http/httputil"
|
|
|
48
|
- "net/url"
|
|
|
49
|
- "os"
|
|
|
50
|
- "path"
|
|
|
51
|
- "strings"
|
|
|
52
|
- "sync"
|
|
|
53
|
- "time"
|
|
|
54
|
-
|
|
|
55
|
- "gopkg.in/yaml.v2"
|
|
|
56
|
-)
|
|
|
57
|
-
|
|
|
58
|
-var defaultSetting = BeegoHTTPSettings{
|
|
|
59
|
- UserAgent: "beegoServer",
|
|
|
60
|
- ConnectTimeout: 60 * time.Second,
|
|
|
61
|
- ReadWriteTimeout: 60 * time.Second,
|
|
|
62
|
- Gzip: true,
|
|
|
63
|
- DumpBody: true,
|
|
|
64
|
-}
|
|
|
65
|
-
|
|
|
66
|
-var defaultCookieJar http.CookieJar
|
|
|
67
|
-var settingMutex sync.Mutex
|
|
|
68
|
-
|
|
|
69
|
-// createDefaultCookie creates a global cookiejar to store cookies.
|
|
|
70
|
-func createDefaultCookie() {
|
|
|
71
|
- settingMutex.Lock()
|
|
|
72
|
- defer settingMutex.Unlock()
|
|
|
73
|
- defaultCookieJar, _ = cookiejar.New(nil)
|
|
|
74
|
-}
|
|
|
75
|
-
|
|
|
76
|
-// SetDefaultSetting Overwrite default settings
|
|
|
77
|
-func SetDefaultSetting(setting BeegoHTTPSettings) {
|
|
|
78
|
- settingMutex.Lock()
|
|
|
79
|
- defer settingMutex.Unlock()
|
|
|
80
|
- defaultSetting = setting
|
|
|
81
|
-}
|
|
|
82
|
-
|
|
|
83
|
-// NewBeegoRequest return *BeegoHttpRequest with specific method
|
|
|
84
|
-func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
|
|
|
85
|
- var resp http.Response
|
|
|
86
|
- u, err := url.Parse(rawurl)
|
|
|
87
|
- if err != nil {
|
|
|
88
|
- log.Println("Httplib:", err)
|
|
|
89
|
- }
|
|
|
90
|
- req := http.Request{
|
|
|
91
|
- URL: u,
|
|
|
92
|
- Method: method,
|
|
|
93
|
- Header: make(http.Header),
|
|
|
94
|
- Proto: "HTTP/1.1",
|
|
|
95
|
- ProtoMajor: 1,
|
|
|
96
|
- ProtoMinor: 1,
|
|
|
97
|
- }
|
|
|
98
|
- return &BeegoHTTPRequest{
|
|
|
99
|
- url: rawurl,
|
|
|
100
|
- req: &req,
|
|
|
101
|
- params: map[string][]string{},
|
|
|
102
|
- files: map[string]string{},
|
|
|
103
|
- setting: defaultSetting,
|
|
|
104
|
- resp: &resp,
|
|
|
105
|
- }
|
|
|
106
|
-}
|
|
|
107
|
-
|
|
|
108
|
-// Get returns *BeegoHttpRequest with GET method.
|
|
|
109
|
-func Get(url string) *BeegoHTTPRequest {
|
|
|
110
|
- return NewBeegoRequest(url, "GET")
|
|
|
111
|
-}
|
|
|
112
|
-
|
|
|
113
|
-// Post returns *BeegoHttpRequest with POST method.
|
|
|
114
|
-func Post(url string) *BeegoHTTPRequest {
|
|
|
115
|
- return NewBeegoRequest(url, "POST")
|
|
|
116
|
-}
|
|
|
117
|
-
|
|
|
118
|
-// Put returns *BeegoHttpRequest with PUT method.
|
|
|
119
|
-func Put(url string) *BeegoHTTPRequest {
|
|
|
120
|
- return NewBeegoRequest(url, "PUT")
|
|
|
121
|
-}
|
|
|
122
|
-
|
|
|
123
|
-// Delete returns *BeegoHttpRequest DELETE method.
|
|
|
124
|
-func Delete(url string) *BeegoHTTPRequest {
|
|
|
125
|
- return NewBeegoRequest(url, "DELETE")
|
|
|
126
|
-}
|
|
|
127
|
-
|
|
|
128
|
-// Head returns *BeegoHttpRequest with HEAD method.
|
|
|
129
|
-func Head(url string) *BeegoHTTPRequest {
|
|
|
130
|
- return NewBeegoRequest(url, "HEAD")
|
|
|
131
|
-}
|
|
|
132
|
-
|
|
|
133
|
-// BeegoHTTPSettings is the http.Client setting
|
|
|
134
|
-type BeegoHTTPSettings struct {
|
|
|
135
|
- ShowDebug bool
|
|
|
136
|
- UserAgent string
|
|
|
137
|
- ConnectTimeout time.Duration
|
|
|
138
|
- ReadWriteTimeout time.Duration
|
|
|
139
|
- TLSClientConfig *tls.Config
|
|
|
140
|
- Proxy func(*http.Request) (*url.URL, error)
|
|
|
141
|
- Transport http.RoundTripper
|
|
|
142
|
- CheckRedirect func(req *http.Request, via []*http.Request) error
|
|
|
143
|
- EnableCookie bool
|
|
|
144
|
- Gzip bool
|
|
|
145
|
- DumpBody bool
|
|
|
146
|
- Retries int // if set to -1 means will retry forever
|
|
|
147
|
-}
|
|
|
148
|
-
|
|
|
149
|
-// BeegoHTTPRequest provides more useful methods for requesting one url than http.Request.
|
|
|
150
|
-type BeegoHTTPRequest struct {
|
|
|
151
|
- url string
|
|
|
152
|
- req *http.Request
|
|
|
153
|
- params map[string][]string
|
|
|
154
|
- files map[string]string
|
|
|
155
|
- setting BeegoHTTPSettings
|
|
|
156
|
- resp *http.Response
|
|
|
157
|
- body []byte
|
|
|
158
|
- dump []byte
|
|
|
159
|
-}
|
|
|
160
|
-
|
|
|
161
|
-// GetRequest return the request object
|
|
|
162
|
-func (b *BeegoHTTPRequest) GetRequest() *http.Request {
|
|
|
163
|
- return b.req
|
|
|
164
|
-}
|
|
|
165
|
-
|
|
|
166
|
-// Setting Change request settings
|
|
|
167
|
-func (b *BeegoHTTPRequest) Setting(setting BeegoHTTPSettings) *BeegoHTTPRequest {
|
|
|
168
|
- b.setting = setting
|
|
|
169
|
- return b
|
|
|
170
|
-}
|
|
|
171
|
-
|
|
|
172
|
-// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
|
|
|
173
|
-func (b *BeegoHTTPRequest) SetBasicAuth(username, password string) *BeegoHTTPRequest {
|
|
|
174
|
- b.req.SetBasicAuth(username, password)
|
|
|
175
|
- return b
|
|
|
176
|
-}
|
|
|
177
|
-
|
|
|
178
|
-// SetEnableCookie sets enable/disable cookiejar
|
|
|
179
|
-func (b *BeegoHTTPRequest) SetEnableCookie(enable bool) *BeegoHTTPRequest {
|
|
|
180
|
- b.setting.EnableCookie = enable
|
|
|
181
|
- return b
|
|
|
182
|
-}
|
|
|
183
|
-
|
|
|
184
|
-// SetUserAgent sets User-Agent header field
|
|
|
185
|
-func (b *BeegoHTTPRequest) SetUserAgent(useragent string) *BeegoHTTPRequest {
|
|
|
186
|
- b.setting.UserAgent = useragent
|
|
|
187
|
- return b
|
|
|
188
|
-}
|
|
|
189
|
-
|
|
|
190
|
-// Debug sets show debug or not when executing request.
|
|
|
191
|
-func (b *BeegoHTTPRequest) Debug(isdebug bool) *BeegoHTTPRequest {
|
|
|
192
|
- b.setting.ShowDebug = isdebug
|
|
|
193
|
- return b
|
|
|
194
|
-}
|
|
|
195
|
-
|
|
|
196
|
-// Retries sets Retries times.
|
|
|
197
|
-// default is 0 means no retried.
|
|
|
198
|
-// -1 means retried forever.
|
|
|
199
|
-// others means retried times.
|
|
|
200
|
-func (b *BeegoHTTPRequest) Retries(times int) *BeegoHTTPRequest {
|
|
|
201
|
- b.setting.Retries = times
|
|
|
202
|
- return b
|
|
|
203
|
-}
|
|
|
204
|
-
|
|
|
205
|
-// DumpBody setting whether need to Dump the Body.
|
|
|
206
|
-func (b *BeegoHTTPRequest) DumpBody(isdump bool) *BeegoHTTPRequest {
|
|
|
207
|
- b.setting.DumpBody = isdump
|
|
|
208
|
- return b
|
|
|
209
|
-}
|
|
|
210
|
-
|
|
|
211
|
-// DumpRequest return the DumpRequest
|
|
|
212
|
-func (b *BeegoHTTPRequest) DumpRequest() []byte {
|
|
|
213
|
- return b.dump
|
|
|
214
|
-}
|
|
|
215
|
-
|
|
|
216
|
-// SetTimeout sets connect time out and read-write time out for BeegoRequest.
|
|
|
217
|
-func (b *BeegoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHTTPRequest {
|
|
|
218
|
- b.setting.ConnectTimeout = connectTimeout
|
|
|
219
|
- b.setting.ReadWriteTimeout = readWriteTimeout
|
|
|
220
|
- return b
|
|
|
221
|
-}
|
|
|
222
|
-
|
|
|
223
|
-// SetTLSClientConfig sets tls connection configurations if visiting https url.
|
|
|
224
|
-func (b *BeegoHTTPRequest) SetTLSClientConfig(config *tls.Config) *BeegoHTTPRequest {
|
|
|
225
|
- b.setting.TLSClientConfig = config
|
|
|
226
|
- return b
|
|
|
227
|
-}
|
|
|
228
|
-
|
|
|
229
|
-// Header add header item string in request.
|
|
|
230
|
-func (b *BeegoHTTPRequest) Header(key, value string) *BeegoHTTPRequest {
|
|
|
231
|
- b.req.Header.Set(key, value)
|
|
|
232
|
- return b
|
|
|
233
|
-}
|
|
|
234
|
-
|
|
|
235
|
-// SetHost set the request host
|
|
|
236
|
-func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {
|
|
|
237
|
- b.req.Host = host
|
|
|
238
|
- return b
|
|
|
239
|
-}
|
|
|
240
|
-
|
|
|
241
|
-// SetProtocolVersion Set the protocol version for incoming requests.
|
|
|
242
|
-// Client requests always use HTTP/1.1.
|
|
|
243
|
-func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {
|
|
|
244
|
- if len(vers) == 0 {
|
|
|
245
|
- vers = "HTTP/1.1"
|
|
|
246
|
- }
|
|
|
247
|
-
|
|
|
248
|
- major, minor, ok := http.ParseHTTPVersion(vers)
|
|
|
249
|
- if ok {
|
|
|
250
|
- b.req.Proto = vers
|
|
|
251
|
- b.req.ProtoMajor = major
|
|
|
252
|
- b.req.ProtoMinor = minor
|
|
|
253
|
- }
|
|
|
254
|
-
|
|
|
255
|
- return b
|
|
|
256
|
-}
|
|
|
257
|
-
|
|
|
258
|
-// SetCookie add cookie into request.
|
|
|
259
|
-func (b *BeegoHTTPRequest) SetCookie(cookie *http.Cookie) *BeegoHTTPRequest {
|
|
|
260
|
- b.req.Header.Add("Cookie", cookie.String())
|
|
|
261
|
- return b
|
|
|
262
|
-}
|
|
|
263
|
-
|
|
|
264
|
-// SetTransport set the setting transport
|
|
|
265
|
-func (b *BeegoHTTPRequest) SetTransport(transport http.RoundTripper) *BeegoHTTPRequest {
|
|
|
266
|
- b.setting.Transport = transport
|
|
|
267
|
- return b
|
|
|
268
|
-}
|
|
|
269
|
-
|
|
|
270
|
-// SetProxy set the http proxy
|
|
|
271
|
-// example:
|
|
|
272
|
-//
|
|
|
273
|
-// func(req *http.Request) (*url.URL, error) {
|
|
|
274
|
-// u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
|
|
|
275
|
-// return u, nil
|
|
|
276
|
-// }
|
|
|
277
|
-func (b *BeegoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHTTPRequest {
|
|
|
278
|
- b.setting.Proxy = proxy
|
|
|
279
|
- return b
|
|
|
280
|
-}
|
|
|
281
|
-
|
|
|
282
|
-// SetCheckRedirect specifies the policy for handling redirects.
|
|
|
283
|
-//
|
|
|
284
|
-// If CheckRedirect is nil, the Client uses its default policy,
|
|
|
285
|
-// which is to stop after 10 consecutive requests.
|
|
|
286
|
-func (b *BeegoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *BeegoHTTPRequest {
|
|
|
287
|
- b.setting.CheckRedirect = redirect
|
|
|
288
|
- return b
|
|
|
289
|
-}
|
|
|
290
|
-
|
|
|
291
|
-// Param adds query param in to request.
|
|
|
292
|
-// params build query string as ?key1=value1&key2=value2...
|
|
|
293
|
-func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
|
|
294
|
- if param, ok := b.params[key]; ok {
|
|
|
295
|
- b.params[key] = append(param, value)
|
|
|
296
|
- } else {
|
|
|
297
|
- b.params[key] = []string{value}
|
|
|
298
|
- }
|
|
|
299
|
- return b
|
|
|
300
|
-}
|
|
|
301
|
-
|
|
|
302
|
-// PostFile add a post file to the request
|
|
|
303
|
-func (b *BeegoHTTPRequest) PostFile(formname, filename string) *BeegoHTTPRequest {
|
|
|
304
|
- b.files[formname] = filename
|
|
|
305
|
- return b
|
|
|
306
|
-}
|
|
|
307
|
-
|
|
|
308
|
-// Body adds request raw body.
|
|
|
309
|
-// it supports string and []byte.
|
|
|
310
|
-func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
|
|
|
311
|
- switch t := data.(type) {
|
|
|
312
|
- case string:
|
|
|
313
|
- bf := bytes.NewBufferString(t)
|
|
|
314
|
- b.req.Body = ioutil.NopCloser(bf)
|
|
|
315
|
- b.req.ContentLength = int64(len(t))
|
|
|
316
|
- case []byte:
|
|
|
317
|
- bf := bytes.NewBuffer(t)
|
|
|
318
|
- b.req.Body = ioutil.NopCloser(bf)
|
|
|
319
|
- b.req.ContentLength = int64(len(t))
|
|
|
320
|
- }
|
|
|
321
|
- return b
|
|
|
322
|
-}
|
|
|
323
|
-
|
|
|
324
|
-// XMLBody adds request raw body encoding by XML.
|
|
|
325
|
-func (b *BeegoHTTPRequest) XMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
|
|
326
|
- if b.req.Body == nil && obj != nil {
|
|
|
327
|
- byts, err := xml.Marshal(obj)
|
|
|
328
|
- if err != nil {
|
|
|
329
|
- return b, err
|
|
|
330
|
- }
|
|
|
331
|
- b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
|
|
332
|
- b.req.ContentLength = int64(len(byts))
|
|
|
333
|
- b.req.Header.Set("Content-Type", "application/xml")
|
|
|
334
|
- }
|
|
|
335
|
- return b, nil
|
|
|
336
|
-}
|
|
|
337
|
-
|
|
|
338
|
-// YAMLBody adds request raw body encoding by YAML.
|
|
|
339
|
-func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
|
|
340
|
- if b.req.Body == nil && obj != nil {
|
|
|
341
|
- byts, err := yaml.Marshal(obj)
|
|
|
342
|
- if err != nil {
|
|
|
343
|
- return b, err
|
|
|
344
|
- }
|
|
|
345
|
- b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
|
|
346
|
- b.req.ContentLength = int64(len(byts))
|
|
|
347
|
- b.req.Header.Set("Content-Type", "application/x+yaml")
|
|
|
348
|
- }
|
|
|
349
|
- return b, nil
|
|
|
350
|
-}
|
|
|
351
|
-
|
|
|
352
|
-// JSONBody adds request raw body encoding by JSON.
|
|
|
353
|
-func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
|
|
354
|
- if b.req.Body == nil && obj != nil {
|
|
|
355
|
- byts, err := json.Marshal(obj)
|
|
|
356
|
- if err != nil {
|
|
|
357
|
- return b, err
|
|
|
358
|
- }
|
|
|
359
|
- b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
|
|
360
|
- b.req.ContentLength = int64(len(byts))
|
|
|
361
|
- b.req.Header.Set("Content-Type", "application/json")
|
|
|
362
|
- }
|
|
|
363
|
- return b, nil
|
|
|
364
|
-}
|
|
|
365
|
-
|
|
|
366
|
-func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
|
|
367
|
- // build GET url with query string
|
|
|
368
|
- if b.req.Method == "GET" && len(paramBody) > 0 {
|
|
|
369
|
- if strings.Contains(b.url, "?") {
|
|
|
370
|
- b.url += "&" + paramBody
|
|
|
371
|
- } else {
|
|
|
372
|
- b.url = b.url + "?" + paramBody
|
|
|
373
|
- }
|
|
|
374
|
- return
|
|
|
375
|
- }
|
|
|
376
|
-
|
|
|
377
|
- // build POST/PUT/PATCH url and body
|
|
|
378
|
- if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH" || b.req.Method == "DELETE") && b.req.Body == nil {
|
|
|
379
|
- // with files
|
|
|
380
|
- if len(b.files) > 0 {
|
|
|
381
|
- pr, pw := io.Pipe()
|
|
|
382
|
- bodyWriter := multipart.NewWriter(pw)
|
|
|
383
|
- go func() {
|
|
|
384
|
- for formname, filename := range b.files {
|
|
|
385
|
- fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
|
|
|
386
|
- if err != nil {
|
|
|
387
|
- log.Println("Httplib:", err)
|
|
|
388
|
- }
|
|
|
389
|
- fh, err := os.Open(filename)
|
|
|
390
|
- if err != nil {
|
|
|
391
|
- log.Println("Httplib:", err)
|
|
|
392
|
- }
|
|
|
393
|
- //iocopy
|
|
|
394
|
- _, err = io.Copy(fileWriter, fh)
|
|
|
395
|
- fh.Close()
|
|
|
396
|
- if err != nil {
|
|
|
397
|
- log.Println("Httplib:", err)
|
|
|
398
|
- }
|
|
|
399
|
- }
|
|
|
400
|
- for k, v := range b.params {
|
|
|
401
|
- for _, vv := range v {
|
|
|
402
|
- bodyWriter.WriteField(k, vv)
|
|
|
403
|
- }
|
|
|
404
|
- }
|
|
|
405
|
- bodyWriter.Close()
|
|
|
406
|
- pw.Close()
|
|
|
407
|
- }()
|
|
|
408
|
- b.Header("Content-Type", bodyWriter.FormDataContentType())
|
|
|
409
|
- b.req.Body = ioutil.NopCloser(pr)
|
|
|
410
|
- b.Header("Transfer-Encoding", "chunked")
|
|
|
411
|
- return
|
|
|
412
|
- }
|
|
|
413
|
-
|
|
|
414
|
- // with params
|
|
|
415
|
- if len(paramBody) > 0 {
|
|
|
416
|
- b.Header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
417
|
- b.Body(paramBody)
|
|
|
418
|
- }
|
|
|
419
|
- }
|
|
|
420
|
-}
|
|
|
421
|
-
|
|
|
422
|
-func (b *BeegoHTTPRequest) getResponse() (*http.Response, error) {
|
|
|
423
|
- if b.resp.StatusCode != 0 {
|
|
|
424
|
- return b.resp, nil
|
|
|
425
|
- }
|
|
|
426
|
- resp, err := b.DoRequest()
|
|
|
427
|
- if err != nil {
|
|
|
428
|
- return nil, err
|
|
|
429
|
- }
|
|
|
430
|
- b.resp = resp
|
|
|
431
|
- return resp, nil
|
|
|
432
|
-}
|
|
|
433
|
-
|
|
|
434
|
-// DoRequest will do the client.Do
|
|
|
435
|
-func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) {
|
|
|
436
|
- var paramBody string
|
|
|
437
|
- if len(b.params) > 0 {
|
|
|
438
|
- var buf bytes.Buffer
|
|
|
439
|
- for k, v := range b.params {
|
|
|
440
|
- for _, vv := range v {
|
|
|
441
|
- buf.WriteString(url.QueryEscape(k))
|
|
|
442
|
- buf.WriteByte('=')
|
|
|
443
|
- buf.WriteString(url.QueryEscape(vv))
|
|
|
444
|
- buf.WriteByte('&')
|
|
|
445
|
- }
|
|
|
446
|
- }
|
|
|
447
|
- paramBody = buf.String()
|
|
|
448
|
- paramBody = paramBody[0 : len(paramBody)-1]
|
|
|
449
|
- }
|
|
|
450
|
-
|
|
|
451
|
- b.buildURL(paramBody)
|
|
|
452
|
- urlParsed, err := url.Parse(b.url)
|
|
|
453
|
- if err != nil {
|
|
|
454
|
- return nil, err
|
|
|
455
|
- }
|
|
|
456
|
-
|
|
|
457
|
- b.req.URL = urlParsed
|
|
|
458
|
-
|
|
|
459
|
- trans := b.setting.Transport
|
|
|
460
|
-
|
|
|
461
|
- if trans == nil {
|
|
|
462
|
- // create default transport
|
|
|
463
|
- trans = &http.Transport{
|
|
|
464
|
- TLSClientConfig: b.setting.TLSClientConfig,
|
|
|
465
|
- Proxy: b.setting.Proxy,
|
|
|
466
|
- Dial: TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout),
|
|
|
467
|
- MaxIdleConnsPerHost: 100,
|
|
|
468
|
- }
|
|
|
469
|
- } else {
|
|
|
470
|
- // if b.transport is *http.Transport then set the settings.
|
|
|
471
|
- if t, ok := trans.(*http.Transport); ok {
|
|
|
472
|
- if t.TLSClientConfig == nil {
|
|
|
473
|
- t.TLSClientConfig = b.setting.TLSClientConfig
|
|
|
474
|
- }
|
|
|
475
|
- if t.Proxy == nil {
|
|
|
476
|
- t.Proxy = b.setting.Proxy
|
|
|
477
|
- }
|
|
|
478
|
- if t.Dial == nil {
|
|
|
479
|
- t.Dial = TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout)
|
|
|
480
|
- }
|
|
|
481
|
- }
|
|
|
482
|
- }
|
|
|
483
|
-
|
|
|
484
|
- var jar http.CookieJar
|
|
|
485
|
- if b.setting.EnableCookie {
|
|
|
486
|
- if defaultCookieJar == nil {
|
|
|
487
|
- createDefaultCookie()
|
|
|
488
|
- }
|
|
|
489
|
- jar = defaultCookieJar
|
|
|
490
|
- }
|
|
|
491
|
-
|
|
|
492
|
- client := &http.Client{
|
|
|
493
|
- Transport: trans,
|
|
|
494
|
- Jar: jar,
|
|
|
495
|
- }
|
|
|
496
|
-
|
|
|
497
|
- if b.setting.UserAgent != "" && b.req.Header.Get("User-Agent") == "" {
|
|
|
498
|
- b.req.Header.Set("User-Agent", b.setting.UserAgent)
|
|
|
499
|
- }
|
|
|
500
|
-
|
|
|
501
|
- if b.setting.CheckRedirect != nil {
|
|
|
502
|
- client.CheckRedirect = b.setting.CheckRedirect
|
|
|
503
|
- }
|
|
|
504
|
-
|
|
|
505
|
- if b.setting.ShowDebug {
|
|
|
506
|
- dump, err := httputil.DumpRequest(b.req, b.setting.DumpBody)
|
|
|
507
|
- if err != nil {
|
|
|
508
|
- log.Println(err.Error())
|
|
|
509
|
- }
|
|
|
510
|
- b.dump = dump
|
|
|
511
|
- }
|
|
|
512
|
- // retries default value is 0, it will run once.
|
|
|
513
|
- // retries equal to -1, it will run forever until success
|
|
|
514
|
- // retries is setted, it will retries fixed times.
|
|
|
515
|
- for i := 0; b.setting.Retries == -1 || i <= b.setting.Retries; i++ {
|
|
|
516
|
- resp, err = client.Do(b.req)
|
|
|
517
|
- if err == nil {
|
|
|
518
|
- break
|
|
|
519
|
- }
|
|
|
520
|
- }
|
|
|
521
|
- return resp, err
|
|
|
522
|
-}
|
|
|
523
|
-
|
|
|
524
|
-// String returns the body string in response.
|
|
|
525
|
-// it calls Response inner.
|
|
|
526
|
-func (b *BeegoHTTPRequest) String() (string, error) {
|
|
|
527
|
- data, err := b.Bytes()
|
|
|
528
|
- if err != nil {
|
|
|
529
|
- return "", err
|
|
|
530
|
- }
|
|
|
531
|
-
|
|
|
532
|
- return string(data), nil
|
|
|
533
|
-}
|
|
|
534
|
-
|
|
|
535
|
-// Bytes returns the body []byte in response.
|
|
|
536
|
-// it calls Response inner.
|
|
|
537
|
-func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {
|
|
|
538
|
- if b.body != nil {
|
|
|
539
|
- return b.body, nil
|
|
|
540
|
- }
|
|
|
541
|
- resp, err := b.getResponse()
|
|
|
542
|
- if err != nil {
|
|
|
543
|
- return nil, err
|
|
|
544
|
- }
|
|
|
545
|
- if resp.Body == nil {
|
|
|
546
|
- return nil, nil
|
|
|
547
|
- }
|
|
|
548
|
- defer resp.Body.Close()
|
|
|
549
|
- if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" {
|
|
|
550
|
- reader, err := gzip.NewReader(resp.Body)
|
|
|
551
|
- if err != nil {
|
|
|
552
|
- return nil, err
|
|
|
553
|
- }
|
|
|
554
|
- b.body, err = ioutil.ReadAll(reader)
|
|
|
555
|
- return b.body, err
|
|
|
556
|
- }
|
|
|
557
|
- b.body, err = ioutil.ReadAll(resp.Body)
|
|
|
558
|
- return b.body, err
|
|
|
559
|
-}
|
|
|
560
|
-
|
|
|
561
|
-// ToFile saves the body data in response to one file.
|
|
|
562
|
-// it calls Response inner.
|
|
|
563
|
-func (b *BeegoHTTPRequest) ToFile(filename string) error {
|
|
|
564
|
- resp, err := b.getResponse()
|
|
|
565
|
- if err != nil {
|
|
|
566
|
- return err
|
|
|
567
|
- }
|
|
|
568
|
- if resp.Body == nil {
|
|
|
569
|
- return nil
|
|
|
570
|
- }
|
|
|
571
|
- defer resp.Body.Close()
|
|
|
572
|
- err = pathExistAndMkdir(filename)
|
|
|
573
|
- if err != nil {
|
|
|
574
|
- return err
|
|
|
575
|
- }
|
|
|
576
|
- f, err := os.Create(filename)
|
|
|
577
|
- if err != nil {
|
|
|
578
|
- return err
|
|
|
579
|
- }
|
|
|
580
|
- defer f.Close()
|
|
|
581
|
- _, err = io.Copy(f, resp.Body)
|
|
|
582
|
- return err
|
|
|
583
|
-}
|
|
|
584
|
-
|
|
|
585
|
-//Check that the file directory exists, there is no automatically created
|
|
|
586
|
-func pathExistAndMkdir(filename string) (err error) {
|
|
|
587
|
- filename = path.Dir(filename)
|
|
|
588
|
- _, err = os.Stat(filename)
|
|
|
589
|
- if err == nil {
|
|
|
590
|
- return nil
|
|
|
591
|
- }
|
|
|
592
|
- if os.IsNotExist(err) {
|
|
|
593
|
- err = os.MkdirAll(filename, os.ModePerm)
|
|
|
594
|
- if err == nil {
|
|
|
595
|
- return nil
|
|
|
596
|
- }
|
|
|
597
|
- }
|
|
|
598
|
- return err
|
|
|
599
|
-}
|
|
|
600
|
-
|
|
|
601
|
-// ToJSON returns the map that marshals from the body bytes as json in response .
|
|
|
602
|
-// it calls Response inner.
|
|
|
603
|
-func (b *BeegoHTTPRequest) ToJSON(v interface{}) error {
|
|
|
604
|
- data, err := b.Bytes()
|
|
|
605
|
- if err != nil {
|
|
|
606
|
- return err
|
|
|
607
|
- }
|
|
|
608
|
- return json.Unmarshal(data, v)
|
|
|
609
|
-}
|
|
|
610
|
-
|
|
|
611
|
-// ToXML returns the map that marshals from the body bytes as xml in response .
|
|
|
612
|
-// it calls Response inner.
|
|
|
613
|
-func (b *BeegoHTTPRequest) ToXML(v interface{}) error {
|
|
|
614
|
- data, err := b.Bytes()
|
|
|
615
|
- if err != nil {
|
|
|
616
|
- return err
|
|
|
617
|
- }
|
|
|
618
|
- return xml.Unmarshal(data, v)
|
|
|
619
|
-}
|
|
|
620
|
-
|
|
|
621
|
-// ToYAML returns the map that marshals from the body bytes as yaml in response .
|
|
|
622
|
-// it calls Response inner.
|
|
|
623
|
-func (b *BeegoHTTPRequest) ToYAML(v interface{}) error {
|
|
|
624
|
- data, err := b.Bytes()
|
|
|
625
|
- if err != nil {
|
|
|
626
|
- return err
|
|
|
627
|
- }
|
|
|
628
|
- return yaml.Unmarshal(data, v)
|
|
|
629
|
-}
|
|
|
630
|
-
|
|
|
631
|
-// Response executes request client gets response mannually.
|
|
|
632
|
-func (b *BeegoHTTPRequest) Response() (*http.Response, error) {
|
|
|
633
|
- return b.getResponse()
|
|
|
634
|
-}
|
|
|
635
|
-
|
|
|
636
|
-// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
|
|
|
637
|
-func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
|
|
|
638
|
- return func(netw, addr string) (net.Conn, error) {
|
|
|
639
|
- conn, err := net.DialTimeout(netw, addr, cTimeout)
|
|
|
640
|
- if err != nil {
|
|
|
641
|
- return nil, err
|
|
|
642
|
- }
|
|
|
643
|
- err = conn.SetDeadline(time.Now().Add(rwTimeout))
|
|
|
644
|
- return conn, err
|
|
|
645
|
- }
|
|
|
646
|
-} |
|
|