Merge pull request #1131 from JessonChan/develop
httplib more fixed
Showing
1 changed file
with
44 additions
and
54 deletions
| ... | @@ -36,6 +36,7 @@ import ( | ... | @@ -36,6 +36,7 @@ import ( |
| 36 | "crypto/tls" | 36 | "crypto/tls" |
| 37 | "encoding/json" | 37 | "encoding/json" |
| 38 | "encoding/xml" | 38 | "encoding/xml" |
| 39 | "fmt" | ||
| 39 | "io" | 40 | "io" |
| 40 | "io/ioutil" | 41 | "io/ioutil" |
| 41 | "log" | 42 | "log" |
| ... | @@ -47,32 +48,20 @@ import ( | ... | @@ -47,32 +48,20 @@ import ( |
| 47 | "net/url" | 48 | "net/url" |
| 48 | "os" | 49 | "os" |
| 49 | "strings" | 50 | "strings" |
| 50 | "sync" | ||
| 51 | "time" | 51 | "time" |
| 52 | ) | 52 | ) |
| 53 | 53 | ||
| 54 | var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false, true} | 54 | var defaultSetting = BeegoHttpSettings{UserAgent: "beegoServer", ConnectTimeout: 60 * time.Second, ReadWriteTimeout: 60 * time.Second, Gzip: true} |
| 55 | var defaultCookieJar http.CookieJar | 55 | var defaultCookieJar http.CookieJar |
| 56 | var settingMutex sync.Mutex | ||
| 57 | 56 | ||
| 58 | // createDefaultCookie creates a global cookiejar to store cookies. | 57 | // createDefaultCookie creates a global cookiejar to store cookies. |
| 59 | func createDefaultCookie() { | 58 | func createDefaultCookie() { |
| 60 | settingMutex.Lock() | ||
| 61 | defer settingMutex.Unlock() | ||
| 62 | defaultCookieJar, _ = cookiejar.New(nil) | 59 | defaultCookieJar, _ = cookiejar.New(nil) |
| 63 | } | 60 | } |
| 64 | 61 | ||
| 65 | // Overwrite default settings | 62 | // Overwrite default settings |
| 66 | func SetDefaultSetting(setting BeegoHttpSettings) { | 63 | func SetDefaultSetting(setting BeegoHttpSettings) { |
| 67 | settingMutex.Lock() | ||
| 68 | defer settingMutex.Unlock() | ||
| 69 | defaultSetting = setting | 64 | defaultSetting = setting |
| 70 | if defaultSetting.ConnectTimeout == 0 { | ||
| 71 | defaultSetting.ConnectTimeout = 60 * time.Second | ||
| 72 | } | ||
| 73 | if defaultSetting.ReadWriteTimeout == 0 { | ||
| 74 | defaultSetting.ReadWriteTimeout = 60 * time.Second | ||
| 75 | } | ||
| 76 | } | 65 | } |
| 77 | 66 | ||
| 78 | // return *BeegoHttpRequest with specific method | 67 | // return *BeegoHttpRequest with specific method |
| ... | @@ -85,7 +74,14 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest { | ... | @@ -85,7 +74,14 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest { |
| 85 | ProtoMajor: 1, | 74 | ProtoMajor: 1, |
| 86 | ProtoMinor: 1, | 75 | ProtoMinor: 1, |
| 87 | } | 76 | } |
| 88 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil, nil} | 77 | return &BeegoHttpRequest{ |
| 78 | url: url, | ||
| 79 | req: &req, | ||
| 80 | params: map[string]string{}, | ||
| 81 | files: map[string]string{}, | ||
| 82 | setting: defaultSetting, | ||
| 83 | resp: &resp, | ||
| 84 | } | ||
| 89 | } | 85 | } |
| 90 | 86 | ||
| 91 | // Get returns *BeegoHttpRequest with GET method. | 87 | // Get returns *BeegoHttpRequest with GET method. |
| ... | @@ -157,14 +153,14 @@ func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { | ... | @@ -157,14 +153,14 @@ func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { |
| 157 | } | 153 | } |
| 158 | 154 | ||
| 159 | // SetUserAgent sets User-Agent header field | 155 | // SetUserAgent sets User-Agent header field |
| 160 | func (b *BeegoHttpRequest) SetUserAgent(useragent string) *BeegoHttpRequest { | 156 | func (b *BeegoHttpRequest) SetUserAgent(userAgent string) *BeegoHttpRequest { |
| 161 | b.setting.UserAgent = useragent | 157 | b.setting.UserAgent = userAgent |
| 162 | return b | 158 | return b |
| 163 | } | 159 | } |
| 164 | 160 | ||
| 165 | // Debug sets show debug or not when executing request. | 161 | // Debug sets show debug or not when executing request. |
| 166 | func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { | 162 | func (b *BeegoHttpRequest) Debug(isDebug bool) *BeegoHttpRequest { |
| 167 | b.setting.ShowDebug = isdebug | 163 | b.setting.ShowDebug = isDebug |
| 168 | return b | 164 | return b |
| 169 | } | 165 | } |
| 170 | 166 | ||
| ... | @@ -283,12 +279,15 @@ func (b *BeegoHttpRequest) JsonBody(obj interface{}) (*BeegoHttpRequest, error) | ... | @@ -283,12 +279,15 @@ func (b *BeegoHttpRequest) JsonBody(obj interface{}) (*BeegoHttpRequest, error) |
| 283 | } | 279 | } |
| 284 | 280 | ||
| 285 | func (b *BeegoHttpRequest) buildUrl(paramBody string) { | 281 | func (b *BeegoHttpRequest) buildUrl(paramBody string) { |
| 282 | if paramBody == "" { | ||
| 283 | return | ||
| 284 | } | ||
| 286 | // build GET url with query string | 285 | // build GET url with query string |
| 287 | if b.req.Method == "GET" && len(paramBody) > 0 { | 286 | if b.req.Method == "GET" { |
| 288 | if strings.Index(b.url, "?") != -1 { | 287 | if strings.Index(b.url, "?") == -1 { |
| 289 | b.url += "&" + paramBody | ||
| 290 | } else { | ||
| 291 | b.url = b.url + "?" + paramBody | 288 | b.url = b.url + "?" + paramBody |
| 289 | } else { | ||
| 290 | b.url += "&" + paramBody | ||
| 292 | } | 291 | } |
| 293 | return | 292 | return |
| 294 | } | 293 | } |
| ... | @@ -341,18 +340,14 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -341,18 +340,14 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 341 | } | 340 | } |
| 342 | var paramBody string | 341 | var paramBody string |
| 343 | if len(b.params) > 0 { | 342 | if len(b.params) > 0 { |
| 344 | var buf bytes.Buffer | ||
| 345 | for k, v := range b.params { | 343 | for k, v := range b.params { |
| 346 | buf.WriteString(url.QueryEscape(k)) | 344 | paramBody += fmt.Sprintf("&%s=%v", url.QueryEscape(k), url.QueryEscape(v)) |
| 347 | buf.WriteByte('=') | ||
| 348 | buf.WriteString(url.QueryEscape(v)) | ||
| 349 | buf.WriteByte('&') | ||
| 350 | } | 345 | } |
| 351 | paramBody = buf.String() | 346 | paramBody = paramBody[1:] |
| 352 | paramBody = paramBody[0 : len(paramBody)-1] | ||
| 353 | } | 347 | } |
| 354 | 348 | ||
| 355 | b.buildUrl(paramBody) | 349 | b.buildUrl(paramBody) |
| 350 | |||
| 356 | url, err := url.Parse(b.url) | 351 | url, err := url.Parse(b.url) |
| 357 | if err != nil { | 352 | if err != nil { |
| 358 | return nil, err | 353 | return nil, err |
| ... | @@ -362,6 +357,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -362,6 +357,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 362 | 357 | ||
| 363 | trans := b.setting.Transport | 358 | trans := b.setting.Transport |
| 364 | 359 | ||
| 360 | if b.setting.ConnectTimeout == 0 { | ||
| 361 | b.setting.ConnectTimeout = 60 * time.Second | ||
| 362 | } | ||
| 363 | if b.setting.ReadWriteTimeout == 0 { | ||
| 364 | b.setting.ReadWriteTimeout = 60 * time.Second | ||
| 365 | } | ||
| 366 | |||
| 365 | if trans == nil { | 367 | if trans == nil { |
| 366 | // create default transport | 368 | // create default transport |
| 367 | trans = &http.Transport{ | 369 | trans = &http.Transport{ |
| ... | @@ -404,17 +406,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -404,17 +406,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 404 | if b.setting.ShowDebug { | 406 | if b.setting.ShowDebug { |
| 405 | dump, err := httputil.DumpRequest(b.req, true) | 407 | dump, err := httputil.DumpRequest(b.req, true) |
| 406 | if err != nil { | 408 | if err != nil { |
| 407 | println(err.Error()) | 409 | log.Println(err.Error()) |
| 408 | } | 410 | } |
| 409 | b.dump = dump | 411 | b.dump = dump |
| 410 | } | 412 | } |
| 411 | 413 | ||
| 412 | resp, err := client.Do(b.req) | 414 | b.resp, err = client.Do(b.req) |
| 413 | if err != nil { | 415 | return b.resp, err |
| 414 | return nil, err | ||
| 415 | } | ||
| 416 | b.resp = resp | ||
| 417 | return resp, nil | ||
| 418 | } | 416 | } |
| 419 | 417 | ||
| 420 | // String returns the body string in response. | 418 | // String returns the body string in response. |
| ... | @@ -435,12 +433,9 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { | ... | @@ -435,12 +433,9 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { |
| 435 | return b.body, nil | 433 | return b.body, nil |
| 436 | } | 434 | } |
| 437 | resp, err := b.getResponse() | 435 | resp, err := b.getResponse() |
| 438 | if err != nil { | 436 | if resp == nil || resp.Body == nil { |
| 439 | return nil, err | 437 | return nil, err |
| 440 | } | 438 | } |
| 441 | if resp.Body == nil { | ||
| 442 | return nil, nil | ||
| 443 | } | ||
| 444 | defer resp.Body.Close() | 439 | defer resp.Body.Close() |
| 445 | if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" { | 440 | if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" { |
| 446 | reader, err := gzip.NewReader(resp.Body) | 441 | reader, err := gzip.NewReader(resp.Body) |
| ... | @@ -451,29 +446,24 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { | ... | @@ -451,29 +446,24 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { |
| 451 | } else { | 446 | } else { |
| 452 | b.body, err = ioutil.ReadAll(resp.Body) | 447 | b.body, err = ioutil.ReadAll(resp.Body) |
| 453 | } | 448 | } |
| 454 | if err != nil { | 449 | return b.body, err |
| 455 | return nil, err | ||
| 456 | } | ||
| 457 | return b.body, nil | ||
| 458 | } | 450 | } |
| 459 | 451 | ||
| 460 | // ToFile saves the body data in response to one file. | 452 | // ToFile saves the body data in response to one file. |
| 461 | // it calls Response inner. | 453 | // it calls Response inner. |
| 462 | func (b *BeegoHttpRequest) ToFile(filename string) error { | 454 | func (b *BeegoHttpRequest) ToFile(filename string) error { |
| 463 | f, err := os.Create(filename) | 455 | resp, err := b.getResponse() |
| 464 | if err != nil { | 456 | if resp == nil || resp.Body == nil { |
| 465 | return err | 457 | return err |
| 466 | } | 458 | } |
| 467 | defer f.Close() | 459 | defer resp.Body.Close() |
| 468 | 460 | ||
| 469 | resp, err := b.getResponse() | 461 | f, err := os.Create(filename) |
| 470 | if err != nil { | 462 | if err != nil { |
| 471 | return err | 463 | return err |
| 472 | } | 464 | } |
| 473 | if resp.Body == nil { | 465 | defer f.Close() |
| 474 | return nil | 466 | |
| 475 | } | ||
| 476 | defer resp.Body.Close() | ||
| 477 | _, err = io.Copy(f, resp.Body) | 467 | _, err = io.Copy(f, resp.Body) |
| 478 | return err | 468 | return err |
| 479 | } | 469 | } |
| ... | @@ -510,7 +500,7 @@ func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, ad | ... | @@ -510,7 +500,7 @@ func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, ad |
| 510 | if err != nil { | 500 | if err != nil { |
| 511 | return nil, err | 501 | return nil, err |
| 512 | } | 502 | } |
| 513 | conn.SetDeadline(time.Now().Add(rwTimeout)) | 503 | err = conn.SetDeadline(time.Now().Add(rwTimeout)) |
| 514 | return conn, nil | 504 | return conn, err |
| 515 | } | 505 | } |
| 516 | } | 506 | } | ... | ... |
-
Please register or sign in to post a comment